Skip to content

Commit

Permalink
WebGPURenderer & Nodes: FogExp2 and revision (#25073)
Browse files Browse the repository at this point in the history
* WebGPU & Nodes: FogExp2 and revision

* follow background error style
  • Loading branch information
sunag authored Dec 5, 2022
1 parent cdf45b8 commit b6b5bb4
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 5 deletions.
3 changes: 3 additions & 0 deletions examples/jsm/nodes/Nodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ import CheckerNode from './procedural/CheckerNode.js';
// fog
import FogNode from './fog/FogNode.js';
import FogRangeNode from './fog/FogRangeNode.js';
import FogExp2Node from './fog/FogExp2Node.js';

// core
export * from './core/constants.js';
Expand Down Expand Up @@ -234,6 +235,7 @@ const nodeLib = {
// fog
FogNode,
FogRangeNode,
FogExp2Node,

// loaders
NodeLoader,
Expand Down Expand Up @@ -358,6 +360,7 @@ export {
// fog
FogNode,
FogRangeNode,
FogExp2Node,

// loaders
NodeLoader,
Expand Down
27 changes: 27 additions & 0 deletions examples/jsm/nodes/fog/FogExp2Node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import FogNode from './FogNode.js';
import { sub, exp, mul, negate, positionView } from '../shadernode/ShaderNodeBaseElements.js';

class FogExp2Node extends FogNode {

constructor( colorNode, densityNode ) {

super( colorNode );

this.isFogExp2Node = true;

this.densityNode = densityNode;

}

construct() {

const depthNode = negate( positionView.z );
const densityNode = this.densityNode;

this.factorNode = sub( 1.0, exp( mul( negate( densityNode ), densityNode, depthNode, depthNode ) ) );

}

}

export default FogExp2Node;
4 changes: 1 addition & 3 deletions examples/jsm/nodes/fog/FogRangeNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@ class FogRangeNode extends FogNode {

}

generate( builder ) {
construct() {

this.factorNode = smoothstep( this.nearNode, this.farNode, negate( positionView.z ) );

return super.generate( builder );

}

}
Expand Down
26 changes: 24 additions & 2 deletions examples/jsm/nodes/materials/NodeMaterial.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
float, vec3, vec4,
assign, label, mul, bypass, attribute,
positionLocal, skinning, instance, modelViewProjection, lightingContext, colorSpace,
materialAlphaTest, materialColor, materialOpacity
materialAlphaTest, materialColor, materialOpacity, reference, rangeFog, exp2Fog
} from '../shadernode/ShaderNodeElements.js';

class NodeMaterial extends ShaderMaterial {
Expand Down Expand Up @@ -140,7 +140,29 @@ class NodeMaterial extends ShaderMaterial {

// FOG

if ( builder.fogNode ) outputNode = vec4( vec3( builder.fogNode.mix( outputNode ) ), outputNode.w );
let fogNode = builder.fogNode;

if ( fogNode?.isNode !== true && builder.scene.fog ) {

const fog = builder.scene.fog;

if ( fog.isFogExp2 ) {

fogNode = exp2Fog( reference( 'color', 'color', fog ), reference( 'density', 'float', fog ) );

} else if ( fog.isFog ) {

fogNode = rangeFog( reference( 'color', 'color', fog ), reference( 'near', 'float', fog ), reference( 'far', 'float', fog ) );

} else {

console.error( 'NodeMaterial: Unsupported fog configuration.', fog );

}

}

if ( fogNode ) outputNode = vec4( vec3( fogNode.mix( outputNode ) ), outputNode.w );

// RESULT

Expand Down
2 changes: 2 additions & 0 deletions examples/jsm/nodes/shadernode/ShaderNodeElements.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import CheckerNode from '../procedural/CheckerNode.js';
// fog
import FogNode from '../fog/FogNode.js';
import FogRangeNode from '../fog/FogRangeNode.js';
import FogExp2Node from '../fog/FogExp2Node.js';

// shader node utils
import { nodeObject, nodeProxy, nodeImmutable } from './ShaderNode.js';
Expand Down Expand Up @@ -147,3 +148,4 @@ export const checker = nodeProxy( CheckerNode );

export const fog = nodeProxy( FogNode );
export const rangeFog = nodeProxy( FogRangeNode );
export const exp2Fog = nodeProxy( FogExp2Node );

0 comments on commit b6b5bb4

Please sign in to comment.