OutlinePass.js 20 KB

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