OutlinePass.js 20 KB

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