OutlinePass.js 20 KB

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