瀏覽代碼

PLYExporter: Add little endian support.

Mugen87 5 年之前
父節點
當前提交
c8484d0ee4

+ 23 - 22
examples/js/exporters/PLYExporter.js

@@ -6,7 +6,7 @@
  *  var exporter = new THREE.PLYExporter();
  *
  *  // second argument is a list of options
- *  exporter.parse(mesh, data => console.log(data), { binary: true, excludeAttributes: [ 'color' ] });
+ *  exporter.parse(mesh, data => console.log(data), { binary: true, excludeAttributes: [ 'color' ], littleEndian: true });
  *
  * Format Definition:
  * http://paulbourke.net/dataformats/ply/
@@ -63,7 +63,8 @@ THREE.PLYExporter.prototype = {
 		// Default options
 		var defaultOptions = {
 			binary: false,
-			excludeAttributes: [] // normal, uv, color, index
+			excludeAttributes: [], // normal, uv, color, index
+			littleEndian: false
 		};
 
 		options = Object.assign( defaultOptions, options );
@@ -148,7 +149,7 @@ THREE.PLYExporter.prototype = {
 
 		var header =
 			'ply\n' +
-			`format ${ options.binary ? 'binary_big_endian' : 'ascii' } 1.0\n` +
+			`format ${ options.binary ? ( options.littleEndian ? 'binary_little_endian' : 'binary_big_endian' ) : 'ascii' } 1.0\n` +
 			`element vertex ${vertexCount}\n` +
 
 			// position
@@ -243,13 +244,13 @@ THREE.PLYExporter.prototype = {
 
 
 					// Position information
-					output.setFloat32( vOffset, vertex.x );
+					output.setFloat32( vOffset, vertex.x, options.littleEndian );
 					vOffset += 4;
 
-					output.setFloat32( vOffset, vertex.y );
+					output.setFloat32( vOffset, vertex.y, options.littleEndian );
 					vOffset += 4;
 
-					output.setFloat32( vOffset, vertex.z );
+					output.setFloat32( vOffset, vertex.z, options.littleEndian );
 					vOffset += 4;
 
 					// Normal information
@@ -263,24 +264,24 @@ THREE.PLYExporter.prototype = {
 
 							vertex.applyMatrix3( normalMatrixWorld ).normalize();
 
-							output.setFloat32( vOffset, vertex.x );
+							output.setFloat32( vOffset, vertex.x, options.littleEndian );
 							vOffset += 4;
 
-							output.setFloat32( vOffset, vertex.y );
+							output.setFloat32( vOffset, vertex.y, options.littleEndian );
 							vOffset += 4;
 
-							output.setFloat32( vOffset, vertex.z );
+							output.setFloat32( vOffset, vertex.z, options.littleEndian );
 							vOffset += 4;
 
 						} else {
 
-							output.setFloat32( vOffset, 0 );
+							output.setFloat32( vOffset, 0, options.littleEndian );
 							vOffset += 4;
 
-							output.setFloat32( vOffset, 0 );
+							output.setFloat32( vOffset, 0, options.littleEndian );
 							vOffset += 4;
 
-							output.setFloat32( vOffset, 0 );
+							output.setFloat32( vOffset, 0, options.littleEndian );
 							vOffset += 4;
 
 						}
@@ -292,18 +293,18 @@ THREE.PLYExporter.prototype = {
 
 						if ( uvs != null ) {
 
-							output.setFloat32( vOffset, uvs.getX( i ) );
+							output.setFloat32( vOffset, uvs.getX( i ), options.littleEndian );
 							vOffset += 4;
 
-							output.setFloat32( vOffset, uvs.getY( i ) );
+							output.setFloat32( vOffset, uvs.getY( i ), options.littleEndian );
 							vOffset += 4;
 
 						} else if ( includeUVs !== false ) {
 
-							output.setFloat32( vOffset, 0 );
+							output.setFloat32( vOffset, 0, options.littleEndian );
 							vOffset += 4;
 
-							output.setFloat32( vOffset, 0 );
+							output.setFloat32( vOffset, 0, options.littleEndian );
 							vOffset += 4;
 
 						}
@@ -352,13 +353,13 @@ THREE.PLYExporter.prototype = {
 							output.setUint8( fOffset, 3 );
 							fOffset += 1;
 
-							output.setUint32( fOffset, indices.getX( i + 0 ) + writtenVertices );
+							output.setUint32( fOffset, indices.getX( i + 0 ) + writtenVertices, options.littleEndian );
 							fOffset += indexByteCount;
 
-							output.setUint32( fOffset, indices.getX( i + 1 ) + writtenVertices );
+							output.setUint32( fOffset, indices.getX( i + 1 ) + writtenVertices, options.littleEndian );
 							fOffset += indexByteCount;
 
-							output.setUint32( fOffset, indices.getX( i + 2 ) + writtenVertices );
+							output.setUint32( fOffset, indices.getX( i + 2 ) + writtenVertices, options.littleEndian );
 							fOffset += indexByteCount;
 
 						}
@@ -370,13 +371,13 @@ THREE.PLYExporter.prototype = {
 							output.setUint8( fOffset, 3 );
 							fOffset += 1;
 
-							output.setUint32( fOffset, writtenVertices + i );
+							output.setUint32( fOffset, writtenVertices + i, options.littleEndian );
 							fOffset += indexByteCount;
 
-							output.setUint32( fOffset, writtenVertices + i + 1 );
+							output.setUint32( fOffset, writtenVertices + i + 1, options.littleEndian );
 							fOffset += indexByteCount;
 
-							output.setUint32( fOffset, writtenVertices + i + 2 );
+							output.setUint32( fOffset, writtenVertices + i + 2, options.littleEndian );
 							fOffset += indexByteCount;
 
 						}

+ 1 - 0
examples/jsm/exporters/PLYExporter.d.ts

@@ -3,6 +3,7 @@ import { Object3D } from '../../../src/Three';
 export interface PLYExporterOptions {
 	binary?: boolean;
 	excludeAttributes?: string[];
+	littleEndian?: boolean;
 }
 
 export class PLYExporter {

+ 23 - 22
examples/jsm/exporters/PLYExporter.js

@@ -6,7 +6,7 @@
  *  var exporter = new PLYExporter();
  *
  *  // second argument is a list of options
- *  exporter.parse(mesh, data => console.log(data), { binary: true, excludeAttributes: [ 'color' ] });
+ *  exporter.parse(mesh, data => console.log(data), { binary: true, excludeAttributes: [ 'color' ], littleEndian: true });
  *
  * Format Definition:
  * http://paulbourke.net/dataformats/ply/
@@ -69,7 +69,8 @@ PLYExporter.prototype = {
 		// Default options
 		var defaultOptions = {
 			binary: false,
-			excludeAttributes: [] // normal, uv, color, index
+			excludeAttributes: [], // normal, uv, color, index
+			littleEndian: false
 		};
 
 		options = Object.assign( defaultOptions, options );
@@ -154,7 +155,7 @@ PLYExporter.prototype = {
 
 		var header =
 			'ply\n' +
-			`format ${ options.binary ? 'binary_big_endian' : 'ascii' } 1.0\n` +
+			`format ${ options.binary ? ( options.littleEndian ? 'binary_little_endian' : 'binary_big_endian' ) : 'ascii' } 1.0\n` +
 			`element vertex ${vertexCount}\n` +
 
 			// position
@@ -249,13 +250,13 @@ PLYExporter.prototype = {
 
 
 					// Position information
-					output.setFloat32( vOffset, vertex.x );
+					output.setFloat32( vOffset, vertex.x, options.littleEndian );
 					vOffset += 4;
 
-					output.setFloat32( vOffset, vertex.y );
+					output.setFloat32( vOffset, vertex.y, options.littleEndian );
 					vOffset += 4;
 
-					output.setFloat32( vOffset, vertex.z );
+					output.setFloat32( vOffset, vertex.z, options.littleEndian );
 					vOffset += 4;
 
 					// Normal information
@@ -269,24 +270,24 @@ PLYExporter.prototype = {
 
 							vertex.applyMatrix3( normalMatrixWorld ).normalize();
 
-							output.setFloat32( vOffset, vertex.x );
+							output.setFloat32( vOffset, vertex.x, options.littleEndian );
 							vOffset += 4;
 
-							output.setFloat32( vOffset, vertex.y );
+							output.setFloat32( vOffset, vertex.y, options.littleEndian );
 							vOffset += 4;
 
-							output.setFloat32( vOffset, vertex.z );
+							output.setFloat32( vOffset, vertex.z, options.littleEndian );
 							vOffset += 4;
 
 						} else {
 
-							output.setFloat32( vOffset, 0 );
+							output.setFloat32( vOffset, 0, options.littleEndian );
 							vOffset += 4;
 
-							output.setFloat32( vOffset, 0 );
+							output.setFloat32( vOffset, 0, options.littleEndian );
 							vOffset += 4;
 
-							output.setFloat32( vOffset, 0 );
+							output.setFloat32( vOffset, 0, options.littleEndian );
 							vOffset += 4;
 
 						}
@@ -298,18 +299,18 @@ PLYExporter.prototype = {
 
 						if ( uvs != null ) {
 
-							output.setFloat32( vOffset, uvs.getX( i ) );
+							output.setFloat32( vOffset, uvs.getX( i ), options.littleEndian );
 							vOffset += 4;
 
-							output.setFloat32( vOffset, uvs.getY( i ) );
+							output.setFloat32( vOffset, uvs.getY( i ), options.littleEndian );
 							vOffset += 4;
 
 						} else if ( includeUVs !== false ) {
 
-							output.setFloat32( vOffset, 0 );
+							output.setFloat32( vOffset, 0, options.littleEndian );
 							vOffset += 4;
 
-							output.setFloat32( vOffset, 0 );
+							output.setFloat32( vOffset, 0, options.littleEndian );
 							vOffset += 4;
 
 						}
@@ -358,13 +359,13 @@ PLYExporter.prototype = {
 							output.setUint8( fOffset, 3 );
 							fOffset += 1;
 
-							output.setUint32( fOffset, indices.getX( i + 0 ) + writtenVertices );
+							output.setUint32( fOffset, indices.getX( i + 0 ) + writtenVertices, options.littleEndian );
 							fOffset += indexByteCount;
 
-							output.setUint32( fOffset, indices.getX( i + 1 ) + writtenVertices );
+							output.setUint32( fOffset, indices.getX( i + 1 ) + writtenVertices, options.littleEndian );
 							fOffset += indexByteCount;
 
-							output.setUint32( fOffset, indices.getX( i + 2 ) + writtenVertices );
+							output.setUint32( fOffset, indices.getX( i + 2 ) + writtenVertices, options.littleEndian );
 							fOffset += indexByteCount;
 
 						}
@@ -376,13 +377,13 @@ PLYExporter.prototype = {
 							output.setUint8( fOffset, 3 );
 							fOffset += 1;
 
-							output.setUint32( fOffset, writtenVertices + i );
+							output.setUint32( fOffset, writtenVertices + i, options.littleEndian );
 							fOffset += indexByteCount;
 
-							output.setUint32( fOffset, writtenVertices + i + 1 );
+							output.setUint32( fOffset, writtenVertices + i + 1, options.littleEndian );
 							fOffset += indexByteCount;
 
-							output.setUint32( fOffset, writtenVertices + i + 2 );
+							output.setUint32( fOffset, writtenVertices + i + 2, options.littleEndian );
 							fOffset += indexByteCount;
 
 						}

+ 17 - 4
examples/misc_exporter_ply.html

@@ -9,7 +9,7 @@
 	<body>
 		<div id="info">
 			<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - exporter - ply<br/><br/>
-			<button id="exportASCII">export ASCII</button> <button id="exportBinary">export binary</button>
+			<button id="exportASCII">export ASCII</button> <button id="exportBinaryBigEndian">export binary (Big Endian)</button> <button id="exportBinaryLittleEndian">export binary (Little Endian)</button>
 		</div>
 
 		<script type="module">
@@ -93,8 +93,11 @@
 				var buttonExportASCII = document.getElementById( 'exportASCII' );
 				buttonExportASCII.addEventListener( 'click', exportASCII );
 
-				var buttonExportBinary = document.getElementById( 'exportBinary' );
-				buttonExportBinary.addEventListener( 'click', exportBinary );
+				var buttonExportBinary = document.getElementById( 'exportBinaryBigEndian' );
+				buttonExportBinary.addEventListener( 'click', exportBinaryBigEndian );
+
+				var buttonExportBinary = document.getElementById( 'exportBinaryLittleEndian' );
+				buttonExportBinary.addEventListener( 'click', exportBinaryLittleEndian );
 
 			}
 
@@ -124,7 +127,7 @@
 
 			}
 
-			function exportBinary() {
+			function exportBinaryBigEndian() {
 
 				exporter.parse( mesh, function ( result ) {
 
@@ -134,6 +137,16 @@
 
 			}
 
+			function exportBinaryLittleEndian() {
+
+				exporter.parse( mesh, function ( result ) {
+
+					saveArrayBuffer( result, 'box.ply' );
+
+				}, { binary: true, littleEndian: true } );
+
+			}
+
 			var link = document.createElement( 'a' );
 			link.style.display = 'none';
 			document.body.appendChild( link );