OutlinePass.js 19 KB

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