OutlinePass.js 20 KB

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