OutlinePass.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. /**
  2. * @author spidersharma / http://eduperiment.com/
  3. */
  4. THREE.OutlinePass = function ( resolution, scene, camera, selectedObjects ) {
  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.renderToScreen = false;
  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.renderTargetDepthBuffer = new THREE.WebGLRenderTarget( this.resolution.x, this.resolution.y, pars );
  34. this.renderTargetDepthBuffer.texture.name = "OutlinePass.depth";
  35. this.renderTargetDepthBuffer.texture.generateMipmaps = false;
  36. this.renderTargetMaskDownSampleBuffer = new THREE.WebGLRenderTarget( resx, resy, pars );
  37. this.renderTargetMaskDownSampleBuffer.texture.name = "OutlinePass.depthDownSample";
  38. this.renderTargetMaskDownSampleBuffer.texture.generateMipmaps = false;
  39. this.renderTargetBlurBuffer1 = new THREE.WebGLRenderTarget( resx, resy, pars );
  40. this.renderTargetBlurBuffer1.texture.name = "OutlinePass.blur1";
  41. this.renderTargetBlurBuffer1.texture.generateMipmaps = false;
  42. this.renderTargetBlurBuffer2 = new THREE.WebGLRenderTarget( Math.round( resx / 2 ), Math.round( resy / 2 ), pars );
  43. this.renderTargetBlurBuffer2.texture.name = "OutlinePass.blur2";
  44. this.renderTargetBlurBuffer2.texture.generateMipmaps = false;
  45. this.edgeDetectionMaterial = this.getEdgeDetectionMaterial();
  46. this.renderTargetEdgeBuffer1 = new THREE.WebGLRenderTarget( resx, resy, pars );
  47. this.renderTargetEdgeBuffer1.texture.name = "OutlinePass.edge1";
  48. this.renderTargetEdgeBuffer1.texture.generateMipmaps = false;
  49. this.renderTargetEdgeBuffer2 = new THREE.WebGLRenderTarget( Math.round( resx / 2 ), Math.round( resy / 2 ), pars );
  50. this.renderTargetEdgeBuffer2.texture.name = "OutlinePass.edge2";
  51. this.renderTargetEdgeBuffer2.texture.generateMipmaps = false;
  52. var MAX_EDGE_THICKNESS = 4;
  53. var MAX_EDGE_GLOW = 4;
  54. this.separableBlurMaterial1 = this.getSeperableBlurMaterial( MAX_EDGE_THICKNESS );
  55. this.separableBlurMaterial1.uniforms[ "texSize" ].value = new THREE.Vector2( resx, resy );
  56. this.separableBlurMaterial1.uniforms[ "kernelRadius" ].value = 1;
  57. this.separableBlurMaterial2 = this.getSeperableBlurMaterial( MAX_EDGE_GLOW );
  58. this.separableBlurMaterial2.uniforms[ "texSize" ].value = new THREE.Vector2( Math.round( resx / 2 ), Math.round( resy / 2 ) );
  59. this.separableBlurMaterial2.uniforms[ "kernelRadius" ].value = MAX_EDGE_GLOW;
  60. // Overlay material
  61. this.overlayMaterial = this.getOverlayMaterial();
  62. // copy material
  63. if ( THREE.CopyShader === undefined )
  64. console.error( "THREE.OutlinePass relies on THREE.CopyShader" );
  65. var copyShader = THREE.CopyShader;
  66. this.copyUniforms = THREE.UniformsUtils.clone( copyShader.uniforms );
  67. this.copyUniforms[ "opacity" ].value = 1.0;
  68. this.materialCopy = new THREE.ShaderMaterial( {
  69. uniforms: this.copyUniforms,
  70. vertexShader: copyShader.vertexShader,
  71. fragmentShader: copyShader.fragmentShader,
  72. blending: THREE.NoBlending,
  73. depthTest: false,
  74. depthWrite: false,
  75. transparent: true
  76. } );
  77. this.enabled = true;
  78. this.needsSwap = false;
  79. this.oldClearColor = new THREE.Color();
  80. this.oldClearAlpha = 1;
  81. this.camera = new THREE.OrthographicCamera( - 1, 1, 1, - 1, 0, 1 );
  82. this.scene = new THREE.Scene();
  83. this.quad = new THREE.Mesh( new THREE.PlaneBufferGeometry( 2, 2 ), null );
  84. this.quad.frustumCulled = false; // Avoid getting clipped
  85. this.scene.add( this.quad );
  86. this.tempPulseColor1 = new THREE.Color();
  87. this.tempPulseColor2 = new THREE.Color();
  88. this.textureMatrix = new THREE.Matrix4();
  89. };
  90. THREE.OutlinePass.prototype = Object.assign( Object.create( THREE.Pass.prototype ), {
  91. constructor: THREE.OutlinePass,
  92. dispose: function () {
  93. this.renderTargetMaskBuffer.dispose();
  94. this.renderTargetDepthBuffer.dispose();
  95. this.renderTargetMaskDownSampleBuffer.dispose();
  96. this.renderTargetBlurBuffer1.dispose();
  97. this.renderTargetBlurBuffer2.dispose();
  98. this.renderTargetEdgeBuffer1.dispose();
  99. this.renderTargetEdgeBuffer2.dispose();
  100. },
  101. setSize: function ( width, height ) {
  102. this.renderTargetMaskBuffer.setSize( width, height );
  103. var resx = Math.round( width / this.downSampleRatio );
  104. var resy = Math.round( height / this.downSampleRatio );
  105. this.renderTargetMaskDownSampleBuffer.setSize( resx, resy );
  106. this.renderTargetBlurBuffer1.setSize( resx, resy );
  107. this.renderTargetEdgeBuffer1.setSize( resx, resy );
  108. this.separableBlurMaterial1.uniforms[ "texSize" ].value = new THREE.Vector2( resx, resy );
  109. resx = Math.round( resx / 2 );
  110. resy = Math.round( resy / 2 );
  111. this.renderTargetBlurBuffer2.setSize( resx, resy );
  112. this.renderTargetEdgeBuffer2.setSize( resx, resy );
  113. this.separableBlurMaterial2.uniforms[ "texSize" ].value = new THREE.Vector2( resx, resy );
  114. },
  115. changeVisibilityOfSelectedObjects: function ( bVisible ) {
  116. function gatherSelectedMeshesCallBack( object ) {
  117. if ( object instanceof THREE.Mesh ) object.visible = bVisible;
  118. }
  119. for ( var i = 0; i < this.selectedObjects.length; i ++ ) {
  120. var selectedObject = this.selectedObjects[ i ];
  121. selectedObject.traverse( gatherSelectedMeshesCallBack );
  122. }
  123. },
  124. changeVisibilityOfNonSelectedObjects: function ( bVisible ) {
  125. var selectedMeshes = [];
  126. function gatherSelectedMeshesCallBack( object ) {
  127. if ( object instanceof THREE.Mesh ) selectedMeshes.push( object );
  128. }
  129. for ( var i = 0; i < this.selectedObjects.length; i ++ ) {
  130. var selectedObject = this.selectedObjects[ i ];
  131. selectedObject.traverse( gatherSelectedMeshesCallBack );
  132. }
  133. function VisibilityChangeCallBack( object ) {
  134. if ( object instanceof THREE.Mesh ) {
  135. var bFound = false;
  136. for ( var i = 0; i < selectedMeshes.length; i ++ ) {
  137. var selectedObjectId = selectedMeshes[ i ].id;
  138. if ( selectedObjectId === object.id ) {
  139. bFound = true;
  140. break;
  141. }
  142. }
  143. if ( ! bFound ) {
  144. var visibility = object.visible;
  145. if ( ! bVisible || object.bVisible ) object.visible = bVisible;
  146. object.bVisible = visibility;
  147. }
  148. }
  149. }
  150. this.renderScene.traverse( VisibilityChangeCallBack );
  151. },
  152. updateTextureMatrix: function () {
  153. this.textureMatrix.set( 0.5, 0.0, 0.0, 0.5,
  154. 0.0, 0.5, 0.0, 0.5,
  155. 0.0, 0.0, 0.5, 0.5,
  156. 0.0, 0.0, 0.0, 1.0 );
  157. this.textureMatrix.multiply( this.renderCamera.projectionMatrix );
  158. this.textureMatrix.multiply( this.renderCamera.matrixWorldInverse );
  159. },
  160. render: function ( renderer, writeBuffer, readBuffer, delta, maskActive ) {
  161. if ( this.selectedObjects.length === 0 ) return;
  162. this.oldClearColor.copy( renderer.getClearColor() );
  163. this.oldClearAlpha = renderer.getClearAlpha();
  164. var oldAutoClear = renderer.autoClear;
  165. renderer.autoClear = false;
  166. if ( maskActive ) renderer.context.disable( renderer.context.STENCIL_TEST );
  167. renderer.setClearColor( 0xffffff, 1 );
  168. // Make selected objects invisible
  169. this.changeVisibilityOfSelectedObjects( false );
  170. // 1. Draw Non Selected objects in the depth buffer
  171. this.renderScene.overrideMaterial = this.depthMaterial;
  172. renderer.render( this.renderScene, this.renderCamera, this.renderTargetDepthBuffer, true );
  173. // Make selected objects visible
  174. this.changeVisibilityOfSelectedObjects( true );
  175. // Update Texture Matrix for Depth compare
  176. this.updateTextureMatrix();
  177. // Make non selected objects invisible, and draw only the selected objects, by comparing the depth buffer of non selected objects
  178. this.changeVisibilityOfNonSelectedObjects( false );
  179. this.renderScene.overrideMaterial = this.prepareMaskMaterial;
  180. this.prepareMaskMaterial.uniforms[ "cameraNearFar" ].value = new THREE.Vector2( this.renderCamera.near, this.renderCamera.far );
  181. this.prepareMaskMaterial.uniforms[ "depthTexture" ].value = this.renderTargetDepthBuffer.texture;
  182. this.prepareMaskMaterial.uniforms[ "textureMatrix" ].value = this.textureMatrix;
  183. renderer.render( this.renderScene, this.renderCamera, this.renderTargetMaskBuffer, true );
  184. this.renderScene.overrideMaterial = null;
  185. this.changeVisibilityOfNonSelectedObjects( true );
  186. // 2. Downsample to Half resolution
  187. this.quad.material = this.materialCopy;
  188. this.copyUniforms[ "tDiffuse" ].value = this.renderTargetMaskBuffer.texture;
  189. renderer.render( this.scene, this.camera, this.renderTargetMaskDownSampleBuffer, true );
  190. this.tempPulseColor1.copy( this.visibleEdgeColor );
  191. this.tempPulseColor2.copy( this.hiddenEdgeColor );
  192. if ( this.pulsePeriod > 0 ) {
  193. var scalar = ( 1 + 0.25 ) / 2 + Math.cos( performance.now() * 0.01 / this.pulsePeriod ) * ( 1.0 - 0.25 ) / 2;
  194. this.tempPulseColor1.multiplyScalar( scalar );
  195. this.tempPulseColor2.multiplyScalar( scalar );
  196. }
  197. // 3. Apply Edge Detection Pass
  198. this.quad.material = this.edgeDetectionMaterial;
  199. this.edgeDetectionMaterial.uniforms[ "maskTexture" ].value = this.renderTargetMaskDownSampleBuffer.texture;
  200. this.edgeDetectionMaterial.uniforms[ "texSize" ].value = new THREE.Vector2( this.renderTargetMaskDownSampleBuffer.width, this.renderTargetMaskDownSampleBuffer.height );
  201. this.edgeDetectionMaterial.uniforms[ "visibleEdgeColor" ].value = this.tempPulseColor1;
  202. this.edgeDetectionMaterial.uniforms[ "hiddenEdgeColor" ].value = this.tempPulseColor2;
  203. renderer.render( this.scene, this.camera, this.renderTargetEdgeBuffer1, true );
  204. // 4. Apply Blur on Half res
  205. this.quad.material = this.separableBlurMaterial1;
  206. this.separableBlurMaterial1.uniforms[ "colorTexture" ].value = this.renderTargetEdgeBuffer1.texture;
  207. this.separableBlurMaterial1.uniforms[ "direction" ].value = THREE.OutlinePass.BlurDirectionX;
  208. this.separableBlurMaterial1.uniforms[ "kernelRadius" ].value = this.edgeThickness;
  209. renderer.render( this.scene, this.camera, this.renderTargetBlurBuffer1, true );
  210. this.separableBlurMaterial1.uniforms[ "colorTexture" ].value = this.renderTargetBlurBuffer1.texture;
  211. this.separableBlurMaterial1.uniforms[ "direction" ].value = THREE.OutlinePass.BlurDirectionY;
  212. renderer.render( this.scene, this.camera, this.renderTargetEdgeBuffer1, true );
  213. // Apply Blur on quarter res
  214. this.quad.material = this.separableBlurMaterial2;
  215. this.separableBlurMaterial2.uniforms[ "colorTexture" ].value = this.renderTargetEdgeBuffer1.texture;
  216. this.separableBlurMaterial2.uniforms[ "direction" ].value = THREE.OutlinePass.BlurDirectionX;
  217. renderer.render( this.scene, this.camera, this.renderTargetBlurBuffer2, true );
  218. this.separableBlurMaterial2.uniforms[ "colorTexture" ].value = this.renderTargetBlurBuffer2.texture;
  219. this.separableBlurMaterial2.uniforms[ "direction" ].value = THREE.OutlinePass.BlurDirectionY;
  220. renderer.render( this.scene, this.camera, this.renderTargetEdgeBuffer2, true );
  221. // Blend it additively over the input texture
  222. this.quad.material = this.overlayMaterial;
  223. this.overlayMaterial.uniforms[ "maskTexture" ].value = this.renderTargetMaskBuffer.texture;
  224. this.overlayMaterial.uniforms[ "edgeTexture1" ].value = this.renderTargetEdgeBuffer1.texture;
  225. this.overlayMaterial.uniforms[ "edgeTexture2" ].value = this.renderTargetEdgeBuffer2.texture;
  226. this.overlayMaterial.uniforms[ "patternTexture" ].value = this.patternTexture;
  227. this.overlayMaterial.uniforms[ "edgeStrength" ].value = this.edgeStrength;
  228. this.overlayMaterial.uniforms[ "edgeGlow" ].value = this.edgeGlow;
  229. this.overlayMaterial.uniforms[ "usePatternTexture" ].value = this.usePatternTexture;
  230. this.overlayMaterial.uniforms[ "visibleEdgeColor" ].value = this.tempPulseColor1;
  231. this.overlayMaterial.uniforms[ "hiddenEdgeColor" ].value = this.tempPulseColor2;
  232. if ( maskActive ) renderer.context.enable( renderer.context.STENCIL_TEST );
  233. renderer.render( this.scene, this.camera, readBuffer, false );
  234. renderer.setClearColor( this.oldClearColor, this.oldClearAlpha );
  235. renderer.autoClear = oldAutoClear;
  236. },
  237. getPrepareMaskMaterial: function () {
  238. return new THREE.ShaderMaterial( {
  239. uniforms: {
  240. "depthTexture": { value: null },
  241. "cameraNearFar": { value: new THREE.Vector2( 0.5, 0.5 ) },
  242. "textureMatrix": { value: new THREE.Matrix4() }
  243. },
  244. vertexShader:
  245. "varying vec2 vUv;\
  246. varying vec4 projTexCoord;\
  247. varying vec4 vPosition;\
  248. uniform mat4 textureMatrix;\
  249. void main() {\
  250. vUv = uv;\
  251. vPosition = modelViewMatrix * vec4( position, 1.0 );\
  252. vec4 worldPosition = modelMatrix * vec4( position, 1.0 );\
  253. projTexCoord = textureMatrix * worldPosition;\
  254. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\n\
  255. }",
  256. fragmentShader:
  257. "#include <packing>\
  258. varying vec2 vUv;\
  259. varying vec4 vPosition;\
  260. varying vec4 projTexCoord;\
  261. uniform sampler2D depthTexture;\
  262. uniform vec2 cameraNearFar;\
  263. \
  264. void main() {\
  265. float depth = unpackRGBAToDepth(texture2DProj( depthTexture, projTexCoord ));\
  266. float viewZ = -perspectiveDepthToViewZ( depth, cameraNearFar.x, cameraNearFar.y );\
  267. float depthTest = (-vPosition.z > viewZ) ? 1.0 : 0.0;\
  268. gl_FragColor = vec4(0.0, depthTest, 1.0, 0.0);\
  269. }"
  270. } );
  271. },
  272. getEdgeDetectionMaterial: function () {
  273. return new THREE.ShaderMaterial( {
  274. uniforms: {
  275. "maskTexture": { value: null },
  276. "texSize": { value: new THREE.Vector2( 0.5, 0.5 ) },
  277. "visibleEdgeColor": { value: new THREE.Vector3( 1.0, 1.0, 1.0 ) },
  278. "hiddenEdgeColor": { value: new THREE.Vector3( 1.0, 1.0, 1.0 ) },
  279. },
  280. vertexShader:
  281. "varying vec2 vUv;\n\
  282. void main() {\n\
  283. vUv = uv;\n\
  284. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\n\
  285. }",
  286. fragmentShader:
  287. "varying vec2 vUv;\
  288. uniform sampler2D maskTexture;\
  289. uniform vec2 texSize;\
  290. uniform vec3 visibleEdgeColor;\
  291. uniform vec3 hiddenEdgeColor;\
  292. \
  293. void main() {\n\
  294. vec2 invSize = 1.0 / texSize;\
  295. vec4 uvOffset = vec4(1.0, 0.0, 0.0, 1.0) * vec4(invSize, invSize);\
  296. vec4 c1 = texture2D( maskTexture, vUv + uvOffset.xy);\
  297. vec4 c2 = texture2D( maskTexture, vUv - uvOffset.xy);\
  298. vec4 c3 = texture2D( maskTexture, vUv + uvOffset.yw);\
  299. vec4 c4 = texture2D( maskTexture, vUv - uvOffset.yw);\
  300. float diff1 = (c1.r - c2.r)*0.5;\
  301. float diff2 = (c3.r - c4.r)*0.5;\
  302. float d = length( vec2(diff1, diff2) );\
  303. float a1 = min(c1.g, c2.g);\
  304. float a2 = min(c3.g, c4.g);\
  305. float visibilityFactor = min(a1, a2);\
  306. vec3 edgeColor = 1.0 - visibilityFactor > 0.001 ? vec3(1,0,0) : vec3(0,1,0);\
  307. gl_FragColor = vec4(edgeColor, 1.0) * vec4(d);\
  308. }"
  309. } );
  310. },
  311. getSeperableBlurMaterial: function ( maxRadius ) {
  312. return new THREE.ShaderMaterial( {
  313. defines: {
  314. "MAX_RADIUS": maxRadius,
  315. },
  316. uniforms: {
  317. "colorTexture": { value: null },
  318. "texSize": { value: new THREE.Vector2( 0.5, 0.5 ) },
  319. "direction": { value: new THREE.Vector2( 0.5, 0.5 ) },
  320. "kernelRadius": { value: 1.0 }
  321. },
  322. vertexShader:
  323. "varying vec2 vUv;\n\
  324. void main() {\n\
  325. vUv = uv;\n\
  326. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\n\
  327. }",
  328. fragmentShader:
  329. "#include <common>\
  330. varying vec2 vUv;\
  331. uniform sampler2D colorTexture;\
  332. uniform vec2 texSize;\
  333. uniform vec2 direction;\
  334. uniform float kernelRadius;\
  335. \
  336. float gaussianPdf(in float x, in float sigma) {\
  337. return 0.39894 * exp( -0.5 * x * x/( sigma * sigma))/sigma;\
  338. }\
  339. void main() {\
  340. vec2 invSize = 1.0 / texSize;\
  341. float weightSum = gaussianPdf(0.0, kernelRadius);\
  342. vec3 diffuseSum = texture2D( colorTexture, vUv).rgb * weightSum;\
  343. vec2 delta = direction * invSize * kernelRadius/float(MAX_RADIUS);\
  344. vec2 uvOffset = delta;\
  345. for( int i = 1; i <= MAX_RADIUS; i ++ ) {\
  346. float w = gaussianPdf(uvOffset.x, kernelRadius);\
  347. vec3 sample1 = texture2D( colorTexture, vUv + uvOffset).rgb;\
  348. vec3 sample2 = texture2D( colorTexture, vUv - uvOffset).rgb;\
  349. diffuseSum += ((sample1 + sample2) * w);\
  350. weightSum += (2.0 * w);\
  351. uvOffset += delta;\
  352. }\
  353. gl_FragColor = vec4(diffuseSum/weightSum, 1.0);\
  354. }"
  355. } );
  356. },
  357. getOverlayMaterial: function () {
  358. return new THREE.ShaderMaterial( {
  359. uniforms: {
  360. "maskTexture": { value: null },
  361. "edgeTexture1": { value: null },
  362. "edgeTexture2": { value: null },
  363. "patternTexture": { value: null },
  364. "edgeStrength": { value: 1.0 },
  365. "edgeGlow": { value: 1.0 },
  366. "usePatternTexture": { value: 0.0 },
  367. "visibleEdgeColor": { value: new THREE.Vector3( 1.0, 1.0, 1.0 ) },
  368. "hiddenEdgeColor": { value: new THREE.Vector3( 1.0, 1.0, 1.0 ) },
  369. },
  370. vertexShader:
  371. "varying vec2 vUv;\n\
  372. void main() {\n\
  373. vUv = uv;\n\
  374. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\n\
  375. }",
  376. fragmentShader:
  377. "varying vec2 vUv;\
  378. uniform sampler2D maskTexture;\
  379. uniform sampler2D edgeTexture1;\
  380. uniform sampler2D edgeTexture2;\
  381. uniform sampler2D patternTexture;\
  382. uniform float edgeStrength;\
  383. uniform float edgeGlow;\
  384. uniform bool usePatternTexture;\
  385. uniform vec3 visibleEdgeColor;\
  386. uniform vec3 hiddenEdgeColor;\
  387. \
  388. void main() {\
  389. vec4 edgeValue1 = texture2D(edgeTexture1, vUv);\
  390. vec4 edgeValue2 = texture2D(edgeTexture2, vUv);\
  391. vec4 maskColor = texture2D(maskTexture, vUv);\
  392. vec4 patternColor = texture2D(patternTexture, 6.0 * vUv);\
  393. float visibilityFactor = 1.0 - maskColor.g > 0.0 ? 1.0 : 0.5;\
  394. vec4 edgeValue = edgeStrength * (edgeValue1 + edgeValue2 * edgeGlow);\
  395. edgeValue.a = max(edgeValue.r, edgeValue.g);\
  396. vec3 edgeColor = edgeValue.r * visibleEdgeColor + edgeValue.g * hiddenEdgeColor;\
  397. vec4 finalColor = vec4(edgeColor, edgeValue.a*1.0);\
  398. if(usePatternTexture)\
  399. finalColor += + visibilityFactor * (1.0 - maskColor.r) * (1.0 - patternColor.r);\
  400. gl_FragColor = finalColor;\
  401. }",
  402. blending: THREE.NormalBlending,
  403. depthTest: false,
  404. depthWrite: false,
  405. transparent: true,
  406. } );
  407. }
  408. } );
  409. THREE.OutlinePass.BlurDirectionX = new THREE.Vector2( 1.0, 0.0 );
  410. THREE.OutlinePass.BlurDirectionY = new THREE.Vector2( 0.0, 1.0 );