OutlinePass.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. /**
  2. * @author spidersharma / http://eduperiment.com/
  3. */
  4. import {
  5. AdditiveBlending,
  6. Color,
  7. DoubleSide,
  8. LinearFilter,
  9. Matrix4,
  10. MeshBasicMaterial,
  11. MeshDepthMaterial,
  12. NoBlending,
  13. RGBADepthPacking,
  14. RGBAFormat,
  15. ShaderMaterial,
  16. UniformsUtils,
  17. Vector2,
  18. Vector3,
  19. WebGLRenderTarget
  20. } from "../../../build/three.module.js";
  21. import { Pass } from "../postprocessing/Pass.js";
  22. import { CopyShader } from "../shaders/CopyShader.js";
  23. var OutlinePass = function ( resolution, scene, camera, selectedObjects ) {
  24. this.renderScene = scene;
  25. this.renderCamera = camera;
  26. this.selectedObjects = selectedObjects !== undefined ? selectedObjects : [];
  27. this.visibleEdgeColor = new Color( 1, 1, 1 );
  28. this.hiddenEdgeColor = new Color( 0.1, 0.04, 0.02 );
  29. this.edgeGlow = 0.0;
  30. this.usePatternTexture = false;
  31. this.edgeThickness = 1.0;
  32. this.edgeStrength = 3.0;
  33. this.downSampleRatio = 2;
  34. this.pulsePeriod = 0;
  35. Pass.call( this );
  36. this.resolution = ( resolution !== undefined ) ? new Vector2( resolution.x, resolution.y ) : new Vector2( 256, 256 );
  37. var pars = { minFilter: LinearFilter, magFilter: LinearFilter, format: RGBAFormat };
  38. var resx = Math.round( this.resolution.x / this.downSampleRatio );
  39. var resy = Math.round( this.resolution.y / this.downSampleRatio );
  40. this.maskBufferMaterial = new MeshBasicMaterial( { color: 0xffffff } );
  41. this.maskBufferMaterial.side = DoubleSide;
  42. this.renderTargetMaskBuffer = new WebGLRenderTarget( this.resolution.x, this.resolution.y, pars );
  43. this.renderTargetMaskBuffer.texture.name = "OutlinePass.mask";
  44. this.renderTargetMaskBuffer.texture.generateMipmaps = false;
  45. this.depthMaterial = new MeshDepthMaterial();
  46. this.depthMaterial.side = DoubleSide;
  47. this.depthMaterial.depthPacking = RGBADepthPacking;
  48. this.depthMaterial.blending = NoBlending;
  49. this.prepareMaskMaterial = this.getPrepareMaskMaterial();
  50. this.prepareMaskMaterial.side = DoubleSide;
  51. this.prepareMaskMaterial.fragmentShader = replaceDepthToViewZ( this.prepareMaskMaterial.fragmentShader, this.renderCamera );
  52. this.renderTargetDepthBuffer = new WebGLRenderTarget( this.resolution.x, this.resolution.y, pars );
  53. this.renderTargetDepthBuffer.texture.name = "OutlinePass.depth";
  54. this.renderTargetDepthBuffer.texture.generateMipmaps = false;
  55. this.renderTargetMaskDownSampleBuffer = new WebGLRenderTarget( resx, resy, pars );
  56. this.renderTargetMaskDownSampleBuffer.texture.name = "OutlinePass.depthDownSample";
  57. this.renderTargetMaskDownSampleBuffer.texture.generateMipmaps = false;
  58. this.renderTargetBlurBuffer1 = new WebGLRenderTarget( resx, resy, pars );
  59. this.renderTargetBlurBuffer1.texture.name = "OutlinePass.blur1";
  60. this.renderTargetBlurBuffer1.texture.generateMipmaps = false;
  61. this.renderTargetBlurBuffer2 = new WebGLRenderTarget( Math.round( resx / 2 ), Math.round( resy / 2 ), pars );
  62. this.renderTargetBlurBuffer2.texture.name = "OutlinePass.blur2";
  63. this.renderTargetBlurBuffer2.texture.generateMipmaps = false;
  64. this.edgeDetectionMaterial = this.getEdgeDetectionMaterial();
  65. this.renderTargetEdgeBuffer1 = new WebGLRenderTarget( resx, resy, pars );
  66. this.renderTargetEdgeBuffer1.texture.name = "OutlinePass.edge1";
  67. this.renderTargetEdgeBuffer1.texture.generateMipmaps = false;
  68. this.renderTargetEdgeBuffer2 = new WebGLRenderTarget( Math.round( resx / 2 ), Math.round( resy / 2 ), pars );
  69. this.renderTargetEdgeBuffer2.texture.name = "OutlinePass.edge2";
  70. this.renderTargetEdgeBuffer2.texture.generateMipmaps = false;
  71. var MAX_EDGE_THICKNESS = 4;
  72. var MAX_EDGE_GLOW = 4;
  73. this.separableBlurMaterial1 = this.getSeperableBlurMaterial( MAX_EDGE_THICKNESS );
  74. this.separableBlurMaterial1.uniforms[ "texSize" ].value.set( resx, resy );
  75. this.separableBlurMaterial1.uniforms[ "kernelRadius" ].value = 1;
  76. this.separableBlurMaterial2 = this.getSeperableBlurMaterial( MAX_EDGE_GLOW );
  77. this.separableBlurMaterial2.uniforms[ "texSize" ].value.set( Math.round( resx / 2 ), Math.round( resy / 2 ) );
  78. this.separableBlurMaterial2.uniforms[ "kernelRadius" ].value = MAX_EDGE_GLOW;
  79. // Overlay material
  80. this.overlayMaterial = this.getOverlayMaterial();
  81. // copy material
  82. if ( CopyShader === undefined )
  83. console.error( "OutlinePass relies on CopyShader" );
  84. var copyShader = CopyShader;
  85. this.copyUniforms = UniformsUtils.clone( copyShader.uniforms );
  86. this.copyUniforms[ "opacity" ].value = 1.0;
  87. this.materialCopy = new ShaderMaterial( {
  88. uniforms: this.copyUniforms,
  89. vertexShader: copyShader.vertexShader,
  90. fragmentShader: copyShader.fragmentShader,
  91. blending: NoBlending,
  92. depthTest: false,
  93. depthWrite: false,
  94. transparent: true
  95. } );
  96. this.enabled = true;
  97. this.needsSwap = false;
  98. this.oldClearColor = new Color();
  99. this.oldClearAlpha = 1;
  100. this.fsQuad = new Pass.FullScreenQuad( null );
  101. this.tempPulseColor1 = new Color();
  102. this.tempPulseColor2 = new Color();
  103. this.textureMatrix = new Matrix4();
  104. function replaceDepthToViewZ( string, camera ) {
  105. var type = camera.isPerspectiveCamera ? 'perspective' : 'orthographic';
  106. return string.replace( /DEPTH_TO_VIEW_Z/g, type + 'DepthToViewZ' );
  107. }
  108. };
  109. OutlinePass.prototype = Object.assign( Object.create( Pass.prototype ), {
  110. constructor: OutlinePass,
  111. dispose: function () {
  112. this.renderTargetMaskBuffer.dispose();
  113. this.renderTargetDepthBuffer.dispose();
  114. this.renderTargetMaskDownSampleBuffer.dispose();
  115. this.renderTargetBlurBuffer1.dispose();
  116. this.renderTargetBlurBuffer2.dispose();
  117. this.renderTargetEdgeBuffer1.dispose();
  118. this.renderTargetEdgeBuffer2.dispose();
  119. },
  120. setSize: function ( width, height ) {
  121. this.renderTargetMaskBuffer.setSize( width, height );
  122. var resx = Math.round( width / this.downSampleRatio );
  123. var resy = Math.round( height / this.downSampleRatio );
  124. this.renderTargetMaskDownSampleBuffer.setSize( resx, resy );
  125. this.renderTargetBlurBuffer1.setSize( resx, resy );
  126. this.renderTargetEdgeBuffer1.setSize( resx, resy );
  127. this.separableBlurMaterial1.uniforms[ "texSize" ].value.set( resx, resy );
  128. resx = Math.round( resx / 2 );
  129. resy = Math.round( resy / 2 );
  130. this.renderTargetBlurBuffer2.setSize( resx, resy );
  131. this.renderTargetEdgeBuffer2.setSize( resx, resy );
  132. this.separableBlurMaterial2.uniforms[ "texSize" ].value.set( resx, resy );
  133. },
  134. changeVisibilityOfSelectedObjects: function ( bVisible ) {
  135. function gatherSelectedMeshesCallBack( object ) {
  136. if ( object.isMesh ) {
  137. if ( bVisible ) {
  138. object.visible = object.userData.oldVisible;
  139. delete object.userData.oldVisible;
  140. } else {
  141. object.userData.oldVisible = object.visible;
  142. object.visible = bVisible;
  143. }
  144. }
  145. }
  146. for ( var i = 0; i < this.selectedObjects.length; i ++ ) {
  147. var selectedObject = this.selectedObjects[ i ];
  148. selectedObject.traverse( gatherSelectedMeshesCallBack );
  149. }
  150. },
  151. changeVisibilityOfNonSelectedObjects: function ( bVisible ) {
  152. var selectedMeshes = [];
  153. function gatherSelectedMeshesCallBack( object ) {
  154. if ( object.isMesh ) selectedMeshes.push( object );
  155. }
  156. for ( var i = 0; i < this.selectedObjects.length; i ++ ) {
  157. var selectedObject = this.selectedObjects[ i ];
  158. selectedObject.traverse( gatherSelectedMeshesCallBack );
  159. }
  160. function VisibilityChangeCallBack( object ) {
  161. if ( object.isMesh || object.isLine || object.isSprite ) {
  162. var bFound = false;
  163. for ( var i = 0; i < selectedMeshes.length; i ++ ) {
  164. var selectedObjectId = selectedMeshes[ i ].id;
  165. if ( selectedObjectId === object.id ) {
  166. bFound = true;
  167. break;
  168. }
  169. }
  170. if ( ! bFound ) {
  171. var visibility = object.visible;
  172. if ( ! bVisible || object.bVisible ) object.visible = bVisible;
  173. object.bVisible = visibility;
  174. }
  175. }
  176. }
  177. this.renderScene.traverse( VisibilityChangeCallBack );
  178. },
  179. updateTextureMatrix: function () {
  180. this.textureMatrix.set( 0.5, 0.0, 0.0, 0.5,
  181. 0.0, 0.5, 0.0, 0.5,
  182. 0.0, 0.0, 0.5, 0.5,
  183. 0.0, 0.0, 0.0, 1.0 );
  184. this.textureMatrix.multiply( this.renderCamera.projectionMatrix );
  185. this.textureMatrix.multiply( this.renderCamera.matrixWorldInverse );
  186. },
  187. render: function ( renderer, writeBuffer, readBuffer, deltaTime, maskActive ) {
  188. if ( this.selectedObjects.length > 0 ) {
  189. this.oldClearColor.copy( renderer.getClearColor() );
  190. this.oldClearAlpha = renderer.getClearAlpha();
  191. var oldAutoClear = renderer.autoClear;
  192. renderer.autoClear = false;
  193. if ( maskActive ) renderer.state.buffers.stencil.setTest( false );
  194. renderer.setClearColor( 0xffffff, 1 );
  195. // Make selected objects invisible
  196. this.changeVisibilityOfSelectedObjects( false );
  197. var currentBackground = this.renderScene.background;
  198. this.renderScene.background = null;
  199. // 1. Draw Non Selected objects in the depth buffer
  200. this.renderScene.overrideMaterial = this.depthMaterial;
  201. renderer.setRenderTarget( this.renderTargetDepthBuffer );
  202. renderer.clear();
  203. renderer.render( this.renderScene, this.renderCamera );
  204. // Make selected objects visible
  205. this.changeVisibilityOfSelectedObjects( true );
  206. // Update Texture Matrix for Depth compare
  207. this.updateTextureMatrix();
  208. // Make non selected objects invisible, and draw only the selected objects, by comparing the depth buffer of non selected objects
  209. this.changeVisibilityOfNonSelectedObjects( false );
  210. this.renderScene.overrideMaterial = this.prepareMaskMaterial;
  211. this.prepareMaskMaterial.uniforms[ "cameraNearFar" ].value.set( this.renderCamera.near, this.renderCamera.far );
  212. this.prepareMaskMaterial.uniforms[ "depthTexture" ].value = this.renderTargetDepthBuffer.texture;
  213. this.prepareMaskMaterial.uniforms[ "textureMatrix" ].value = this.textureMatrix;
  214. renderer.setRenderTarget( this.renderTargetMaskBuffer );
  215. renderer.clear();
  216. renderer.render( this.renderScene, this.renderCamera );
  217. this.renderScene.overrideMaterial = null;
  218. this.changeVisibilityOfNonSelectedObjects( true );
  219. this.renderScene.background = currentBackground;
  220. // 2. Downsample to Half resolution
  221. this.fsQuad.material = this.materialCopy;
  222. this.copyUniforms[ "tDiffuse" ].value = this.renderTargetMaskBuffer.texture;
  223. renderer.setRenderTarget( this.renderTargetMaskDownSampleBuffer );
  224. renderer.clear();
  225. this.fsQuad.render( renderer );
  226. this.tempPulseColor1.copy( this.visibleEdgeColor );
  227. this.tempPulseColor2.copy( this.hiddenEdgeColor );
  228. if ( this.pulsePeriod > 0 ) {
  229. var scalar = ( 1 + 0.25 ) / 2 + Math.cos( performance.now() * 0.01 / this.pulsePeriod ) * ( 1.0 - 0.25 ) / 2;
  230. this.tempPulseColor1.multiplyScalar( scalar );
  231. this.tempPulseColor2.multiplyScalar( scalar );
  232. }
  233. // 3. Apply Edge Detection Pass
  234. this.fsQuad.material = this.edgeDetectionMaterial;
  235. this.edgeDetectionMaterial.uniforms[ "maskTexture" ].value = this.renderTargetMaskDownSampleBuffer.texture;
  236. this.edgeDetectionMaterial.uniforms[ "texSize" ].value.set( this.renderTargetMaskDownSampleBuffer.width, this.renderTargetMaskDownSampleBuffer.height );
  237. this.edgeDetectionMaterial.uniforms[ "visibleEdgeColor" ].value = this.tempPulseColor1;
  238. this.edgeDetectionMaterial.uniforms[ "hiddenEdgeColor" ].value = this.tempPulseColor2;
  239. renderer.setRenderTarget( this.renderTargetEdgeBuffer1 );
  240. renderer.clear();
  241. this.fsQuad.render( renderer );
  242. // 4. Apply Blur on Half res
  243. this.fsQuad.material = this.separableBlurMaterial1;
  244. this.separableBlurMaterial1.uniforms[ "colorTexture" ].value = this.renderTargetEdgeBuffer1.texture;
  245. this.separableBlurMaterial1.uniforms[ "direction" ].value = OutlinePass.BlurDirectionX;
  246. this.separableBlurMaterial1.uniforms[ "kernelRadius" ].value = this.edgeThickness;
  247. renderer.setRenderTarget( this.renderTargetBlurBuffer1 );
  248. renderer.clear();
  249. this.fsQuad.render( renderer );
  250. this.separableBlurMaterial1.uniforms[ "colorTexture" ].value = this.renderTargetBlurBuffer1.texture;
  251. this.separableBlurMaterial1.uniforms[ "direction" ].value = OutlinePass.BlurDirectionY;
  252. renderer.setRenderTarget( this.renderTargetEdgeBuffer1 );
  253. renderer.clear();
  254. this.fsQuad.render( renderer );
  255. // Apply Blur on quarter res
  256. this.fsQuad.material = this.separableBlurMaterial2;
  257. this.separableBlurMaterial2.uniforms[ "colorTexture" ].value = this.renderTargetEdgeBuffer1.texture;
  258. this.separableBlurMaterial2.uniforms[ "direction" ].value = OutlinePass.BlurDirectionX;
  259. renderer.setRenderTarget( this.renderTargetBlurBuffer2 );
  260. renderer.clear();
  261. this.fsQuad.render( renderer );
  262. this.separableBlurMaterial2.uniforms[ "colorTexture" ].value = this.renderTargetBlurBuffer2.texture;
  263. this.separableBlurMaterial2.uniforms[ "direction" ].value = OutlinePass.BlurDirectionY;
  264. renderer.setRenderTarget( this.renderTargetEdgeBuffer2 );
  265. renderer.clear();
  266. this.fsQuad.render( renderer );
  267. // Blend it additively over the input texture
  268. this.fsQuad.material = this.overlayMaterial;
  269. this.overlayMaterial.uniforms[ "maskTexture" ].value = this.renderTargetMaskBuffer.texture;
  270. this.overlayMaterial.uniforms[ "edgeTexture1" ].value = this.renderTargetEdgeBuffer1.texture;
  271. this.overlayMaterial.uniforms[ "edgeTexture2" ].value = this.renderTargetEdgeBuffer2.texture;
  272. this.overlayMaterial.uniforms[ "patternTexture" ].value = this.patternTexture;
  273. this.overlayMaterial.uniforms[ "edgeStrength" ].value = this.edgeStrength;
  274. this.overlayMaterial.uniforms[ "edgeGlow" ].value = this.edgeGlow;
  275. this.overlayMaterial.uniforms[ "usePatternTexture" ].value = this.usePatternTexture;
  276. if ( maskActive ) renderer.state.buffers.stencil.setTest( true );
  277. renderer.setRenderTarget( readBuffer );
  278. this.fsQuad.render( renderer );
  279. renderer.setClearColor( this.oldClearColor, this.oldClearAlpha );
  280. renderer.autoClear = oldAutoClear;
  281. }
  282. if ( this.renderToScreen ) {
  283. this.fsQuad.material = this.materialCopy;
  284. this.copyUniforms[ "tDiffuse" ].value = readBuffer.texture;
  285. renderer.setRenderTarget( null );
  286. this.fsQuad.render( renderer );
  287. }
  288. },
  289. getPrepareMaskMaterial: function () {
  290. return new ShaderMaterial( {
  291. uniforms: {
  292. "depthTexture": { value: null },
  293. "cameraNearFar": { value: new Vector2( 0.5, 0.5 ) },
  294. "textureMatrix": { value: null }
  295. },
  296. vertexShader: [
  297. '#include <morphtarget_pars_vertex>',
  298. '#include <skinning_pars_vertex>',
  299. 'varying vec4 projTexCoord;',
  300. 'varying vec4 vPosition;',
  301. 'uniform mat4 textureMatrix;',
  302. 'void main() {',
  303. ' #include <skinbase_vertex>',
  304. ' #include <begin_vertex>',
  305. ' #include <morphtarget_vertex>',
  306. ' #include <skinning_vertex>',
  307. ' #include <project_vertex>',
  308. ' vPosition = mvPosition;',
  309. ' vec4 worldPosition = modelMatrix * vec4( position, 1.0 );',
  310. ' projTexCoord = textureMatrix * worldPosition;',
  311. '}'
  312. ].join( '\n' ),
  313. fragmentShader: [
  314. '#include <packing>',
  315. 'varying vec4 vPosition;',
  316. 'varying vec4 projTexCoord;',
  317. 'uniform sampler2D depthTexture;',
  318. 'uniform vec2 cameraNearFar;',
  319. 'void main() {',
  320. ' float depth = unpackRGBAToDepth(texture2DProj( depthTexture, projTexCoord ));',
  321. ' float viewZ = - DEPTH_TO_VIEW_Z( depth, cameraNearFar.x, cameraNearFar.y );',
  322. ' float depthTest = (-vPosition.z > viewZ) ? 1.0 : 0.0;',
  323. ' gl_FragColor = vec4(0.0, depthTest, 1.0, 1.0);',
  324. '}'
  325. ].join( '\n' )
  326. } );
  327. },
  328. getEdgeDetectionMaterial: function () {
  329. return new ShaderMaterial( {
  330. uniforms: {
  331. "maskTexture": { value: null },
  332. "texSize": { value: new Vector2( 0.5, 0.5 ) },
  333. "visibleEdgeColor": { value: new Vector3( 1.0, 1.0, 1.0 ) },
  334. "hiddenEdgeColor": { value: new Vector3( 1.0, 1.0, 1.0 ) },
  335. },
  336. vertexShader:
  337. "varying vec2 vUv;\n\
  338. void main() {\n\
  339. vUv = uv;\n\
  340. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\n\
  341. }",
  342. fragmentShader:
  343. "varying vec2 vUv;\
  344. uniform sampler2D maskTexture;\
  345. uniform vec2 texSize;\
  346. uniform vec3 visibleEdgeColor;\
  347. uniform vec3 hiddenEdgeColor;\
  348. \
  349. void main() {\n\
  350. vec2 invSize = 1.0 / texSize;\
  351. vec4 uvOffset = vec4(1.0, 0.0, 0.0, 1.0) * vec4(invSize, invSize);\
  352. vec4 c1 = texture2D( maskTexture, vUv + uvOffset.xy);\
  353. vec4 c2 = texture2D( maskTexture, vUv - uvOffset.xy);\
  354. vec4 c3 = texture2D( maskTexture, vUv + uvOffset.yw);\
  355. vec4 c4 = texture2D( maskTexture, vUv - uvOffset.yw);\
  356. float diff1 = (c1.r - c2.r)*0.5;\
  357. float diff2 = (c3.r - c4.r)*0.5;\
  358. float d = length( vec2(diff1, diff2) );\
  359. float a1 = min(c1.g, c2.g);\
  360. float a2 = min(c3.g, c4.g);\
  361. float visibilityFactor = min(a1, a2);\
  362. vec3 edgeColor = 1.0 - visibilityFactor > 0.001 ? visibleEdgeColor : hiddenEdgeColor;\
  363. gl_FragColor = vec4(edgeColor, 1.0) * vec4(d);\
  364. }"
  365. } );
  366. },
  367. getSeperableBlurMaterial: function ( maxRadius ) {
  368. return new ShaderMaterial( {
  369. defines: {
  370. "MAX_RADIUS": maxRadius,
  371. },
  372. uniforms: {
  373. "colorTexture": { value: null },
  374. "texSize": { value: new Vector2( 0.5, 0.5 ) },
  375. "direction": { value: new Vector2( 0.5, 0.5 ) },
  376. "kernelRadius": { value: 1.0 }
  377. },
  378. vertexShader:
  379. "varying vec2 vUv;\n\
  380. void main() {\n\
  381. vUv = uv;\n\
  382. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\n\
  383. }",
  384. fragmentShader:
  385. "#include <common>\
  386. varying vec2 vUv;\
  387. uniform sampler2D colorTexture;\
  388. uniform vec2 texSize;\
  389. uniform vec2 direction;\
  390. uniform float kernelRadius;\
  391. \
  392. float gaussianPdf(in float x, in float sigma) {\
  393. return 0.39894 * exp( -0.5 * x * x/( sigma * sigma))/sigma;\
  394. }\
  395. void main() {\
  396. vec2 invSize = 1.0 / texSize;\
  397. float weightSum = gaussianPdf(0.0, kernelRadius);\
  398. vec4 diffuseSum = texture2D( colorTexture, vUv) * weightSum;\
  399. vec2 delta = direction * invSize * kernelRadius/float(MAX_RADIUS);\
  400. vec2 uvOffset = delta;\
  401. for( int i = 1; i <= MAX_RADIUS; i ++ ) {\
  402. float w = gaussianPdf(uvOffset.x, kernelRadius);\
  403. vec4 sample1 = texture2D( colorTexture, vUv + uvOffset);\
  404. vec4 sample2 = texture2D( colorTexture, vUv - uvOffset);\
  405. diffuseSum += ((sample1 + sample2) * w);\
  406. weightSum += (2.0 * w);\
  407. uvOffset += delta;\
  408. }\
  409. gl_FragColor = diffuseSum/weightSum;\
  410. }"
  411. } );
  412. },
  413. getOverlayMaterial: function () {
  414. return new ShaderMaterial( {
  415. uniforms: {
  416. "maskTexture": { value: null },
  417. "edgeTexture1": { value: null },
  418. "edgeTexture2": { value: null },
  419. "patternTexture": { value: null },
  420. "edgeStrength": { value: 1.0 },
  421. "edgeGlow": { value: 1.0 },
  422. "usePatternTexture": { value: 0.0 }
  423. },
  424. vertexShader:
  425. "varying vec2 vUv;\n\
  426. void main() {\n\
  427. vUv = uv;\n\
  428. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\n\
  429. }",
  430. fragmentShader:
  431. "varying vec2 vUv;\
  432. uniform sampler2D maskTexture;\
  433. uniform sampler2D edgeTexture1;\
  434. uniform sampler2D edgeTexture2;\
  435. uniform sampler2D patternTexture;\
  436. uniform float edgeStrength;\
  437. uniform float edgeGlow;\
  438. uniform bool usePatternTexture;\
  439. \
  440. void main() {\
  441. vec4 edgeValue1 = texture2D(edgeTexture1, vUv);\
  442. vec4 edgeValue2 = texture2D(edgeTexture2, vUv);\
  443. vec4 maskColor = texture2D(maskTexture, vUv);\
  444. vec4 patternColor = texture2D(patternTexture, 6.0 * vUv);\
  445. float visibilityFactor = 1.0 - maskColor.g > 0.0 ? 1.0 : 0.5;\
  446. vec4 edgeValue = edgeValue1 + edgeValue2 * edgeGlow;\
  447. vec4 finalColor = edgeStrength * maskColor.r * edgeValue;\
  448. if(usePatternTexture)\
  449. finalColor += + visibilityFactor * (1.0 - maskColor.r) * (1.0 - patternColor.r);\
  450. gl_FragColor = finalColor;\
  451. }",
  452. blending: AdditiveBlending,
  453. depthTest: false,
  454. depthWrite: false,
  455. transparent: true
  456. } );
  457. }
  458. } );
  459. OutlinePass.BlurDirectionX = new Vector2( 1.0, 0.0 );
  460. OutlinePass.BlurDirectionY = new Vector2( 0.0, 1.0 );
  461. export { OutlinePass };