OutlinePass.js 19 KB

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