0
0

OutlinePass.js 20 KB

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