|
| 1 | +import { positionView } from '../accessors/Position.js'; |
| 2 | +import { smoothstep } from '../math/MathNode.js'; |
| 3 | +import { Fn, vec4 } from '../tsl/TSLBase.js'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Returns a node that represents the `z` coordinate in view space |
| 7 | + * for the current fragment. It's a different representation of the |
| 8 | + * default depth value. |
| 9 | + * |
| 10 | + * This value can be part of a computation that defines how the fog |
| 11 | + * density increases when moving away from the camera. |
| 12 | + * |
| 13 | + * @param {NodeBuilder} builder - The current node builder. |
| 14 | + * @return {Node} The viewZ node. |
| 15 | + */ |
| 16 | +function getViewZNode( builder ) { |
| 17 | + |
| 18 | + let viewZ; |
| 19 | + |
| 20 | + const getViewZ = builder.context.getViewZ; |
| 21 | + |
| 22 | + if ( getViewZ !== undefined ) { |
| 23 | + |
| 24 | + viewZ = getViewZ( this ); |
| 25 | + |
| 26 | + } |
| 27 | + |
| 28 | + return ( viewZ || positionView.z ).negate(); |
| 29 | + |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * Constructs a new range factor node. |
| 34 | + * |
| 35 | + * @param {Node} near - Defines the near value. |
| 36 | + * @param {Node} far - Defines the far value. |
| 37 | + */ |
| 38 | +export const rangeFogFactor = Fn( ( [ near, far ], builder ) => { |
| 39 | + |
| 40 | + const viewZ = getViewZNode( builder ); |
| 41 | + |
| 42 | + return smoothstep( near, far, viewZ ); |
| 43 | + |
| 44 | +} ); |
| 45 | + |
| 46 | +/** |
| 47 | + * Represents an exponential squared fog. This type of fog gives |
| 48 | + * a clear view near the camera and a faster than exponentially |
| 49 | + * densening fog farther from the camera. |
| 50 | + * |
| 51 | + * @param {Node} density - Defines the fog density. |
| 52 | + */ |
| 53 | +export const densityFogFactor = Fn( ( [ density ], builder ) => { |
| 54 | + |
| 55 | + const viewZ = getViewZNode( builder ); |
| 56 | + |
| 57 | + return density.mul( density, viewZ, viewZ ).negate().exp().oneMinus(); |
| 58 | + |
| 59 | +} ); |
| 60 | + |
| 61 | +/** |
| 62 | + * This class can be used to configure a fog for the scene. |
| 63 | + * Nodes of this type are assigned to `Scene.fogNode`. |
| 64 | + * |
| 65 | + * @param {Node} color - Defines the color of the fog. |
| 66 | + * @param {Node} factor - Defines how the fog is factored in the scene. |
| 67 | + */ |
| 68 | +export const fog = Fn( ( [ color, factor ] ) => { |
| 69 | + |
| 70 | + return vec4( color.toVec3(), factor.toFloat() ); |
| 71 | + |
| 72 | +} ); |
| 73 | + |
| 74 | +// Deprecated |
| 75 | + |
| 76 | +export function rangeFog( color, near, far ) { // @deprecated, r171 |
| 77 | + |
| 78 | + console.warn( 'THREE.TSL: "rangeFog( color, near, far )" is deprecated. Use "fog( color, rangeFog( near, far ) )" instead.' ); |
| 79 | + return fog( color, rangeFogFactor( near, far ) ); |
| 80 | + |
| 81 | +} |
| 82 | + |
| 83 | +export function densityFog( color, density ) { // @deprecated, r171 |
| 84 | + |
| 85 | + console.warn( 'THREE.TSL: "densityFog( color, density )" is deprecated. Use "fog( color, densityFogFactor( density ) )" instead.' ); |
| 86 | + return fog( color, densityFogFactor( density ) ); |
| 87 | + |
| 88 | +} |
0 commit comments