Bladeren bron

WebGPURenderer: Reaaply align of `frontFace` and fix `FrontFacingNode` (#31784)

* Reapply https://github.com/mrdoob/three.js/pull/31769

* fix frontFace webgpu backend

* cleanup
sunag 4 maanden geleden
bovenliggende
commit
47aaa5385e

+ 4 - 8
src/nodes/display/FrontFacingNode.js

@@ -1,7 +1,7 @@
 import Node from '../core/Node.js';
 import Node from '../core/Node.js';
 import { nodeImmutable, float, Fn } from '../tsl/TSLBase.js';
 import { nodeImmutable, float, Fn } from '../tsl/TSLBase.js';
 
 
-import { BackSide, DoubleSide, WebGLCoordinateSystem } from '../../constants.js';
+import { BackSide, DoubleSide } from '../../constants.js';
 
 
 /**
 /**
  * This node can be used to evaluate whether a primitive is front or back facing.
  * This node can be used to evaluate whether a primitive is front or back facing.
@@ -40,15 +40,11 @@ class FrontFacingNode extends Node {
 
 
 		//
 		//
 
 
-		const { renderer, material } = builder;
+		const { material } = builder;
 
 
-		if ( renderer.coordinateSystem === WebGLCoordinateSystem ) {
+		if ( material.side === BackSide ) {
 
 
-			if ( material.side === BackSide ) {
-
-				return 'false';
-
-			}
+			return 'false';
 
 
 		}
 		}
 
 

+ 6 - 0
src/renderers/webgpu/WebGPUBackend.js

@@ -1806,6 +1806,11 @@ class WebGPUBackend extends Backend {
 		const utils = this.utils;
 		const utils = this.utils;
 		const renderContext = renderObject.context;
 		const renderContext = renderObject.context;
 
 
+		// meshes with negative scale have a different frontFace render pipeline
+		// descriptor value so the following must be honored in the cache key
+
+		const frontFaceCW = ( object.isMesh && object.matrixWorld.determinant() < 0 );
+
 		return [
 		return [
 			material.transparent, material.blending, material.premultipliedAlpha,
 			material.transparent, material.blending, material.premultipliedAlpha,
 			material.blendSrc, material.blendDst, material.blendEquation,
 			material.blendSrc, material.blendDst, material.blendEquation,
@@ -1816,6 +1821,7 @@ class WebGPUBackend extends Backend {
 			material.stencilFail, material.stencilZFail, material.stencilZPass,
 			material.stencilFail, material.stencilZFail, material.stencilZPass,
 			material.stencilFuncMask, material.stencilWriteMask,
 			material.stencilFuncMask, material.stencilWriteMask,
 			material.side,
 			material.side,
+			frontFaceCW,
 			utils.getSampleCountRenderContext( renderContext ),
 			utils.getSampleCountRenderContext( renderContext ),
 			utils.getCurrentColorSpace( renderContext ), utils.getCurrentColorFormat( renderContext ), utils.getCurrentDepthStencilFormat( renderContext ),
 			utils.getCurrentColorSpace( renderContext ), utils.getCurrentColorFormat( renderContext ), utils.getCurrentDepthStencilFormat( renderContext ),
 			utils.getPrimitiveTopology( object, material ),
 			utils.getPrimitiveTopology( object, material ),

+ 9 - 18
src/renderers/webgpu/utils/WebGPUPipelineUtils.js

@@ -5,7 +5,7 @@ import {
 } from './WebGPUConstants.js';
 } from './WebGPUConstants.js';
 
 
 import {
 import {
-	FrontSide, BackSide, DoubleSide,
+	BackSide, DoubleSide,
 	NeverDepth, AlwaysDepth, LessDepth, LessEqualDepth, EqualDepth, GreaterEqualDepth, GreaterDepth, NotEqualDepth,
 	NeverDepth, AlwaysDepth, LessDepth, LessEqualDepth, EqualDepth, GreaterEqualDepth, GreaterDepth, NotEqualDepth,
 	NoBlending, NormalBlending, AdditiveBlending, SubtractiveBlending, MultiplyBlending, CustomBlending,
 	NoBlending, NormalBlending, AdditiveBlending, SubtractiveBlending, MultiplyBlending, CustomBlending,
 	ZeroFactor, OneFactor, SrcColorFactor, OneMinusSrcColorFactor, SrcAlphaFactor, OneMinusSrcAlphaFactor, DstColorFactor,
 	ZeroFactor, OneFactor, SrcColorFactor, OneMinusSrcColorFactor, SrcAlphaFactor, OneMinusSrcAlphaFactor, DstColorFactor,
@@ -672,6 +672,8 @@ class WebGPUPipelineUtils {
 		const descriptor = {};
 		const descriptor = {};
 		const utils = this.backend.utils;
 		const utils = this.backend.utils;
 
 
+		//
+
 		descriptor.topology = utils.getPrimitiveTopology( object, material );
 		descriptor.topology = utils.getPrimitiveTopology( object, material );
 
 
 		if ( geometry.index !== null && object.isLine === true && object.isLineSegments !== true ) {
 		if ( geometry.index !== null && object.isLine === true && object.isLineSegments !== true ) {
@@ -680,28 +682,17 @@ class WebGPUPipelineUtils {
 
 
 		}
 		}
 
 
-		switch ( material.side ) {
+		//
 
 
-			case FrontSide:
-				descriptor.frontFace = GPUFrontFace.CCW;
-				descriptor.cullMode = GPUCullMode.Back;
-				break;
+		let flipSided = ( material.side === BackSide );
 
 
-			case BackSide:
-				descriptor.frontFace = GPUFrontFace.CCW;
-				descriptor.cullMode = GPUCullMode.Front;
-				break;
+		if ( object.isMesh && object.matrixWorld.determinant() < 0 ) flipSided = ! flipSided;
 
 
-			case DoubleSide:
-				descriptor.frontFace = GPUFrontFace.CCW;
-				descriptor.cullMode = GPUCullMode.None;
-				break;
+		descriptor.frontFace = ( flipSided === true ) ? GPUFrontFace.CW : GPUFrontFace.CCW;
 
 
-			default:
-				console.error( 'THREE.WebGPUPipelineUtils: Unknown material.side value.', material.side );
-				break;
+		//
 
 
-		}
+		descriptor.cullMode = ( material.side === DoubleSide ) ? GPUCullMode.None : GPUCullMode.Back;
 
 
 		return descriptor;
 		return descriptor;
 
 

粤ICP备19079148号