123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407 |
- import {
- AddEquation,
- Color,
- CustomBlending,
- DepthTexture,
- DstAlphaFactor,
- DstColorFactor,
- HalfFloatType,
- MeshNormalMaterial,
- NearestFilter,
- NoBlending,
- ShaderMaterial,
- UniformsUtils,
- DepthStencilFormat,
- UnsignedInt248Type,
- Vector2,
- WebGLRenderTarget,
- ZeroFactor
- } from 'three';
- import { Pass, FullScreenQuad } from './Pass.js';
- import { SAOShader } from '../shaders/SAOShader.js';
- import { BlurShaderUtils, DepthLimitedBlurShader } from '../shaders/DepthLimitedBlurShader.js';
- import { CopyShader } from '../shaders/CopyShader.js';
- /**
- * A SAO implementation inspired from @bhouston previous SAO work.
- *
- * `SAOPass` provides better quality than {@link SSAOPass} but is also more expensive.
- *
- * ```js
- * const saoPass = new SAOPass( scene, camera );
- * composer.addPass( saoPass );
- * ```
- *
- * @augments Pass
- * @three_import import { SAOPass } from 'three/addons/postprocessing/SAOPass.js';
- */
- class SAOPass extends Pass {
- /**
- * Constructs a new SAO pass.
- *
- * @param {Scene} scene - The scene to compute the AO for.
- * @param {Camera} camera - The camera.
- * @param {Vector2} [resolution] - The effect's resolution.
- */
- constructor( scene, camera, resolution = new Vector2( 256, 256 ) ) {
- super();
- /**
- * The scene to render the AO for.
- *
- * @type {Scene}
- */
- this.scene = scene;
- /**
- * The camera.
- *
- * @type {Camera}
- */
- this.camera = camera;
- /**
- * Overwritten to perform a clear operation by default.
- *
- * @type {boolean}
- * @default true
- */
- this.clear = true;
- /**
- * Overwritten to disable the swap.
- *
- * @type {boolean}
- * @default false
- */
- this.needsSwap = false;
- this._originalClearColor = new Color();
- this._oldClearColor = new Color();
- this._oldClearAlpha = 1;
- /**
- * The SAO parameter.
- *
- * @type {Object}
- */
- this.params = {
- output: 0,
- saoBias: 0.5,
- saoIntensity: 0.18,
- saoScale: 1,
- saoKernelRadius: 100,
- saoMinResolution: 0,
- saoBlur: true,
- saoBlurRadius: 8,
- saoBlurStdDev: 4,
- saoBlurDepthCutoff: 0.01
- };
- /**
- * The effect's resolution.
- *
- * @type {Vector2}
- * @default (256,256)
- */
- this.resolution = new Vector2( resolution.x, resolution.y );
- this.saoRenderTarget = new WebGLRenderTarget( this.resolution.x, this.resolution.y, { type: HalfFloatType } );
- this.blurIntermediateRenderTarget = this.saoRenderTarget.clone();
- const depthTexture = new DepthTexture();
- depthTexture.format = DepthStencilFormat;
- depthTexture.type = UnsignedInt248Type;
- this.normalRenderTarget = new WebGLRenderTarget( this.resolution.x, this.resolution.y, {
- minFilter: NearestFilter,
- magFilter: NearestFilter,
- type: HalfFloatType,
- depthTexture: depthTexture
- } );
- this.normalMaterial = new MeshNormalMaterial();
- this.normalMaterial.blending = NoBlending;
- this.saoMaterial = new ShaderMaterial( {
- defines: Object.assign( {}, SAOShader.defines ),
- fragmentShader: SAOShader.fragmentShader,
- vertexShader: SAOShader.vertexShader,
- uniforms: UniformsUtils.clone( SAOShader.uniforms )
- } );
- this.saoMaterial.defines[ 'PERSPECTIVE_CAMERA' ] = this.camera.isPerspectiveCamera ? 1 : 0;
- this.saoMaterial.uniforms[ 'tDepth' ].value = depthTexture;
- this.saoMaterial.uniforms[ 'tNormal' ].value = this.normalRenderTarget.texture;
- this.saoMaterial.uniforms[ 'size' ].value.set( this.resolution.x, this.resolution.y );
- this.saoMaterial.uniforms[ 'cameraInverseProjectionMatrix' ].value.copy( this.camera.projectionMatrixInverse );
- this.saoMaterial.uniforms[ 'cameraProjectionMatrix' ].value = this.camera.projectionMatrix;
- this.saoMaterial.blending = NoBlending;
- this.vBlurMaterial = new ShaderMaterial( {
- uniforms: UniformsUtils.clone( DepthLimitedBlurShader.uniforms ),
- defines: Object.assign( {}, DepthLimitedBlurShader.defines ),
- vertexShader: DepthLimitedBlurShader.vertexShader,
- fragmentShader: DepthLimitedBlurShader.fragmentShader
- } );
- this.vBlurMaterial.defines[ 'DEPTH_PACKING' ] = 0;
- this.vBlurMaterial.defines[ 'PERSPECTIVE_CAMERA' ] = this.camera.isPerspectiveCamera ? 1 : 0;
- this.vBlurMaterial.uniforms[ 'tDiffuse' ].value = this.saoRenderTarget.texture;
- this.vBlurMaterial.uniforms[ 'tDepth' ].value = depthTexture;
- this.vBlurMaterial.uniforms[ 'size' ].value.set( this.resolution.x, this.resolution.y );
- this.vBlurMaterial.blending = NoBlending;
- this.hBlurMaterial = new ShaderMaterial( {
- uniforms: UniformsUtils.clone( DepthLimitedBlurShader.uniforms ),
- defines: Object.assign( {}, DepthLimitedBlurShader.defines ),
- vertexShader: DepthLimitedBlurShader.vertexShader,
- fragmentShader: DepthLimitedBlurShader.fragmentShader
- } );
- this.hBlurMaterial.defines[ 'DEPTH_PACKING' ] = 0;
- this.hBlurMaterial.defines[ 'PERSPECTIVE_CAMERA' ] = this.camera.isPerspectiveCamera ? 1 : 0;
- this.hBlurMaterial.uniforms[ 'tDiffuse' ].value = this.blurIntermediateRenderTarget.texture;
- this.hBlurMaterial.uniforms[ 'tDepth' ].value = depthTexture;
- this.hBlurMaterial.uniforms[ 'size' ].value.set( this.resolution.x, this.resolution.y );
- this.hBlurMaterial.blending = NoBlending;
- this.materialCopy = new ShaderMaterial( {
- uniforms: UniformsUtils.clone( CopyShader.uniforms ),
- vertexShader: CopyShader.vertexShader,
- fragmentShader: CopyShader.fragmentShader,
- blending: NoBlending
- } );
- this.materialCopy.transparent = true;
- this.materialCopy.depthTest = false;
- this.materialCopy.depthWrite = false;
- this.materialCopy.blending = CustomBlending;
- this.materialCopy.blendSrc = DstColorFactor;
- this.materialCopy.blendDst = ZeroFactor;
- this.materialCopy.blendEquation = AddEquation;
- this.materialCopy.blendSrcAlpha = DstAlphaFactor;
- this.materialCopy.blendDstAlpha = ZeroFactor;
- this.materialCopy.blendEquationAlpha = AddEquation;
- this.fsQuad = new FullScreenQuad( null );
- }
- /**
- * Performs the SAO pass.
- *
- * @param {WebGLRenderer} renderer - The renderer.
- * @param {WebGLRenderTarget} writeBuffer - The write buffer. This buffer is intended as the rendering
- * destination for the pass.
- * @param {WebGLRenderTarget} readBuffer - The read buffer. The pass can access the result from the
- * previous pass from this buffer.
- * @param {number} deltaTime - The delta time in seconds.
- * @param {boolean} maskActive - Whether masking is active or not.
- */
- render( renderer, writeBuffer, readBuffer/*, deltaTime, maskActive*/ ) {
- // Rendering readBuffer first when rendering to screen
- if ( this.renderToScreen ) {
- this.materialCopy.blending = NoBlending;
- this.materialCopy.uniforms[ 'tDiffuse' ].value = readBuffer.texture;
- this.materialCopy.needsUpdate = true;
- this._renderPass( renderer, this.materialCopy, null );
- }
- renderer.getClearColor( this._oldClearColor );
- this._oldClearAlpha = renderer.getClearAlpha();
- const oldAutoClear = renderer.autoClear;
- renderer.autoClear = false;
- this.saoMaterial.uniforms[ 'bias' ].value = this.params.saoBias;
- this.saoMaterial.uniforms[ 'intensity' ].value = this.params.saoIntensity;
- this.saoMaterial.uniforms[ 'scale' ].value = this.params.saoScale;
- this.saoMaterial.uniforms[ 'kernelRadius' ].value = this.params.saoKernelRadius;
- this.saoMaterial.uniforms[ 'minResolution' ].value = this.params.saoMinResolution;
- this.saoMaterial.uniforms[ 'cameraNear' ].value = this.camera.near;
- this.saoMaterial.uniforms[ 'cameraFar' ].value = this.camera.far;
- // this.saoMaterial.uniforms['randomSeed'].value = Math.random();
- const depthCutoff = this.params.saoBlurDepthCutoff * ( this.camera.far - this.camera.near );
- this.vBlurMaterial.uniforms[ 'depthCutoff' ].value = depthCutoff;
- this.hBlurMaterial.uniforms[ 'depthCutoff' ].value = depthCutoff;
- this.vBlurMaterial.uniforms[ 'cameraNear' ].value = this.camera.near;
- this.vBlurMaterial.uniforms[ 'cameraFar' ].value = this.camera.far;
- this.hBlurMaterial.uniforms[ 'cameraNear' ].value = this.camera.near;
- this.hBlurMaterial.uniforms[ 'cameraFar' ].value = this.camera.far;
- this.params.saoBlurRadius = Math.floor( this.params.saoBlurRadius );
- if ( ( this.prevStdDev !== this.params.saoBlurStdDev ) || ( this.prevNumSamples !== this.params.saoBlurRadius ) ) {
- BlurShaderUtils.configure( this.vBlurMaterial, this.params.saoBlurRadius, this.params.saoBlurStdDev, new Vector2( 0, 1 ) );
- BlurShaderUtils.configure( this.hBlurMaterial, this.params.saoBlurRadius, this.params.saoBlurStdDev, new Vector2( 1, 0 ) );
- this.prevStdDev = this.params.saoBlurStdDev;
- this.prevNumSamples = this.params.saoBlurRadius;
- }
- // render normal and depth
- this._renderOverride( renderer, this.normalMaterial, this.normalRenderTarget, 0x7777ff, 1.0 );
- // Rendering SAO texture
- this._renderPass( renderer, this.saoMaterial, this.saoRenderTarget, 0xffffff, 1.0 );
- // Blurring SAO texture
- if ( this.params.saoBlur ) {
- this._renderPass( renderer, this.vBlurMaterial, this.blurIntermediateRenderTarget, 0xffffff, 1.0 );
- this._renderPass( renderer, this.hBlurMaterial, this.saoRenderTarget, 0xffffff, 1.0 );
- }
- const outputMaterial = this.materialCopy;
- // Setting up SAO rendering
- if ( this.params.output === SAOPass.OUTPUT.Normal ) {
- this.materialCopy.uniforms[ 'tDiffuse' ].value = this.normalRenderTarget.texture;
- this.materialCopy.needsUpdate = true;
- } else {
- this.materialCopy.uniforms[ 'tDiffuse' ].value = this.saoRenderTarget.texture;
- this.materialCopy.needsUpdate = true;
- }
- // Blending depends on output
- if ( this.params.output === SAOPass.OUTPUT.Default ) {
- outputMaterial.blending = CustomBlending;
- } else {
- outputMaterial.blending = NoBlending;
- }
- // Rendering SAOPass result on top of previous pass
- this._renderPass( renderer, outputMaterial, this.renderToScreen ? null : readBuffer );
- renderer.setClearColor( this._oldClearColor, this._oldClearAlpha );
- renderer.autoClear = oldAutoClear;
- }
- /**
- * Sets the size of the pass.
- *
- * @param {number} width - The width to set.
- * @param {number} height - The width to set.
- */
- setSize( width, height ) {
- this.saoRenderTarget.setSize( width, height );
- this.blurIntermediateRenderTarget.setSize( width, height );
- this.normalRenderTarget.setSize( width, height );
- this.saoMaterial.uniforms[ 'size' ].value.set( width, height );
- this.saoMaterial.uniforms[ 'cameraInverseProjectionMatrix' ].value.copy( this.camera.projectionMatrixInverse );
- this.saoMaterial.uniforms[ 'cameraProjectionMatrix' ].value = this.camera.projectionMatrix;
- this.saoMaterial.needsUpdate = true;
- this.vBlurMaterial.uniforms[ 'size' ].value.set( width, height );
- this.vBlurMaterial.needsUpdate = true;
- this.hBlurMaterial.uniforms[ 'size' ].value.set( width, height );
- this.hBlurMaterial.needsUpdate = true;
- }
- /**
- * Frees the GPU-related resources allocated by this instance. Call this
- * method whenever the pass is no longer used in your app.
- */
- dispose() {
- this.saoRenderTarget.dispose();
- this.blurIntermediateRenderTarget.dispose();
- this.normalRenderTarget.dispose();
- this.normalMaterial.dispose();
- this.saoMaterial.dispose();
- this.vBlurMaterial.dispose();
- this.hBlurMaterial.dispose();
- this.materialCopy.dispose();
- this.fsQuad.dispose();
- }
- // internal
- _renderPass( renderer, passMaterial, renderTarget, clearColor, clearAlpha ) {
- // save original state
- renderer.getClearColor( this._originalClearColor );
- const originalClearAlpha = renderer.getClearAlpha();
- const originalAutoClear = renderer.autoClear;
- renderer.setRenderTarget( renderTarget );
- // setup pass state
- renderer.autoClear = false;
- if ( ( clearColor !== undefined ) && ( clearColor !== null ) ) {
- renderer.setClearColor( clearColor );
- renderer.setClearAlpha( clearAlpha || 0.0 );
- renderer.clear();
- }
- this.fsQuad.material = passMaterial;
- this.fsQuad.render( renderer );
- // restore original state
- renderer.autoClear = originalAutoClear;
- renderer.setClearColor( this._originalClearColor );
- renderer.setClearAlpha( originalClearAlpha );
- }
- _renderOverride( renderer, overrideMaterial, renderTarget, clearColor, clearAlpha ) {
- renderer.getClearColor( this._originalClearColor );
- const originalClearAlpha = renderer.getClearAlpha();
- const originalAutoClear = renderer.autoClear;
- renderer.setRenderTarget( renderTarget );
- renderer.autoClear = false;
- clearColor = overrideMaterial.clearColor || clearColor;
- clearAlpha = overrideMaterial.clearAlpha || clearAlpha;
- if ( ( clearColor !== undefined ) && ( clearColor !== null ) ) {
- renderer.setClearColor( clearColor );
- renderer.setClearAlpha( clearAlpha || 0.0 );
- renderer.clear();
- }
- this.scene.overrideMaterial = overrideMaterial;
- renderer.render( this.scene, this.camera );
- this.scene.overrideMaterial = null;
- // restore original state
- renderer.autoClear = originalAutoClear;
- renderer.setClearColor( this._originalClearColor );
- renderer.setClearAlpha( originalClearAlpha );
- }
- }
- SAOPass.OUTPUT = {
- 'Default': 0,
- 'SAO': 1,
- 'Normal': 2
- };
- export { SAOPass };
|