未验证 提交 5069d476 编写于 作者: M Michael Herzog 提交者: GitHub

Examples: Convert exporters to ES6. (#21605)

上级 85a366ce
......@@ -15,11 +15,17 @@
*/
/* global DracoEncoderModule */
var DRACOExporter = function () {};
DRACOExporter.prototype = {
constructor: DRACOExporter,
parse: function ( object, options ) {
class DRACOExporter {
parse( object, options = {
decodeSpeed: 5,
encodeSpeed: 5,
encoderMethod: DRACOExporter.MESH_EDGEBREAKER_ENCODING,
quantization: [ 16, 8, 8, 8, 8 ],
exportUvs: true,
exportNormals: true,
exportColor: false
} ) {
if ( object.isBufferGeometry === true ) {
......@@ -33,25 +39,11 @@
}
if ( options === undefined ) {
options = {
decodeSpeed: 5,
encodeSpeed: 5,
encoderMethod: DRACOExporter.MESH_EDGEBREAKER_ENCODING,
quantization: [ 16, 8, 8, 8, 8 ],
exportUvs: true,
exportNormals: true,
exportColor: false
};
}
var geometry = object.geometry;
var dracoEncoder = DracoEncoderModule();
var encoder = new dracoEncoder.Encoder();
var builder;
var dracoObject;
const geometry = object.geometry;
const dracoEncoder = DracoEncoderModule();
const encoder = new dracoEncoder.Encoder();
let builder;
let dracoObject;
if ( geometry.isBufferGeometry !== true ) {
......@@ -63,9 +55,9 @@
builder = new dracoEncoder.MeshBuilder();
dracoObject = new dracoEncoder.Mesh();
var vertices = geometry.getAttribute( 'position' );
const vertices = geometry.getAttribute( 'position' );
builder.AddFloatAttributeToMesh( dracoObject, dracoEncoder.POSITION, vertices.count, vertices.itemSize, vertices.array );
var faces = geometry.getIndex();
const faces = geometry.getIndex();
if ( faces !== null ) {
......@@ -73,9 +65,9 @@
} else {
var faces = new ( vertices.count > 65535 ? Uint32Array : Uint16Array )( vertices.count );
const faces = new ( vertices.count > 65535 ? Uint32Array : Uint16Array )( vertices.count );
for ( var i = 0; i < faces.length; i ++ ) {
for ( let i = 0; i < faces.length; i ++ ) {
faces[ i ] = i;
......@@ -87,7 +79,7 @@
if ( options.exportNormals === true ) {
var normals = geometry.getAttribute( 'normal' );
const normals = geometry.getAttribute( 'normal' );
if ( normals !== undefined ) {
......@@ -99,7 +91,7 @@
if ( options.exportUvs === true ) {
var uvs = geometry.getAttribute( 'uv' );
const uvs = geometry.getAttribute( 'uv' );
if ( uvs !== undefined ) {
......@@ -111,7 +103,7 @@
if ( options.exportColor === true ) {
var colors = geometry.getAttribute( 'color' );
const colors = geometry.getAttribute( 'color' );
if ( colors !== undefined ) {
......@@ -125,12 +117,12 @@
builder = new dracoEncoder.PointCloudBuilder();
dracoObject = new dracoEncoder.PointCloud();
var vertices = geometry.getAttribute( 'position' );
const vertices = geometry.getAttribute( 'position' );
builder.AddFloatAttribute( dracoObject, dracoEncoder.POSITION, vertices.count, vertices.itemSize, vertices.array );
if ( options.exportColor === true ) {
var colors = geometry.getAttribute( 'color' );
const colors = geometry.getAttribute( 'color' );
if ( colors !== undefined ) {
......@@ -147,10 +139,10 @@
} //Compress using draco encoder
var encodedData = new dracoEncoder.DracoInt8Array(); //Sets the desired encoding and decoding speed for the given options from 0 (slowest speed, but the best compression) to 10 (fastest, but the worst compression).
const encodedData = new dracoEncoder.DracoInt8Array(); //Sets the desired encoding and decoding speed for the given options from 0 (slowest speed, but the best compression) to 10 (fastest, but the worst compression).
var encodeSpeed = options.encodeSpeed !== undefined ? options.encodeSpeed : 5;
var decodeSpeed = options.decodeSpeed !== undefined ? options.decodeSpeed : 5;
const encodeSpeed = options.encodeSpeed !== undefined ? options.encodeSpeed : 5;
const decodeSpeed = options.decodeSpeed !== undefined ? options.decodeSpeed : 5;
encoder.SetSpeedOptions( encodeSpeed, decodeSpeed ); // Sets the desired encoding method for a given geometry.
if ( options.encoderMethod !== undefined ) {
......@@ -163,7 +155,7 @@
if ( options.quantization !== undefined ) {
for ( var i = 0; i < 5; i ++ ) {
for ( let i = 0; i < 5; i ++ ) {
if ( options.quantization[ i ] !== undefined ) {
......@@ -175,7 +167,7 @@
}
var length;
let length;
if ( object.isMesh === true ) {
......@@ -196,9 +188,9 @@
} //Copy encoded data to buffer.
var outputData = new Int8Array( new ArrayBuffer( length ) );
const outputData = new Int8Array( new ArrayBuffer( length ) );
for ( var i = 0; i < length; i ++ ) {
for ( let i = 0; i < length; i ++ ) {
outputData[ i ] = encodedData.GetValue( i );
......@@ -210,7 +202,9 @@
return outputData;
}
}; // Encoder methods
} // Encoder methods
DRACOExporter.MESH_EDGEBREAKER_ENCODING = 1;
DRACOExporter.MESH_SEQUENTIAL_ENCODING = 0; // Geometry type
......
......@@ -5,68 +5,8 @@
* - mmd-parser https://github.com/takahirox/mmd-parser
*/
var MMDExporter = function () {
class MMDExporter {
// Unicode to Shift_JIS table
var u2sTable;
function unicodeToShiftjis( str ) {
if ( u2sTable === undefined ) {
var encoder = new MMDParser.CharsetEncoder(); // eslint-disable-line no-undef
var table = encoder.s2uTable;
u2sTable = {};
var keys = Object.keys( table );
for ( var i = 0, il = keys.length; i < il; i ++ ) {
var key = keys[ i ];
var value = table[ key ];
key = parseInt( key );
u2sTable[ value ] = key;
}
}
var array = [];
for ( var i = 0, il = str.length; i < il; i ++ ) {
var code = str.charCodeAt( i );
var value = u2sTable[ code ];
if ( value === undefined ) {
throw 'cannot convert charcode 0x' + code.toString( 16 );
} else if ( value > 0xff ) {
array.push( value >> 8 & 0xff );
array.push( value & 0xff );
} else {
array.push( value & 0xff );
}
}
return new Uint8Array( array );
}
function getBindBones( skin ) {
// any more efficient ways?
var poseSkin = skin.clone();
poseSkin.pose();
return poseSkin.skeleton.bones;
}
/* TODO: implement
// mesh -> pmd
this.parsePmd = function ( object ) {
......@@ -79,13 +19,17 @@
};
*/
/* TODO: implement
// animation + skeleton -> vmd
this.parseVmd = function ( object ) {
};
*/
/*
* skeleton -> vpd
* Returns Shift_JIS encoded Uint8Array. Otherwise return strings.
*/
this.parseVpd = function ( skin, outputShiftJis, useOriginalBones ) {
parseVpd( skin, outputShiftJis, useOriginalBones ) {
if ( skin.isSkinnedMesh !== true ) {
......@@ -97,7 +41,7 @@
function toStringsFromNumber( num ) {
if ( Math.abs( num ) < 1e-6 ) num = 0;
var a = num.toString();
let a = num.toString();
if ( a.indexOf( '.' ) === - 1 ) {
......@@ -106,18 +50,18 @@
}
a += '000000';
var index = a.indexOf( '.' );
var d = a.slice( 0, index );
var p = a.slice( index + 1, index + 7 );
const index = a.indexOf( '.' );
const d = a.slice( 0, index );
const p = a.slice( index + 1, index + 7 );
return d + '.' + p;
}
function toStringsFromArray( array ) {
var a = [];
const a = [];
for ( var i = 0, il = array.length; i < il; i ++ ) {
for ( let i = 0, il = array.length; i < il; i ++ ) {
a.push( toStringsFromNumber( array[ i ] ) );
......@@ -128,23 +72,23 @@
}
skin.updateMatrixWorld( true );
var bones = skin.skeleton.bones;
var bones2 = getBindBones( skin );
var position = new THREE.Vector3();
var quaternion = new THREE.Quaternion();
var quaternion2 = new THREE.Quaternion();
var matrix = new THREE.Matrix4();
var array = [];
const bones = skin.skeleton.bones;
const bones2 = getBindBones( skin );
const position = new THREE.Vector3();
const quaternion = new THREE.Quaternion();
const quaternion2 = new THREE.Quaternion();
const matrix = new THREE.Matrix4();
const array = [];
array.push( 'Vocaloid Pose Data file' );
array.push( '' );
array.push( ( skin.name !== '' ? skin.name.replace( /\s/g, '_' ) : 'skin' ) + '.osm;' );
array.push( bones.length + ';' );
array.push( '' );
for ( var i = 0, il = bones.length; i < il; i ++ ) {
for ( let i = 0, il = bones.length; i < il; i ++ ) {
var bone = bones[ i ];
var bone2 = bones2[ i ];
const bone = bones[ i ];
const bone2 = bones2[ i ];
/*
* use the bone matrix saved before solving IK.
* see CCDIKSolver for the detail.
......@@ -162,8 +106,8 @@
position.setFromMatrixPosition( matrix );
quaternion.setFromRotationMatrix( matrix );
var pArray = position.sub( bone2.position ).toArray();
var qArray = quaternion2.copy( bone2.quaternion ).conjugate().multiply( quaternion ).toArray(); // right to left
const pArray = position.sub( bone2.position ).toArray();
const qArray = quaternion2.copy( bone2.quaternion ).conjugate().multiply( quaternion ).toArray(); // right to left
pArray[ 2 ] = - pArray[ 2 ];
qArray[ 0 ] = - qArray[ 0 ];
......@@ -177,17 +121,73 @@
}
array.push( '' );
var lines = array.join( '\n' );
const lines = array.join( '\n' );
return outputShiftJis === true ? unicodeToShiftjis( lines ) : lines;
};
/* TODO: implement
// animation + skeleton -> vmd
this.parseVmd = function ( object ) {
};
*/
}
} // Unicode to Shift_JIS table
let u2sTable;
function unicodeToShiftjis( str ) {
if ( u2sTable === undefined ) {
const encoder = new MMDParser.CharsetEncoder(); // eslint-disable-line no-undef
const table = encoder.s2uTable;
u2sTable = {};
const keys = Object.keys( table );
for ( let i = 0, il = keys.length; i < il; i ++ ) {
let key = keys[ i ];
const value = table[ key ];
key = parseInt( key );
u2sTable[ value ] = key;
}
}
const array = [];
for ( let i = 0, il = str.length; i < il; i ++ ) {
const code = str.charCodeAt( i );
const value = u2sTable[ code ];
if ( value === undefined ) {
throw 'cannot convert charcode 0x' + code.toString( 16 );
} else if ( value > 0xff ) {
array.push( value >> 8 & 0xff );
array.push( value & 0xff );
} else {
array.push( value & 0xff );
}
}
return new Uint8Array( array );
}
function getBindBones( skin ) {
// any more efficient ways?
const poseSkin = skin.clone();
poseSkin.pose();
return poseSkin.skeleton.bones;
};
}
THREE.MMDExporter = MMDExporter;
......
( function () {
var OBJExporter = function () {};
OBJExporter.prototype = {
constructor: OBJExporter,
parse: function ( object ) {
var output = '';
var indexVertex = 0;
var indexVertexUvs = 0;
var indexNormals = 0;
var vertex = new THREE.Vector3();
var color = new THREE.Color();
var normal = new THREE.Vector3();
var uv = new THREE.Vector2();
var i,
j,
k,
l,
m,
face = [];
var parseMesh = function ( mesh ) {
var nbVertex = 0;
var nbNormals = 0;
var nbVertexUvs = 0;
var geometry = mesh.geometry;
var normalMatrixWorld = new THREE.Matrix3();
class OBJExporter {
parse( object ) {
let output = '';
let indexVertex = 0;
let indexVertexUvs = 0;
let indexNormals = 0;
const vertex = new THREE.Vector3();
const color = new THREE.Color();
const normal = new THREE.Vector3();
const uv = new THREE.Vector2();
const face = [];
function parseMesh( mesh ) {
let nbVertex = 0;
let nbNormals = 0;
let nbVertexUvs = 0;
const geometry = mesh.geometry;
const normalMatrixWorld = new THREE.Matrix3();
if ( geometry.isBufferGeometry !== true ) {
......@@ -36,10 +29,10 @@
} // shortcuts
var vertices = geometry.getAttribute( 'position' );
var normals = geometry.getAttribute( 'normal' );
var uvs = geometry.getAttribute( 'uv' );
var indices = geometry.getIndex(); // name of the mesh object
const vertices = geometry.getAttribute( 'position' );
const normals = geometry.getAttribute( 'normal' );
const uvs = geometry.getAttribute( 'uv' );
const indices = geometry.getIndex(); // name of the mesh object
output += 'o ' + mesh.name + '\n'; // name of the mesh material
......@@ -52,7 +45,7 @@
if ( vertices !== undefined ) {
for ( i = 0, l = vertices.count; i < l; i ++, nbVertex ++ ) {
for ( let i = 0, l = vertices.count; i < l; i ++, nbVertex ++ ) {
vertex.x = vertices.getX( i );
vertex.y = vertices.getY( i );
......@@ -69,7 +62,7 @@
if ( uvs !== undefined ) {
for ( i = 0, l = uvs.count; i < l; i ++, nbVertexUvs ++ ) {
for ( let i = 0, l = uvs.count; i < l; i ++, nbVertexUvs ++ ) {
uv.x = uvs.getX( i );
uv.y = uvs.getY( i ); // transform the uv to export format
......@@ -85,7 +78,7 @@
normalMatrixWorld.getNormalMatrix( mesh.matrixWorld );
for ( i = 0, l = normals.count; i < l; i ++, nbNormals ++ ) {
for ( let i = 0, l = normals.count; i < l; i ++, nbNormals ++ ) {
normal.x = normals.getX( i );
normal.y = normals.getY( i );
......@@ -102,11 +95,11 @@
if ( indices !== null ) {
for ( i = 0, l = indices.count; i < l; i += 3 ) {
for ( let i = 0, l = indices.count; i < l; i += 3 ) {
for ( m = 0; m < 3; m ++ ) {
for ( let m = 0; m < 3; m ++ ) {
j = indices.getX( i + m ) + 1;
const j = indices.getX( i + m ) + 1;
face[ m ] = indexVertex + j + ( normals || uvs ? '/' + ( uvs ? indexVertexUvs + j : '' ) + ( normals ? '/' + ( indexNormals + j ) : '' ) : '' );
} // transform the face to export format
......@@ -118,11 +111,11 @@
} else {
for ( i = 0, l = vertices.count; i < l; i += 3 ) {
for ( let i = 0, l = vertices.count; i < l; i += 3 ) {
for ( m = 0; m < 3; m ++ ) {
for ( let m = 0; m < 3; m ++ ) {
j = i + m + 1;
const j = i + m + 1;
face[ m ] = indexVertex + j + ( normals || uvs ? '/' + ( uvs ? indexVertexUvs + j : '' ) + ( normals ? '/' + ( indexNormals + j ) : '' ) : '' );
} // transform the face to export format
......@@ -139,13 +132,13 @@
indexVertexUvs += nbVertexUvs;
indexNormals += nbNormals;
};
}
var parseLine = function ( line ) {
function parseLine( line ) {
var nbVertex = 0;
var geometry = line.geometry;
var type = line.type;
let nbVertex = 0;
const geometry = line.geometry;
const type = line.type;
if ( geometry.isBufferGeometry !== true ) {
......@@ -154,13 +147,13 @@
} // shortcuts
var vertices = geometry.getAttribute( 'position' ); // name of the line object
const vertices = geometry.getAttribute( 'position' ); // name of the line object
output += 'o ' + line.name + '\n';
if ( vertices !== undefined ) {
for ( i = 0, l = vertices.count; i < l; i ++, nbVertex ++ ) {
for ( let i = 0, l = vertices.count; i < l; i ++, nbVertex ++ ) {
vertex.x = vertices.getX( i );
vertex.y = vertices.getY( i );
......@@ -178,7 +171,7 @@
output += 'l ';
for ( j = 1, l = vertices.count; j <= l; j ++ ) {
for ( let j = 1, l = vertices.count; j <= l; j ++ ) {
output += indexVertex + j + ' ';
......@@ -190,7 +183,7 @@
if ( type === 'LineSegments' ) {
for ( j = 1, k = j + 1, l = vertices.count; j < l; j += 2, k = j + 1 ) {
for ( let j = 1, k = j + 1, l = vertices.count; j < l; j += 2, k = j + 1 ) {
output += 'l ' + ( indexVertex + j ) + ' ' + ( indexVertex + k ) + '\n';
......@@ -201,12 +194,12 @@
indexVertex += nbVertex;
};
}
var parsePoints = function ( points ) {
function parsePoints( points ) {
var nbVertex = 0;
var geometry = points.geometry;
let nbVertex = 0;
const geometry = points.geometry;
if ( geometry.isBufferGeometry !== true ) {
......@@ -214,13 +207,13 @@
}
var vertices = geometry.getAttribute( 'position' );
var colors = geometry.getAttribute( 'color' );
const vertices = geometry.getAttribute( 'position' );
const colors = geometry.getAttribute( 'color' );
output += 'o ' + points.name + '\n';
if ( vertices !== undefined ) {
for ( i = 0, l = vertices.count; i < l; i ++, nbVertex ++ ) {
for ( let i = 0, l = vertices.count; i < l; i ++, nbVertex ++ ) {
vertex.fromBufferAttribute( vertices, i );
vertex.applyMatrix4( points.matrixWorld );
......@@ -241,7 +234,7 @@
output += 'p ';
for ( j = 1, l = vertices.count; j <= l; j ++ ) {
for ( let j = 1, l = vertices.count; j <= l; j ++ ) {
output += indexVertex + j + ' ';
......@@ -251,7 +244,7 @@
indexVertex += nbVertex;
};
}
object.traverse( function ( child ) {
......@@ -277,7 +270,8 @@
return output;
}
};
}
THREE.OBJExporter = OBJExporter;
......
......@@ -4,7 +4,7 @@
* https://github.com/gkjohnson/ply-exporter-js
*
* Usage:
* var exporter = new PLYExporter();
* const exporter = new PLYExporter();
*
* // second argument is a list of options
* exporter.parse(mesh, data => console.log(data), { binary: true, excludeAttributes: [ 'color' ], littleEndian: true });
......@@ -13,11 +13,9 @@
* http://paulbourke.net/dataformats/ply/
*/
var PLYExporter = function () {};
class PLYExporter {
PLYExporter.prototype = {
constructor: PLYExporter,
parse: function ( object, onDone, options ) {
parse( object, onDone, options ) {
if ( onDone && typeof onDone === 'object' ) {
......@@ -34,8 +32,8 @@
if ( child.isMesh === true ) {
var mesh = child;
var geometry = mesh.geometry;
const mesh = child;
const geometry = mesh.geometry;
if ( geometry.isBufferGeometry !== true ) {
......@@ -56,27 +54,27 @@
} // Default options
var defaultOptions = {
const defaultOptions = {
binary: false,
excludeAttributes: [],
// normal, uv, color, index
littleEndian: false
};
options = Object.assign( defaultOptions, options );
var excludeAttributes = options.excludeAttributes;
var includeNormals = false;
var includeColors = false;
var includeUVs = false; // count the vertices, check which properties are used,
const excludeAttributes = options.excludeAttributes;
let includeNormals = false;
let includeColors = false;
let includeUVs = false; // count the vertices, check which properties are used,
// and cache the BufferGeometry
var vertexCount = 0;
var faceCount = 0;
let vertexCount = 0;
let faceCount = 0;
object.traverse( function ( child ) {
if ( child.isMesh === true ) {
var mesh = child;
var geometry = mesh.geometry;
const mesh = child;
const geometry = mesh.geometry;
if ( geometry.isBufferGeometry !== true ) {
......@@ -84,11 +82,11 @@
}
var vertices = geometry.getAttribute( 'position' );
var normals = geometry.getAttribute( 'normal' );
var uvs = geometry.getAttribute( 'uv' );
var colors = geometry.getAttribute( 'color' );
var indices = geometry.getIndex();
const vertices = geometry.getAttribute( 'position' );
const normals = geometry.getAttribute( 'normal' );
const uvs = geometry.getAttribute( 'uv' );
const colors = geometry.getAttribute( 'color' );
const indices = geometry.getIndex();
if ( vertices === undefined ) {
......@@ -105,7 +103,7 @@
}
} );
var includeIndices = excludeAttributes.indexOf( 'index' ) === - 1;
const includeIndices = excludeAttributes.indexOf( 'index' ) === - 1;
includeNormals = includeNormals && excludeAttributes.indexOf( 'normal' ) === - 1;
includeColors = includeColors && excludeAttributes.indexOf( 'color' ) === - 1;
includeUVs = includeUVs && excludeAttributes.indexOf( 'uv' ) === - 1;
......@@ -120,8 +118,8 @@
}
var indexByteCount = 4;
var header = 'ply\n' + `format ${options.binary ? options.littleEndian ? 'binary_little_endian' : 'binary_big_endian' : 'ascii'} 1.0\n` + `element vertex ${vertexCount}\n` + // position
const indexByteCount = 4;
let header = 'ply\n' + `format ${options.binary ? options.littleEndian ? 'binary_little_endian' : 'binary_big_endian' : 'ascii'} 1.0\n` + `element vertex ${vertexCount}\n` + // position
'property float x\n' + 'property float y\n' + 'property float z\n';
if ( includeNormals === true ) {
......@@ -154,37 +152,37 @@
header += 'end_header\n'; // Generate attribute data
var vertex = new THREE.Vector3();
var normalMatrixWorld = new THREE.Matrix3();
var result = null;
const vertex = new THREE.Vector3();
const normalMatrixWorld = new THREE.Matrix3();
let result = null;
if ( options.binary === true ) {
// Binary File Generation
var headerBin = new TextEncoder().encode( header ); // 3 position values at 4 bytes
const headerBin = new TextEncoder().encode( header ); // 3 position values at 4 bytes
// 3 normal values at 4 bytes
// 3 color channels with 1 byte
// 2 uv values at 4 bytes
var vertexListLength = vertexCount * ( 4 * 3 + ( includeNormals ? 4 * 3 : 0 ) + ( includeColors ? 3 : 0 ) + ( includeUVs ? 4 * 2 : 0 ) ); // 1 byte shape desciptor
const vertexListLength = vertexCount * ( 4 * 3 + ( includeNormals ? 4 * 3 : 0 ) + ( includeColors ? 3 : 0 ) + ( includeUVs ? 4 * 2 : 0 ) ); // 1 byte shape desciptor
// 3 vertex indices at ${indexByteCount} bytes
var faceListLength = includeIndices ? faceCount * ( indexByteCount * 3 + 1 ) : 0;
var output = new DataView( new ArrayBuffer( headerBin.length + vertexListLength + faceListLength ) );
const faceListLength = includeIndices ? faceCount * ( indexByteCount * 3 + 1 ) : 0;
const output = new DataView( new ArrayBuffer( headerBin.length + vertexListLength + faceListLength ) );
new Uint8Array( output.buffer ).set( headerBin, 0 );
var vOffset = headerBin.length;
var fOffset = headerBin.length + vertexListLength;
var writtenVertices = 0;
let vOffset = headerBin.length;
let fOffset = headerBin.length + vertexListLength;
let writtenVertices = 0;
traverseMeshes( function ( mesh, geometry ) {
var vertices = geometry.getAttribute( 'position' );
var normals = geometry.getAttribute( 'normal' );
var uvs = geometry.getAttribute( 'uv' );
var colors = geometry.getAttribute( 'color' );
var indices = geometry.getIndex();
const vertices = geometry.getAttribute( 'position' );
const normals = geometry.getAttribute( 'normal' );
const uvs = geometry.getAttribute( 'uv' );
const colors = geometry.getAttribute( 'color' );
const indices = geometry.getIndex();
normalMatrixWorld.getNormalMatrix( mesh.matrixWorld );
for ( var i = 0, l = vertices.count; i < l; i ++ ) {
for ( let i = 0, l = vertices.count; i < l; i ++ ) {
vertex.x = vertices.getX( i );
vertex.y = vertices.getY( i );
......@@ -279,7 +277,7 @@
// Create the face list
if ( indices !== null ) {
for ( var i = 0, l = indices.count; i < l; i += 3 ) {
for ( let i = 0, l = indices.count; i < l; i += 3 ) {
output.setUint8( fOffset, 3 );
fOffset += 1;
......@@ -294,7 +292,7 @@
} else {
for ( var i = 0, l = vertices.count; i < l; i += 3 ) {
for ( let i = 0, l = vertices.count; i < l; i += 3 ) {
output.setUint8( fOffset, 3 );
fOffset += 1;
......@@ -322,26 +320,26 @@
// Ascii File Generation
// count the number of vertices
var writtenVertices = 0;
var vertexList = '';
var faceList = '';
let writtenVertices = 0;
let vertexList = '';
let faceList = '';
traverseMeshes( function ( mesh, geometry ) {
var vertices = geometry.getAttribute( 'position' );
var normals = geometry.getAttribute( 'normal' );
var uvs = geometry.getAttribute( 'uv' );
var colors = geometry.getAttribute( 'color' );
var indices = geometry.getIndex();
const vertices = geometry.getAttribute( 'position' );
const normals = geometry.getAttribute( 'normal' );
const uvs = geometry.getAttribute( 'uv' );
const colors = geometry.getAttribute( 'color' );
const indices = geometry.getIndex();
normalMatrixWorld.getNormalMatrix( mesh.matrixWorld ); // form each line
for ( var i = 0, l = vertices.count; i < l; i ++ ) {
for ( let i = 0, l = vertices.count; i < l; i ++ ) {
vertex.x = vertices.getX( i );
vertex.y = vertices.getY( i );
vertex.z = vertices.getZ( i );
vertex.applyMatrix4( mesh.matrixWorld ); // Position information
var line = vertex.x + ' ' + vertex.y + ' ' + vertex.z; // Normal information
let line = vertex.x + ' ' + vertex.y + ' ' + vertex.z; // Normal information
if ( includeNormals === true ) {
......@@ -400,7 +398,7 @@
if ( indices !== null ) {
for ( var i = 0, l = indices.count; i < l; i += 3 ) {
for ( let i = 0, l = indices.count; i < l; i += 3 ) {
faceList += `3 ${indices.getX( i + 0 ) + writtenVertices}`;
faceList += ` ${indices.getX( i + 1 ) + writtenVertices}`;
......@@ -410,7 +408,7 @@
} else {
for ( var i = 0, l = vertices.count; i < l; i += 3 ) {
for ( let i = 0, l = vertices.count; i < l; i += 3 ) {
faceList += `3 ${writtenVertices + i} ${writtenVertices + i + 1} ${writtenVertices + i + 2}\n`;
......@@ -433,7 +431,8 @@
return result;
}
};
}
THREE.PLYExporter = PLYExporter;
......
......@@ -2,29 +2,26 @@
/**
* Usage:
* var exporter = new STLExporter();
* const exporter = new STLExporter();
*
* // second argument is a list of options
* var data = exporter.parse( mesh, { binary: true } );
* const data = exporter.parse( mesh, { binary: true } );
*
*/
var STLExporter = function () {};
class STLExporter {
STLExporter.prototype = {
constructor: STLExporter,
parse: function ( scene, options ) {
parse( scene, options = {} ) {
if ( options === undefined ) options = {};
var binary = options.binary !== undefined ? options.binary : false; //
const binary = options.binary !== undefined ? options.binary : false; //
var objects = [];
var triangles = 0;
const objects = [];
let triangles = 0;
scene.traverse( function ( object ) {
if ( object.isMesh ) {
var geometry = object.geometry;
const geometry = object.geometry;
if ( geometry.isBufferGeometry !== true ) {
......@@ -32,8 +29,8 @@
}
var index = geometry.index;
var positionAttribute = geometry.getAttribute( 'position' );
const index = geometry.index;
const positionAttribute = geometry.getAttribute( 'position' );
triangles += index !== null ? index.count / 3 : positionAttribute.count / 3;
objects.push( {
object3d: object,
......@@ -43,13 +40,13 @@
}
} );
var output;
var offset = 80; // skip header
let output;
let offset = 80; // skip header
if ( binary === true ) {
var bufferLength = triangles * 2 + triangles * 3 * 4 * 4 + 80 + 4;
var arrayBuffer = new ArrayBuffer( bufferLength );
const bufferLength = triangles * 2 + triangles * 3 * 4 * 4 + 80 + 4;
const arrayBuffer = new ArrayBuffer( bufferLength );
output = new DataView( arrayBuffer );
output.setUint32( offset, triangles, true );
offset += 4;
......@@ -61,28 +58,28 @@
}
var vA = new THREE.Vector3();
var vB = new THREE.Vector3();
var vC = new THREE.Vector3();
var cb = new THREE.Vector3();
var ab = new THREE.Vector3();
var normal = new THREE.Vector3();
const vA = new THREE.Vector3();
const vB = new THREE.Vector3();
const vC = new THREE.Vector3();
const cb = new THREE.Vector3();
const ab = new THREE.Vector3();
const normal = new THREE.Vector3();
for ( var i = 0, il = objects.length; i < il; i ++ ) {
for ( let i = 0, il = objects.length; i < il; i ++ ) {
var object = objects[ i ].object3d;
var geometry = objects[ i ].geometry;
var index = geometry.index;
var positionAttribute = geometry.getAttribute( 'position' );
const object = objects[ i ].object3d;
const geometry = objects[ i ].geometry;
const index = geometry.index;
const positionAttribute = geometry.getAttribute( 'position' );
if ( index !== null ) {
// indexed geometry
for ( var j = 0; j < index.count; j += 3 ) {
for ( let j = 0; j < index.count; j += 3 ) {
var a = index.getX( j + 0 );
var b = index.getX( j + 1 );
var c = index.getX( j + 2 );
const a = index.getX( j + 0 );
const b = index.getX( j + 1 );
const c = index.getX( j + 2 );
writeFace( a, b, c, positionAttribute, object );
}
......@@ -90,11 +87,11 @@
} else {
// non-indexed geometry
for ( var j = 0; j < positionAttribute.count; j += 3 ) {
for ( let j = 0; j < positionAttribute.count; j += 3 ) {
var a = j + 0;
var b = j + 1;
var c = j + 2;
const a = j + 0;
const b = j + 1;
const c = j + 2;
writeFace( a, b, c, positionAttribute, object );
}
......@@ -192,7 +189,8 @@
}
}
};
}
THREE.STLExporter = STLExporter;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册