OutlinePass.js 19 KB

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