OutlinePass.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  1. import {
  2. AdditiveBlending,
  3. Color,
  4. DoubleSide,
  5. HalfFloatType,
  6. Matrix4,
  7. MeshDepthMaterial,
  8. NoBlending,
  9. RGBADepthPacking,
  10. ShaderMaterial,
  11. UniformsUtils,
  12. Vector2,
  13. Vector3,
  14. WebGLRenderTarget
  15. } from 'three';
  16. import { Pass, FullScreenQuad } from './Pass.js';
  17. import { CopyShader } from '../shaders/CopyShader.js';
  18. /**
  19. * A pass for rendering outlines around selected objects.
  20. *
  21. * ```js
  22. * const resolution = new THREE.Vector2( window.innerWidth, window.innerHeight );
  23. * const outlinePass = new OutlinePass( resolution, scene, camera );
  24. * composer.addPass( outlinePass );
  25. * ```
  26. *
  27. * @augments Pass
  28. * @three_import import { OutlinePass } from 'three/addons/postprocessing/OutlinePass.js';
  29. */
  30. class OutlinePass extends Pass {
  31. /**
  32. * Constructs a new outline pass.
  33. *
  34. * @param {Vector2} [resolution] - The effect's resolution.
  35. * @param {Scene} scene - The scene to render.
  36. * @param {Camera} camera - The camera.
  37. * @param {Array<Object3D>} [selectedObjects] - The selected 3D objects that should receive an outline.
  38. *
  39. */
  40. constructor( resolution, scene, camera, selectedObjects ) {
  41. super();
  42. /**
  43. * The scene to render.
  44. *
  45. * @type {Object}
  46. */
  47. this.renderScene = scene;
  48. /**
  49. * The camera.
  50. *
  51. * @type {Object}
  52. */
  53. this.renderCamera = camera;
  54. /**
  55. * The selected 3D objects that should receive an outline.
  56. *
  57. * @type {Array<Object3D>}
  58. */
  59. this.selectedObjects = selectedObjects !== undefined ? selectedObjects : [];
  60. /**
  61. * The visible edge color.
  62. *
  63. * @type {Color}
  64. * @default (1,1,1)
  65. */
  66. this.visibleEdgeColor = new Color( 1, 1, 1 );
  67. /**
  68. * The hidden edge color.
  69. *
  70. * @type {Color}
  71. * @default (0.1,0.04,0.02)
  72. */
  73. this.hiddenEdgeColor = new Color( 0.1, 0.04, 0.02 );
  74. /**
  75. * Can be used for an animated glow/pulse effect.
  76. *
  77. * @type {number}
  78. * @default 0
  79. */
  80. this.edgeGlow = 0.0;
  81. /**
  82. * Whether to use a pattern texture for to highlight selected
  83. * 3D objects or not.
  84. *
  85. * @type {boolean}
  86. * @default false
  87. */
  88. this.usePatternTexture = false;
  89. /**
  90. * Can be used to highlight selected 3D objects. Requires to set
  91. * {@link OutlinePass#usePatternTexture} to `true`.
  92. *
  93. * @type {?Texture}
  94. * @default null
  95. */
  96. this.patternTexture = null;
  97. /**
  98. * The edge thickness.
  99. *
  100. * @type {number}
  101. * @default 1
  102. */
  103. this.edgeThickness = 1.0;
  104. /**
  105. * The edge strength.
  106. *
  107. * @type {number}
  108. * @default 3
  109. */
  110. this.edgeStrength = 3.0;
  111. /**
  112. * The downsample ratio. The effect can be rendered in a much
  113. * lower resolution than the beauty pass.
  114. *
  115. * @type {number}
  116. * @default 2
  117. */
  118. this.downSampleRatio = 2;
  119. /**
  120. * The pulse period.
  121. *
  122. * @type {number}
  123. * @default 0
  124. */
  125. this.pulsePeriod = 0;
  126. this._visibilityCache = new Map();
  127. this._selectionCache = new Set();
  128. /**
  129. * The effect's resolution.
  130. *
  131. * @type {Vector2}
  132. * @default (256,256)
  133. */
  134. this.resolution = ( resolution !== undefined ) ? new Vector2( resolution.x, resolution.y ) : new Vector2( 256, 256 );
  135. const resx = Math.round( this.resolution.x / this.downSampleRatio );
  136. const resy = Math.round( this.resolution.y / this.downSampleRatio );
  137. this.renderTargetMaskBuffer = new WebGLRenderTarget( this.resolution.x, this.resolution.y );
  138. this.renderTargetMaskBuffer.texture.name = 'OutlinePass.mask';
  139. this.renderTargetMaskBuffer.texture.generateMipmaps = false;
  140. this.depthMaterial = new MeshDepthMaterial();
  141. this.depthMaterial.side = DoubleSide;
  142. this.depthMaterial.depthPacking = RGBADepthPacking;
  143. this.depthMaterial.blending = NoBlending;
  144. this.prepareMaskMaterial = this._getPrepareMaskMaterial();
  145. this.prepareMaskMaterial.side = DoubleSide;
  146. this.prepareMaskMaterial.fragmentShader = replaceDepthToViewZ( this.prepareMaskMaterial.fragmentShader, this.renderCamera );
  147. this.renderTargetDepthBuffer = new WebGLRenderTarget( this.resolution.x, this.resolution.y, { type: HalfFloatType } );
  148. this.renderTargetDepthBuffer.texture.name = 'OutlinePass.depth';
  149. this.renderTargetDepthBuffer.texture.generateMipmaps = false;
  150. this.renderTargetMaskDownSampleBuffer = new WebGLRenderTarget( resx, resy, { type: HalfFloatType } );
  151. this.renderTargetMaskDownSampleBuffer.texture.name = 'OutlinePass.depthDownSample';
  152. this.renderTargetMaskDownSampleBuffer.texture.generateMipmaps = false;
  153. this.renderTargetBlurBuffer1 = new WebGLRenderTarget( resx, resy, { type: HalfFloatType } );
  154. this.renderTargetBlurBuffer1.texture.name = 'OutlinePass.blur1';
  155. this.renderTargetBlurBuffer1.texture.generateMipmaps = false;
  156. this.renderTargetBlurBuffer2 = new WebGLRenderTarget( Math.round( resx / 2 ), Math.round( resy / 2 ), { type: HalfFloatType } );
  157. this.renderTargetBlurBuffer2.texture.name = 'OutlinePass.blur2';
  158. this.renderTargetBlurBuffer2.texture.generateMipmaps = false;
  159. this.edgeDetectionMaterial = this._getEdgeDetectionMaterial();
  160. this.renderTargetEdgeBuffer1 = new WebGLRenderTarget( resx, resy, { type: HalfFloatType } );
  161. this.renderTargetEdgeBuffer1.texture.name = 'OutlinePass.edge1';
  162. this.renderTargetEdgeBuffer1.texture.generateMipmaps = false;
  163. this.renderTargetEdgeBuffer2 = new WebGLRenderTarget( Math.round( resx / 2 ), Math.round( resy / 2 ), { type: HalfFloatType } );
  164. this.renderTargetEdgeBuffer2.texture.name = 'OutlinePass.edge2';
  165. this.renderTargetEdgeBuffer2.texture.generateMipmaps = false;
  166. const MAX_EDGE_THICKNESS = 4;
  167. const MAX_EDGE_GLOW = 4;
  168. this.separableBlurMaterial1 = this._getSeparableBlurMaterial( MAX_EDGE_THICKNESS );
  169. this.separableBlurMaterial1.uniforms[ 'texSize' ].value.set( resx, resy );
  170. this.separableBlurMaterial1.uniforms[ 'kernelRadius' ].value = 1;
  171. this.separableBlurMaterial2 = this._getSeparableBlurMaterial( MAX_EDGE_GLOW );
  172. this.separableBlurMaterial2.uniforms[ 'texSize' ].value.set( Math.round( resx / 2 ), Math.round( resy / 2 ) );
  173. this.separableBlurMaterial2.uniforms[ 'kernelRadius' ].value = MAX_EDGE_GLOW;
  174. // Overlay material
  175. this.overlayMaterial = this._getOverlayMaterial();
  176. // copy material
  177. const copyShader = CopyShader;
  178. this.copyUniforms = UniformsUtils.clone( copyShader.uniforms );
  179. this.materialCopy = new ShaderMaterial( {
  180. uniforms: this.copyUniforms,
  181. vertexShader: copyShader.vertexShader,
  182. fragmentShader: copyShader.fragmentShader,
  183. blending: NoBlending,
  184. depthTest: false,
  185. depthWrite: false
  186. } );
  187. this.enabled = true;
  188. this.needsSwap = false;
  189. this._oldClearColor = new Color();
  190. this.oldClearAlpha = 1;
  191. this._fsQuad = new FullScreenQuad( null );
  192. this.tempPulseColor1 = new Color();
  193. this.tempPulseColor2 = new Color();
  194. this.textureMatrix = new Matrix4();
  195. function replaceDepthToViewZ( string, camera ) {
  196. const type = camera.isPerspectiveCamera ? 'perspective' : 'orthographic';
  197. return string.replace( /DEPTH_TO_VIEW_Z/g, type + 'DepthToViewZ' );
  198. }
  199. }
  200. /**
  201. * Frees the GPU-related resources allocated by this instance. Call this
  202. * method whenever the pass is no longer used in your app.
  203. */
  204. dispose() {
  205. this.renderTargetMaskBuffer.dispose();
  206. this.renderTargetDepthBuffer.dispose();
  207. this.renderTargetMaskDownSampleBuffer.dispose();
  208. this.renderTargetBlurBuffer1.dispose();
  209. this.renderTargetBlurBuffer2.dispose();
  210. this.renderTargetEdgeBuffer1.dispose();
  211. this.renderTargetEdgeBuffer2.dispose();
  212. this.depthMaterial.dispose();
  213. this.prepareMaskMaterial.dispose();
  214. this.edgeDetectionMaterial.dispose();
  215. this.separableBlurMaterial1.dispose();
  216. this.separableBlurMaterial2.dispose();
  217. this.overlayMaterial.dispose();
  218. this.materialCopy.dispose();
  219. this._fsQuad.dispose();
  220. }
  221. /**
  222. * Sets the size of the pass.
  223. *
  224. * @param {number} width - The width to set.
  225. * @param {number} height - The width to set.
  226. */
  227. setSize( width, height ) {
  228. this.renderTargetMaskBuffer.setSize( width, height );
  229. this.renderTargetDepthBuffer.setSize( width, height );
  230. let resx = Math.round( width / this.downSampleRatio );
  231. let resy = Math.round( height / this.downSampleRatio );
  232. this.renderTargetMaskDownSampleBuffer.setSize( resx, resy );
  233. this.renderTargetBlurBuffer1.setSize( resx, resy );
  234. this.renderTargetEdgeBuffer1.setSize( resx, resy );
  235. this.separableBlurMaterial1.uniforms[ 'texSize' ].value.set( resx, resy );
  236. resx = Math.round( resx / 2 );
  237. resy = Math.round( resy / 2 );
  238. this.renderTargetBlurBuffer2.setSize( resx, resy );
  239. this.renderTargetEdgeBuffer2.setSize( resx, resy );
  240. this.separableBlurMaterial2.uniforms[ 'texSize' ].value.set( resx, resy );
  241. }
  242. /**
  243. * Performs the Outline pass.
  244. *
  245. * @param {WebGLRenderer} renderer - The renderer.
  246. * @param {WebGLRenderTarget} writeBuffer - The write buffer. This buffer is intended as the rendering
  247. * destination for the pass.
  248. * @param {WebGLRenderTarget} readBuffer - The read buffer. The pass can access the result from the
  249. * previous pass from this buffer.
  250. * @param {number} deltaTime - The delta time in seconds.
  251. * @param {boolean} maskActive - Whether masking is active or not.
  252. */
  253. render( renderer, writeBuffer, readBuffer, deltaTime, maskActive ) {
  254. if ( this.selectedObjects.length > 0 ) {
  255. renderer.getClearColor( this._oldClearColor );
  256. this.oldClearAlpha = renderer.getClearAlpha();
  257. const oldAutoClear = renderer.autoClear;
  258. renderer.autoClear = false;
  259. if ( maskActive ) renderer.state.buffers.stencil.setTest( false );
  260. renderer.setClearColor( 0xffffff, 1 );
  261. this._updateSelectionCache();
  262. // Make selected objects invisible
  263. this._changeVisibilityOfSelectedObjects( false );
  264. const currentBackground = this.renderScene.background;
  265. const currentOverrideMaterial = this.renderScene.overrideMaterial;
  266. this.renderScene.background = null;
  267. // 1. Draw Non Selected objects in the depth buffer
  268. this.renderScene.overrideMaterial = this.depthMaterial;
  269. renderer.setRenderTarget( this.renderTargetDepthBuffer );
  270. renderer.clear();
  271. renderer.render( this.renderScene, this.renderCamera );
  272. // Make selected objects visible
  273. this._changeVisibilityOfSelectedObjects( true );
  274. this._visibilityCache.clear();
  275. // Update Texture Matrix for Depth compare
  276. this._updateTextureMatrix();
  277. // Make non selected objects invisible, and draw only the selected objects, by comparing the depth buffer of non selected objects
  278. this._changeVisibilityOfNonSelectedObjects( false );
  279. this.renderScene.overrideMaterial = this.prepareMaskMaterial;
  280. this.prepareMaskMaterial.uniforms[ 'cameraNearFar' ].value.set( this.renderCamera.near, this.renderCamera.far );
  281. this.prepareMaskMaterial.uniforms[ 'depthTexture' ].value = this.renderTargetDepthBuffer.texture;
  282. this.prepareMaskMaterial.uniforms[ 'textureMatrix' ].value = this.textureMatrix;
  283. renderer.setRenderTarget( this.renderTargetMaskBuffer );
  284. renderer.clear();
  285. renderer.render( this.renderScene, this.renderCamera );
  286. this._changeVisibilityOfNonSelectedObjects( true );
  287. this._visibilityCache.clear();
  288. this._selectionCache.clear();
  289. this.renderScene.background = currentBackground;
  290. this.renderScene.overrideMaterial = currentOverrideMaterial;
  291. // 2. Downsample to Half resolution
  292. this._fsQuad.material = this.materialCopy;
  293. this.copyUniforms[ 'tDiffuse' ].value = this.renderTargetMaskBuffer.texture;
  294. renderer.setRenderTarget( this.renderTargetMaskDownSampleBuffer );
  295. renderer.clear();
  296. this._fsQuad.render( renderer );
  297. this.tempPulseColor1.copy( this.visibleEdgeColor );
  298. this.tempPulseColor2.copy( this.hiddenEdgeColor );
  299. if ( this.pulsePeriod > 0 ) {
  300. const scalar = ( 1 + 0.25 ) / 2 + Math.cos( performance.now() * 0.01 / this.pulsePeriod ) * ( 1.0 - 0.25 ) / 2;
  301. this.tempPulseColor1.multiplyScalar( scalar );
  302. this.tempPulseColor2.multiplyScalar( scalar );
  303. }
  304. // 3. Apply Edge Detection Pass
  305. this._fsQuad.material = this.edgeDetectionMaterial;
  306. this.edgeDetectionMaterial.uniforms[ 'maskTexture' ].value = this.renderTargetMaskDownSampleBuffer.texture;
  307. this.edgeDetectionMaterial.uniforms[ 'texSize' ].value.set( this.renderTargetMaskDownSampleBuffer.width, this.renderTargetMaskDownSampleBuffer.height );
  308. this.edgeDetectionMaterial.uniforms[ 'visibleEdgeColor' ].value = this.tempPulseColor1;
  309. this.edgeDetectionMaterial.uniforms[ 'hiddenEdgeColor' ].value = this.tempPulseColor2;
  310. renderer.setRenderTarget( this.renderTargetEdgeBuffer1 );
  311. renderer.clear();
  312. this._fsQuad.render( renderer );
  313. // 4. Apply Blur on Half res
  314. this._fsQuad.material = this.separableBlurMaterial1;
  315. this.separableBlurMaterial1.uniforms[ 'colorTexture' ].value = this.renderTargetEdgeBuffer1.texture;
  316. this.separableBlurMaterial1.uniforms[ 'direction' ].value = OutlinePass.BlurDirectionX;
  317. this.separableBlurMaterial1.uniforms[ 'kernelRadius' ].value = this.edgeThickness;
  318. renderer.setRenderTarget( this.renderTargetBlurBuffer1 );
  319. renderer.clear();
  320. this._fsQuad.render( renderer );
  321. this.separableBlurMaterial1.uniforms[ 'colorTexture' ].value = this.renderTargetBlurBuffer1.texture;
  322. this.separableBlurMaterial1.uniforms[ 'direction' ].value = OutlinePass.BlurDirectionY;
  323. renderer.setRenderTarget( this.renderTargetEdgeBuffer1 );
  324. renderer.clear();
  325. this._fsQuad.render( renderer );
  326. // Apply Blur on quarter res
  327. this._fsQuad.material = this.separableBlurMaterial2;
  328. this.separableBlurMaterial2.uniforms[ 'colorTexture' ].value = this.renderTargetEdgeBuffer1.texture;
  329. this.separableBlurMaterial2.uniforms[ 'direction' ].value = OutlinePass.BlurDirectionX;
  330. renderer.setRenderTarget( this.renderTargetBlurBuffer2 );
  331. renderer.clear();
  332. this._fsQuad.render( renderer );
  333. this.separableBlurMaterial2.uniforms[ 'colorTexture' ].value = this.renderTargetBlurBuffer2.texture;
  334. this.separableBlurMaterial2.uniforms[ 'direction' ].value = OutlinePass.BlurDirectionY;
  335. renderer.setRenderTarget( this.renderTargetEdgeBuffer2 );
  336. renderer.clear();
  337. this._fsQuad.render( renderer );
  338. // Blend it additively over the input texture
  339. this._fsQuad.material = this.overlayMaterial;
  340. this.overlayMaterial.uniforms[ 'maskTexture' ].value = this.renderTargetMaskBuffer.texture;
  341. this.overlayMaterial.uniforms[ 'edgeTexture1' ].value = this.renderTargetEdgeBuffer1.texture;
  342. this.overlayMaterial.uniforms[ 'edgeTexture2' ].value = this.renderTargetEdgeBuffer2.texture;
  343. this.overlayMaterial.uniforms[ 'patternTexture' ].value = this.patternTexture;
  344. this.overlayMaterial.uniforms[ 'edgeStrength' ].value = this.edgeStrength;
  345. this.overlayMaterial.uniforms[ 'edgeGlow' ].value = this.edgeGlow;
  346. this.overlayMaterial.uniforms[ 'usePatternTexture' ].value = this.usePatternTexture;
  347. if ( maskActive ) renderer.state.buffers.stencil.setTest( true );
  348. renderer.setRenderTarget( readBuffer );
  349. this._fsQuad.render( renderer );
  350. renderer.setClearColor( this._oldClearColor, this.oldClearAlpha );
  351. renderer.autoClear = oldAutoClear;
  352. }
  353. if ( this.renderToScreen ) {
  354. this._fsQuad.material = this.materialCopy;
  355. this.copyUniforms[ 'tDiffuse' ].value = readBuffer.texture;
  356. renderer.setRenderTarget( null );
  357. this._fsQuad.render( renderer );
  358. }
  359. }
  360. // internals
  361. _updateSelectionCache() {
  362. const cache = this._selectionCache;
  363. function gatherSelectedMeshesCallBack( object ) {
  364. if ( object.isMesh ) cache.add( object );
  365. }
  366. cache.clear();
  367. for ( let i = 0; i < this.selectedObjects.length; i ++ ) {
  368. const selectedObject = this.selectedObjects[ i ];
  369. selectedObject.traverse( gatherSelectedMeshesCallBack );
  370. }
  371. }
  372. _changeVisibilityOfSelectedObjects( bVisible ) {
  373. const cache = this._visibilityCache;
  374. for ( const mesh of this._selectionCache ) {
  375. if ( bVisible === true ) {
  376. mesh.visible = cache.get( mesh );
  377. } else {
  378. cache.set( mesh, mesh.visible );
  379. mesh.visible = bVisible;
  380. }
  381. }
  382. }
  383. _changeVisibilityOfNonSelectedObjects( bVisible ) {
  384. const visibilityCache = this._visibilityCache;
  385. const selectionCache = this._selectionCache;
  386. function VisibilityChangeCallBack( object ) {
  387. if ( object.isMesh || object.isSprite ) {
  388. // only meshes and sprites are supported by OutlinePass
  389. if ( ! selectionCache.has( object ) ) {
  390. const visibility = object.visible;
  391. if ( bVisible === false || visibilityCache.get( object ) === true ) {
  392. object.visible = bVisible;
  393. }
  394. visibilityCache.set( object, visibility );
  395. }
  396. } else if ( object.isPoints || object.isLine ) {
  397. // the visibility of points and lines is always set to false in order to
  398. // not affect the outline computation
  399. if ( bVisible === true ) {
  400. object.visible = visibilityCache.get( object ); // restore
  401. } else {
  402. visibilityCache.set( object, object.visible );
  403. object.visible = bVisible;
  404. }
  405. }
  406. }
  407. this.renderScene.traverse( VisibilityChangeCallBack );
  408. }
  409. _updateTextureMatrix() {
  410. this.textureMatrix.set( 0.5, 0.0, 0.0, 0.5,
  411. 0.0, 0.5, 0.0, 0.5,
  412. 0.0, 0.0, 0.5, 0.5,
  413. 0.0, 0.0, 0.0, 1.0 );
  414. this.textureMatrix.multiply( this.renderCamera.projectionMatrix );
  415. this.textureMatrix.multiply( this.renderCamera.matrixWorldInverse );
  416. }
  417. _getPrepareMaskMaterial() {
  418. return new ShaderMaterial( {
  419. uniforms: {
  420. 'depthTexture': { value: null },
  421. 'cameraNearFar': { value: new Vector2( 0.5, 0.5 ) },
  422. 'textureMatrix': { value: null }
  423. },
  424. vertexShader:
  425. `#include <batching_pars_vertex>
  426. #include <morphtarget_pars_vertex>
  427. #include <skinning_pars_vertex>
  428. varying vec4 projTexCoord;
  429. varying vec4 vPosition;
  430. uniform mat4 textureMatrix;
  431. void main() {
  432. #include <batching_vertex>
  433. #include <skinbase_vertex>
  434. #include <begin_vertex>
  435. #include <morphtarget_vertex>
  436. #include <skinning_vertex>
  437. #include <project_vertex>
  438. vPosition = mvPosition;
  439. vec4 worldPosition = vec4( transformed, 1.0 );
  440. #ifdef USE_INSTANCING
  441. worldPosition = instanceMatrix * worldPosition;
  442. #endif
  443. worldPosition = modelMatrix * worldPosition;
  444. projTexCoord = textureMatrix * worldPosition;
  445. }`,
  446. fragmentShader:
  447. `#include <packing>
  448. varying vec4 vPosition;
  449. varying vec4 projTexCoord;
  450. uniform sampler2D depthTexture;
  451. uniform vec2 cameraNearFar;
  452. void main() {
  453. float depth = unpackRGBAToDepth(texture2DProj( depthTexture, projTexCoord ));
  454. float viewZ = - DEPTH_TO_VIEW_Z( depth, cameraNearFar.x, cameraNearFar.y );
  455. float depthTest = (-vPosition.z > viewZ) ? 1.0 : 0.0;
  456. gl_FragColor = vec4(0.0, depthTest, 1.0, 1.0);
  457. }`
  458. } );
  459. }
  460. _getEdgeDetectionMaterial() {
  461. return new ShaderMaterial( {
  462. uniforms: {
  463. 'maskTexture': { value: null },
  464. 'texSize': { value: new Vector2( 0.5, 0.5 ) },
  465. 'visibleEdgeColor': { value: new Vector3( 1.0, 1.0, 1.0 ) },
  466. 'hiddenEdgeColor': { value: new Vector3( 1.0, 1.0, 1.0 ) },
  467. },
  468. vertexShader:
  469. `varying vec2 vUv;
  470. void main() {
  471. vUv = uv;
  472. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  473. }`,
  474. fragmentShader:
  475. `varying vec2 vUv;
  476. uniform sampler2D maskTexture;
  477. uniform vec2 texSize;
  478. uniform vec3 visibleEdgeColor;
  479. uniform vec3 hiddenEdgeColor;
  480. void main() {
  481. vec2 invSize = 1.0 / texSize;
  482. vec4 uvOffset = vec4(1.0, 0.0, 0.0, 1.0) * vec4(invSize, invSize);
  483. vec4 c1 = texture2D( maskTexture, vUv + uvOffset.xy);
  484. vec4 c2 = texture2D( maskTexture, vUv - uvOffset.xy);
  485. vec4 c3 = texture2D( maskTexture, vUv + uvOffset.yw);
  486. vec4 c4 = texture2D( maskTexture, vUv - uvOffset.yw);
  487. float diff1 = (c1.r - c2.r)*0.5;
  488. float diff2 = (c3.r - c4.r)*0.5;
  489. float d = length( vec2(diff1, diff2) );
  490. float a1 = min(c1.g, c2.g);
  491. float a2 = min(c3.g, c4.g);
  492. float visibilityFactor = min(a1, a2);
  493. vec3 edgeColor = 1.0 - visibilityFactor > 0.001 ? visibleEdgeColor : hiddenEdgeColor;
  494. gl_FragColor = vec4(edgeColor, 1.0) * vec4(d);
  495. }`
  496. } );
  497. }
  498. _getSeparableBlurMaterial( maxRadius ) {
  499. return new ShaderMaterial( {
  500. defines: {
  501. 'MAX_RADIUS': maxRadius,
  502. },
  503. uniforms: {
  504. 'colorTexture': { value: null },
  505. 'texSize': { value: new Vector2( 0.5, 0.5 ) },
  506. 'direction': { value: new Vector2( 0.5, 0.5 ) },
  507. 'kernelRadius': { value: 1.0 }
  508. },
  509. vertexShader:
  510. `varying vec2 vUv;
  511. void main() {
  512. vUv = uv;
  513. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  514. }`,
  515. fragmentShader:
  516. `#include <common>
  517. varying vec2 vUv;
  518. uniform sampler2D colorTexture;
  519. uniform vec2 texSize;
  520. uniform vec2 direction;
  521. uniform float kernelRadius;
  522. float gaussianPdf(in float x, in float sigma) {
  523. return 0.39894 * exp( -0.5 * x * x/( sigma * sigma))/sigma;
  524. }
  525. void main() {
  526. vec2 invSize = 1.0 / texSize;
  527. float sigma = kernelRadius/2.0;
  528. float weightSum = gaussianPdf(0.0, sigma);
  529. vec4 diffuseSum = texture2D( colorTexture, vUv) * weightSum;
  530. vec2 delta = direction * invSize * kernelRadius/float(MAX_RADIUS);
  531. vec2 uvOffset = delta;
  532. for( int i = 1; i <= MAX_RADIUS; i ++ ) {
  533. float x = kernelRadius * float(i) / float(MAX_RADIUS);
  534. float w = gaussianPdf(x, sigma);
  535. vec4 sample1 = texture2D( colorTexture, vUv + uvOffset);
  536. vec4 sample2 = texture2D( colorTexture, vUv - uvOffset);
  537. diffuseSum += ((sample1 + sample2) * w);
  538. weightSum += (2.0 * w);
  539. uvOffset += delta;
  540. }
  541. gl_FragColor = diffuseSum/weightSum;
  542. }`
  543. } );
  544. }
  545. _getOverlayMaterial() {
  546. return new ShaderMaterial( {
  547. uniforms: {
  548. 'maskTexture': { value: null },
  549. 'edgeTexture1': { value: null },
  550. 'edgeTexture2': { value: null },
  551. 'patternTexture': { value: null },
  552. 'edgeStrength': { value: 1.0 },
  553. 'edgeGlow': { value: 1.0 },
  554. 'usePatternTexture': { value: 0.0 }
  555. },
  556. vertexShader:
  557. `varying vec2 vUv;
  558. void main() {
  559. vUv = uv;
  560. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  561. }`,
  562. fragmentShader:
  563. `varying vec2 vUv;
  564. uniform sampler2D maskTexture;
  565. uniform sampler2D edgeTexture1;
  566. uniform sampler2D edgeTexture2;
  567. uniform sampler2D patternTexture;
  568. uniform float edgeStrength;
  569. uniform float edgeGlow;
  570. uniform bool usePatternTexture;
  571. void main() {
  572. vec4 edgeValue1 = texture2D(edgeTexture1, vUv);
  573. vec4 edgeValue2 = texture2D(edgeTexture2, vUv);
  574. vec4 maskColor = texture2D(maskTexture, vUv);
  575. vec4 patternColor = texture2D(patternTexture, 6.0 * vUv);
  576. float visibilityFactor = 1.0 - maskColor.g > 0.0 ? 1.0 : 0.5;
  577. vec4 edgeValue = edgeValue1 + edgeValue2 * edgeGlow;
  578. vec4 finalColor = edgeStrength * maskColor.r * edgeValue;
  579. if(usePatternTexture)
  580. finalColor += + visibilityFactor * (1.0 - maskColor.r) * (1.0 - patternColor.r);
  581. gl_FragColor = finalColor;
  582. }`,
  583. blending: AdditiveBlending,
  584. depthTest: false,
  585. depthWrite: false,
  586. transparent: true
  587. } );
  588. }
  589. }
  590. OutlinePass.BlurDirectionX = new Vector2( 1.0, 0.0 );
  591. OutlinePass.BlurDirectionY = new Vector2( 0.0, 1.0 );
  592. export { OutlinePass };