OutlinePass.js 20 KB

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