Browse Source

PLYLoader allow custom attributes (#25001)

Alan Kalbfleisch 2 năm trước cách đây
mục cha
commit
f67d87194a
1 tập tin đã thay đổi với 68 bổ sung16 xóa
  1. 68 16
      examples/jsm/loaders/PLYLoader.js

+ 68 - 16
examples/jsm/loaders/PLYLoader.js

@@ -31,6 +31,16 @@ import {
  *	diffuse_blue: 'blue'
  * } );
  *
+ * Custom properties outside of the defaults for position, uv, normal
+ * and color attributes can be added using the setCustomPropertyMapping method.
+ * For example, the following maps the element properties “custom_property_a”
+ * and “custom_property_b” to an attribute “customAttribute” with an item size of 2.
+ * Attribute item sizes are set from the number of element properties in the property array.
+ *
+ * loader.setCustomPropertyMapping( {
+ *	customAttribute: ['custom_property_a', 'custom_property_b'],
+ * } );
+ *
  */
 
 const _color = new Color();
@@ -42,6 +52,7 @@ class PLYLoader extends Loader {
 		super( manager );
 
 		this.propertyNameMapping = {};
+		this.customPropertyMapping = {};
 
 	}
 
@@ -86,6 +97,12 @@ class PLYLoader extends Loader {
 
 	}
 
+	setCustomPropertyNameMapping( mapping ) {
+
+		this.customPropertyMapping = mapping;
+
+	}
+
 	parse( data ) {
 
 		function parseHeader( data ) {
@@ -260,18 +277,32 @@ class PLYLoader extends Loader {
 
 		}
 
+		function createBuffer() {
+
+			const buffer = {
+			  indices: [],
+			  vertices: [],
+			  normals: [],
+			  uvs: [],
+			  faceVertexUvs: [],
+			  colors: [],
+			};
+
+			for ( const customProperty of Object.keys( scope.customPropertyMapping ) ) {
+
+			  buffer[ customProperty ] = [];
+
+			}
+
+			return buffer;
+
+		}
+
 		function parseASCII( data, header ) {
 
 			// PLY ascii format specification, as per http://en.wikipedia.org/wiki/PLY_(file_format)
 
-			const buffer = {
-				indices: [],
-				vertices: [],
-				normals: [],
-				uvs: [],
-				faceVertexUvs: [],
-				colors: []
-			};
+			const buffer = createBuffer();
 
 			let result;
 
@@ -357,6 +388,24 @@ class PLYLoader extends Loader {
 
 			}
 
+			// custom buffer data
+
+			for ( const customProperty of Object.keys( scope.customPropertyMapping ) ) {
+
+				if ( buffer[ customProperty ].length > 0 ) {
+
+				  	geometry.setAttribute(
+						customProperty,
+						new Float32BufferAttribute(
+					  		buffer[ customProperty ],
+					  		scope.customPropertyMapping[ customProperty ].length
+						)
+				  	);
+
+				}
+
+			}
+
 			geometry.computeBoundingSphere();
 
 			return geometry;
@@ -419,6 +468,16 @@ class PLYLoader extends Loader {
 
 				}
 
+				for ( const customProperty of Object.keys( scope.customPropertyMapping ) ) {
+
+					for ( const elementProperty of scope.customPropertyMapping[ customProperty ] ) {
+
+					  buffer[ customProperty ].push( element[ elementProperty ] );
+
+					}
+
+				}
+
 			} else if ( elementName === 'face' ) {
 
 				const vertex_indices = element.vertex_indices || element.vertex_index; // issue #9338
@@ -506,14 +565,7 @@ class PLYLoader extends Loader {
 
 		function parseBinary( data, header ) {
 
-			const buffer = {
-				indices: [],
-				vertices: [],
-				normals: [],
-				uvs: [],
-				faceVertexUvs: [],
-				colors: []
-			};
+			const buffer = createBuffer();
 
 			const little_endian = ( header.format === 'binary_little_endian' );
 			const body = new DataView( data, header.headerLength );