Jelajahi Sumber

USDComposer: Fixed variant composition.

Mr.doob 1 bulan lalu
induk
melakukan
9a711cc1f4

+ 28 - 0
examples/jsm/loaders/usd/USDAParser.js

@@ -217,6 +217,34 @@ class USDAParser {
 
 			}
 
+			if ( key === 'variants' ) {
+
+				const variantSelection = {};
+				const variants = data[ key ];
+
+				for ( const vKey in variants ) {
+
+					const match = vKey.match( /^string\s+(\w+)$/ );
+					if ( match ) {
+
+						const variantSetName = match[ 1 ];
+						const variantValue = variants[ vKey ].replace( /"/g, '' );
+						variantSelection[ variantSetName ] = variantValue;
+
+					}
+
+				}
+
+				if ( Object.keys( variantSelection ).length > 0 ) {
+
+					primFields.variantSelection = variantSelection;
+
+				}
+
+				continue;
+
+			}
+
 			if ( key.startsWith( 'rel ' ) ) {
 
 				const relName = key.slice( 4 );

+ 28 - 5
examples/jsm/loaders/usd/USDComposer.js

@@ -766,6 +766,34 @@ class USDComposer {
 	_getAttributes( path ) {
 
 		const attrs = {};
+
+		this._collectAttributesFromPath( path, attrs );
+
+		// Collect overrides from sibling variants
+		const variantMatch = path.match( /^(.+?)\/\{(\w+)=(\w+)\}\/(.+)$/ );
+		if ( variantMatch ) {
+
+			const basePath = variantMatch[ 1 ];
+			const relativePath = variantMatch[ 4 ];
+			const variantPaths = this._getVariantPaths( basePath );
+
+			for ( const vp of variantPaths ) {
+
+				if ( path.startsWith( vp ) ) continue;
+
+				const overridePath = vp + '/' + relativePath;
+				this._collectAttributesFromPath( overridePath, attrs );
+
+			}
+
+		}
+
+		return attrs;
+
+	}
+
+	_collectAttributesFromPath( path, attrs ) {
+
 		const prefix = path + '.';
 
 		for ( const attrPath in this.specsByPath ) {
@@ -781,7 +809,6 @@ class USDComposer {
 
 			}
 
-			// Include elementSize for skinning attributes
 			if ( attrSpec.fields?.elementSize !== undefined ) {
 
 				attrs[ attrName + ':elementSize' ] = attrSpec.fields.elementSize;
@@ -796,8 +823,6 @@ class USDComposer {
 
 		}
 
-		return attrs;
-
 	}
 
 	/**
@@ -830,7 +855,6 @@ class USDComposer {
 
 		}
 
-		// Apply displayColor if no texture/color was set
 		const displayColor = attrs[ 'primvars:displayColor' ];
 		if ( displayColor && displayColor.length >= 3 ) {
 
@@ -856,7 +880,6 @@ class USDComposer {
 
 		}
 
-		// Apply displayOpacity for transparency
 		const displayOpacity = attrs[ 'primvars:displayOpacity' ];
 		if ( displayOpacity && displayOpacity.length >= 1 ) {
 

粤ICP备19079148号