|
| 1 | +import TempNode from '../core/TempNode.js'; |
| 2 | +import { uv } from '../accessors/UVNode.js'; |
| 3 | +import { addNodeElement, tslFn, nodeObject, float, int, vec2, vec3, vec4, mat2, If } from '../shadernode/ShaderNode.js'; |
| 4 | +import { NodeUpdateType } from '../core/constants.js'; |
| 5 | +import { uniform } from '../core/UniformNode.js'; |
| 6 | +import { uniforms } from '../accessors/UniformsNode.js'; |
| 7 | +import { abs, dot, sin, cos, PI, pow, max } from '../math/MathNode.js'; |
| 8 | +import { loop } from '../utils/LoopNode.js'; |
| 9 | +import { luminance } from './ColorAdjustmentNode.js'; |
| 10 | +import { textureSize } from '../accessors/TextureSizeNode.js'; |
| 11 | +import { Vector2 } from '../../math/Vector2.js'; |
| 12 | +import { Vector3 } from '../../math/Vector3.js'; |
| 13 | + |
| 14 | +class DenoiseNode extends TempNode { |
| 15 | + |
| 16 | + constructor( textureNode, depthNode, normalNode, noiseNode, camera ) { |
| 17 | + |
| 18 | + super(); |
| 19 | + |
| 20 | + this.textureNode = textureNode; |
| 21 | + this.depthNode = depthNode; |
| 22 | + this.normalNode = normalNode; |
| 23 | + this.noiseNode = noiseNode; |
| 24 | + |
| 25 | + this.cameraProjectionMatrixInverse = uniform( camera.projectionMatrixInverse ); |
| 26 | + this.lumaPhi = uniform( 5 ); |
| 27 | + this.depthPhi = uniform( 5 ); |
| 28 | + this.normalPhi = uniform( 5 ); |
| 29 | + this.radius = uniform( 5 ); |
| 30 | + this.index = uniform( 0 ); |
| 31 | + |
| 32 | + this._resolution = uniform( new Vector2() ); |
| 33 | + this._sampleVectors = uniforms( generatePdSamplePointInitializer( 16, 2, 1 ) ); |
| 34 | + |
| 35 | + this.updateBeforeType = NodeUpdateType.RENDER; |
| 36 | + |
| 37 | + } |
| 38 | + |
| 39 | + updateBefore() { |
| 40 | + |
| 41 | + const map = this.textureNode.value; |
| 42 | + |
| 43 | + this._resolution.value.set( map.image.width, map.image.height ); |
| 44 | + |
| 45 | + } |
| 46 | + |
| 47 | + setup() { |
| 48 | + |
| 49 | + const uvNode = uv(); |
| 50 | + |
| 51 | + const sampleTexture = ( uv ) => this.textureNode.uv( uv ); |
| 52 | + const sampleDepth = ( uv ) => this.depthNode.uv( uv ).x; |
| 53 | + const sampleNormal = ( uv ) => this.normalNode.uv( uv ); |
| 54 | + const sampleNoise = ( uv ) => this.noiseNode.uv( uv ); |
| 55 | + |
| 56 | + const getViewPosition = tslFn( ( [ screenPosition, depth ] ) => { |
| 57 | + |
| 58 | + screenPosition = vec2( screenPosition.x, screenPosition.y.oneMinus() ).mul( 2.0 ).sub( 1.0 ); |
| 59 | + |
| 60 | + const clipSpacePosition = vec4( vec3( screenPosition, depth ), 1.0 ); |
| 61 | + const viewSpacePosition = vec4( this.cameraProjectionMatrixInverse.mul( clipSpacePosition ) ); |
| 62 | + |
| 63 | + return viewSpacePosition.xyz.div( viewSpacePosition.w ); |
| 64 | + |
| 65 | + } ); |
| 66 | + |
| 67 | + const denoiseSample = tslFn( ( [ center, viewNormal, viewPosition, sampleUv ] ) => { |
| 68 | + |
| 69 | + const texel = sampleTexture( sampleUv ); |
| 70 | + const depth = sampleDepth( sampleUv ); |
| 71 | + const normal = sampleNormal( sampleUv ).rgb.normalize(); |
| 72 | + const neighborColor = texel.rgb; |
| 73 | + const viewPos = getViewPosition( sampleUv, depth ); |
| 74 | + |
| 75 | + const normalDiff = dot( viewNormal, normal ).toVar(); |
| 76 | + const normalSimilarity = pow( max( normalDiff, 0 ), this.normalPhi ).toVar(); |
| 77 | + const lumaDiff = abs( luminance( neighborColor ).sub( luminance( center ) ) ).toVar(); |
| 78 | + const lumaSimilarity = max( float( 1.0 ).sub( lumaDiff.div( this.lumaPhi ) ), 0 ).toVar(); |
| 79 | + const depthDiff = abs( dot( viewPosition.sub( viewPos ), viewNormal ) ).toVar(); |
| 80 | + const depthSimilarity = max( float( 1.0 ).sub( depthDiff.div( this.depthPhi ) ), 0 ); |
| 81 | + const w = lumaSimilarity.mul( depthSimilarity ).mul( normalSimilarity ); |
| 82 | + |
| 83 | + return vec4( neighborColor.mul( w ), w ); |
| 84 | + |
| 85 | + } ); |
| 86 | + |
| 87 | + const denoise = tslFn( () => { |
| 88 | + |
| 89 | + const depth = sampleDepth( uvNode ); |
| 90 | + const viewNormal = sampleNormal( uvNode ).rgb.normalize(); |
| 91 | + |
| 92 | + depth.greaterThanEqual( 1.0 ).discard(); |
| 93 | + dot( viewNormal, viewNormal ).equal( 0.0 ).discard(); |
| 94 | + |
| 95 | + const texel = sampleTexture( uvNode ); |
| 96 | + const center = vec3( texel.rgb ); |
| 97 | + |
| 98 | + const viewPosition = getViewPosition( uvNode, depth ); |
| 99 | + |
| 100 | + const noiseResolution = textureSize( this.noiseNode, 0 ); |
| 101 | + let noiseUv = vec2( uvNode.x, uvNode.y.oneMinus() ); |
| 102 | + noiseUv = noiseUv.mul( this._resolution.div( noiseResolution ) ); |
| 103 | + const noiseTexel = sampleNoise( noiseUv ); |
| 104 | + |
| 105 | + const x = sin( noiseTexel.element( this.index.mod( 4 ).mul( 2 ).mul( PI ) ) ); |
| 106 | + const y = cos( noiseTexel.element( this.index.mod( 4 ).mul( 2 ).mul( PI ) ) ); |
| 107 | + |
| 108 | + const noiseVec = vec2( x, y ); |
| 109 | + const rotationMatrix = mat2( noiseVec.x, noiseVec.y.negate(), noiseVec.x, noiseVec.y ); |
| 110 | + |
| 111 | + const totalWeight = float( 1.0 ).toVar(); |
| 112 | + const denoised = vec3( texel.rgb ).toVar(); |
| 113 | + |
| 114 | + loop( { start: int( 0 ), end: int( 16 ), type: 'int', condition: '<' }, ( { i } ) => { |
| 115 | + |
| 116 | + const sampleDir = this._sampleVectors.element( i ).toVar(); |
| 117 | + const offset = rotationMatrix.mul( sampleDir.xy.mul( float( 1.0 ).add( sampleDir.z.mul( this.radius.sub( 1 ) ) ) ) ).div( this._resolution ).toVar(); |
| 118 | + const sampleUv = uvNode.add( offset ).toVar(); |
| 119 | + |
| 120 | + const result = denoiseSample( center, viewNormal, viewPosition, sampleUv ); |
| 121 | + |
| 122 | + denoised.addAssign( result.xyz ); |
| 123 | + totalWeight.addAssign( result.w ); |
| 124 | + |
| 125 | + } ); |
| 126 | + |
| 127 | + If( totalWeight.greaterThan( float( 0 ) ), () => { |
| 128 | + |
| 129 | + denoised.divAssign( totalWeight ); |
| 130 | + |
| 131 | + } ); |
| 132 | + |
| 133 | + return vec4( denoised, 1.0 ); |
| 134 | + |
| 135 | + |
| 136 | + } ); |
| 137 | + |
| 138 | + const outputNode = denoise(); |
| 139 | + |
| 140 | + return outputNode; |
| 141 | + |
| 142 | + } |
| 143 | + |
| 144 | +} |
| 145 | + |
| 146 | +function generatePdSamplePointInitializer( samples, rings, radiusExponent ) { |
| 147 | + |
| 148 | + const poissonDisk = generateDenoiseSamples( samples, rings, radiusExponent ); |
| 149 | + |
| 150 | + const array = []; |
| 151 | + |
| 152 | + for ( let i = 0; i < samples; i ++ ) { |
| 153 | + |
| 154 | + const sample = poissonDisk[ i ]; |
| 155 | + array.push( sample ); |
| 156 | + |
| 157 | + } |
| 158 | + |
| 159 | + return array; |
| 160 | + |
| 161 | +} |
| 162 | + |
| 163 | +function generateDenoiseSamples( numSamples, numRings, radiusExponent ) { |
| 164 | + |
| 165 | + const samples = []; |
| 166 | + |
| 167 | + for ( let i = 0; i < numSamples; i ++ ) { |
| 168 | + |
| 169 | + const angle = 2 * Math.PI * numRings * i / numSamples; |
| 170 | + const radius = Math.pow( i / ( numSamples - 1 ), radiusExponent ); |
| 171 | + samples.push( new Vector3( Math.cos( angle ), Math.sin( angle ), radius ) ); |
| 172 | + |
| 173 | + } |
| 174 | + |
| 175 | + return samples; |
| 176 | + |
| 177 | +} |
| 178 | + |
| 179 | +export const denoise = ( node, depthNode, normalNode, noiseNode, camera ) => nodeObject( new DenoiseNode( nodeObject( node ).toTexture(), nodeObject( depthNode ), nodeObject( normalNode ), nodeObject( noiseNode ), camera ) ); |
| 180 | + |
| 181 | +addNodeElement( 'denoise', denoise ); |
| 182 | + |
| 183 | +export default DenoiseNode; |
0 commit comments