Browse Source

USDZLoader: Added normals handling.

Mr.doob 2 years ago
parent
commit
f82bbc9c0d
1 changed files with 21 additions and 8 deletions
  1. 21 8
      examples/jsm/loaders/USDZLoader.js

+ 21 - 8
examples/jsm/loaders/USDZLoader.js

@@ -291,22 +291,35 @@ class USDZLoader extends Loader {
 
 
 		function buildGeometry( data ) {
 		function buildGeometry( data ) {
 
 
-			const geometry = new BufferGeometry();
-			
-			const positions = JSON.parse( data[ 'point3f[] points' ].replace( /[()]*/g, '' ) );
-			const attribute = new BufferAttribute( new Float32Array( positions ), 3 );
+			let geometry = new BufferGeometry();
 
 
 			if ( 'int[] faceVertexIndices' in data ) {
 			if ( 'int[] faceVertexIndices' in data ) {
 
 
 				const indices = JSON.parse( data[ 'int[] faceVertexIndices' ] );
 				const indices = JSON.parse( data[ 'int[] faceVertexIndices' ] );
-				geometry.setAttribute( 'position', toFlatBufferAttribute( attribute, indices ) );
+				geometry.setIndex( new BufferAttribute( new Uint16Array( indices ), 1 ) );
 
 
-			} else {
+			}
+
+			if ( 'point3f[] points' in data ) {
 
 
+				const positions = JSON.parse( data[ 'point3f[] points' ].replace( /[()]*/g, '' ) );
+				const attribute = new BufferAttribute( new Float32Array( positions ), 3 );
 				geometry.setAttribute( 'position', attribute );
 				geometry.setAttribute( 'position', attribute );
 
 
 			}
 			}
 
 
+			if ( 'normal3f[] normals' in data ) {
+
+				const normals = JSON.parse( data[ 'normal3f[] normals' ].replace( /[()]*/g, '' ) );
+				const attribute = new BufferAttribute( new Float32Array( normals ), 3 );
+				geometry.setAttribute( 'normal', attribute );
+
+			} else {
+
+				geometry.computeVertexNormals();
+
+			}
+
 			if ( 'texCoord2f[] primvars:st' in data ) {
 			if ( 'texCoord2f[] primvars:st' in data ) {
 
 
 				const uvs = JSON.parse( data[ 'texCoord2f[] primvars:st' ].replace( /[()]*/g, '' ) );
 				const uvs = JSON.parse( data[ 'texCoord2f[] primvars:st' ].replace( /[()]*/g, '' ) );
@@ -314,6 +327,8 @@ class USDZLoader extends Loader {
 
 
 				if ( 'int[] primvars:st:indices' in data ) {
 				if ( 'int[] primvars:st:indices' in data ) {
 
 
+					geometry = geometry.toNonIndexed();
+
 					const indices = JSON.parse( data[ 'int[] primvars:st:indices' ] );
 					const indices = JSON.parse( data[ 'int[] primvars:st:indices' ] );
 					geometry.setAttribute( 'uv', toFlatBufferAttribute( attribute, indices ) );
 					geometry.setAttribute( 'uv', toFlatBufferAttribute( attribute, indices ) );
 
 
@@ -324,8 +339,6 @@ class USDZLoader extends Loader {
 				}
 				}
 
 
 			}
 			}
-
-			geometry.computeVertexNormals();
 			
 			
 			return geometry;
 			return geometry;