添加关照、全局等高线、修改图层问题

This commit is contained in:
2025-07-17 18:54:05 +08:00
parent c781d38c0c
commit b274b62671
4594 changed files with 791769 additions and 4921 deletions

View File

@ -0,0 +1,59 @@
import F_Schlick from './F_Schlick.js';
import V_GGX_SmithCorrelated from './V_GGX_SmithCorrelated.js';
import V_GGX_SmithCorrelated_Anisotropic from './V_GGX_SmithCorrelated_Anisotropic.js';
import D_GGX from './D_GGX.js';
import D_GGX_Anisotropic from './D_GGX_Anisotropic.js';
import { transformedNormalView } from '../../accessors/NormalNode.js';
import { positionViewDirection } from '../../accessors/PositionNode.js';
import { iridescence, alphaT, anisotropyT, anisotropyB } from '../../core/PropertyNode.js';
import { tslFn, defined } from '../../shadernode/ShaderNode.js';
// GGX Distribution, Schlick Fresnel, GGX_SmithCorrelated Visibility
const BRDF_GGX = tslFn( ( inputs ) => {
const { lightDirection, f0, f90, roughness, f, USE_IRIDESCENCE, USE_ANISOTROPY } = inputs;
const normalView = inputs.normalView || transformedNormalView;
const alpha = roughness.pow2(); // UE4's roughness
const halfDir = lightDirection.add( positionViewDirection ).normalize();
const dotNL = normalView.dot( lightDirection ).clamp();
const dotNV = normalView.dot( positionViewDirection ).clamp(); // @ TODO: Move to core dotNV
const dotNH = normalView.dot( halfDir ).clamp();
const dotVH = positionViewDirection.dot( halfDir ).clamp();
let F = F_Schlick( { f0, f90, dotVH } );
let V, D;
if ( defined( USE_IRIDESCENCE ) ) {
F = iridescence.mix( F, f );
}
if ( defined( USE_ANISOTROPY ) ) {
const dotTL = anisotropyT.dot( lightDirection );
const dotTV = anisotropyT.dot( positionViewDirection );
const dotTH = anisotropyT.dot( halfDir );
const dotBL = anisotropyB.dot( lightDirection );
const dotBV = anisotropyB.dot( positionViewDirection );
const dotBH = anisotropyB.dot( halfDir );
V = V_GGX_SmithCorrelated_Anisotropic( { alphaT, alphaB: alpha, dotTV, dotBV, dotTL, dotBL, dotNV, dotNL } );
D = D_GGX_Anisotropic( { alphaT, alphaB: alpha, dotNH, dotTH, dotBH } );
} else {
V = V_GGX_SmithCorrelated( { alpha, dotNL, dotNV } );
D = D_GGX( { alpha, dotNH } );
}
return F.mul( V ).mul( D );
} ); // validated
export default BRDF_GGX;

View File

@ -0,0 +1,9 @@
import { tslFn } from '../../shadernode/ShaderNode.js';
const BRDF_Lambert = tslFn( ( inputs ) => {
return inputs.diffuseColor.mul( 1 / Math.PI ); // punctual light
} ); // validated
export default BRDF_Lambert;

View File

@ -0,0 +1,57 @@
import { transformedNormalView } from '../../accessors/NormalNode.js';
import { positionViewDirection } from '../../accessors/PositionNode.js';
import { sheen, sheenRoughness } from '../../core/PropertyNode.js';
import { tslFn, float } from '../../shadernode/ShaderNode.js';
// https://github.com/google/filament/blob/master/shaders/src/brdf.fs
const D_Charlie = tslFn( ( { roughness, dotNH } ) => {
const alpha = roughness.pow2();
// Estevez and Kulla 2017, "Production Friendly Microfacet Sheen BRDF"
const invAlpha = float( 1.0 ).div( alpha );
const cos2h = dotNH.pow2();
const sin2h = cos2h.oneMinus().max( 0.0078125 ); // 2^(-14/2), so sin2h^2 > 0 in fp16
return float( 2.0 ).add( invAlpha ).mul( sin2h.pow( invAlpha.mul( 0.5 ) ) ).div( 2.0 * Math.PI );
} ).setLayout( {
name: 'D_Charlie',
type: 'float',
inputs: [
{ name: 'roughness', type: 'float' },
{ name: 'dotNH', type: 'float' }
]
} );
// https://github.com/google/filament/blob/master/shaders/src/brdf.fs
const V_Neubelt = tslFn( ( { dotNV, dotNL } ) => {
// Neubelt and Pettineo 2013, "Crafting a Next-gen Material Pipeline for The Order: 1886"
return float( 1.0 ).div( float( 4.0 ).mul( dotNL.add( dotNV ).sub( dotNL.mul( dotNV ) ) ) );
} ).setLayout( {
name: 'V_Neubelt',
type: 'float',
inputs: [
{ name: 'dotNV', type: 'float' },
{ name: 'dotNL', type: 'float' }
]
} );
const BRDF_Sheen = tslFn( ( { lightDirection } ) => {
const halfDir = lightDirection.add( positionViewDirection ).normalize();
const dotNL = transformedNormalView.dot( lightDirection ).clamp();
const dotNV = transformedNormalView.dot( positionViewDirection ).clamp();
const dotNH = transformedNormalView.dot( halfDir ).clamp();
const D = D_Charlie( { roughness: sheenRoughness, dotNH } );
const V = V_Neubelt( { dotNV, dotNL } );
return sheen.mul( D ).mul( V );
} );
export default BRDF_Sheen;

View File

@ -0,0 +1,30 @@
import { tslFn, vec2, vec4 } from '../../shadernode/ShaderNode.js';
// Analytical approximation of the DFG LUT, one half of the
// split-sum approximation used in indirect specular lighting.
// via 'environmentBRDF' from "Physically Based Shading on Mobile"
// https://www.unrealengine.com/blog/physically-based-shading-on-mobile
const DFGApprox = tslFn( ( { roughness, dotNV } ) => {
const c0 = vec4( - 1, - 0.0275, - 0.572, 0.022 );
const c1 = vec4( 1, 0.0425, 1.04, - 0.04 );
const r = roughness.mul( c0 ).add( c1 );
const a004 = r.x.mul( r.x ).min( dotNV.mul( - 9.28 ).exp2() ).mul( r.x ).add( r.y );
const fab = vec2( - 1.04, 1.04 ).mul( a004 ).add( r.zw );
return fab;
} ).setLayout( {
name: 'DFGApprox',
type: 'vec2',
inputs: [
{ name: 'roughness', type: 'float' },
{ name: 'dotNV', type: 'vec3' }
]
} );
export default DFGApprox;

View File

@ -0,0 +1,23 @@
import { tslFn } from '../../shadernode/ShaderNode.js';
// Microfacet Models for Refraction through Rough Surfaces - equation (33)
// http://graphicrants.blogspot.com/2013/08/specular-brdf-reference.html
// alpha is "roughness squared" in Disneys reparameterization
const D_GGX = tslFn( ( { alpha, dotNH } ) => {
const a2 = alpha.pow2();
const denom = dotNH.pow2().mul( a2.oneMinus() ).oneMinus(); // avoid alpha = 0 with dotNH = 1
return a2.div( denom.pow2() ).mul( 1 / Math.PI );
} ).setLayout( {
name: 'D_GGX',
type: 'float',
inputs: [
{ name: 'alpha', type: 'float' },
{ name: 'dotNH', type: 'float' }
]
} ); // validated
export default D_GGX;

View File

@ -0,0 +1,28 @@
import { tslFn, float, vec3 } from '../../shadernode/ShaderNode.js';
const RECIPROCAL_PI = float( 1 / Math.PI );
// https://google.github.io/filament/Filament.md.html#materialsystem/anisotropicmodel/anisotropicspecularbrdf
const D_GGX_Anisotropic = tslFn( ( { alphaT, alphaB, dotNH, dotTH, dotBH } ) => {
const a2 = alphaT.mul( alphaB );
const v = vec3( alphaB.mul( dotTH ), alphaT.mul( dotBH ), a2.mul( dotNH ) );
const v2 = v.dot( v );
const w2 = a2.div( v2 );
return RECIPROCAL_PI.mul( a2.mul( w2.pow2() ) );
} ).setLayout( {
name: 'D_GGX_Anisotropic',
type: 'float',
inputs: [
{ name: 'alphaT', type: 'float', qualifier: 'in' },
{ name: 'alphaB', type: 'float', qualifier: 'in' },
{ name: 'dotNH', type: 'float', qualifier: 'in' },
{ name: 'dotTH', type: 'float', qualifier: 'in' },
{ name: 'dotBH', type: 'float', qualifier: 'in' }
]
} );
export default D_GGX_Anisotropic;

View File

@ -0,0 +1,13 @@
import DFGApprox from './DFGApprox.js';
import { tslFn } from '../../shadernode/ShaderNode.js';
const EnvironmentBRDF = tslFn( ( inputs ) => {
const { dotNV, specularColor, specularF90, roughness } = inputs;
const fab = DFGApprox( { dotNV, roughness } );
return specularColor.mul( fab.x ).add( specularF90.mul( fab.y ) );
} );
export default EnvironmentBRDF;

View File

@ -0,0 +1,16 @@
import { tslFn } from '../../shadernode/ShaderNode.js';
const F_Schlick = tslFn( ( { f0, f90, dotVH } ) => {
// Original approximation by Christophe Schlick '94
// float fresnel = pow( 1.0 - dotVH, 5.0 );
// Optimized variant (presented by Epic at SIGGRAPH '13)
// https://cdn2.unrealengine.com/Resources/files/2013SiggraphPresentationsNotes-26915738.pdf
const fresnel = dotVH.mul( - 5.55473 ).sub( 6.98316 ).mul( dotVH ).exp2();
return f0.mul( fresnel.oneMinus() ).add( f90.mul( fresnel ) );
} ); // validated
export default F_Schlick;

View File

@ -0,0 +1,21 @@
import { tslFn, vec3 } from '../../shadernode/ShaderNode.js';
const Schlick_to_F0 = tslFn( ( { f, f90, dotVH } ) => {
const x = dotVH.oneMinus().saturate();
const x2 = x.mul( x );
const x5 = x.mul( x2, x2 ).clamp( 0, .9999 );
return f.sub( vec3( f90 ).mul( x5 ) ).div( x5.oneMinus() );
} ).setLayout( {
name: 'Schlick_to_F0',
type: 'vec3',
inputs: [
{ name: 'f', type: 'vec3' },
{ name: 'f90', type: 'float' },
{ name: 'dotVH', type: 'float' }
]
} );
export default Schlick_to_F0;

View File

@ -0,0 +1,26 @@
import { div } from '../../math/OperatorNode.js';
import { EPSILON } from '../../math/MathNode.js';
import { tslFn } from '../../shadernode/ShaderNode.js';
// Moving Frostbite to Physically Based Rendering 3.0 - page 12, listing 2
// https://seblagarde.files.wordpress.com/2015/07/course_notes_moving_frostbite_to_pbr_v32.pdf
const V_GGX_SmithCorrelated = tslFn( ( { alpha, dotNL, dotNV } ) => {
const a2 = alpha.pow2();
const gv = dotNL.mul( a2.add( a2.oneMinus().mul( dotNV.pow2() ) ).sqrt() );
const gl = dotNV.mul( a2.add( a2.oneMinus().mul( dotNL.pow2() ) ).sqrt() );
return div( 0.5, gv.add( gl ).max( EPSILON ) );
} ).setLayout( {
name: 'V_GGX_SmithCorrelated',
type: 'float',
inputs: [
{ name: 'alpha', type: 'float' },
{ name: 'dotNL', type: 'float' },
{ name: 'dotNV', type: 'float' }
]
} ); // validated
export default V_GGX_SmithCorrelated;

View File

@ -0,0 +1,29 @@
import { div } from '../../math/OperatorNode.js';
import { tslFn, vec3 } from '../../shadernode/ShaderNode.js';
// https://google.github.io/filament/Filament.md.html#materialsystem/anisotropicmodel/anisotropicspecularbrdf
const V_GGX_SmithCorrelated_Anisotropic = tslFn( ( { alphaT, alphaB, dotTV, dotBV, dotTL, dotBL, dotNV, dotNL } ) => {
const gv = dotNL.mul( vec3( alphaT.mul( dotTV ), alphaB.mul( dotBV ), dotNV ).length() );
const gl = dotNV.mul( vec3( alphaT.mul( dotTL ), alphaB.mul( dotBL ), dotNL ).length() );
const v = div( 0.5, gv.add( gl ) );
return v.saturate();
} ).setLayout( {
name: 'V_GGX_SmithCorrelated_Anisotropic',
type: 'float',
inputs: [
{ name: 'alphaT', type: 'float', qualifier: 'in' },
{ name: 'alphaB', type: 'float', qualifier: 'in' },
{ name: 'dotTV', type: 'float', qualifier: 'in' },
{ name: 'dotBV', type: 'float', qualifier: 'in' },
{ name: 'dotTL', type: 'float', qualifier: 'in' },
{ name: 'dotBL', type: 'float', qualifier: 'in' },
{ name: 'dotNV', type: 'float', qualifier: 'in' },
{ name: 'dotNL', type: 'float', qualifier: 'in' }
]
} );
export default V_GGX_SmithCorrelated_Anisotropic;

View File

@ -0,0 +1,67 @@
import LightingModel from '../core/LightingModel.js';
import F_Schlick from './BSDF/F_Schlick.js';
import BRDF_Lambert from './BSDF/BRDF_Lambert.js';
import { diffuseColor } from '../core/PropertyNode.js';
import { transformedNormalView } from '../accessors/NormalNode.js';
import { materialSpecularStrength } from '../accessors/MaterialNode.js';
import { shininess, specularColor } from '../core/PropertyNode.js';
import { positionViewDirection } from '../accessors/PositionNode.js';
import { tslFn, float } from '../shadernode/ShaderNode.js';
const G_BlinnPhong_Implicit = () => float( 0.25 );
const D_BlinnPhong = tslFn( ( { dotNH } ) => {
return shininess.mul( float( 0.5 ) ).add( 1.0 ).mul( float( 1 / Math.PI ) ).mul( dotNH.pow( shininess ) );
} );
const BRDF_BlinnPhong = tslFn( ( { lightDirection } ) => {
const halfDir = lightDirection.add( positionViewDirection ).normalize();
const dotNH = transformedNormalView.dot( halfDir ).clamp();
const dotVH = positionViewDirection.dot( halfDir ).clamp();
const F = F_Schlick( { f0: specularColor, f90: 1.0, dotVH } );
const G = G_BlinnPhong_Implicit();
const D = D_BlinnPhong( { dotNH } );
return F.mul( G ).mul( D );
} );
class PhongLightingModel extends LightingModel {
constructor( specular = true ) {
super();
this.specular = specular;
}
direct( { lightDirection, lightColor, reflectedLight } ) {
const dotNL = transformedNormalView.dot( lightDirection ).clamp();
const irradiance = dotNL.mul( lightColor );
reflectedLight.directDiffuse.addAssign( irradiance.mul( BRDF_Lambert( { diffuseColor: diffuseColor.rgb } ) ) );
if ( this.specular === true ) {
reflectedLight.directSpecular.addAssign( irradiance.mul( BRDF_BlinnPhong( { lightDirection } ) ).mul( materialSpecularStrength ) );
}
}
indirectDiffuse( { irradiance, reflectedLight } ) {
reflectedLight.indirectDiffuse.addAssign( irradiance.mul( BRDF_Lambert( { diffuseColor } ) ) );
}
}
export default PhongLightingModel;

View File

@ -0,0 +1,547 @@
import BRDF_Lambert from './BSDF/BRDF_Lambert.js';
import BRDF_GGX from './BSDF/BRDF_GGX.js';
import DFGApprox from './BSDF/DFGApprox.js';
import EnvironmentBRDF from './BSDF/EnvironmentBRDF.js';
import F_Schlick from './BSDF/F_Schlick.js';
import Schlick_to_F0 from './BSDF/Schlick_to_F0.js';
import BRDF_Sheen from './BSDF/BRDF_Sheen.js';
import LightingModel from '../core/LightingModel.js';
import { diffuseColor, specularColor, specularF90, roughness, clearcoat, clearcoatRoughness, sheen, sheenRoughness, iridescence, iridescenceIOR, iridescenceThickness, ior, thickness, transmission, attenuationDistance, attenuationColor } from '../core/PropertyNode.js';
import { transformedNormalView, transformedClearcoatNormalView, transformedNormalWorld } from '../accessors/NormalNode.js';
import { positionViewDirection, positionWorld } from '../accessors/PositionNode.js';
import { tslFn, float, vec2, vec3, vec4, mat3, If } from '../shadernode/ShaderNode.js';
import { cond } from '../math/CondNode.js';
import { mix, normalize, refract, length, clamp, log2, log, exp, smoothstep } from '../math/MathNode.js';
import { div } from '../math/OperatorNode.js';
import { cameraPosition, cameraProjectionMatrix, cameraViewMatrix } from '../accessors/CameraNode.js';
import { modelWorldMatrix } from '../accessors/ModelNode.js';
import { viewportResolution } from '../display/ViewportNode.js';
import { viewportMipTexture } from '../display/ViewportTextureNode.js';
//
// Transmission
//
const getVolumeTransmissionRay = tslFn( ( [ n, v, thickness, ior, modelMatrix ] ) => {
// Direction of refracted light.
const refractionVector = vec3( refract( v.negate(), normalize( n ), div( 1.0, ior ) ) );
// Compute rotation-independant scaling of the model matrix.
const modelScale = vec3(
length( modelMatrix[ 0 ].xyz ),
length( modelMatrix[ 1 ].xyz ),
length( modelMatrix[ 2 ].xyz )
);
// The thickness is specified in local space.
return normalize( refractionVector ).mul( thickness.mul( modelScale ) );
} ).setLayout( {
name: 'getVolumeTransmissionRay',
type: 'vec3',
inputs: [
{ name: 'n', type: 'vec3' },
{ name: 'v', type: 'vec3' },
{ name: 'thickness', type: 'float' },
{ name: 'ior', type: 'float' },
{ name: 'modelMatrix', type: 'mat4' }
]
} );
const applyIorToRoughness = tslFn( ( [ roughness, ior ] ) => {
// Scale roughness with IOR so that an IOR of 1.0 results in no microfacet refraction and
// an IOR of 1.5 results in the default amount of microfacet refraction.
return roughness.mul( clamp( ior.mul( 2.0 ).sub( 2.0 ), 0.0, 1.0 ) );
} ).setLayout( {
name: 'applyIorToRoughness',
type: 'float',
inputs: [
{ name: 'roughness', type: 'float' },
{ name: 'ior', type: 'float' }
]
} );
const singleViewportMipTexture = viewportMipTexture();
const getTransmissionSample = tslFn( ( [ fragCoord, roughness, ior ] ) => {
const transmissionSample = singleViewportMipTexture.uv( fragCoord );
//const transmissionSample = viewportMipTexture( fragCoord );
const lod = log2( float( viewportResolution.x ) ).mul( applyIorToRoughness( roughness, ior ) );
return transmissionSample.bicubic( lod );
} );
const volumeAttenuation = tslFn( ( [ transmissionDistance, attenuationColor, attenuationDistance ] ) => {
If( attenuationDistance.notEqual( 0 ), () => {
// Compute light attenuation using Beer's law.
const attenuationCoefficient = log( attenuationColor ).negate().div( attenuationDistance );
const transmittance = exp( attenuationCoefficient.negate().mul( transmissionDistance ) );
return transmittance;
} );
// Attenuation distance is +∞, i.e. the transmitted color is not attenuated at all.
return vec3( 1.0 );
} ).setLayout( {
name: 'volumeAttenuation',
type: 'vec3',
inputs: [
{ name: 'transmissionDistance', type: 'float' },
{ name: 'attenuationColor', type: 'vec3' },
{ name: 'attenuationDistance', type: 'float' }
]
} );
const getIBLVolumeRefraction = tslFn( ( [ n, v, roughness, diffuseColor, specularColor, specularF90, position, modelMatrix, viewMatrix, projMatrix, ior, thickness, attenuationColor, attenuationDistance ] ) => {
const transmissionRay = getVolumeTransmissionRay( n, v, thickness, ior, modelMatrix );
const refractedRayExit = position.add( transmissionRay );
// Project refracted vector on the framebuffer, while mapping to normalized device coordinates.
const ndcPos = projMatrix.mul( viewMatrix.mul( vec4( refractedRayExit, 1.0 ) ) );
const refractionCoords = vec2( ndcPos.xy.div( ndcPos.w ) ).toVar();
refractionCoords.addAssign( 1.0 );
refractionCoords.divAssign( 2.0 );
refractionCoords.assign( vec2( refractionCoords.x, refractionCoords.y.oneMinus() ) ); // webgpu
// Sample framebuffer to get pixel the refracted ray hits.
const transmittedLight = getTransmissionSample( refractionCoords, roughness, ior );
const transmittance = diffuseColor.mul( volumeAttenuation( length( transmissionRay ), attenuationColor, attenuationDistance ) );
const attenuatedColor = transmittance.rgb.mul( transmittedLight.rgb );
const dotNV = n.dot( v ).clamp();
// Get the specular component.
const F = vec3( EnvironmentBRDF( { // n, v, specularColor, specularF90, roughness
dotNV,
specularColor,
specularF90,
roughness
} ) );
// As less light is transmitted, the opacity should be increased. This simple approximation does a decent job
// of modulating a CSS background, and has no effect when the buffer is opaque, due to a solid object or clear color.
const transmittanceFactor = transmittance.r.add( transmittance.g, transmittance.b ).div( 3.0 );
return vec4( F.oneMinus().mul( attenuatedColor ), transmittedLight.a.oneMinus().mul( transmittanceFactor ).oneMinus() );
} );
//
// Iridescence
//
// XYZ to linear-sRGB color space
const XYZ_TO_REC709 = mat3(
3.2404542, - 0.9692660, 0.0556434,
- 1.5371385, 1.8760108, - 0.2040259,
- 0.4985314, 0.0415560, 1.0572252
);
// Assume air interface for top
// Note: We don't handle the case fresnel0 == 1
const Fresnel0ToIor = ( fresnel0 ) => {
const sqrtF0 = fresnel0.sqrt();
return vec3( 1.0 ).add( sqrtF0 ).div( vec3( 1.0 ).sub( sqrtF0 ) );
};
// ior is a value between 1.0 and 3.0. 1.0 is air interface
const IorToFresnel0 = ( transmittedIor, incidentIor ) => {
return transmittedIor.sub( incidentIor ).div( transmittedIor.add( incidentIor ) ).pow2();
};
// Fresnel equations for dielectric/dielectric interfaces.
// Ref: https://belcour.github.io/blog/research/2017/05/01/brdf-thin-film.html
// Evaluation XYZ sensitivity curves in Fourier space
const evalSensitivity = ( OPD, shift ) => {
const phase = OPD.mul( 2.0 * Math.PI * 1.0e-9 );
const val = vec3( 5.4856e-13, 4.4201e-13, 5.2481e-13 );
const pos = vec3( 1.6810e+06, 1.7953e+06, 2.2084e+06 );
const VAR = vec3( 4.3278e+09, 9.3046e+09, 6.6121e+09 );
const x = float( 9.7470e-14 * Math.sqrt( 2.0 * Math.PI * 4.5282e+09 ) ).mul( phase.mul( 2.2399e+06 ).add( shift.x ).cos() ).mul( phase.pow2().mul( - 4.5282e+09 ).exp() );
let xyz = val.mul( VAR.mul( 2.0 * Math.PI ).sqrt() ).mul( pos.mul( phase ).add( shift ).cos() ).mul( phase.pow2().negate().mul( VAR ).exp() );
xyz = vec3( xyz.x.add( x ), xyz.y, xyz.z ).div( 1.0685e-7 );
const rgb = XYZ_TO_REC709.mul( xyz );
return rgb;
};
const evalIridescence = tslFn( ( { outsideIOR, eta2, cosTheta1, thinFilmThickness, baseF0 } ) => {
// Force iridescenceIOR -> outsideIOR when thinFilmThickness -> 0.0
const iridescenceIOR = mix( outsideIOR, eta2, smoothstep( 0.0, 0.03, thinFilmThickness ) );
// Evaluate the cosTheta on the base layer (Snell law)
const sinTheta2Sq = outsideIOR.div( iridescenceIOR ).pow2().mul( float( 1 ).sub( cosTheta1.pow2() ) );
// Handle TIR:
const cosTheta2Sq = float( 1 ).sub( sinTheta2Sq );
/*if ( cosTheta2Sq < 0.0 ) {
return vec3( 1.0 );
}*/
const cosTheta2 = cosTheta2Sq.sqrt();
// First interface
const R0 = IorToFresnel0( iridescenceIOR, outsideIOR );
const R12 = F_Schlick( { f0: R0, f90: 1.0, dotVH: cosTheta1 } );
//const R21 = R12;
const T121 = R12.oneMinus();
const phi12 = iridescenceIOR.lessThan( outsideIOR ).cond( Math.PI, 0.0 );
const phi21 = float( Math.PI ).sub( phi12 );
// Second interface
const baseIOR = Fresnel0ToIor( baseF0.clamp( 0.0, 0.9999 ) ); // guard against 1.0
const R1 = IorToFresnel0( baseIOR, iridescenceIOR.vec3() );
const R23 = F_Schlick( { f0: R1, f90: 1.0, dotVH: cosTheta2 } );
const phi23 = vec3(
baseIOR.x.lessThan( iridescenceIOR ).cond( Math.PI, 0.0 ),
baseIOR.y.lessThan( iridescenceIOR ).cond( Math.PI, 0.0 ),
baseIOR.z.lessThan( iridescenceIOR ).cond( Math.PI, 0.0 )
);
// Phase shift
const OPD = iridescenceIOR.mul( thinFilmThickness, cosTheta2, 2.0 );
const phi = vec3( phi21 ).add( phi23 );
// Compound terms
const R123 = R12.mul( R23 ).clamp( 1e-5, 0.9999 );
const r123 = R123.sqrt();
const Rs = T121.pow2().mul( R23 ).div( vec3( 1.0 ).sub( R123 ) );
// Reflectance term for m = 0 (DC term amplitude)
const C0 = R12.add( Rs );
let I = C0;
// Reflectance term for m > 0 (pairs of diracs)
let Cm = Rs.sub( T121 );
for ( let m = 1; m <= 2; ++ m ) {
Cm = Cm.mul( r123 );
const Sm = evalSensitivity( float( m ).mul( OPD ), float( m ).mul( phi ) ).mul( 2.0 );
I = I.add( Cm.mul( Sm ) );
}
// Since out of gamut colors might be produced, negative color values are clamped to 0.
return I.max( vec3( 0.0 ) );
} ).setLayout( {
name: 'evalIridescence',
type: 'vec3',
inputs: [
{ name: 'outsideIOR', type: 'float' },
{ name: 'eta2', type: 'float' },
{ name: 'cosTheta1', type: 'float' },
{ name: 'thinFilmThickness', type: 'float' },
{ name: 'baseF0', type: 'vec3' }
]
} );
//
// Sheen
//
// This is a curve-fit approxmation to the "Charlie sheen" BRDF integrated over the hemisphere from
// Estevez and Kulla 2017, "Production Friendly Microfacet Sheen BRDF". The analysis can be found
// in the Sheen section of https://drive.google.com/file/d/1T0D1VSyR4AllqIJTQAraEIzjlb5h4FKH/view?usp=sharing
const IBLSheenBRDF = tslFn( ( { normal, viewDir, roughness } ) => {
const dotNV = normal.dot( viewDir ).saturate();
const r2 = roughness.pow2();
const a = cond(
roughness.lessThan( 0.25 ),
float( - 339.2 ).mul( r2 ).add( float( 161.4 ).mul( roughness ) ).sub( 25.9 ),
float( - 8.48 ).mul( r2 ).add( float( 14.3 ).mul( roughness ) ).sub( 9.95 )
);
const b = cond(
roughness.lessThan( 0.25 ),
float( 44.0 ).mul( r2 ).sub( float( 23.7 ).mul( roughness ) ).add( 3.26 ),
float( 1.97 ).mul( r2 ).sub( float( 3.27 ).mul( roughness ) ).add( 0.72 )
);
const DG = cond( roughness.lessThan( 0.25 ), 0.0, float( 0.1 ).mul( roughness ).sub( 0.025 ) ).add( a.mul( dotNV ).add( b ).exp() );
return DG.mul( 1.0 / Math.PI ).saturate();
} );
const clearcoatF0 = vec3( 0.04 );
const clearcoatF90 = vec3( 1 );
//
class PhysicalLightingModel extends LightingModel {
constructor( clearcoat = false, sheen = false, iridescence = false, anisotropy = false, transmission = false ) {
super();
this.clearcoat = clearcoat;
this.sheen = sheen;
this.iridescence = iridescence;
this.anisotropy = anisotropy;
this.transmission = transmission;
this.clearcoatRadiance = null;
this.clearcoatSpecularDirect = null;
this.clearcoatSpecularIndirect = null;
this.sheenSpecularDirect = null;
this.sheenSpecularIndirect = null;
this.iridescenceFresnel = null;
this.iridescenceF0 = null;
}
start( context ) {
if ( this.clearcoat === true ) {
this.clearcoatRadiance = vec3().temp( 'clearcoatRadiance' );
this.clearcoatSpecularDirect = vec3().temp( 'clearcoatSpecularDirect' );
this.clearcoatSpecularIndirect = vec3().temp( 'clearcoatSpecularIndirect' );
}
if ( this.sheen === true ) {
this.sheenSpecularDirect = vec3().temp( 'sheenSpecularDirect' );
this.sheenSpecularIndirect = vec3().temp( 'sheenSpecularIndirect' );
}
if ( this.iridescence === true ) {
const dotNVi = transformedNormalView.dot( positionViewDirection ).clamp();
this.iridescenceFresnel = evalIridescence( {
outsideIOR: float( 1.0 ),
eta2: iridescenceIOR,
cosTheta1: dotNVi,
thinFilmThickness: iridescenceThickness,
baseF0: specularColor
} );
this.iridescenceF0 = Schlick_to_F0( { f: this.iridescenceFresnel, f90: 1.0, dotVH: dotNVi } );
}
if ( this.transmission === true ) {
const position = positionWorld;
const v = cameraPosition.sub( positionWorld ).normalize(); // TODO: Create Node for this, same issue in MaterialX
const n = transformedNormalWorld;
context.backdrop = getIBLVolumeRefraction(
n,
v,
roughness,
diffuseColor,
specularColor,
specularF90, // specularF90
position, // positionWorld
modelWorldMatrix, // modelMatrix
cameraViewMatrix, // viewMatrix
cameraProjectionMatrix, // projMatrix
ior,
thickness,
attenuationColor,
attenuationDistance
);
context.backdropAlpha = transmission;
diffuseColor.a.mulAssign( mix( 1, context.backdrop.a, transmission ) );
}
}
// Fdez-Agüera's "Multiple-Scattering Microfacet Model for Real-Time Image Based Lighting"
// Approximates multiscattering in order to preserve energy.
// http://www.jcgt.org/published/0008/01/03/
computeMultiscattering( singleScatter, multiScatter, specularF90 ) {
const dotNV = transformedNormalView.dot( positionViewDirection ).clamp(); // @ TODO: Move to core dotNV
const fab = DFGApprox( { roughness, dotNV } );
const Fr = this.iridescenceF0 ? iridescence.mix( specularColor, this.iridescenceF0 ) : specularColor;
const FssEss = Fr.mul( fab.x ).add( specularF90.mul( fab.y ) );
const Ess = fab.x.add( fab.y );
const Ems = Ess.oneMinus();
const Favg = specularColor.add( specularColor.oneMinus().mul( 0.047619 ) ); // 1/21
const Fms = FssEss.mul( Favg ).div( Ems.mul( Favg ).oneMinus() );
singleScatter.addAssign( FssEss );
multiScatter.addAssign( Fms.mul( Ems ) );
}
direct( { lightDirection, lightColor, reflectedLight } ) {
const dotNL = transformedNormalView.dot( lightDirection ).clamp();
const irradiance = dotNL.mul( lightColor );
if ( this.sheen === true ) {
this.sheenSpecularDirect.addAssign( irradiance.mul( BRDF_Sheen( { lightDirection } ) ) );
}
if ( this.clearcoat === true ) {
const dotNLcc = transformedClearcoatNormalView.dot( lightDirection ).clamp();
const ccIrradiance = dotNLcc.mul( lightColor );
this.clearcoatSpecularDirect.addAssign( ccIrradiance.mul( BRDF_GGX( { lightDirection, f0: clearcoatF0, f90: clearcoatF90, roughness: clearcoatRoughness, normalView: transformedClearcoatNormalView } ) ) );
}
reflectedLight.directDiffuse.addAssign( irradiance.mul( BRDF_Lambert( { diffuseColor: diffuseColor.rgb } ) ) );
reflectedLight.directSpecular.addAssign( irradiance.mul( BRDF_GGX( { lightDirection, f0: specularColor, f90: 1, roughness, iridescence: this.iridescence, f: this.iridescenceFresnel, USE_IRIDESCENCE: this.iridescence, USE_ANISOTROPY: this.anisotropy } ) ) );
}
indirectDiffuse( { irradiance, reflectedLight } ) {
reflectedLight.indirectDiffuse.addAssign( irradiance.mul( BRDF_Lambert( { diffuseColor } ) ) );
}
indirectSpecular( { radiance, iblIrradiance, reflectedLight } ) {
if ( this.sheen === true ) {
this.sheenSpecularIndirect.addAssign( iblIrradiance.mul(
sheen,
IBLSheenBRDF( {
normal: transformedNormalView,
viewDir: positionViewDirection,
roughness: sheenRoughness
} )
) );
}
if ( this.clearcoat === true ) {
const dotNVcc = transformedClearcoatNormalView.dot( positionViewDirection ).clamp();
const clearcoatEnv = EnvironmentBRDF( {
dotNV: dotNVcc,
specularColor: clearcoatF0,
specularF90: clearcoatF90,
roughness: clearcoatRoughness
} );
this.clearcoatSpecularIndirect.addAssign( this.clearcoatRadiance.mul( clearcoatEnv ) );
}
// Both indirect specular and indirect diffuse light accumulate here
const singleScattering = vec3().temp( 'singleScattering' );
const multiScattering = vec3().temp( 'multiScattering' );
const cosineWeightedIrradiance = iblIrradiance.mul( 1 / Math.PI );
this.computeMultiscattering( singleScattering, multiScattering, specularF90 );
const totalScattering = singleScattering.add( multiScattering );
const diffuse = diffuseColor.mul( totalScattering.r.max( totalScattering.g ).max( totalScattering.b ).oneMinus() );
reflectedLight.indirectSpecular.addAssign( radiance.mul( singleScattering ) );
reflectedLight.indirectSpecular.addAssign( multiScattering.mul( cosineWeightedIrradiance ) );
reflectedLight.indirectDiffuse.addAssign( diffuse.mul( cosineWeightedIrradiance ) );
}
ambientOcclusion( { ambientOcclusion, reflectedLight } ) {
const dotNV = transformedNormalView.dot( positionViewDirection ).clamp(); // @ TODO: Move to core dotNV
const aoNV = dotNV.add( ambientOcclusion );
const aoExp = roughness.mul( - 16.0 ).oneMinus().negate().exp2();
const aoNode = ambientOcclusion.sub( aoNV.pow( aoExp ).oneMinus() ).clamp();
if ( this.clearcoat === true ) {
this.clearcoatSpecularIndirect.mulAssign( ambientOcclusion );
}
if ( this.sheen === true ) {
this.sheenSpecularIndirect.mulAssign( ambientOcclusion );
}
reflectedLight.indirectDiffuse.mulAssign( ambientOcclusion );
reflectedLight.indirectSpecular.mulAssign( aoNode );
}
finish( context ) {
const { outgoingLight } = context;
if ( this.clearcoat === true ) {
const dotNVcc = transformedClearcoatNormalView.dot( positionViewDirection ).clamp();
const Fcc = F_Schlick( {
dotVH: dotNVcc,
f0: clearcoatF0,
f90: clearcoatF90
} );
const clearcoatLight = outgoingLight.mul( clearcoat.mul( Fcc ).oneMinus() ).add( this.clearcoatSpecularDirect.add( this.clearcoatSpecularIndirect ).mul( clearcoat ) );
outgoingLight.assign( clearcoatLight );
}
if ( this.sheen === true ) {
const sheenEnergyComp = sheen.r.max( sheen.g ).max( sheen.b ).mul( 0.157 ).oneMinus();
const sheenLight = outgoingLight.mul( sheenEnergyComp ).add( this.sheenSpecularDirect, this.sheenSpecularIndirect );
outgoingLight.assign( sheenLight );
}
}
}
export default PhysicalLightingModel;

View File

@ -0,0 +1,31 @@
import LightingModel from '../core/LightingModel.js';
import { diffuseColor } from '../core/PropertyNode.js';
import { float } from '../shadernode/ShaderNode.js';
class ShadowMaskModel extends LightingModel {
constructor() {
super();
this.shadowNode = float( 1 ).toVar( 'shadowMask' );
}
direct( { shadowMask } ) {
this.shadowNode.mulAssign( shadowMask );
}
finish( context ) {
diffuseColor.a.mulAssign( this.shadowNode.oneMinus() );
context.outgoingLight.rgb.assign( diffuseColor.rgb ); // TODO: Optimize LightsNode to avoid this assignment
}
}
export default ShadowMaskModel;

View File

@ -0,0 +1,13 @@
import { normalGeometry } from '../../accessors/NormalNode.js';
import { tslFn } from '../../shadernode/ShaderNode.js';
const getGeometryRoughness = tslFn( () => {
const dxy = normalGeometry.dFdx().abs().max( normalGeometry.dFdy().abs() );
const geometryRoughness = dxy.x.max( dxy.y ).max( dxy.z );
return geometryRoughness;
} );
export default getGeometryRoughness;

View File

@ -0,0 +1,18 @@
import getGeometryRoughness from './getGeometryRoughness.js';
import { tslFn } from '../../shadernode/ShaderNode.js';
const getRoughness = tslFn( ( inputs ) => {
const { roughness } = inputs;
const geometryRoughness = getGeometryRoughness();
let roughnessFactor = roughness.max( 0.0525 ); // 0.0525 corresponds to the base mip of a 256 cubemap.
roughnessFactor = roughnessFactor.add( geometryRoughness );
roughnessFactor = roughnessFactor.min( 1.0 );
return roughnessFactor;
} );
export default getRoughness;