OutlinePass.js 22 KB

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