Kaynağa Gözat

WebGPURenderer: ShadowMaterial (#28200)

sunag 1 yıl önce
ebeveyn
işleme
50d4f74070

+ 31 - 0
examples/jsm/nodes/functions/ShadowMaskModel.js

@@ -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;

+ 5 - 1
examples/jsm/nodes/lighting/AnalyticLightNode.js

@@ -26,6 +26,7 @@ class AnalyticLightNode extends LightingNode {
 
 		this.rtt = null;
 		this.shadowNode = null;
+		this.shadowMaskNode = null;
 
 		this.color = new Color();
 		this._defaultColorNode = uniform( this.color );
@@ -155,11 +156,13 @@ class AnalyticLightNode extends LightingNode {
 			//
 
 			const shadowColor = texture( rtt.texture, shadowCoord );
+			const shadowMaskNode = frustumTest.mix( 1, shadowNode.mix( shadowColor.a.mix( 1, shadowColor ), 1 ) );
 
 			this.rtt = rtt;
-			this.colorNode = this.colorNode.mul( frustumTest.mix( 1, shadowNode.mix( shadowColor.a.mix( 1, shadowColor ), 1 ) ) );
+			this.colorNode = this.colorNode.mul( shadowMaskNode );
 
 			this.shadowNode = shadowNode;
+			this.shadowMaskNode = shadowMaskNode;
 
 			//
 
@@ -222,6 +225,7 @@ class AnalyticLightNode extends LightingNode {
 		this.rtt.dispose();
 
 		this.shadowNode = null;
+		this.shadowMaskNode = null;
 		this.rtt = null;
 
 		this.colorNode = this._defaultColorNode;

+ 2 - 1
examples/jsm/nodes/lighting/DirectionalLightNode.js

@@ -26,7 +26,8 @@ class DirectionalLightNode extends AnalyticLightNode {
 		lightingModel.direct( {
 			lightDirection,
 			lightColor,
-			reflectedLight
+			reflectedLight,
+			shadowMask: this.shadowMaskNode
 		}, builder.stack, builder );
 
 	}

+ 2 - 1
examples/jsm/nodes/lighting/PointLightNode.js

@@ -54,7 +54,8 @@ class PointLightNode extends AnalyticLightNode {
 		lightingModel.direct( {
 			lightDirection,
 			lightColor,
-			reflectedLight
+			reflectedLight,
+			shadowMask: this.shadowMaskNode
 		}, builder.stack, builder );
 
 	}

+ 2 - 1
examples/jsm/nodes/lighting/SpotLightNode.js

@@ -75,7 +75,8 @@ class SpotLightNode extends AnalyticLightNode {
 		lightingModel.direct( {
 			lightDirection,
 			lightColor,
-			reflectedLight
+			reflectedLight,
+			shadowMask: this.shadowMaskNode
 		}, builder.stack, builder );
 
 	}

+ 1 - 0
examples/jsm/nodes/materials/Materials.js

@@ -14,3 +14,4 @@ export { default as MeshPhysicalNodeMaterial } from './MeshPhysicalNodeMaterial.
 export { default as MeshSSSNodeMaterial } from './MeshSSSNodeMaterial.js';
 export { default as PointsNodeMaterial } from './PointsNodeMaterial.js';
 export { default as SpriteNodeMaterial } from './SpriteNodeMaterial.js';
+export { default as ShadowNodeMaterial } from './ShadowNodeMaterial.js';

+ 34 - 0
examples/jsm/nodes/materials/ShadowNodeMaterial.js

@@ -0,0 +1,34 @@
+import NodeMaterial, { addNodeMaterial } from './NodeMaterial.js';
+import ShadowMaskModel from '../functions/ShadowMaskModel.js';
+
+import { ShadowMaterial } from 'three';
+
+const defaultValues = new ShadowMaterial();
+
+class ShadowNodeMaterial extends NodeMaterial {
+
+	constructor( parameters ) {
+
+		super();
+
+		this.isShadowNodeMaterial = true;
+
+		this.lights = true;
+
+		this.setDefaultValues( defaultValues );
+
+		this.setValues( parameters );
+
+	}
+
+	setupLightingModel( /*builder*/ ) {
+
+		return new ShadowMaskModel();
+
+	}
+
+}
+
+export default ShadowNodeMaterial;
+
+addNodeMaterial( 'ShadowNodeMaterial', ShadowNodeMaterial );

+ 0 - 1
examples/jsm/renderers/webgpu/nodes/WGSLNodeBuilder.js

@@ -916,7 +916,6 @@ ${ flowData.code }
 
 			this.vertexShader = this._getWGSLVertexCode( shadersData.vertex );
 			this.fragmentShader = this._getWGSLFragmentCode( shadersData.fragment );
-			//console.log( this.fragmentShader );
 
 		} else {