OutlinePass.js 19 KB

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