OutlinePass.js 20 KB

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