Browse Source

USDZLoader: Fix geometry and material parsing. (#28647)

* USDZLoader: Fix index handling in `buildGeometry()`.

* USDZLoader: Fix shader selection in `buildMaterial()`.
Michael Herzog 1 year ago
parent
commit
1485992081
1 changed files with 29 additions and 35 deletions
  1. 29 35
      examples/jsm/loaders/USDZLoader.js

+ 29 - 35
examples/jsm/loaders/USDZLoader.js

@@ -338,15 +338,27 @@ class USDZLoader extends Loader {
 
 			if ( ! data ) return undefined;
 
-			let geometry = new BufferGeometry();
+			const geometry = new BufferGeometry();
+			let indices = null;
 			let uvs = null;
 
+			// index
+
+			if ( 'int[] faceVertexIndices' in data ) {
+
+				indices = JSON.parse( data[ 'int[] faceVertexIndices' ] );
+
+			}
+
 			// position
 
 			if ( 'point3f[] points' in data ) {
 
 				const positions = JSON.parse( data[ 'point3f[] points' ].replace( /[()]*/g, '' ) );
-				const attribute = new BufferAttribute( new Float32Array( positions ), 3 );
+				let attribute = new BufferAttribute( new Float32Array( positions ), 3 );
+
+				if ( indices !== null ) attribute = toFlatBufferAttribute( attribute, indices );
+
 				geometry.setAttribute( 'position', attribute );
 
 			}
@@ -362,19 +374,11 @@ class USDZLoader extends Loader {
 			if ( 'texCoord2f[] primvars:st' in data ) {
 
 				uvs = JSON.parse( data[ 'texCoord2f[] primvars:st' ].replace( /[()]*/g, '' ) );
-				const attribute = new BufferAttribute( new Float32Array( uvs ), 2 );
-				geometry.setAttribute( 'uv', attribute );
-
-			}
-
-			// index
-
-			if ( 'int[] faceVertexIndices' in data ) {
+				let attribute = new BufferAttribute( new Float32Array( uvs ), 2 );
 
-				const indices = JSON.parse( data[ 'int[] faceVertexIndices' ] );
-				geometry.setIndex( indices );
+				if ( indices !== null ) attribute = toFlatBufferAttribute( attribute, indices );
 
-				geometry = geometry.toNonIndexed();
+				geometry.setAttribute( 'uv', attribute );
 
 			}
 
@@ -383,7 +387,6 @@ class USDZLoader extends Loader {
 				// custom uv index, overwrite uvs with new data
 
 				const attribute = new BufferAttribute( new Float32Array( uvs ), 2 );
-
 				const indices = JSON.parse( data[ 'int[] primvars:st:indices' ] );
 				geometry.setAttribute( 'uv', toFlatBufferAttribute( attribute, indices ) );
 
@@ -394,7 +397,14 @@ class USDZLoader extends Loader {
 			if ( 'normal3f[] normals' in data ) {
 
 				const normals = JSON.parse( data[ 'normal3f[] normals' ].replace( /[()]*/g, '' ) );
-				const attribute = new BufferAttribute( new Float32Array( normals ), 3 );
+				let attribute = new BufferAttribute( new Float32Array( normals ), 3 );
+
+				if ( attribute.count !== geometry.attributes.position.count && indices !== null ) {
+
+					attribute = toFlatBufferAttribute( attribute, indices );
+
+				}
+
 				geometry.setAttribute( 'normal', attribute );
 
 			} else {
@@ -504,9 +514,11 @@ class USDZLoader extends Loader {
 
 			if ( data !== undefined ) {
 
-				if ( 'def Shader "PreviewSurface"' in data ) {
+				const surfaceConnection = data[ 'token outputs:surface.connect' ];
+				const surfaceName = /(\w+).output/.exec( surfaceConnection )[ 1 ];
+				const surface = data[ `def Shader "${surfaceName}"` ];
 
-					const surface = data[ 'def Shader "PreviewSurface"' ];
+				if ( surface !== undefined ) {
 
 					if ( 'color3f inputs:diffuseColor.connect' in surface ) {
 
@@ -675,24 +687,6 @@ class USDZLoader extends Loader {
 
 				}
 
-				if ( 'def Shader "diffuseColor_texture"' in data ) {
-
-					const sampler = data[ 'def Shader "diffuseColor_texture"' ];
-
-					material.map = buildTexture( sampler );
-					material.map.colorSpace = SRGBColorSpace;
-
-				}
-
-				if ( 'def Shader "normal_texture"' in data ) {
-
-					const sampler = data[ 'def Shader "normal_texture"' ];
-
-					material.normalMap = buildTexture( sampler );
-					material.normalMap.colorSpace = NoColorSpace;
-
-				}
-
 			}
 
 			return material;