diff --git a/examples/files.json b/examples/files.json index df2beea6a62ee5..a68d269d829064 100644 --- a/examples/files.json +++ b/examples/files.json @@ -332,6 +332,7 @@ "webgpu_depth_texture", "webgpu_display_stereo", "webgpu_equirectangular", + "webgpu_fog_height", "webgpu_hdr", "webgpu_instance_mesh", "webgpu_instance_path", diff --git a/examples/screenshots/webgpu_fog_height.jpg b/examples/screenshots/webgpu_fog_height.jpg new file mode 100644 index 00000000000000..80573f9fa7f404 Binary files /dev/null and b/examples/screenshots/webgpu_fog_height.jpg differ diff --git a/examples/webgpu_fog_height.html b/examples/webgpu_fog_height.html new file mode 100644 index 00000000000000..a37a102d422583 --- /dev/null +++ b/examples/webgpu_fog_height.html @@ -0,0 +1,148 @@ + + + + three.js webgpu - height fog + + + + + + +
+ + +
+ three.jsHeight Fog +
+ + + Exponential Height Fog with TSL. + +
+ + + + + + diff --git a/src/Three.TSL.js b/src/Three.TSL.js index 63d38f9039a891..608c2fb17a326f 100644 --- a/src/Three.TSL.js +++ b/src/Three.TSL.js @@ -177,6 +177,7 @@ export const equal = TSL.equal; export const equirectUV = TSL.equirectUV; export const exp = TSL.exp; export const exp2 = TSL.exp2; +export const exponentialHeightFogFactor = TSL.exponentialHeightFogFactor; export const expression = TSL.expression; export const faceDirection = TSL.faceDirection; export const faceForward = TSL.faceForward; diff --git a/src/nodes/fog/Fog.js b/src/nodes/fog/Fog.js index e84383f184f677..644b2646b5304c 100644 --- a/src/nodes/fog/Fog.js +++ b/src/nodes/fog/Fog.js @@ -1,4 +1,4 @@ -import { positionView } from '../accessors/Position.js'; +import { positionView, positionWorld } from '../accessors/Position.js'; import { smoothstep } from '../math/MathNode.js'; import { Fn, output, vec4 } from '../tsl/TSLBase.js'; @@ -62,6 +62,25 @@ export const densityFogFactor = Fn( ( [ density ], builder ) => { } ); +/** + * Constructs a new height fog factor node. This fog factor requires a Y-up coordinate system. + * + * @tsl + * @function + * @param {Node} density - Defines the fog density. + * @param {Node} height - The height threshold in world space. Everything below this y-coordinate is affected by fog. + */ +export const exponentialHeightFogFactor = Fn( ( [ density, height ], builder ) => { + + const viewZ = getViewZNode( builder ); + + const distance = height.sub( positionWorld.y ).max( 0 ).toConst(); + const m = distance.mul( viewZ ).toConst(); + + return density.mul( density, m, m ).negate().exp().oneMinus(); + +} ); + /** * This class can be used to configure a fog for the scene. * Nodes of this type are assigned to `Scene.fogNode`.