OutlinePass.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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.fsQuad = new THREE.Pass.FullScreenQuad( null );
  82. this.tempPulseColor1 = new THREE.Color();
  83. this.tempPulseColor2 = new THREE.Color();
  84. this.textureMatrix = new THREE.Matrix4();
  85. function replaceDepthToViewZ( string, camera ) {
  86. var type = camera.isPerspectiveCamera ? 'perspective' : 'orthographic';
  87. return string.replace( /DEPTH_TO_VIEW_Z/g, type + 'DepthToViewZ' );
  88. }
  89. };
  90. THREE.OutlinePass.prototype = Object.assign( Object.create( THREE.Pass.prototype ), {
  91. constructor: THREE.OutlinePass,
  92. dispose: function () {
  93. this.renderTargetMaskBuffer.dispose();
  94. this.renderTargetDepthBuffer.dispose();
  95. this.renderTargetMaskDownSampleBuffer.dispose();
  96. this.renderTargetBlurBuffer1.dispose();
  97. this.renderTargetBlurBuffer2.dispose();
  98. this.renderTargetEdgeBuffer1.dispose();
  99. this.renderTargetEdgeBuffer2.dispose();
  100. },
  101. setSize: function ( width, height ) {
  102. this.renderTargetMaskBuffer.setSize( width, height );
  103. var resx = Math.round( width / this.downSampleRatio );
  104. var resy = Math.round( height / this.downSampleRatio );
  105. this.renderTargetMaskDownSampleBuffer.setSize( resx, resy );
  106. this.renderTargetBlurBuffer1.setSize( resx, resy );
  107. this.renderTargetEdgeBuffer1.setSize( resx, resy );
  108. this.separableBlurMaterial1.uniforms[ "texSize" ].value = new THREE.Vector2( resx, resy );
  109. resx = Math.round( resx / 2 );
  110. resy = Math.round( resy / 2 );
  111. this.renderTargetBlurBuffer2.setSize( resx, resy );
  112. this.renderTargetEdgeBuffer2.setSize( resx, resy );
  113. this.separableBlurMaterial2.uniforms[ "texSize" ].value = new THREE.Vector2( resx, resy );
  114. },
  115. changeVisibilityOfSelectedObjects: function ( bVisible ) {
  116. function gatherSelectedMeshesCallBack( object ) {
  117. if ( object.isMesh ) {
  118. if ( bVisible ) {
  119. object.visible = object.userData.oldVisible;
  120. delete object.userData.oldVisible;
  121. } else {
  122. object.userData.oldVisible = object.visible;
  123. object.visible = bVisible;
  124. }
  125. }
  126. }
  127. for ( var i = 0; i < this.selectedObjects.length; i ++ ) {
  128. var selectedObject = this.selectedObjects[ i ];
  129. selectedObject.traverse( gatherSelectedMeshesCallBack );
  130. }
  131. },
  132. changeVisibilityOfNonSelectedObjects: function ( bVisible ) {
  133. var selectedMeshes = [];
  134. function gatherSelectedMeshesCallBack( object ) {
  135. if ( object.isMesh ) selectedMeshes.push( object );
  136. }
  137. for ( var i = 0; i < this.selectedObjects.length; i ++ ) {
  138. var selectedObject = this.selectedObjects[ i ];
  139. selectedObject.traverse( gatherSelectedMeshesCallBack );
  140. }
  141. function VisibilityChangeCallBack( object ) {
  142. if ( object.isMesh || object.isLine || object.isSprite ) {
  143. var bFound = false;
  144. for ( var i = 0; i < selectedMeshes.length; i ++ ) {
  145. var selectedObjectId = selectedMeshes[ i ].id;
  146. if ( selectedObjectId === object.id ) {
  147. bFound = true;
  148. break;
  149. }
  150. }
  151. if ( ! bFound ) {
  152. var visibility = object.visible;
  153. if ( ! bVisible || object.bVisible ) object.visible = bVisible;
  154. object.bVisible = visibility;
  155. }
  156. }
  157. }
  158. this.renderScene.traverse( VisibilityChangeCallBack );
  159. },
  160. updateTextureMatrix: function () {
  161. this.textureMatrix.set( 0.5, 0.0, 0.0, 0.5,
  162. 0.0, 0.5, 0.0, 0.5,
  163. 0.0, 0.0, 0.5, 0.5,
  164. 0.0, 0.0, 0.0, 1.0 );
  165. this.textureMatrix.multiply( this.renderCamera.projectionMatrix );
  166. this.textureMatrix.multiply( this.renderCamera.matrixWorldInverse );
  167. },
  168. render: function ( renderer, writeBuffer, readBuffer, deltaTime, maskActive ) {
  169. if ( this.selectedObjects.length > 0 ) {
  170. this.oldClearColor.copy( renderer.getClearColor() );
  171. this.oldClearAlpha = renderer.getClearAlpha();
  172. var oldAutoClear = renderer.autoClear;
  173. renderer.autoClear = false;
  174. if ( maskActive ) renderer.state.buffers.stencil.setTest( false );
  175. renderer.setClearColor( 0xffffff, 1 );
  176. // Make selected objects invisible
  177. this.changeVisibilityOfSelectedObjects( false );
  178. var currentBackground = this.renderScene.background;
  179. this.renderScene.background = null;
  180. // 1. Draw Non Selected objects in the depth buffer
  181. this.renderScene.overrideMaterial = this.depthMaterial;
  182. renderer.setRenderTarget( this.renderTargetDepthBuffer );
  183. renderer.clear();
  184. renderer.render( this.renderScene, this.renderCamera );
  185. // Make selected objects visible
  186. this.changeVisibilityOfSelectedObjects( true );
  187. // Update Texture Matrix for Depth compare
  188. this.updateTextureMatrix();
  189. // Make non selected objects invisible, and draw only the selected objects, by comparing the depth buffer of non selected objects
  190. this.changeVisibilityOfNonSelectedObjects( false );
  191. this.renderScene.overrideMaterial = this.prepareMaskMaterial;
  192. this.prepareMaskMaterial.uniforms[ "cameraNearFar" ].value = new THREE.Vector2( this.renderCamera.near, this.renderCamera.far );
  193. this.prepareMaskMaterial.uniforms[ "depthTexture" ].value = this.renderTargetDepthBuffer.texture;
  194. this.prepareMaskMaterial.uniforms[ "textureMatrix" ].value = this.textureMatrix;
  195. renderer.setRenderTarget( this.renderTargetMaskBuffer );
  196. renderer.clear();
  197. renderer.render( this.renderScene, this.renderCamera );
  198. this.renderScene.overrideMaterial = null;
  199. this.changeVisibilityOfNonSelectedObjects( true );
  200. this.renderScene.background = currentBackground;
  201. // 2. Downsample to Half resolution
  202. this.fsQuad.material = this.materialCopy;
  203. this.copyUniforms[ "tDiffuse" ].value = this.renderTargetMaskBuffer.texture;
  204. renderer.setRenderTarget( this.renderTargetMaskDownSampleBuffer );
  205. renderer.clear();
  206. this.fsQuad.render( renderer );
  207. this.tempPulseColor1.copy( this.visibleEdgeColor );
  208. this.tempPulseColor2.copy( this.hiddenEdgeColor );
  209. if ( this.pulsePeriod > 0 ) {
  210. var scalar = ( 1 + 0.25 ) / 2 + Math.cos( performance.now() * 0.01 / this.pulsePeriod ) * ( 1.0 - 0.25 ) / 2;
  211. this.tempPulseColor1.multiplyScalar( scalar );
  212. this.tempPulseColor2.multiplyScalar( scalar );
  213. }
  214. // 3. Apply Edge Detection Pass
  215. this.fsQuad.material = this.edgeDetectionMaterial;
  216. this.edgeDetectionMaterial.uniforms[ "maskTexture" ].value = this.renderTargetMaskDownSampleBuffer.texture;
  217. this.edgeDetectionMaterial.uniforms[ "texSize" ].value = new THREE.Vector2( this.renderTargetMaskDownSampleBuffer.width, this.renderTargetMaskDownSampleBuffer.height );
  218. this.edgeDetectionMaterial.uniforms[ "visibleEdgeColor" ].value = this.tempPulseColor1;
  219. this.edgeDetectionMaterial.uniforms[ "hiddenEdgeColor" ].value = this.tempPulseColor2;
  220. renderer.setRenderTarget( this.renderTargetEdgeBuffer1 );
  221. renderer.clear();
  222. this.fsQuad.render( renderer );
  223. // 4. Apply Blur on Half res
  224. this.fsQuad.material = this.separableBlurMaterial1;
  225. this.separableBlurMaterial1.uniforms[ "colorTexture" ].value = this.renderTargetEdgeBuffer1.texture;
  226. this.separableBlurMaterial1.uniforms[ "direction" ].value = THREE.OutlinePass.BlurDirectionX;
  227. this.separableBlurMaterial1.uniforms[ "kernelRadius" ].value = this.edgeThickness;
  228. renderer.setRenderTarget( this.renderTargetBlurBuffer1 );
  229. renderer.clear();
  230. this.fsQuad.render( renderer );
  231. this.separableBlurMaterial1.uniforms[ "colorTexture" ].value = this.renderTargetBlurBuffer1.texture;
  232. this.separableBlurMaterial1.uniforms[ "direction" ].value = THREE.OutlinePass.BlurDirectionY;
  233. renderer.setRenderTarget( this.renderTargetEdgeBuffer1 );
  234. renderer.clear();
  235. this.fsQuad.render( renderer );
  236. // Apply Blur on quarter res
  237. this.fsQuad.material = this.separableBlurMaterial2;
  238. this.separableBlurMaterial2.uniforms[ "colorTexture" ].value = this.renderTargetEdgeBuffer1.texture;
  239. this.separableBlurMaterial2.uniforms[ "direction" ].value = THREE.OutlinePass.BlurDirectionX;
  240. renderer.setRenderTarget( this.renderTargetBlurBuffer2 );
  241. renderer.clear();
  242. this.fsQuad.render( renderer );
  243. this.separableBlurMaterial2.uniforms[ "colorTexture" ].value = this.renderTargetBlurBuffer2.texture;
  244. this.separableBlurMaterial2.uniforms[ "direction" ].value = THREE.OutlinePass.BlurDirectionY;
  245. renderer.setRenderTarget( this.renderTargetEdgeBuffer2 );
  246. renderer.clear();
  247. this.fsQuad.render( renderer );
  248. // Blend it additively over the input texture
  249. this.fsQuad.material = this.overlayMaterial;
  250. this.overlayMaterial.uniforms[ "maskTexture" ].value = this.renderTargetMaskBuffer.texture;
  251. this.overlayMaterial.uniforms[ "edgeTexture1" ].value = this.renderTargetEdgeBuffer1.texture;
  252. this.overlayMaterial.uniforms[ "edgeTexture2" ].value = this.renderTargetEdgeBuffer2.texture;
  253. this.overlayMaterial.uniforms[ "patternTexture" ].value = this.patternTexture;
  254. this.overlayMaterial.uniforms[ "edgeStrength" ].value = this.edgeStrength;
  255. this.overlayMaterial.uniforms[ "edgeGlow" ].value = this.edgeGlow;
  256. this.overlayMaterial.uniforms[ "usePatternTexture" ].value = this.usePatternTexture;
  257. if ( maskActive ) renderer.state.buffers.stencil.setTest( true );
  258. renderer.setRenderTarget( readBuffer );
  259. this.fsQuad.render( renderer );
  260. renderer.setClearColor( this.oldClearColor, this.oldClearAlpha );
  261. renderer.autoClear = oldAutoClear;
  262. }
  263. if ( this.renderToScreen ) {
  264. this.fsQuad.material = this.materialCopy;
  265. this.copyUniforms[ "tDiffuse" ].value = readBuffer.texture;
  266. renderer.setRenderTarget( null );
  267. this.fsQuad.render( renderer );
  268. }
  269. },
  270. getPrepareMaskMaterial: function () {
  271. return new THREE.ShaderMaterial( {
  272. uniforms: {
  273. "depthTexture": { value: null },
  274. "cameraNearFar": { value: new THREE.Vector2( 0.5, 0.5 ) },
  275. "textureMatrix": { value: new THREE.Matrix4() }
  276. },
  277. vertexShader: [
  278. 'varying vec4 projTexCoord;',
  279. 'varying vec4 vPosition;',
  280. 'uniform mat4 textureMatrix;',
  281. 'void main() {',
  282. ' vPosition = modelViewMatrix * vec4( position, 1.0 );',
  283. ' vec4 worldPosition = modelMatrix * vec4( position, 1.0 );',
  284. ' projTexCoord = textureMatrix * worldPosition;',
  285. ' gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );',
  286. '}'
  287. ].join( '\n' ),
  288. fragmentShader: [
  289. '#include <packing>',
  290. 'varying vec4 vPosition;',
  291. 'varying vec4 projTexCoord;',
  292. 'uniform sampler2D depthTexture;',
  293. 'uniform vec2 cameraNearFar;',
  294. 'void main() {',
  295. ' float depth = unpackRGBAToDepth(texture2DProj( depthTexture, projTexCoord ));',
  296. ' float viewZ = - DEPTH_TO_VIEW_Z( depth, cameraNearFar.x, cameraNearFar.y );',
  297. ' float depthTest = (-vPosition.z > viewZ) ? 1.0 : 0.0;',
  298. ' gl_FragColor = vec4(0.0, depthTest, 1.0, 1.0);',
  299. '}'
  300. ].join( '\n' )
  301. } );
  302. },
  303. getEdgeDetectionMaterial: function () {
  304. return new THREE.ShaderMaterial( {
  305. uniforms: {
  306. "maskTexture": { value: null },
  307. "texSize": { value: new THREE.Vector2( 0.5, 0.5 ) },
  308. "visibleEdgeColor": { value: new THREE.Vector3( 1.0, 1.0, 1.0 ) },
  309. "hiddenEdgeColor": { value: new THREE.Vector3( 1.0, 1.0, 1.0 ) },
  310. },
  311. vertexShader:
  312. "varying vec2 vUv;\n\
  313. void main() {\n\
  314. vUv = uv;\n\
  315. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\n\
  316. }",
  317. fragmentShader:
  318. "varying vec2 vUv;\
  319. uniform sampler2D maskTexture;\
  320. uniform vec2 texSize;\
  321. uniform vec3 visibleEdgeColor;\
  322. uniform vec3 hiddenEdgeColor;\
  323. \
  324. void main() {\n\
  325. vec2 invSize = 1.0 / texSize;\
  326. vec4 uvOffset = vec4(1.0, 0.0, 0.0, 1.0) * vec4(invSize, invSize);\
  327. vec4 c1 = texture2D( maskTexture, vUv + uvOffset.xy);\
  328. vec4 c2 = texture2D( maskTexture, vUv - uvOffset.xy);\
  329. vec4 c3 = texture2D( maskTexture, vUv + uvOffset.yw);\
  330. vec4 c4 = texture2D( maskTexture, vUv - uvOffset.yw);\
  331. float diff1 = (c1.r - c2.r)*0.5;\
  332. float diff2 = (c3.r - c4.r)*0.5;\
  333. float d = length( vec2(diff1, diff2) );\
  334. float a1 = min(c1.g, c2.g);\
  335. float a2 = min(c3.g, c4.g);\
  336. float visibilityFactor = min(a1, a2);\
  337. vec3 edgeColor = 1.0 - visibilityFactor > 0.001 ? visibleEdgeColor : hiddenEdgeColor;\
  338. gl_FragColor = vec4(edgeColor, 1.0) * vec4(d);\
  339. }"
  340. } );
  341. },
  342. getSeperableBlurMaterial: function ( maxRadius ) {
  343. return new THREE.ShaderMaterial( {
  344. defines: {
  345. "MAX_RADIUS": maxRadius,
  346. },
  347. uniforms: {
  348. "colorTexture": { value: null },
  349. "texSize": { value: new THREE.Vector2( 0.5, 0.5 ) },
  350. "direction": { value: new THREE.Vector2( 0.5, 0.5 ) },
  351. "kernelRadius": { value: 1.0 }
  352. },
  353. vertexShader:
  354. "varying vec2 vUv;\n\
  355. void main() {\n\
  356. vUv = uv;\n\
  357. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\n\
  358. }",
  359. fragmentShader:
  360. "#include <common>\
  361. varying vec2 vUv;\
  362. uniform sampler2D colorTexture;\
  363. uniform vec2 texSize;\
  364. uniform vec2 direction;\
  365. uniform float kernelRadius;\
  366. \
  367. float gaussianPdf(in float x, in float sigma) {\
  368. return 0.39894 * exp( -0.5 * x * x/( sigma * sigma))/sigma;\
  369. }\
  370. void main() {\
  371. vec2 invSize = 1.0 / texSize;\
  372. float weightSum = gaussianPdf(0.0, kernelRadius);\
  373. vec4 diffuseSum = texture2D( colorTexture, vUv) * weightSum;\
  374. vec2 delta = direction * invSize * kernelRadius/float(MAX_RADIUS);\
  375. vec2 uvOffset = delta;\
  376. for( int i = 1; i <= MAX_RADIUS; i ++ ) {\
  377. float w = gaussianPdf(uvOffset.x, kernelRadius);\
  378. vec4 sample1 = texture2D( colorTexture, vUv + uvOffset);\
  379. vec4 sample2 = texture2D( colorTexture, vUv - uvOffset);\
  380. diffuseSum += ((sample1 + sample2) * w);\
  381. weightSum += (2.0 * w);\
  382. uvOffset += delta;\
  383. }\
  384. gl_FragColor = diffuseSum/weightSum;\
  385. }"
  386. } );
  387. },
  388. getOverlayMaterial: function () {
  389. return new THREE.ShaderMaterial( {
  390. uniforms: {
  391. "maskTexture": { value: null },
  392. "edgeTexture1": { value: null },
  393. "edgeTexture2": { value: null },
  394. "patternTexture": { value: null },
  395. "edgeStrength": { value: 1.0 },
  396. "edgeGlow": { value: 1.0 },
  397. "usePatternTexture": { value: 0.0 }
  398. },
  399. vertexShader:
  400. "varying vec2 vUv;\n\
  401. void main() {\n\
  402. vUv = uv;\n\
  403. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\n\
  404. }",
  405. fragmentShader:
  406. "varying vec2 vUv;\
  407. uniform sampler2D maskTexture;\
  408. uniform sampler2D edgeTexture1;\
  409. uniform sampler2D edgeTexture2;\
  410. uniform sampler2D patternTexture;\
  411. uniform float edgeStrength;\
  412. uniform float edgeGlow;\
  413. uniform bool usePatternTexture;\
  414. \
  415. void main() {\
  416. vec4 edgeValue1 = texture2D(edgeTexture1, vUv);\
  417. vec4 edgeValue2 = texture2D(edgeTexture2, vUv);\
  418. vec4 maskColor = texture2D(maskTexture, vUv);\
  419. vec4 patternColor = texture2D(patternTexture, 6.0 * vUv);\
  420. float visibilityFactor = 1.0 - maskColor.g > 0.0 ? 1.0 : 0.5;\
  421. vec4 edgeValue = edgeValue1 + edgeValue2 * edgeGlow;\
  422. vec4 finalColor = edgeStrength * maskColor.r * edgeValue;\
  423. if(usePatternTexture)\
  424. finalColor += + visibilityFactor * (1.0 - maskColor.r) * (1.0 - patternColor.r);\
  425. gl_FragColor = finalColor;\
  426. }",
  427. blending: THREE.AdditiveBlending,
  428. depthTest: false,
  429. depthWrite: false,
  430. transparent: true
  431. } );
  432. }
  433. } );
  434. THREE.OutlinePass.BlurDirectionX = new THREE.Vector2( 1.0, 0.0 );
  435. THREE.OutlinePass.BlurDirectionY = new THREE.Vector2( 0.0, 1.0 );