OutlinePass.js 20 KB

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