|
@@ -21,9 +21,10 @@ import {
|
|
|
Vector3
|
|
|
} from '../../../build/three.module.js';
|
|
|
|
|
|
-var GLTFExporter = ( function () {
|
|
|
|
|
|
- function GLTFExporter() {
|
|
|
+class GLTFExporter {
|
|
|
+
|
|
|
+ constructor() {
|
|
|
|
|
|
this.pluginCallbacks = [];
|
|
|
|
|
@@ -47,285 +48,281 @@ var GLTFExporter = ( function () {
|
|
|
|
|
|
}
|
|
|
|
|
|
- GLTFExporter.prototype = {
|
|
|
-
|
|
|
- constructor: GLTFExporter,
|
|
|
-
|
|
|
- register: function ( callback ) {
|
|
|
-
|
|
|
- if ( this.pluginCallbacks.indexOf( callback ) === - 1 ) {
|
|
|
-
|
|
|
- this.pluginCallbacks.push( callback );
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- return this;
|
|
|
-
|
|
|
- },
|
|
|
-
|
|
|
- unregister: function ( callback ) {
|
|
|
-
|
|
|
- if ( this.pluginCallbacks.indexOf( callback ) !== - 1 ) {
|
|
|
-
|
|
|
- this.pluginCallbacks.splice( this.pluginCallbacks.indexOf( callback ), 1 );
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- return this;
|
|
|
+ register( callback ) {
|
|
|
|
|
|
- },
|
|
|
+ if ( this.pluginCallbacks.indexOf( callback ) === - 1 ) {
|
|
|
|
|
|
- /**
|
|
|
- * Parse scenes and generate GLTF output
|
|
|
- * @param {Scene or [THREE.Scenes]} input Scene or Array of THREE.Scenes
|
|
|
- * @param {Function} onDone Callback on completed
|
|
|
- * @param {Object} options options
|
|
|
- */
|
|
|
- parse: function ( input, onDone, options ) {
|
|
|
-
|
|
|
- var writer = new GLTFWriter();
|
|
|
- var plugins = [];
|
|
|
-
|
|
|
- for ( var i = 0, il = this.pluginCallbacks.length; i < il; i ++ ) {
|
|
|
-
|
|
|
- plugins.push( this.pluginCallbacks[ i ]( writer ) );
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- writer.setPlugins( plugins );
|
|
|
- writer.write( input, onDone, options );
|
|
|
+ this.pluginCallbacks.push( callback );
|
|
|
|
|
|
}
|
|
|
|
|
|
- };
|
|
|
+ return this;
|
|
|
|
|
|
- //------------------------------------------------------------------------------
|
|
|
- // Constants
|
|
|
- //------------------------------------------------------------------------------
|
|
|
-
|
|
|
- var WEBGL_CONSTANTS = {
|
|
|
- POINTS: 0x0000,
|
|
|
- LINES: 0x0001,
|
|
|
- LINE_LOOP: 0x0002,
|
|
|
- LINE_STRIP: 0x0003,
|
|
|
- TRIANGLES: 0x0004,
|
|
|
- TRIANGLE_STRIP: 0x0005,
|
|
|
- TRIANGLE_FAN: 0x0006,
|
|
|
-
|
|
|
- UNSIGNED_BYTE: 0x1401,
|
|
|
- UNSIGNED_SHORT: 0x1403,
|
|
|
- FLOAT: 0x1406,
|
|
|
- UNSIGNED_INT: 0x1405,
|
|
|
- ARRAY_BUFFER: 0x8892,
|
|
|
- ELEMENT_ARRAY_BUFFER: 0x8893,
|
|
|
-
|
|
|
- NEAREST: 0x2600,
|
|
|
- LINEAR: 0x2601,
|
|
|
- NEAREST_MIPMAP_NEAREST: 0x2700,
|
|
|
- LINEAR_MIPMAP_NEAREST: 0x2701,
|
|
|
- NEAREST_MIPMAP_LINEAR: 0x2702,
|
|
|
- LINEAR_MIPMAP_LINEAR: 0x2703,
|
|
|
-
|
|
|
- CLAMP_TO_EDGE: 33071,
|
|
|
- MIRRORED_REPEAT: 33648,
|
|
|
- REPEAT: 10497
|
|
|
- };
|
|
|
-
|
|
|
- var THREE_TO_WEBGL = {};
|
|
|
-
|
|
|
- THREE_TO_WEBGL[ NearestFilter ] = WEBGL_CONSTANTS.NEAREST;
|
|
|
- THREE_TO_WEBGL[ NearestMipmapNearestFilter ] = WEBGL_CONSTANTS.NEAREST_MIPMAP_NEAREST;
|
|
|
- THREE_TO_WEBGL[ NearestMipmapLinearFilter ] = WEBGL_CONSTANTS.NEAREST_MIPMAP_LINEAR;
|
|
|
- THREE_TO_WEBGL[ LinearFilter ] = WEBGL_CONSTANTS.LINEAR;
|
|
|
- THREE_TO_WEBGL[ LinearMipmapNearestFilter ] = WEBGL_CONSTANTS.LINEAR_MIPMAP_NEAREST;
|
|
|
- THREE_TO_WEBGL[ LinearMipmapLinearFilter ] = WEBGL_CONSTANTS.LINEAR_MIPMAP_LINEAR;
|
|
|
+ }
|
|
|
|
|
|
- THREE_TO_WEBGL[ ClampToEdgeWrapping ] = WEBGL_CONSTANTS.CLAMP_TO_EDGE;
|
|
|
- THREE_TO_WEBGL[ RepeatWrapping ] = WEBGL_CONSTANTS.REPEAT;
|
|
|
- THREE_TO_WEBGL[ MirroredRepeatWrapping ] = WEBGL_CONSTANTS.MIRRORED_REPEAT;
|
|
|
+ unregister( callback ) {
|
|
|
|
|
|
- var PATH_PROPERTIES = {
|
|
|
- scale: 'scale',
|
|
|
- position: 'translation',
|
|
|
- quaternion: 'rotation',
|
|
|
- morphTargetInfluences: 'weights'
|
|
|
- };
|
|
|
+ if ( this.pluginCallbacks.indexOf( callback ) !== - 1 ) {
|
|
|
|
|
|
- // GLB constants
|
|
|
- // https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#glb-file-format-specification
|
|
|
+ this.pluginCallbacks.splice( this.pluginCallbacks.indexOf( callback ), 1 );
|
|
|
|
|
|
- var GLB_HEADER_BYTES = 12;
|
|
|
- var GLB_HEADER_MAGIC = 0x46546C67;
|
|
|
- var GLB_VERSION = 2;
|
|
|
+ }
|
|
|
|
|
|
- var GLB_CHUNK_PREFIX_BYTES = 8;
|
|
|
- var GLB_CHUNK_TYPE_JSON = 0x4E4F534A;
|
|
|
- var GLB_CHUNK_TYPE_BIN = 0x004E4942;
|
|
|
+ return this;
|
|
|
|
|
|
- //------------------------------------------------------------------------------
|
|
|
- // Utility functions
|
|
|
- //------------------------------------------------------------------------------
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
- * Compare two arrays
|
|
|
- * @param {Array} array1 Array 1 to compare
|
|
|
- * @param {Array} array2 Array 2 to compare
|
|
|
- * @return {Boolean} Returns true if both arrays are equal
|
|
|
+ * Parse scenes and generate GLTF output
|
|
|
+ * @param {Scene or [THREE.Scenes]} input Scene or Array of THREE.Scenes
|
|
|
+ * @param {Function} onDone Callback on completed
|
|
|
+ * @param {Object} options options
|
|
|
*/
|
|
|
- function equalArray( array1, array2 ) {
|
|
|
-
|
|
|
- return ( array1.length === array2.length ) && array1.every( function ( element, index ) {
|
|
|
+ parse( input, onDone, options ) {
|
|
|
|
|
|
- return element === array2[ index ];
|
|
|
+ const writer = new GLTFWriter();
|
|
|
+ const plugins = [];
|
|
|
|
|
|
- } );
|
|
|
-
|
|
|
- }
|
|
|
+ for ( let i = 0, il = this.pluginCallbacks.length; i < il; i ++ ) {
|
|
|
|
|
|
- /**
|
|
|
- * Converts a string to an ArrayBuffer.
|
|
|
- * @param {string} text
|
|
|
- * @return {ArrayBuffer}
|
|
|
- */
|
|
|
- function stringToArrayBuffer( text ) {
|
|
|
+ plugins.push( this.pluginCallbacks[ i ]( writer ) );
|
|
|
|
|
|
- if ( window.TextEncoder !== undefined ) {
|
|
|
+ }
|
|
|
|
|
|
- return new TextEncoder().encode( text ).buffer;
|
|
|
+ writer.setPlugins( plugins );
|
|
|
+ writer.write( input, onDone, options );
|
|
|
|
|
|
- }
|
|
|
+ }
|
|
|
|
|
|
- var array = new Uint8Array( new ArrayBuffer( text.length ) );
|
|
|
+}
|
|
|
+
|
|
|
+//------------------------------------------------------------------------------
|
|
|
+// Constants
|
|
|
+//------------------------------------------------------------------------------
|
|
|
+
|
|
|
+const WEBGL_CONSTANTS = {
|
|
|
+ POINTS: 0x0000,
|
|
|
+ LINES: 0x0001,
|
|
|
+ LINE_LOOP: 0x0002,
|
|
|
+ LINE_STRIP: 0x0003,
|
|
|
+ TRIANGLES: 0x0004,
|
|
|
+ TRIANGLE_STRIP: 0x0005,
|
|
|
+ TRIANGLE_FAN: 0x0006,
|
|
|
+
|
|
|
+ UNSIGNED_BYTE: 0x1401,
|
|
|
+ UNSIGNED_SHORT: 0x1403,
|
|
|
+ FLOAT: 0x1406,
|
|
|
+ UNSIGNED_INT: 0x1405,
|
|
|
+ ARRAY_BUFFER: 0x8892,
|
|
|
+ ELEMENT_ARRAY_BUFFER: 0x8893,
|
|
|
+
|
|
|
+ NEAREST: 0x2600,
|
|
|
+ LINEAR: 0x2601,
|
|
|
+ NEAREST_MIPMAP_NEAREST: 0x2700,
|
|
|
+ LINEAR_MIPMAP_NEAREST: 0x2701,
|
|
|
+ NEAREST_MIPMAP_LINEAR: 0x2702,
|
|
|
+ LINEAR_MIPMAP_LINEAR: 0x2703,
|
|
|
+
|
|
|
+ CLAMP_TO_EDGE: 33071,
|
|
|
+ MIRRORED_REPEAT: 33648,
|
|
|
+ REPEAT: 10497
|
|
|
+};
|
|
|
+
|
|
|
+const THREE_TO_WEBGL = {};
|
|
|
+
|
|
|
+THREE_TO_WEBGL[ NearestFilter ] = WEBGL_CONSTANTS.NEAREST;
|
|
|
+THREE_TO_WEBGL[ NearestMipmapNearestFilter ] = WEBGL_CONSTANTS.NEAREST_MIPMAP_NEAREST;
|
|
|
+THREE_TO_WEBGL[ NearestMipmapLinearFilter ] = WEBGL_CONSTANTS.NEAREST_MIPMAP_LINEAR;
|
|
|
+THREE_TO_WEBGL[ LinearFilter ] = WEBGL_CONSTANTS.LINEAR;
|
|
|
+THREE_TO_WEBGL[ LinearMipmapNearestFilter ] = WEBGL_CONSTANTS.LINEAR_MIPMAP_NEAREST;
|
|
|
+THREE_TO_WEBGL[ LinearMipmapLinearFilter ] = WEBGL_CONSTANTS.LINEAR_MIPMAP_LINEAR;
|
|
|
+
|
|
|
+THREE_TO_WEBGL[ ClampToEdgeWrapping ] = WEBGL_CONSTANTS.CLAMP_TO_EDGE;
|
|
|
+THREE_TO_WEBGL[ RepeatWrapping ] = WEBGL_CONSTANTS.REPEAT;
|
|
|
+THREE_TO_WEBGL[ MirroredRepeatWrapping ] = WEBGL_CONSTANTS.MIRRORED_REPEAT;
|
|
|
+
|
|
|
+const PATH_PROPERTIES = {
|
|
|
+ scale: 'scale',
|
|
|
+ position: 'translation',
|
|
|
+ quaternion: 'rotation',
|
|
|
+ morphTargetInfluences: 'weights'
|
|
|
+};
|
|
|
+
|
|
|
+// GLB constants
|
|
|
+// https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#glb-file-format-specification
|
|
|
+
|
|
|
+const GLB_HEADER_BYTES = 12;
|
|
|
+const GLB_HEADER_MAGIC = 0x46546C67;
|
|
|
+const GLB_VERSION = 2;
|
|
|
+
|
|
|
+const GLB_CHUNK_PREFIX_BYTES = 8;
|
|
|
+const GLB_CHUNK_TYPE_JSON = 0x4E4F534A;
|
|
|
+const GLB_CHUNK_TYPE_BIN = 0x004E4942;
|
|
|
+
|
|
|
+//------------------------------------------------------------------------------
|
|
|
+// Utility functions
|
|
|
+//------------------------------------------------------------------------------
|
|
|
+
|
|
|
+/**
|
|
|
+ * Compare two arrays
|
|
|
+ * @param {Array} array1 Array 1 to compare
|
|
|
+ * @param {Array} array2 Array 2 to compare
|
|
|
+ * @return {Boolean} Returns true if both arrays are equal
|
|
|
+ */
|
|
|
+function equalArray( array1, array2 ) {
|
|
|
+
|
|
|
+ return ( array1.length === array2.length ) && array1.every( function ( element, index ) {
|
|
|
+
|
|
|
+ return element === array2[ index ];
|
|
|
+
|
|
|
+ } );
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * Converts a string to an ArrayBuffer.
|
|
|
+ * @param {string} text
|
|
|
+ * @return {ArrayBuffer}
|
|
|
+ */
|
|
|
+function stringToArrayBuffer( text ) {
|
|
|
+
|
|
|
+ if ( window.TextEncoder !== undefined ) {
|
|
|
+
|
|
|
+ return new TextEncoder().encode( text ).buffer;
|
|
|
|
|
|
- for ( var i = 0, il = text.length; i < il; i ++ ) {
|
|
|
+ }
|
|
|
|
|
|
- var value = text.charCodeAt( i );
|
|
|
+ const array = new Uint8Array( new ArrayBuffer( text.length ) );
|
|
|
|
|
|
- // Replacing multi-byte character with space(0x20).
|
|
|
- array[ i ] = value > 0xFF ? 0x20 : value;
|
|
|
+ for ( let i = 0, il = text.length; i < il; i ++ ) {
|
|
|
|
|
|
- }
|
|
|
+ const value = text.charCodeAt( i );
|
|
|
|
|
|
- return array.buffer;
|
|
|
+ // Replacing multi-byte character with space(0x20).
|
|
|
+ array[ i ] = value > 0xFF ? 0x20 : value;
|
|
|
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * Is identity matrix
|
|
|
- *
|
|
|
- * @param {Matrix4} matrix
|
|
|
- * @returns {Boolean} Returns true, if parameter is identity matrix
|
|
|
- */
|
|
|
- function isIdentityMatrix( matrix ) {
|
|
|
+ return array.buffer;
|
|
|
|
|
|
- return equalArray( matrix.elements, [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ] );
|
|
|
+}
|
|
|
|
|
|
- }
|
|
|
+/**
|
|
|
+ * Is identity matrix
|
|
|
+ *
|
|
|
+ * @param {Matrix4} matrix
|
|
|
+ * @returns {Boolean} Returns true, if parameter is identity matrix
|
|
|
+ */
|
|
|
+function isIdentityMatrix( matrix ) {
|
|
|
|
|
|
- /**
|
|
|
- * Get the min and max vectors from the given attribute
|
|
|
- * @param {BufferAttribute} attribute Attribute to find the min/max in range from start to start + count
|
|
|
- * @param {Integer} start
|
|
|
- * @param {Integer} count
|
|
|
- * @return {Object} Object containing the `min` and `max` values (As an array of attribute.itemSize components)
|
|
|
- */
|
|
|
- function getMinMax( attribute, start, count ) {
|
|
|
+ return equalArray( matrix.elements, [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ] );
|
|
|
|
|
|
- var output = {
|
|
|
+}
|
|
|
|
|
|
- min: new Array( attribute.itemSize ).fill( Number.POSITIVE_INFINITY ),
|
|
|
- max: new Array( attribute.itemSize ).fill( Number.NEGATIVE_INFINITY )
|
|
|
+/**
|
|
|
+ * Get the min and max vectors from the given attribute
|
|
|
+ * @param {BufferAttribute} attribute Attribute to find the min/max in range from start to start + count
|
|
|
+ * @param {Integer} start
|
|
|
+ * @param {Integer} count
|
|
|
+ * @return {Object} Object containing the `min` and `max` values (As an array of attribute.itemSize components)
|
|
|
+ */
|
|
|
+function getMinMax( attribute, start, count ) {
|
|
|
|
|
|
- };
|
|
|
+ const output = {
|
|
|
|
|
|
- for ( var i = start; i < start + count; i ++ ) {
|
|
|
+ min: new Array( attribute.itemSize ).fill( Number.POSITIVE_INFINITY ),
|
|
|
+ max: new Array( attribute.itemSize ).fill( Number.NEGATIVE_INFINITY )
|
|
|
|
|
|
- for ( var a = 0; a < attribute.itemSize; a ++ ) {
|
|
|
+ };
|
|
|
|
|
|
- var value;
|
|
|
+ for ( let i = start; i < start + count; i ++ ) {
|
|
|
|
|
|
- if ( attribute.itemSize > 4 ) {
|
|
|
+ for ( let a = 0; a < attribute.itemSize; a ++ ) {
|
|
|
|
|
|
- // no support for interleaved data for itemSize > 4
|
|
|
+ let value;
|
|
|
|
|
|
- value = attribute.array[ i * attribute.itemSize + a ];
|
|
|
+ if ( attribute.itemSize > 4 ) {
|
|
|
|
|
|
- } else {
|
|
|
+ // no support for interleaved data for itemSize > 4
|
|
|
|
|
|
- if ( a === 0 ) value = attribute.getX( i );
|
|
|
- else if ( a === 1 ) value = attribute.getY( i );
|
|
|
- else if ( a === 2 ) value = attribute.getZ( i );
|
|
|
- else if ( a === 3 ) value = attribute.getW( i );
|
|
|
+ value = attribute.array[ i * attribute.itemSize + a ];
|
|
|
|
|
|
- }
|
|
|
+ } else {
|
|
|
|
|
|
- output.min[ a ] = Math.min( output.min[ a ], value );
|
|
|
- output.max[ a ] = Math.max( output.max[ a ], value );
|
|
|
+ if ( a === 0 ) value = attribute.getX( i );
|
|
|
+ else if ( a === 1 ) value = attribute.getY( i );
|
|
|
+ else if ( a === 2 ) value = attribute.getZ( i );
|
|
|
+ else if ( a === 3 ) value = attribute.getW( i );
|
|
|
|
|
|
}
|
|
|
|
|
|
- }
|
|
|
+ output.min[ a ] = Math.min( output.min[ a ], value );
|
|
|
+ output.max[ a ] = Math.max( output.max[ a ], value );
|
|
|
|
|
|
- return output;
|
|
|
+ }
|
|
|
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * Get the required size + padding for a buffer, rounded to the next 4-byte boundary.
|
|
|
- * https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#data-alignment
|
|
|
- *
|
|
|
- * @param {Integer} bufferSize The size the original buffer.
|
|
|
- * @returns {Integer} new buffer size with required padding.
|
|
|
- *
|
|
|
- */
|
|
|
- function getPaddedBufferSize( bufferSize ) {
|
|
|
+ return output;
|
|
|
|
|
|
- return Math.ceil( bufferSize / 4 ) * 4;
|
|
|
+}
|
|
|
|
|
|
- }
|
|
|
+/**
|
|
|
+ * Get the required size + padding for a buffer, rounded to the next 4-byte boundary.
|
|
|
+ * https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#data-alignment
|
|
|
+ *
|
|
|
+ * @param {Integer} bufferSize The size the original buffer.
|
|
|
+ * @returns {Integer} new buffer size with required padding.
|
|
|
+ *
|
|
|
+ */
|
|
|
+function getPaddedBufferSize( bufferSize ) {
|
|
|
|
|
|
- /**
|
|
|
- * Returns a buffer aligned to 4-byte boundary.
|
|
|
- *
|
|
|
- * @param {ArrayBuffer} arrayBuffer Buffer to pad
|
|
|
- * @param {Integer} paddingByte (Optional)
|
|
|
- * @returns {ArrayBuffer} The same buffer if it's already aligned to 4-byte boundary or a new buffer
|
|
|
- */
|
|
|
- function getPaddedArrayBuffer( arrayBuffer, paddingByte ) {
|
|
|
+ return Math.ceil( bufferSize / 4 ) * 4;
|
|
|
|
|
|
- paddingByte = paddingByte || 0;
|
|
|
+}
|
|
|
|
|
|
- var paddedLength = getPaddedBufferSize( arrayBuffer.byteLength );
|
|
|
+/**
|
|
|
+ * Returns a buffer aligned to 4-byte boundary.
|
|
|
+ *
|
|
|
+ * @param {ArrayBuffer} arrayBuffer Buffer to pad
|
|
|
+ * @param {Integer} paddingByte (Optional)
|
|
|
+ * @returns {ArrayBuffer} The same buffer if it's already aligned to 4-byte boundary or a new buffer
|
|
|
+ */
|
|
|
+function getPaddedArrayBuffer( arrayBuffer, paddingByte = 0 ) {
|
|
|
|
|
|
- if ( paddedLength !== arrayBuffer.byteLength ) {
|
|
|
+ const paddedLength = getPaddedBufferSize( arrayBuffer.byteLength );
|
|
|
|
|
|
- var array = new Uint8Array( paddedLength );
|
|
|
- array.set( new Uint8Array( arrayBuffer ) );
|
|
|
+ if ( paddedLength !== arrayBuffer.byteLength ) {
|
|
|
|
|
|
- if ( paddingByte !== 0 ) {
|
|
|
+ const array = new Uint8Array( paddedLength );
|
|
|
+ array.set( new Uint8Array( arrayBuffer ) );
|
|
|
|
|
|
- for ( var i = arrayBuffer.byteLength; i < paddedLength; i ++ ) {
|
|
|
+ if ( paddingByte !== 0 ) {
|
|
|
|
|
|
- array[ i ] = paddingByte;
|
|
|
+ for ( let i = arrayBuffer.byteLength; i < paddedLength; i ++ ) {
|
|
|
|
|
|
- }
|
|
|
+ array[ i ] = paddingByte;
|
|
|
|
|
|
}
|
|
|
|
|
|
- return array.buffer;
|
|
|
-
|
|
|
}
|
|
|
|
|
|
- return arrayBuffer;
|
|
|
+ return array.buffer;
|
|
|
|
|
|
}
|
|
|
|
|
|
- var cachedCanvas = null;
|
|
|
+ return arrayBuffer;
|
|
|
|
|
|
- /**
|
|
|
- * Writer
|
|
|
- */
|
|
|
- function GLTFWriter() {
|
|
|
+}
|
|
|
+
|
|
|
+let cachedCanvas = null;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Writer
|
|
|
+ */
|
|
|
+class GLTFWriter {
|
|
|
+
|
|
|
+ constructor() {
|
|
|
|
|
|
this.plugins = [];
|
|
|
|
|
@@ -360,2058 +357,2044 @@ var GLTFExporter = ( function () {
|
|
|
|
|
|
}
|
|
|
|
|
|
- GLTFWriter.prototype = {
|
|
|
-
|
|
|
- constructor: GLTFWriter,
|
|
|
+ setPlugins( plugins ) {
|
|
|
|
|
|
- setPlugins: function ( plugins ) {
|
|
|
+ this.plugins = plugins;
|
|
|
|
|
|
- this.plugins = plugins;
|
|
|
-
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- /**
|
|
|
- * Parse scenes and generate GLTF output
|
|
|
- * @param {Scene or [THREE.Scenes]} input Scene or Array of THREE.Scenes
|
|
|
- * @param {Function} onDone Callback on completed
|
|
|
- * @param {Object} options options
|
|
|
- */
|
|
|
- write: function ( input, onDone, options ) {
|
|
|
+ /**
|
|
|
+ * Parse scenes and generate GLTF output
|
|
|
+ * @param {Scene or [THREE.Scenes]} input Scene or Array of THREE.Scenes
|
|
|
+ * @param {Function} onDone Callback on completed
|
|
|
+ * @param {Object} options options
|
|
|
+ */
|
|
|
+ write( input, onDone, options ) {
|
|
|
|
|
|
- this.options = Object.assign( {}, {
|
|
|
- // default options
|
|
|
- binary: false,
|
|
|
- trs: false,
|
|
|
- onlyVisible: true,
|
|
|
- truncateDrawRange: true,
|
|
|
- embedImages: true,
|
|
|
- maxTextureSize: Infinity,
|
|
|
- animations: [],
|
|
|
- includeCustomExtensions: false
|
|
|
- }, options );
|
|
|
+ this.options = Object.assign( {}, {
|
|
|
+ // default options
|
|
|
+ binary: false,
|
|
|
+ trs: false,
|
|
|
+ onlyVisible: true,
|
|
|
+ truncateDrawRange: true,
|
|
|
+ embedImages: true,
|
|
|
+ maxTextureSize: Infinity,
|
|
|
+ animations: [],
|
|
|
+ includeCustomExtensions: false
|
|
|
+ }, options );
|
|
|
|
|
|
- if ( this.options.animations.length > 0 ) {
|
|
|
+ if ( this.options.animations.length > 0 ) {
|
|
|
|
|
|
- // Only TRS properties, and not matrices, may be targeted by animation.
|
|
|
- this.options.trs = true;
|
|
|
+ // Only TRS properties, and not matrices, may be targeted by animation.
|
|
|
+ this.options.trs = true;
|
|
|
|
|
|
- }
|
|
|
+ }
|
|
|
|
|
|
- this.processInput( input );
|
|
|
+ this.processInput( input );
|
|
|
|
|
|
- var writer = this;
|
|
|
+ const writer = this;
|
|
|
|
|
|
- Promise.all( this.pending ).then( function () {
|
|
|
+ Promise.all( this.pending ).then( function () {
|
|
|
|
|
|
- var buffers = writer.buffers;
|
|
|
- var json = writer.json;
|
|
|
- var options = writer.options;
|
|
|
- var extensionsUsed = writer.extensionsUsed;
|
|
|
+ const buffers = writer.buffers;
|
|
|
+ const json = writer.json;
|
|
|
+ const options = writer.options;
|
|
|
+ const extensionsUsed = writer.extensionsUsed;
|
|
|
|
|
|
- // Merge buffers.
|
|
|
- var blob = new Blob( buffers, { type: 'application/octet-stream' } );
|
|
|
+ // Merge buffers.
|
|
|
+ const blob = new Blob( buffers, { type: 'application/octet-stream' } );
|
|
|
|
|
|
- // Declare extensions.
|
|
|
- var extensionsUsedList = Object.keys( extensionsUsed );
|
|
|
+ // Declare extensions.
|
|
|
+ const extensionsUsedList = Object.keys( extensionsUsed );
|
|
|
|
|
|
- if ( extensionsUsedList.length > 0 ) json.extensionsUsed = extensionsUsedList;
|
|
|
+ if ( extensionsUsedList.length > 0 ) json.extensionsUsed = extensionsUsedList;
|
|
|
|
|
|
- // Update bytelength of the single buffer.
|
|
|
- if ( json.buffers && json.buffers.length > 0 ) json.buffers[ 0 ].byteLength = blob.size;
|
|
|
+ // Update bytelength of the single buffer.
|
|
|
+ if ( json.buffers && json.buffers.length > 0 ) json.buffers[ 0 ].byteLength = blob.size;
|
|
|
|
|
|
- if ( options.binary === true ) {
|
|
|
+ if ( options.binary === true ) {
|
|
|
|
|
|
- // https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#glb-file-format-specification
|
|
|
+ // https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#glb-file-format-specification
|
|
|
|
|
|
- var reader = new window.FileReader();
|
|
|
- reader.readAsArrayBuffer( blob );
|
|
|
- reader.onloadend = function () {
|
|
|
+ const reader = new window.FileReader();
|
|
|
+ reader.readAsArrayBuffer( blob );
|
|
|
+ reader.onloadend = function () {
|
|
|
|
|
|
- // Binary chunk.
|
|
|
- var binaryChunk = getPaddedArrayBuffer( reader.result );
|
|
|
- var binaryChunkPrefix = new DataView( new ArrayBuffer( GLB_CHUNK_PREFIX_BYTES ) );
|
|
|
- binaryChunkPrefix.setUint32( 0, binaryChunk.byteLength, true );
|
|
|
- binaryChunkPrefix.setUint32( 4, GLB_CHUNK_TYPE_BIN, true );
|
|
|
-
|
|
|
- // JSON chunk.
|
|
|
- var jsonChunk = getPaddedArrayBuffer( stringToArrayBuffer( JSON.stringify( json ) ), 0x20 );
|
|
|
- var jsonChunkPrefix = new DataView( new ArrayBuffer( GLB_CHUNK_PREFIX_BYTES ) );
|
|
|
- jsonChunkPrefix.setUint32( 0, jsonChunk.byteLength, true );
|
|
|
- jsonChunkPrefix.setUint32( 4, GLB_CHUNK_TYPE_JSON, true );
|
|
|
-
|
|
|
- // GLB header.
|
|
|
- var header = new ArrayBuffer( GLB_HEADER_BYTES );
|
|
|
- var headerView = new DataView( header );
|
|
|
- headerView.setUint32( 0, GLB_HEADER_MAGIC, true );
|
|
|
- headerView.setUint32( 4, GLB_VERSION, true );
|
|
|
- var totalByteLength = GLB_HEADER_BYTES
|
|
|
- + jsonChunkPrefix.byteLength + jsonChunk.byteLength
|
|
|
- + binaryChunkPrefix.byteLength + binaryChunk.byteLength;
|
|
|
- headerView.setUint32( 8, totalByteLength, true );
|
|
|
-
|
|
|
- var glbBlob = new Blob( [
|
|
|
- header,
|
|
|
- jsonChunkPrefix,
|
|
|
- jsonChunk,
|
|
|
- binaryChunkPrefix,
|
|
|
- binaryChunk
|
|
|
- ], { type: 'application/octet-stream' } );
|
|
|
-
|
|
|
- var glbReader = new window.FileReader();
|
|
|
- glbReader.readAsArrayBuffer( glbBlob );
|
|
|
- glbReader.onloadend = function () {
|
|
|
-
|
|
|
- onDone( glbReader.result );
|
|
|
-
|
|
|
- };
|
|
|
+ // Binary chunk.
|
|
|
+ const binaryChunk = getPaddedArrayBuffer( reader.result );
|
|
|
+ const binaryChunkPrefix = new DataView( new ArrayBuffer( GLB_CHUNK_PREFIX_BYTES ) );
|
|
|
+ binaryChunkPrefix.setUint32( 0, binaryChunk.byteLength, true );
|
|
|
+ binaryChunkPrefix.setUint32( 4, GLB_CHUNK_TYPE_BIN, true );
|
|
|
+
|
|
|
+ // JSON chunk.
|
|
|
+ const jsonChunk = getPaddedArrayBuffer( stringToArrayBuffer( JSON.stringify( json ) ), 0x20 );
|
|
|
+ const jsonChunkPrefix = new DataView( new ArrayBuffer( GLB_CHUNK_PREFIX_BYTES ) );
|
|
|
+ jsonChunkPrefix.setUint32( 0, jsonChunk.byteLength, true );
|
|
|
+ jsonChunkPrefix.setUint32( 4, GLB_CHUNK_TYPE_JSON, true );
|
|
|
+
|
|
|
+ // GLB header.
|
|
|
+ const header = new ArrayBuffer( GLB_HEADER_BYTES );
|
|
|
+ const headerView = new DataView( header );
|
|
|
+ headerView.setUint32( 0, GLB_HEADER_MAGIC, true );
|
|
|
+ headerView.setUint32( 4, GLB_VERSION, true );
|
|
|
+ const totalByteLength = GLB_HEADER_BYTES
|
|
|
+ + jsonChunkPrefix.byteLength + jsonChunk.byteLength
|
|
|
+ + binaryChunkPrefix.byteLength + binaryChunk.byteLength;
|
|
|
+ headerView.setUint32( 8, totalByteLength, true );
|
|
|
+
|
|
|
+ const glbBlob = new Blob( [
|
|
|
+ header,
|
|
|
+ jsonChunkPrefix,
|
|
|
+ jsonChunk,
|
|
|
+ binaryChunkPrefix,
|
|
|
+ binaryChunk
|
|
|
+ ], { type: 'application/octet-stream' } );
|
|
|
+
|
|
|
+ const glbReader = new window.FileReader();
|
|
|
+ glbReader.readAsArrayBuffer( glbBlob );
|
|
|
+ glbReader.onloadend = function () {
|
|
|
+
|
|
|
+ onDone( glbReader.result );
|
|
|
|
|
|
};
|
|
|
|
|
|
- } else {
|
|
|
+ };
|
|
|
|
|
|
- if ( json.buffers && json.buffers.length > 0 ) {
|
|
|
+ } else {
|
|
|
|
|
|
- var reader = new window.FileReader();
|
|
|
- reader.readAsDataURL( blob );
|
|
|
- reader.onloadend = function () {
|
|
|
+ if ( json.buffers && json.buffers.length > 0 ) {
|
|
|
|
|
|
- var base64data = reader.result;
|
|
|
- json.buffers[ 0 ].uri = base64data;
|
|
|
- onDone( json );
|
|
|
+ const reader = new window.FileReader();
|
|
|
+ reader.readAsDataURL( blob );
|
|
|
+ reader.onloadend = function () {
|
|
|
|
|
|
- };
|
|
|
+ const base64data = reader.result;
|
|
|
+ json.buffers[ 0 ].uri = base64data;
|
|
|
+ onDone( json );
|
|
|
|
|
|
- } else {
|
|
|
+ };
|
|
|
|
|
|
- onDone( json );
|
|
|
+ } else {
|
|
|
|
|
|
- }
|
|
|
+ onDone( json );
|
|
|
|
|
|
}
|
|
|
|
|
|
- } );
|
|
|
-
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- /**
|
|
|
- * Serializes a userData.
|
|
|
- *
|
|
|
- * @param {THREE.Object3D|THREE.Material} object
|
|
|
- * @param {Object} objectDef
|
|
|
- */
|
|
|
- serializeUserData: function ( object, objectDef ) {
|
|
|
+ } );
|
|
|
|
|
|
- if ( Object.keys( object.userData ).length === 0 ) return;
|
|
|
+ }
|
|
|
|
|
|
- var options = this.options;
|
|
|
- var extensionsUsed = this.extensionsUsed;
|
|
|
+ /**
|
|
|
+ * Serializes a userData.
|
|
|
+ *
|
|
|
+ * @param {THREE.Object3D|THREE.Material} object
|
|
|
+ * @param {Object} objectDef
|
|
|
+ */
|
|
|
+ serializeUserData( object, objectDef ) {
|
|
|
|
|
|
- try {
|
|
|
+ if ( Object.keys( object.userData ).length === 0 ) return;
|
|
|
|
|
|
- var json = JSON.parse( JSON.stringify( object.userData ) );
|
|
|
+ const options = this.options;
|
|
|
+ const extensionsUsed = this.extensionsUsed;
|
|
|
|
|
|
- if ( options.includeCustomExtensions && json.gltfExtensions ) {
|
|
|
+ try {
|
|
|
|
|
|
- if ( objectDef.extensions === undefined ) objectDef.extensions = {};
|
|
|
+ const json = JSON.parse( JSON.stringify( object.userData ) );
|
|
|
|
|
|
- for ( var extensionName in json.gltfExtensions ) {
|
|
|
+ if ( options.includeCustomExtensions && json.gltfExtensions ) {
|
|
|
|
|
|
- objectDef.extensions[ extensionName ] = json.gltfExtensions[ extensionName ];
|
|
|
- extensionsUsed[ extensionName ] = true;
|
|
|
+ if ( objectDef.extensions === undefined ) objectDef.extensions = {};
|
|
|
|
|
|
- }
|
|
|
+ for ( const extensionName in json.gltfExtensions ) {
|
|
|
|
|
|
- delete json.gltfExtensions;
|
|
|
+ objectDef.extensions[ extensionName ] = json.gltfExtensions[ extensionName ];
|
|
|
+ extensionsUsed[ extensionName ] = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
- if ( Object.keys( json ).length > 0 ) objectDef.extras = json;
|
|
|
+ delete json.gltfExtensions;
|
|
|
|
|
|
- } catch ( error ) {
|
|
|
+ }
|
|
|
|
|
|
- console.warn( 'THREE.GLTFExporter: userData of \'' + object.name + '\' ' +
|
|
|
- 'won\'t be serialized because of JSON.stringify error - ' + error.message );
|
|
|
+ if ( Object.keys( json ).length > 0 ) objectDef.extras = json;
|
|
|
|
|
|
- }
|
|
|
+ } catch ( error ) {
|
|
|
|
|
|
- },
|
|
|
+ console.warn( 'THREE.GLTFExporter: userData of \'' + object.name + '\' ' +
|
|
|
+ 'won\'t be serialized because of JSON.stringify error - ' + error.message );
|
|
|
|
|
|
- /**
|
|
|
- * Assign and return a temporal unique id for an object
|
|
|
- * especially which doesn't have .uuid
|
|
|
- * @param {Object} object
|
|
|
- * @return {Integer}
|
|
|
- */
|
|
|
- getUID: function ( object ) {
|
|
|
+ }
|
|
|
|
|
|
- if ( ! this.uids.has( object ) ) this.uids.set( object, this.uid ++ );
|
|
|
+ }
|
|
|
|
|
|
- return this.uids.get( object );
|
|
|
+ /**
|
|
|
+ * Assign and return a temporal unique id for an object
|
|
|
+ * especially which doesn't have .uuid
|
|
|
+ * @param {Object} object
|
|
|
+ * @return {Integer}
|
|
|
+ */
|
|
|
+ getUID( object ) {
|
|
|
|
|
|
- },
|
|
|
+ if ( ! this.uids.has( object ) ) this.uids.set( object, this.uid ++ );
|
|
|
|
|
|
- /**
|
|
|
- * Checks if normal attribute values are normalized.
|
|
|
- *
|
|
|
- * @param {BufferAttribute} normal
|
|
|
- * @returns {Boolean}
|
|
|
- */
|
|
|
- isNormalizedNormalAttribute: function ( normal ) {
|
|
|
+ return this.uids.get( object );
|
|
|
|
|
|
- var cache = this.cache;
|
|
|
+ }
|
|
|
|
|
|
- if ( cache.attributesNormalized.has( normal ) ) return false;
|
|
|
+ /**
|
|
|
+ * Checks if normal attribute values are normalized.
|
|
|
+ *
|
|
|
+ * @param {BufferAttribute} normal
|
|
|
+ * @returns {Boolean}
|
|
|
+ */
|
|
|
+ isNormalizedNormalAttribute( normal ) {
|
|
|
|
|
|
- var v = new Vector3();
|
|
|
+ const cache = this.cache;
|
|
|
|
|
|
- for ( var i = 0, il = normal.count; i < il; i ++ ) {
|
|
|
+ if ( cache.attributesNormalized.has( normal ) ) return false;
|
|
|
|
|
|
- // 0.0005 is from glTF-validator
|
|
|
- if ( Math.abs( v.fromBufferAttribute( normal, i ).length() - 1.0 ) > 0.0005 ) return false;
|
|
|
+ const v = new Vector3();
|
|
|
|
|
|
- }
|
|
|
+ for ( let i = 0, il = normal.count; i < il; i ++ ) {
|
|
|
|
|
|
- return true;
|
|
|
+ // 0.0005 is from glTF-validator
|
|
|
+ if ( Math.abs( v.fromBufferAttribute( normal, i ).length() - 1.0 ) > 0.0005 ) return false;
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- /**
|
|
|
- * Creates normalized normal buffer attribute.
|
|
|
- *
|
|
|
- * @param {BufferAttribute} normal
|
|
|
- * @returns {BufferAttribute}
|
|
|
- *
|
|
|
- */
|
|
|
- createNormalizedNormalAttribute: function ( normal ) {
|
|
|
+ return true;
|
|
|
|
|
|
- var cache = this.cache;
|
|
|
+ }
|
|
|
|
|
|
- if ( cache.attributesNormalized.has( normal ) ) return cache.attributesNormalized.get( normal );
|
|
|
+ /**
|
|
|
+ * Creates normalized normal buffer attribute.
|
|
|
+ *
|
|
|
+ * @param {BufferAttribute} normal
|
|
|
+ * @returns {BufferAttribute}
|
|
|
+ *
|
|
|
+ */
|
|
|
+ createNormalizedNormalAttribute( normal ) {
|
|
|
|
|
|
- var attribute = normal.clone();
|
|
|
- var v = new Vector3();
|
|
|
+ const cache = this.cache;
|
|
|
|
|
|
- for ( var i = 0, il = attribute.count; i < il; i ++ ) {
|
|
|
+ if ( cache.attributesNormalized.has( normal ) ) return cache.attributesNormalized.get( normal );
|
|
|
|
|
|
- v.fromBufferAttribute( attribute, i );
|
|
|
+ const attribute = normal.clone();
|
|
|
+ const v = new Vector3();
|
|
|
|
|
|
- if ( v.x === 0 && v.y === 0 && v.z === 0 ) {
|
|
|
+ for ( let i = 0, il = attribute.count; i < il; i ++ ) {
|
|
|
|
|
|
- // if values can't be normalized set (1, 0, 0)
|
|
|
- v.setX( 1.0 );
|
|
|
+ v.fromBufferAttribute( attribute, i );
|
|
|
|
|
|
- } else {
|
|
|
+ if ( v.x === 0 && v.y === 0 && v.z === 0 ) {
|
|
|
|
|
|
- v.normalize();
|
|
|
+ // if values can't be normalized set (1, 0, 0)
|
|
|
+ v.setX( 1.0 );
|
|
|
|
|
|
- }
|
|
|
+ } else {
|
|
|
|
|
|
- attribute.setXYZ( i, v.x, v.y, v.z );
|
|
|
+ v.normalize();
|
|
|
|
|
|
}
|
|
|
|
|
|
- cache.attributesNormalized.set( normal, attribute );
|
|
|
+ attribute.setXYZ( i, v.x, v.y, v.z );
|
|
|
|
|
|
- return attribute;
|
|
|
+ }
|
|
|
|
|
|
- },
|
|
|
+ cache.attributesNormalized.set( normal, attribute );
|
|
|
|
|
|
- /**
|
|
|
- * Applies a texture transform, if present, to the map definition. Requires
|
|
|
- * the KHR_texture_transform extension.
|
|
|
- *
|
|
|
- * @param {Object} mapDef
|
|
|
- * @param {THREE.Texture} texture
|
|
|
- */
|
|
|
- applyTextureTransform: function ( mapDef, texture ) {
|
|
|
+ return attribute;
|
|
|
|
|
|
- var didTransform = false;
|
|
|
- var transformDef = {};
|
|
|
+ }
|
|
|
|
|
|
- if ( texture.offset.x !== 0 || texture.offset.y !== 0 ) {
|
|
|
+ /**
|
|
|
+ * Applies a texture transform, if present, to the map definition. Requires
|
|
|
+ * the KHR_texture_transform extension.
|
|
|
+ *
|
|
|
+ * @param {Object} mapDef
|
|
|
+ * @param {THREE.Texture} texture
|
|
|
+ */
|
|
|
+ applyTextureTransform( mapDef, texture ) {
|
|
|
|
|
|
- transformDef.offset = texture.offset.toArray();
|
|
|
- didTransform = true;
|
|
|
+ let didTransform = false;
|
|
|
+ const transformDef = {};
|
|
|
|
|
|
- }
|
|
|
+ if ( texture.offset.x !== 0 || texture.offset.y !== 0 ) {
|
|
|
|
|
|
- if ( texture.rotation !== 0 ) {
|
|
|
+ transformDef.offset = texture.offset.toArray();
|
|
|
+ didTransform = true;
|
|
|
|
|
|
- transformDef.rotation = texture.rotation;
|
|
|
- didTransform = true;
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
+ if ( texture.rotation !== 0 ) {
|
|
|
|
|
|
- if ( texture.repeat.x !== 1 || texture.repeat.y !== 1 ) {
|
|
|
+ transformDef.rotation = texture.rotation;
|
|
|
+ didTransform = true;
|
|
|
|
|
|
- transformDef.scale = texture.repeat.toArray();
|
|
|
- didTransform = true;
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
+ if ( texture.repeat.x !== 1 || texture.repeat.y !== 1 ) {
|
|
|
|
|
|
- if ( didTransform ) {
|
|
|
+ transformDef.scale = texture.repeat.toArray();
|
|
|
+ didTransform = true;
|
|
|
|
|
|
- mapDef.extensions = mapDef.extensions || {};
|
|
|
- mapDef.extensions[ 'KHR_texture_transform' ] = transformDef;
|
|
|
- this.extensionsUsed[ 'KHR_texture_transform' ] = true;
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
+ if ( didTransform ) {
|
|
|
|
|
|
- },
|
|
|
+ mapDef.extensions = mapDef.extensions || {};
|
|
|
+ mapDef.extensions[ 'KHR_texture_transform' ] = transformDef;
|
|
|
+ this.extensionsUsed[ 'KHR_texture_transform' ] = true;
|
|
|
|
|
|
- /**
|
|
|
- * Process a buffer to append to the default one.
|
|
|
- * @param {ArrayBuffer} buffer
|
|
|
- * @return {Integer}
|
|
|
- */
|
|
|
- processBuffer: function ( buffer ) {
|
|
|
+ }
|
|
|
|
|
|
- var json = this.json;
|
|
|
- var buffers = this.buffers;
|
|
|
+ }
|
|
|
|
|
|
- if ( ! json.buffers ) json.buffers = [ { byteLength: 0 } ];
|
|
|
+ /**
|
|
|
+ * Process a buffer to append to the default one.
|
|
|
+ * @param {ArrayBuffer} buffer
|
|
|
+ * @return {Integer}
|
|
|
+ */
|
|
|
+ processBuffer( buffer ) {
|
|
|
|
|
|
- // All buffers are merged before export.
|
|
|
- buffers.push( buffer );
|
|
|
+ const json = this.json;
|
|
|
+ const buffers = this.buffers;
|
|
|
|
|
|
- return 0;
|
|
|
+ if ( ! json.buffers ) json.buffers = [ { byteLength: 0 } ];
|
|
|
|
|
|
- },
|
|
|
+ // All buffers are merged before export.
|
|
|
+ buffers.push( buffer );
|
|
|
|
|
|
- /**
|
|
|
- * Process and generate a BufferView
|
|
|
- * @param {BufferAttribute} attribute
|
|
|
- * @param {number} componentType
|
|
|
- * @param {number} start
|
|
|
- * @param {number} count
|
|
|
- * @param {number} target (Optional) Target usage of the BufferView
|
|
|
- * @return {Object}
|
|
|
- */
|
|
|
- processBufferView: function ( attribute, componentType, start, count, target ) {
|
|
|
+ return 0;
|
|
|
|
|
|
- var json = this.json;
|
|
|
+ }
|
|
|
|
|
|
- if ( ! json.bufferViews ) json.bufferViews = [];
|
|
|
+ /**
|
|
|
+ * Process and generate a BufferView
|
|
|
+ * @param {BufferAttribute} attribute
|
|
|
+ * @param {number} componentType
|
|
|
+ * @param {number} start
|
|
|
+ * @param {number} count
|
|
|
+ * @param {number} target (Optional) Target usage of the BufferView
|
|
|
+ * @return {Object}
|
|
|
+ */
|
|
|
+ processBufferView( attribute, componentType, start, count, target ) {
|
|
|
|
|
|
- // Create a new dataview and dump the attribute's array into it
|
|
|
+ const json = this.json;
|
|
|
|
|
|
- var componentSize;
|
|
|
+ if ( ! json.bufferViews ) json.bufferViews = [];
|
|
|
|
|
|
- if ( componentType === WEBGL_CONSTANTS.UNSIGNED_BYTE ) {
|
|
|
+ // Create a new dataview and dump the attribute's array into it
|
|
|
|
|
|
- componentSize = 1;
|
|
|
+ let componentSize;
|
|
|
|
|
|
- } else if ( componentType === WEBGL_CONSTANTS.UNSIGNED_SHORT ) {
|
|
|
+ if ( componentType === WEBGL_CONSTANTS.UNSIGNED_BYTE ) {
|
|
|
|
|
|
- componentSize = 2;
|
|
|
+ componentSize = 1;
|
|
|
|
|
|
- } else {
|
|
|
+ } else if ( componentType === WEBGL_CONSTANTS.UNSIGNED_SHORT ) {
|
|
|
|
|
|
- componentSize = 4;
|
|
|
+ componentSize = 2;
|
|
|
|
|
|
- }
|
|
|
+ } else {
|
|
|
|
|
|
- var byteLength = getPaddedBufferSize( count * attribute.itemSize * componentSize );
|
|
|
- var dataView = new DataView( new ArrayBuffer( byteLength ) );
|
|
|
- var offset = 0;
|
|
|
+ componentSize = 4;
|
|
|
|
|
|
- for ( var i = start; i < start + count; i ++ ) {
|
|
|
+ }
|
|
|
|
|
|
- for ( var a = 0; a < attribute.itemSize; a ++ ) {
|
|
|
+ const byteLength = getPaddedBufferSize( count * attribute.itemSize * componentSize );
|
|
|
+ const dataView = new DataView( new ArrayBuffer( byteLength ) );
|
|
|
+ let offset = 0;
|
|
|
|
|
|
- var value;
|
|
|
+ for ( let i = start; i < start + count; i ++ ) {
|
|
|
|
|
|
- if ( attribute.itemSize > 4 ) {
|
|
|
+ for ( let a = 0; a < attribute.itemSize; a ++ ) {
|
|
|
|
|
|
- // no support for interleaved data for itemSize > 4
|
|
|
+ let value;
|
|
|
|
|
|
- value = attribute.array[ i * attribute.itemSize + a ];
|
|
|
+ if ( attribute.itemSize > 4 ) {
|
|
|
|
|
|
- } else {
|
|
|
+ // no support for interleaved data for itemSize > 4
|
|
|
|
|
|
- if ( a === 0 ) value = attribute.getX( i );
|
|
|
- else if ( a === 1 ) value = attribute.getY( i );
|
|
|
- else if ( a === 2 ) value = attribute.getZ( i );
|
|
|
- else if ( a === 3 ) value = attribute.getW( i );
|
|
|
+ value = attribute.array[ i * attribute.itemSize + a ];
|
|
|
|
|
|
- }
|
|
|
+ } else {
|
|
|
|
|
|
- if ( componentType === WEBGL_CONSTANTS.FLOAT ) {
|
|
|
+ if ( a === 0 ) value = attribute.getX( i );
|
|
|
+ else if ( a === 1 ) value = attribute.getY( i );
|
|
|
+ else if ( a === 2 ) value = attribute.getZ( i );
|
|
|
+ else if ( a === 3 ) value = attribute.getW( i );
|
|
|
|
|
|
- dataView.setFloat32( offset, value, true );
|
|
|
+ }
|
|
|
|
|
|
- } else if ( componentType === WEBGL_CONSTANTS.UNSIGNED_INT ) {
|
|
|
+ if ( componentType === WEBGL_CONSTANTS.FLOAT ) {
|
|
|
|
|
|
- dataView.setUint32( offset, value, true );
|
|
|
+ dataView.setFloat32( offset, value, true );
|
|
|
|
|
|
- } else if ( componentType === WEBGL_CONSTANTS.UNSIGNED_SHORT ) {
|
|
|
+ } else if ( componentType === WEBGL_CONSTANTS.UNSIGNED_INT ) {
|
|
|
|
|
|
- dataView.setUint16( offset, value, true );
|
|
|
+ dataView.setUint32( offset, value, true );
|
|
|
|
|
|
- } else if ( componentType === WEBGL_CONSTANTS.UNSIGNED_BYTE ) {
|
|
|
+ } else if ( componentType === WEBGL_CONSTANTS.UNSIGNED_SHORT ) {
|
|
|
|
|
|
- dataView.setUint8( offset, value );
|
|
|
+ dataView.setUint16( offset, value, true );
|
|
|
|
|
|
- }
|
|
|
+ } else if ( componentType === WEBGL_CONSTANTS.UNSIGNED_BYTE ) {
|
|
|
|
|
|
- offset += componentSize;
|
|
|
+ dataView.setUint8( offset, value );
|
|
|
|
|
|
}
|
|
|
|
|
|
+ offset += componentSize;
|
|
|
+
|
|
|
}
|
|
|
|
|
|
- var bufferViewDef = {
|
|
|
+ }
|
|
|
|
|
|
- buffer: this.processBuffer( dataView.buffer ),
|
|
|
- byteOffset: this.byteOffset,
|
|
|
- byteLength: byteLength
|
|
|
+ const bufferViewDef = {
|
|
|
|
|
|
- };
|
|
|
+ buffer: this.processBuffer( dataView.buffer ),
|
|
|
+ byteOffset: this.byteOffset,
|
|
|
+ byteLength: byteLength
|
|
|
|
|
|
- if ( target !== undefined ) bufferViewDef.target = target;
|
|
|
-
|
|
|
- if ( target === WEBGL_CONSTANTS.ARRAY_BUFFER ) {
|
|
|
+ };
|
|
|
|
|
|
- // Only define byteStride for vertex attributes.
|
|
|
- bufferViewDef.byteStride = attribute.itemSize * componentSize;
|
|
|
+ if ( target !== undefined ) bufferViewDef.target = target;
|
|
|
|
|
|
- }
|
|
|
+ if ( target === WEBGL_CONSTANTS.ARRAY_BUFFER ) {
|
|
|
|
|
|
- this.byteOffset += byteLength;
|
|
|
+ // Only define byteStride for vertex attributes.
|
|
|
+ bufferViewDef.byteStride = attribute.itemSize * componentSize;
|
|
|
|
|
|
- json.bufferViews.push( bufferViewDef );
|
|
|
+ }
|
|
|
|
|
|
- // @TODO Merge bufferViews where possible.
|
|
|
- var output = {
|
|
|
+ this.byteOffset += byteLength;
|
|
|
|
|
|
- id: json.bufferViews.length - 1,
|
|
|
- byteLength: 0
|
|
|
+ json.bufferViews.push( bufferViewDef );
|
|
|
|
|
|
- };
|
|
|
+ // @TODO Merge bufferViews where possible.
|
|
|
+ const output = {
|
|
|
|
|
|
- return output;
|
|
|
+ id: json.bufferViews.length - 1,
|
|
|
+ byteLength: 0
|
|
|
|
|
|
- },
|
|
|
+ };
|
|
|
|
|
|
- /**
|
|
|
- * Process and generate a BufferView from an image Blob.
|
|
|
- * @param {Blob} blob
|
|
|
- * @return {Promise<Integer>}
|
|
|
- */
|
|
|
- processBufferViewImage: function ( blob ) {
|
|
|
+ return output;
|
|
|
|
|
|
- var writer = this;
|
|
|
- var json = writer.json;
|
|
|
+ }
|
|
|
|
|
|
- if ( ! json.bufferViews ) json.bufferViews = [];
|
|
|
+ /**
|
|
|
+ * Process and generate a BufferView from an image Blob.
|
|
|
+ * @param {Blob} blob
|
|
|
+ * @return {Promise<Integer>}
|
|
|
+ */
|
|
|
+ processBufferViewImage( blob ) {
|
|
|
|
|
|
- return new Promise( function ( resolve ) {
|
|
|
+ const writer = this;
|
|
|
+ const json = writer.json;
|
|
|
|
|
|
- var reader = new window.FileReader();
|
|
|
- reader.readAsArrayBuffer( blob );
|
|
|
- reader.onloadend = function () {
|
|
|
+ if ( ! json.bufferViews ) json.bufferViews = [];
|
|
|
|
|
|
- var buffer = getPaddedArrayBuffer( reader.result );
|
|
|
+ return new Promise( function ( resolve ) {
|
|
|
|
|
|
- var bufferViewDef = {
|
|
|
- buffer: writer.processBuffer( buffer ),
|
|
|
- byteOffset: writer.byteOffset,
|
|
|
- byteLength: buffer.byteLength
|
|
|
- };
|
|
|
+ const reader = new window.FileReader();
|
|
|
+ reader.readAsArrayBuffer( blob );
|
|
|
+ reader.onloadend = function () {
|
|
|
|
|
|
- writer.byteOffset += buffer.byteLength;
|
|
|
- resolve( json.bufferViews.push( bufferViewDef ) - 1 );
|
|
|
+ const buffer = getPaddedArrayBuffer( reader.result );
|
|
|
|
|
|
+ const bufferViewDef = {
|
|
|
+ buffer: writer.processBuffer( buffer ),
|
|
|
+ byteOffset: writer.byteOffset,
|
|
|
+ byteLength: buffer.byteLength
|
|
|
};
|
|
|
|
|
|
- } );
|
|
|
+ writer.byteOffset += buffer.byteLength;
|
|
|
+ resolve( json.bufferViews.push( bufferViewDef ) - 1 );
|
|
|
|
|
|
- },
|
|
|
+ };
|
|
|
|
|
|
- /**
|
|
|
- * Process attribute to generate an accessor
|
|
|
- * @param {BufferAttribute} attribute Attribute to process
|
|
|
- * @param {THREE.BufferGeometry} geometry (Optional) Geometry used for truncated draw range
|
|
|
- * @param {Integer} start (Optional)
|
|
|
- * @param {Integer} count (Optional)
|
|
|
- * @return {Integer|null} Index of the processed accessor on the "accessors" array
|
|
|
- */
|
|
|
- processAccessor: function ( attribute, geometry, start, count ) {
|
|
|
+ } );
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
- var options = this.options;
|
|
|
- var json = this.json;
|
|
|
+ /**
|
|
|
+ * Process attribute to generate an accessor
|
|
|
+ * @param {BufferAttribute} attribute Attribute to process
|
|
|
+ * @param {THREE.BufferGeometry} geometry (Optional) Geometry used for truncated draw range
|
|
|
+ * @param {Integer} start (Optional)
|
|
|
+ * @param {Integer} count (Optional)
|
|
|
+ * @return {Integer|null} Index of the processed accessor on the "accessors" array
|
|
|
+ */
|
|
|
+ processAccessor( attribute, geometry, start, count ) {
|
|
|
|
|
|
- var types = {
|
|
|
+ const options = this.options;
|
|
|
+ const json = this.json;
|
|
|
|
|
|
- 1: 'SCALAR',
|
|
|
- 2: 'VEC2',
|
|
|
- 3: 'VEC3',
|
|
|
- 4: 'VEC4',
|
|
|
- 16: 'MAT4'
|
|
|
+ const types = {
|
|
|
|
|
|
- };
|
|
|
+ 1: 'SCALAR',
|
|
|
+ 2: 'VEC2',
|
|
|
+ 3: 'VEC3',
|
|
|
+ 4: 'VEC4',
|
|
|
+ 16: 'MAT4'
|
|
|
|
|
|
- var componentType;
|
|
|
+ };
|
|
|
|
|
|
- // Detect the component type of the attribute array (float, uint or ushort)
|
|
|
- if ( attribute.array.constructor === Float32Array ) {
|
|
|
+ let componentType;
|
|
|
|
|
|
- componentType = WEBGL_CONSTANTS.FLOAT;
|
|
|
+ // Detect the component type of the attribute array (float, uint or ushort)
|
|
|
+ if ( attribute.array.constructor === Float32Array ) {
|
|
|
|
|
|
- } else if ( attribute.array.constructor === Uint32Array ) {
|
|
|
+ componentType = WEBGL_CONSTANTS.FLOAT;
|
|
|
|
|
|
- componentType = WEBGL_CONSTANTS.UNSIGNED_INT;
|
|
|
+ } else if ( attribute.array.constructor === Uint32Array ) {
|
|
|
|
|
|
- } else if ( attribute.array.constructor === Uint16Array ) {
|
|
|
+ componentType = WEBGL_CONSTANTS.UNSIGNED_INT;
|
|
|
|
|
|
- componentType = WEBGL_CONSTANTS.UNSIGNED_SHORT;
|
|
|
+ } else if ( attribute.array.constructor === Uint16Array ) {
|
|
|
|
|
|
- } else if ( attribute.array.constructor === Uint8Array ) {
|
|
|
+ componentType = WEBGL_CONSTANTS.UNSIGNED_SHORT;
|
|
|
|
|
|
- componentType = WEBGL_CONSTANTS.UNSIGNED_BYTE;
|
|
|
+ } else if ( attribute.array.constructor === Uint8Array ) {
|
|
|
|
|
|
- } else {
|
|
|
+ componentType = WEBGL_CONSTANTS.UNSIGNED_BYTE;
|
|
|
|
|
|
- throw new Error( 'THREE.GLTFExporter: Unsupported bufferAttribute component type.' );
|
|
|
+ } else {
|
|
|
|
|
|
- }
|
|
|
+ throw new Error( 'THREE.GLTFExporter: Unsupported bufferAttribute component type.' );
|
|
|
|
|
|
- if ( start === undefined ) start = 0;
|
|
|
- if ( count === undefined ) count = attribute.count;
|
|
|
+ }
|
|
|
|
|
|
- // @TODO Indexed buffer geometry with drawRange not supported yet
|
|
|
- if ( options.truncateDrawRange && geometry !== undefined && geometry.index === null ) {
|
|
|
+ if ( start === undefined ) start = 0;
|
|
|
+ if ( count === undefined ) count = attribute.count;
|
|
|
|
|
|
- var end = start + count;
|
|
|
- var end2 = geometry.drawRange.count === Infinity
|
|
|
- ? attribute.count
|
|
|
- : geometry.drawRange.start + geometry.drawRange.count;
|
|
|
+ // @TODO Indexed buffer geometry with drawRange not supported yet
|
|
|
+ if ( options.truncateDrawRange && geometry !== undefined && geometry.index === null ) {
|
|
|
|
|
|
- start = Math.max( start, geometry.drawRange.start );
|
|
|
- count = Math.min( end, end2 ) - start;
|
|
|
+ const end = start + count;
|
|
|
+ const end2 = geometry.drawRange.count === Infinity
|
|
|
+ ? attribute.count
|
|
|
+ : geometry.drawRange.start + geometry.drawRange.count;
|
|
|
|
|
|
- if ( count < 0 ) count = 0;
|
|
|
+ start = Math.max( start, geometry.drawRange.start );
|
|
|
+ count = Math.min( end, end2 ) - start;
|
|
|
|
|
|
- }
|
|
|
+ if ( count < 0 ) count = 0;
|
|
|
|
|
|
- // Skip creating an accessor if the attribute doesn't have data to export
|
|
|
- if ( count === 0 ) return null;
|
|
|
+ }
|
|
|
|
|
|
- var minMax = getMinMax( attribute, start, count );
|
|
|
- var bufferViewTarget;
|
|
|
+ // Skip creating an accessor if the attribute doesn't have data to export
|
|
|
+ if ( count === 0 ) return null;
|
|
|
|
|
|
- // If geometry isn't provided, don't infer the target usage of the bufferView. For
|
|
|
- // animation samplers, target must not be set.
|
|
|
- if ( geometry !== undefined ) {
|
|
|
+ const minMax = getMinMax( attribute, start, count );
|
|
|
+ let bufferViewTarget;
|
|
|
|
|
|
- bufferViewTarget = attribute === geometry.index ? WEBGL_CONSTANTS.ELEMENT_ARRAY_BUFFER : WEBGL_CONSTANTS.ARRAY_BUFFER;
|
|
|
+ // If geometry isn't provided, don't infer the target usage of the bufferView. For
|
|
|
+ // animation samplers, target must not be set.
|
|
|
+ if ( geometry !== undefined ) {
|
|
|
|
|
|
- }
|
|
|
+ bufferViewTarget = attribute === geometry.index ? WEBGL_CONSTANTS.ELEMENT_ARRAY_BUFFER : WEBGL_CONSTANTS.ARRAY_BUFFER;
|
|
|
|
|
|
- var bufferView = this.processBufferView( attribute, componentType, start, count, bufferViewTarget );
|
|
|
+ }
|
|
|
|
|
|
- var accessorDef = {
|
|
|
+ const bufferView = this.processBufferView( attribute, componentType, start, count, bufferViewTarget );
|
|
|
|
|
|
- bufferView: bufferView.id,
|
|
|
- byteOffset: bufferView.byteOffset,
|
|
|
- componentType: componentType,
|
|
|
- count: count,
|
|
|
- max: minMax.max,
|
|
|
- min: minMax.min,
|
|
|
- type: types[ attribute.itemSize ]
|
|
|
+ const accessorDef = {
|
|
|
|
|
|
- };
|
|
|
+ bufferView: bufferView.id,
|
|
|
+ byteOffset: bufferView.byteOffset,
|
|
|
+ componentType: componentType,
|
|
|
+ count: count,
|
|
|
+ max: minMax.max,
|
|
|
+ min: minMax.min,
|
|
|
+ type: types[ attribute.itemSize ]
|
|
|
|
|
|
- if ( attribute.normalized === true ) accessorDef.normalized = true;
|
|
|
- if ( ! json.accessors ) json.accessors = [];
|
|
|
+ };
|
|
|
|
|
|
- return json.accessors.push( accessorDef ) - 1;
|
|
|
+ if ( attribute.normalized === true ) accessorDef.normalized = true;
|
|
|
+ if ( ! json.accessors ) json.accessors = [];
|
|
|
|
|
|
- },
|
|
|
+ return json.accessors.push( accessorDef ) - 1;
|
|
|
|
|
|
- /**
|
|
|
- * Process image
|
|
|
- * @param {Image} image to process
|
|
|
- * @param {Integer} format of the image (e.g. RGBFormat, RGBAFormat etc)
|
|
|
- * @param {Boolean} flipY before writing out the image
|
|
|
- * @return {Integer} Index of the processed texture in the "images" array
|
|
|
- */
|
|
|
- processImage: function ( image, format, flipY ) {
|
|
|
+ }
|
|
|
|
|
|
- var writer = this;
|
|
|
- var cache = writer.cache;
|
|
|
- var json = writer.json;
|
|
|
- var options = writer.options;
|
|
|
- var pending = writer.pending;
|
|
|
+ /**
|
|
|
+ * Process image
|
|
|
+ * @param {Image} image to process
|
|
|
+ * @param {Integer} format of the image (e.g. RGBFormat, RGBAFormat etc)
|
|
|
+ * @param {Boolean} flipY before writing out the image
|
|
|
+ * @return {Integer} Index of the processed texture in the "images" array
|
|
|
+ */
|
|
|
+ processImage( image, format, flipY ) {
|
|
|
|
|
|
- if ( ! cache.images.has( image ) ) cache.images.set( image, {} );
|
|
|
+ const writer = this;
|
|
|
+ const cache = writer.cache;
|
|
|
+ const json = writer.json;
|
|
|
+ const options = writer.options;
|
|
|
+ const pending = writer.pending;
|
|
|
|
|
|
- var cachedImages = cache.images.get( image );
|
|
|
- var mimeType = format === RGBAFormat ? 'image/png' : 'image/jpeg';
|
|
|
- var key = mimeType + ':flipY/' + flipY.toString();
|
|
|
+ if ( ! cache.images.has( image ) ) cache.images.set( image, {} );
|
|
|
|
|
|
- if ( cachedImages[ key ] !== undefined ) return cachedImages[ key ];
|
|
|
+ const cachedImages = cache.images.get( image );
|
|
|
+ const mimeType = format === RGBAFormat ? 'image/png' : 'image/jpeg';
|
|
|
+ const key = mimeType + ':flipY/' + flipY.toString();
|
|
|
|
|
|
- if ( ! json.images ) json.images = [];
|
|
|
+ if ( cachedImages[ key ] !== undefined ) return cachedImages[ key ];
|
|
|
|
|
|
- var imageDef = { mimeType: mimeType };
|
|
|
+ if ( ! json.images ) json.images = [];
|
|
|
|
|
|
- if ( options.embedImages ) {
|
|
|
+ const imageDef = { mimeType: mimeType };
|
|
|
|
|
|
- var canvas = cachedCanvas = cachedCanvas || document.createElement( 'canvas' );
|
|
|
+ if ( options.embedImages ) {
|
|
|
|
|
|
- canvas.width = Math.min( image.width, options.maxTextureSize );
|
|
|
- canvas.height = Math.min( image.height, options.maxTextureSize );
|
|
|
+ const canvas = cachedCanvas = cachedCanvas || document.createElement( 'canvas' );
|
|
|
|
|
|
- var ctx = canvas.getContext( '2d' );
|
|
|
+ canvas.width = Math.min( image.width, options.maxTextureSize );
|
|
|
+ canvas.height = Math.min( image.height, options.maxTextureSize );
|
|
|
|
|
|
- if ( flipY === true ) {
|
|
|
+ const ctx = canvas.getContext( '2d' );
|
|
|
|
|
|
- ctx.translate( 0, canvas.height );
|
|
|
- ctx.scale( 1, - 1 );
|
|
|
+ if ( flipY === true ) {
|
|
|
|
|
|
- }
|
|
|
+ ctx.translate( 0, canvas.height );
|
|
|
+ ctx.scale( 1, - 1 );
|
|
|
|
|
|
- if ( ( typeof HTMLImageElement !== 'undefined' && image instanceof HTMLImageElement ) ||
|
|
|
- ( typeof HTMLCanvasElement !== 'undefined' && image instanceof HTMLCanvasElement ) ||
|
|
|
- ( typeof OffscreenCanvas !== 'undefined' && image instanceof OffscreenCanvas ) ||
|
|
|
- ( typeof ImageBitmap !== 'undefined' && image instanceof ImageBitmap ) ) {
|
|
|
+ }
|
|
|
|
|
|
- ctx.drawImage( image, 0, 0, canvas.width, canvas.height );
|
|
|
+ if ( ( typeof HTMLImageElement !== 'undefined' && image instanceof HTMLImageElement ) ||
|
|
|
+ ( typeof HTMLCanvasElement !== 'undefined' && image instanceof HTMLCanvasElement ) ||
|
|
|
+ ( typeof OffscreenCanvas !== 'undefined' && image instanceof OffscreenCanvas ) ||
|
|
|
+ ( typeof ImageBitmap !== 'undefined' && image instanceof ImageBitmap ) ) {
|
|
|
|
|
|
- } else {
|
|
|
+ ctx.drawImage( image, 0, 0, canvas.width, canvas.height );
|
|
|
|
|
|
- if ( format !== RGBAFormat && format !== RGBFormat ) {
|
|
|
+ } else {
|
|
|
|
|
|
- console.error( 'GLTFExporter: Only RGB and RGBA formats are supported.' );
|
|
|
+ if ( format !== RGBAFormat && format !== RGBFormat ) {
|
|
|
|
|
|
- }
|
|
|
+ console.error( 'GLTFExporter: Only RGB and RGBA formats are supported.' );
|
|
|
|
|
|
- if ( image.width > options.maxTextureSize || image.height > options.maxTextureSize ) {
|
|
|
+ }
|
|
|
|
|
|
- console.warn( 'GLTFExporter: Image size is bigger than maxTextureSize', image );
|
|
|
+ if ( image.width > options.maxTextureSize || image.height > options.maxTextureSize ) {
|
|
|
|
|
|
- }
|
|
|
+ console.warn( 'GLTFExporter: Image size is bigger than maxTextureSize', image );
|
|
|
|
|
|
- var data = image.data;
|
|
|
+ }
|
|
|
|
|
|
- if ( format === RGBFormat ) {
|
|
|
+ let data = image.data;
|
|
|
|
|
|
- data = new Uint8ClampedArray( image.height * image.width * 4 );
|
|
|
+ if ( format === RGBFormat ) {
|
|
|
|
|
|
- for ( var i = 0, j = 0; i < data.length; i += 4, j += 3 ) {
|
|
|
+ data = new Uint8ClampedArray( image.height * image.width * 4 );
|
|
|
|
|
|
- data[ i + 0 ] = image.data[ j + 0 ];
|
|
|
- data[ i + 1 ] = image.data[ j + 1 ];
|
|
|
- data[ i + 2 ] = image.data[ j + 2 ];
|
|
|
- data[ i + 3 ] = 255;
|
|
|
+ for ( let i = 0, j = 0; i < data.length; i += 4, j += 3 ) {
|
|
|
|
|
|
- }
|
|
|
+ data[ i + 0 ] = image.data[ j + 0 ];
|
|
|
+ data[ i + 1 ] = image.data[ j + 1 ];
|
|
|
+ data[ i + 2 ] = image.data[ j + 2 ];
|
|
|
+ data[ i + 3 ] = 255;
|
|
|
|
|
|
}
|
|
|
|
|
|
- ctx.putImageData( new ImageData( data, image.width, image.height ), 0, 0 );
|
|
|
-
|
|
|
}
|
|
|
|
|
|
- if ( options.binary === true ) {
|
|
|
-
|
|
|
- pending.push( new Promise( function ( resolve ) {
|
|
|
+ ctx.putImageData( new ImageData( data, image.width, image.height ), 0, 0 );
|
|
|
|
|
|
- canvas.toBlob( function ( blob ) {
|
|
|
+ }
|
|
|
|
|
|
- writer.processBufferViewImage( blob ).then( function ( bufferViewIndex ) {
|
|
|
+ if ( options.binary === true ) {
|
|
|
|
|
|
- imageDef.bufferView = bufferViewIndex;
|
|
|
- resolve();
|
|
|
+ pending.push( new Promise( function ( resolve ) {
|
|
|
|
|
|
- } );
|
|
|
+ canvas.toBlob( function ( blob ) {
|
|
|
|
|
|
- }, mimeType );
|
|
|
+ writer.processBufferViewImage( blob ).then( function ( bufferViewIndex ) {
|
|
|
|
|
|
- } ) );
|
|
|
+ imageDef.bufferView = bufferViewIndex;
|
|
|
+ resolve();
|
|
|
|
|
|
- } else {
|
|
|
+ } );
|
|
|
|
|
|
- imageDef.uri = canvas.toDataURL( mimeType );
|
|
|
+ }, mimeType );
|
|
|
|
|
|
- }
|
|
|
+ } ) );
|
|
|
|
|
|
} else {
|
|
|
|
|
|
- imageDef.uri = image.src;
|
|
|
+ imageDef.uri = canvas.toDataURL( mimeType );
|
|
|
|
|
|
}
|
|
|
|
|
|
- var index = json.images.push( imageDef ) - 1;
|
|
|
- cachedImages[ key ] = index;
|
|
|
- return index;
|
|
|
+ } else {
|
|
|
|
|
|
- },
|
|
|
+ imageDef.uri = image.src;
|
|
|
|
|
|
- /**
|
|
|
- * Process sampler
|
|
|
- * @param {Texture} map Texture to process
|
|
|
- * @return {Integer} Index of the processed texture in the "samplers" array
|
|
|
- */
|
|
|
- processSampler: function ( map ) {
|
|
|
-
|
|
|
- var json = this.json;
|
|
|
+ }
|
|
|
|
|
|
- if ( ! json.samplers ) json.samplers = [];
|
|
|
+ const index = json.images.push( imageDef ) - 1;
|
|
|
+ cachedImages[ key ] = index;
|
|
|
+ return index;
|
|
|
|
|
|
- var samplerDef = {
|
|
|
- magFilter: THREE_TO_WEBGL[ map.magFilter ],
|
|
|
- minFilter: THREE_TO_WEBGL[ map.minFilter ],
|
|
|
- wrapS: THREE_TO_WEBGL[ map.wrapS ],
|
|
|
- wrapT: THREE_TO_WEBGL[ map.wrapT ]
|
|
|
- };
|
|
|
+ }
|
|
|
|
|
|
- return json.samplers.push( samplerDef ) - 1;
|
|
|
+ /**
|
|
|
+ * Process sampler
|
|
|
+ * @param {Texture} map Texture to process
|
|
|
+ * @return {Integer} Index of the processed texture in the "samplers" array
|
|
|
+ */
|
|
|
+ processSampler( map ) {
|
|
|
|
|
|
- },
|
|
|
+ const json = this.json;
|
|
|
|
|
|
- /**
|
|
|
- * Process texture
|
|
|
- * @param {Texture} map Map to process
|
|
|
- * @return {Integer} Index of the processed texture in the "textures" array
|
|
|
- */
|
|
|
- processTexture: function ( map ) {
|
|
|
+ if ( ! json.samplers ) json.samplers = [];
|
|
|
|
|
|
- var cache = this.cache;
|
|
|
- var json = this.json;
|
|
|
+ const samplerDef = {
|
|
|
+ magFilter: THREE_TO_WEBGL[ map.magFilter ],
|
|
|
+ minFilter: THREE_TO_WEBGL[ map.minFilter ],
|
|
|
+ wrapS: THREE_TO_WEBGL[ map.wrapS ],
|
|
|
+ wrapT: THREE_TO_WEBGL[ map.wrapT ]
|
|
|
+ };
|
|
|
|
|
|
- if ( cache.textures.has( map ) ) return cache.textures.get( map );
|
|
|
+ return json.samplers.push( samplerDef ) - 1;
|
|
|
|
|
|
- if ( ! json.textures ) json.textures = [];
|
|
|
+ }
|
|
|
|
|
|
- var textureDef = {
|
|
|
- sampler: this.processSampler( map ),
|
|
|
- source: this.processImage( map.image, map.format, map.flipY )
|
|
|
- };
|
|
|
+ /**
|
|
|
+ * Process texture
|
|
|
+ * @param {Texture} map Map to process
|
|
|
+ * @return {Integer} Index of the processed texture in the "textures" array
|
|
|
+ */
|
|
|
+ processTexture( map ) {
|
|
|
|
|
|
- if ( map.name ) textureDef.name = map.name;
|
|
|
+ const cache = this.cache;
|
|
|
+ const json = this.json;
|
|
|
|
|
|
- this._invokeAll( function ( ext ) {
|
|
|
+ if ( cache.textures.has( map ) ) return cache.textures.get( map );
|
|
|
|
|
|
- ext.writeTexture && ext.writeTexture( map, textureDef );
|
|
|
+ if ( ! json.textures ) json.textures = [];
|
|
|
|
|
|
- } );
|
|
|
+ const textureDef = {
|
|
|
+ sampler: this.processSampler( map ),
|
|
|
+ source: this.processImage( map.image, map.format, map.flipY )
|
|
|
+ };
|
|
|
|
|
|
- var index = json.textures.push( textureDef ) - 1;
|
|
|
- cache.textures.set( map, index );
|
|
|
- return index;
|
|
|
+ if ( map.name ) textureDef.name = map.name;
|
|
|
|
|
|
- },
|
|
|
+ this._invokeAll( function ( ext ) {
|
|
|
|
|
|
- /**
|
|
|
- * Process material
|
|
|
- * @param {THREE.Material} material Material to process
|
|
|
- * @return {Integer|null} Index of the processed material in the "materials" array
|
|
|
- */
|
|
|
- processMaterial: function ( material ) {
|
|
|
+ ext.writeTexture && ext.writeTexture( map, textureDef );
|
|
|
|
|
|
- var cache = this.cache;
|
|
|
- var json = this.json;
|
|
|
+ } );
|
|
|
|
|
|
- if ( cache.materials.has( material ) ) return cache.materials.get( material );
|
|
|
+ const index = json.textures.push( textureDef ) - 1;
|
|
|
+ cache.textures.set( map, index );
|
|
|
+ return index;
|
|
|
|
|
|
- if ( material.isShaderMaterial ) {
|
|
|
+ }
|
|
|
|
|
|
- console.warn( 'GLTFExporter: THREE.ShaderMaterial not supported.' );
|
|
|
- return null;
|
|
|
+ /**
|
|
|
+ * Process material
|
|
|
+ * @param {THREE.Material} material Material to process
|
|
|
+ * @return {Integer|null} Index of the processed material in the "materials" array
|
|
|
+ */
|
|
|
+ processMaterial( material ) {
|
|
|
|
|
|
- }
|
|
|
+ const cache = this.cache;
|
|
|
+ const json = this.json;
|
|
|
|
|
|
- if ( ! json.materials ) json.materials = [];
|
|
|
+ if ( cache.materials.has( material ) ) return cache.materials.get( material );
|
|
|
|
|
|
- // @QUESTION Should we avoid including any attribute that has the default value?
|
|
|
- var materialDef = { pbrMetallicRoughness: {} };
|
|
|
+ if ( material.isShaderMaterial ) {
|
|
|
|
|
|
- if ( material.isMeshStandardMaterial !== true && material.isMeshBasicMaterial !== true ) {
|
|
|
+ console.warn( 'GLTFExporter: THREE.ShaderMaterial not supported.' );
|
|
|
+ return null;
|
|
|
|
|
|
- console.warn( 'GLTFExporter: Use MeshStandardMaterial or MeshBasicMaterial for best results.' );
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
+ if ( ! json.materials ) json.materials = [];
|
|
|
|
|
|
- // pbrMetallicRoughness.baseColorFactor
|
|
|
- var color = material.color.toArray().concat( [ material.opacity ] );
|
|
|
+ // @QUESTION Should we avoid including any attribute that has the default value?
|
|
|
+ const materialDef = { pbrMetallicRoughness: {} };
|
|
|
|
|
|
- if ( ! equalArray( color, [ 1, 1, 1, 1 ] ) ) {
|
|
|
+ if ( material.isMeshStandardMaterial !== true && material.isMeshBasicMaterial !== true ) {
|
|
|
|
|
|
- materialDef.pbrMetallicRoughness.baseColorFactor = color;
|
|
|
+ console.warn( 'GLTFExporter: Use MeshStandardMaterial or MeshBasicMaterial for best results.' );
|
|
|
|
|
|
- }
|
|
|
+ }
|
|
|
|
|
|
- if ( material.isMeshStandardMaterial ) {
|
|
|
+ // pbrMetallicRoughness.baseColorFactor
|
|
|
+ const color = material.color.toArray().concat( [ material.opacity ] );
|
|
|
|
|
|
- materialDef.pbrMetallicRoughness.metallicFactor = material.metalness;
|
|
|
- materialDef.pbrMetallicRoughness.roughnessFactor = material.roughness;
|
|
|
+ if ( ! equalArray( color, [ 1, 1, 1, 1 ] ) ) {
|
|
|
|
|
|
- } else {
|
|
|
+ materialDef.pbrMetallicRoughness.baseColorFactor = color;
|
|
|
|
|
|
- materialDef.pbrMetallicRoughness.metallicFactor = 0.5;
|
|
|
- materialDef.pbrMetallicRoughness.roughnessFactor = 0.5;
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
+ if ( material.isMeshStandardMaterial ) {
|
|
|
|
|
|
- // pbrMetallicRoughness.metallicRoughnessTexture
|
|
|
- if ( material.metalnessMap || material.roughnessMap ) {
|
|
|
+ materialDef.pbrMetallicRoughness.metallicFactor = material.metalness;
|
|
|
+ materialDef.pbrMetallicRoughness.roughnessFactor = material.roughness;
|
|
|
|
|
|
- if ( material.metalnessMap === material.roughnessMap ) {
|
|
|
+ } else {
|
|
|
|
|
|
- var metalRoughMapDef = { index: this.processTexture( material.metalnessMap ) };
|
|
|
- this.applyTextureTransform( metalRoughMapDef, material.metalnessMap );
|
|
|
- materialDef.pbrMetallicRoughness.metallicRoughnessTexture = metalRoughMapDef;
|
|
|
+ materialDef.pbrMetallicRoughness.metallicFactor = 0.5;
|
|
|
+ materialDef.pbrMetallicRoughness.roughnessFactor = 0.5;
|
|
|
|
|
|
- } else {
|
|
|
+ }
|
|
|
|
|
|
- console.warn( 'THREE.GLTFExporter: Ignoring metalnessMap and roughnessMap because they are not the same Texture.' );
|
|
|
+ // pbrMetallicRoughness.metallicRoughnessTexture
|
|
|
+ if ( material.metalnessMap || material.roughnessMap ) {
|
|
|
|
|
|
- }
|
|
|
+ if ( material.metalnessMap === material.roughnessMap ) {
|
|
|
|
|
|
- }
|
|
|
+ const metalRoughMapDef = { index: this.processTexture( material.metalnessMap ) };
|
|
|
+ this.applyTextureTransform( metalRoughMapDef, material.metalnessMap );
|
|
|
+ materialDef.pbrMetallicRoughness.metallicRoughnessTexture = metalRoughMapDef;
|
|
|
|
|
|
- // pbrMetallicRoughness.baseColorTexture or pbrSpecularGlossiness diffuseTexture
|
|
|
- if ( material.map ) {
|
|
|
+ } else {
|
|
|
|
|
|
- var baseColorMapDef = { index: this.processTexture( material.map ) };
|
|
|
- this.applyTextureTransform( baseColorMapDef, material.map );
|
|
|
- materialDef.pbrMetallicRoughness.baseColorTexture = baseColorMapDef;
|
|
|
+ console.warn( 'THREE.GLTFExporter: Ignoring metalnessMap and roughnessMap because they are not the same Texture.' );
|
|
|
|
|
|
}
|
|
|
|
|
|
- if ( material.emissive ) {
|
|
|
+ }
|
|
|
|
|
|
- // emissiveFactor
|
|
|
- var emissive = material.emissive.clone().multiplyScalar( material.emissiveIntensity ).toArray();
|
|
|
+ // pbrMetallicRoughness.baseColorTexture or pbrSpecularGlossiness diffuseTexture
|
|
|
+ if ( material.map ) {
|
|
|
|
|
|
- if ( ! equalArray( emissive, [ 0, 0, 0 ] ) ) {
|
|
|
+ const baseColorMapDef = { index: this.processTexture( material.map ) };
|
|
|
+ this.applyTextureTransform( baseColorMapDef, material.map );
|
|
|
+ materialDef.pbrMetallicRoughness.baseColorTexture = baseColorMapDef;
|
|
|
|
|
|
- materialDef.emissiveFactor = emissive;
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
+ if ( material.emissive ) {
|
|
|
|
|
|
- // emissiveTexture
|
|
|
- if ( material.emissiveMap ) {
|
|
|
+ // emissiveFactor
|
|
|
+ const emissive = material.emissive.clone().multiplyScalar( material.emissiveIntensity ).toArray();
|
|
|
|
|
|
- var emissiveMapDef = { index: this.processTexture( material.emissiveMap ) };
|
|
|
- this.applyTextureTransform( emissiveMapDef, material.emissiveMap );
|
|
|
- materialDef.emissiveTexture = emissiveMapDef;
|
|
|
+ if ( ! equalArray( emissive, [ 0, 0, 0 ] ) ) {
|
|
|
|
|
|
- }
|
|
|
+ materialDef.emissiveFactor = emissive;
|
|
|
|
|
|
}
|
|
|
|
|
|
- // normalTexture
|
|
|
- if ( material.normalMap ) {
|
|
|
+ // emissiveTexture
|
|
|
+ if ( material.emissiveMap ) {
|
|
|
|
|
|
- var normalMapDef = { index: this.processTexture( material.normalMap ) };
|
|
|
+ const emissiveMapDef = { index: this.processTexture( material.emissiveMap ) };
|
|
|
+ this.applyTextureTransform( emissiveMapDef, material.emissiveMap );
|
|
|
+ materialDef.emissiveTexture = emissiveMapDef;
|
|
|
|
|
|
- if ( material.normalScale && material.normalScale.x !== - 1 ) {
|
|
|
+ }
|
|
|
|
|
|
- if ( material.normalScale.x !== material.normalScale.y ) {
|
|
|
+ }
|
|
|
|
|
|
- console.warn( 'THREE.GLTFExporter: Normal scale components are different, ignoring Y and exporting X.' );
|
|
|
+ // normalTexture
|
|
|
+ if ( material.normalMap ) {
|
|
|
|
|
|
- }
|
|
|
+ const normalMapDef = { index: this.processTexture( material.normalMap ) };
|
|
|
|
|
|
- normalMapDef.scale = material.normalScale.x;
|
|
|
+ if ( material.normalScale && material.normalScale.x !== - 1 ) {
|
|
|
+
|
|
|
+ if ( material.normalScale.x !== material.normalScale.y ) {
|
|
|
+
|
|
|
+ console.warn( 'THREE.GLTFExporter: Normal scale components are different, ignoring Y and exporting X.' );
|
|
|
|
|
|
}
|
|
|
|
|
|
- this.applyTextureTransform( normalMapDef, material.normalMap );
|
|
|
- materialDef.normalTexture = normalMapDef;
|
|
|
+ normalMapDef.scale = material.normalScale.x;
|
|
|
|
|
|
}
|
|
|
|
|
|
- // occlusionTexture
|
|
|
- if ( material.aoMap ) {
|
|
|
+ this.applyTextureTransform( normalMapDef, material.normalMap );
|
|
|
+ materialDef.normalTexture = normalMapDef;
|
|
|
|
|
|
- var occlusionMapDef = {
|
|
|
- index: this.processTexture( material.aoMap ),
|
|
|
- texCoord: 1
|
|
|
- };
|
|
|
+ }
|
|
|
|
|
|
- if ( material.aoMapIntensity !== 1.0 ) {
|
|
|
+ // occlusionTexture
|
|
|
+ if ( material.aoMap ) {
|
|
|
|
|
|
- occlusionMapDef.strength = material.aoMapIntensity;
|
|
|
+ const occlusionMapDef = {
|
|
|
+ index: this.processTexture( material.aoMap ),
|
|
|
+ texCoord: 1
|
|
|
+ };
|
|
|
|
|
|
- }
|
|
|
+ if ( material.aoMapIntensity !== 1.0 ) {
|
|
|
|
|
|
- this.applyTextureTransform( occlusionMapDef, material.aoMap );
|
|
|
- materialDef.occlusionTexture = occlusionMapDef;
|
|
|
+ occlusionMapDef.strength = material.aoMapIntensity;
|
|
|
|
|
|
}
|
|
|
|
|
|
- // alphaMode
|
|
|
- if ( material.transparent ) {
|
|
|
+ this.applyTextureTransform( occlusionMapDef, material.aoMap );
|
|
|
+ materialDef.occlusionTexture = occlusionMapDef;
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
- materialDef.alphaMode = 'BLEND';
|
|
|
+ // alphaMode
|
|
|
+ if ( material.transparent ) {
|
|
|
|
|
|
- } else {
|
|
|
+ materialDef.alphaMode = 'BLEND';
|
|
|
|
|
|
- if ( material.alphaTest > 0.0 ) {
|
|
|
+ } else {
|
|
|
|
|
|
- materialDef.alphaMode = 'MASK';
|
|
|
- materialDef.alphaCutoff = material.alphaTest;
|
|
|
+ if ( material.alphaTest > 0.0 ) {
|
|
|
|
|
|
- }
|
|
|
+ materialDef.alphaMode = 'MASK';
|
|
|
+ materialDef.alphaCutoff = material.alphaTest;
|
|
|
|
|
|
}
|
|
|
|
|
|
- // doubleSided
|
|
|
- if ( material.side === DoubleSide ) materialDef.doubleSided = true;
|
|
|
- if ( material.name !== '' ) materialDef.name = material.name;
|
|
|
+ }
|
|
|
|
|
|
- this.serializeUserData( material, materialDef );
|
|
|
+ // doubleSided
|
|
|
+ if ( material.side === DoubleSide ) materialDef.doubleSided = true;
|
|
|
+ if ( material.name !== '' ) materialDef.name = material.name;
|
|
|
|
|
|
- this._invokeAll( function ( ext ) {
|
|
|
+ this.serializeUserData( material, materialDef );
|
|
|
|
|
|
- ext.writeMaterial && ext.writeMaterial( material, materialDef );
|
|
|
+ this._invokeAll( function ( ext ) {
|
|
|
|
|
|
- } );
|
|
|
+ ext.writeMaterial && ext.writeMaterial( material, materialDef );
|
|
|
|
|
|
- var index = json.materials.push( materialDef ) - 1;
|
|
|
- cache.materials.set( material, index );
|
|
|
- return index;
|
|
|
+ } );
|
|
|
|
|
|
- },
|
|
|
+ const index = json.materials.push( materialDef ) - 1;
|
|
|
+ cache.materials.set( material, index );
|
|
|
+ return index;
|
|
|
|
|
|
- /**
|
|
|
- * Process mesh
|
|
|
- * @param {THREE.Mesh} mesh Mesh to process
|
|
|
- * @return {Integer|null} Index of the processed mesh in the "meshes" array
|
|
|
- */
|
|
|
- processMesh: function ( mesh ) {
|
|
|
+ }
|
|
|
|
|
|
- var cache = this.cache;
|
|
|
- var json = this.json;
|
|
|
+ /**
|
|
|
+ * Process mesh
|
|
|
+ * @param {THREE.Mesh} mesh Mesh to process
|
|
|
+ * @return {Integer|null} Index of the processed mesh in the "meshes" array
|
|
|
+ */
|
|
|
+ processMesh( mesh ) {
|
|
|
|
|
|
- var meshCacheKeyParts = [ mesh.geometry.uuid ];
|
|
|
+ const cache = this.cache;
|
|
|
+ const json = this.json;
|
|
|
|
|
|
- if ( Array.isArray( mesh.material ) ) {
|
|
|
+ const meshCacheKeyParts = [ mesh.geometry.uuid ];
|
|
|
|
|
|
- for ( var i = 0, l = mesh.material.length; i < l; i ++ ) {
|
|
|
+ if ( Array.isArray( mesh.material ) ) {
|
|
|
|
|
|
- meshCacheKeyParts.push( mesh.material[ i ].uuid );
|
|
|
+ for ( let i = 0, l = mesh.material.length; i < l; i ++ ) {
|
|
|
|
|
|
- }
|
|
|
+ meshCacheKeyParts.push( mesh.material[ i ].uuid );
|
|
|
|
|
|
- } else {
|
|
|
+ }
|
|
|
|
|
|
- meshCacheKeyParts.push( mesh.material.uuid );
|
|
|
+ } else {
|
|
|
|
|
|
- }
|
|
|
+ meshCacheKeyParts.push( mesh.material.uuid );
|
|
|
|
|
|
- var meshCacheKey = meshCacheKeyParts.join( ':' );
|
|
|
+ }
|
|
|
|
|
|
- if ( cache.meshes.has( meshCacheKey ) ) return cache.meshes.get( meshCacheKey );
|
|
|
+ const meshCacheKey = meshCacheKeyParts.join( ':' );
|
|
|
|
|
|
- var geometry = mesh.geometry;
|
|
|
- var mode;
|
|
|
+ if ( cache.meshes.has( meshCacheKey ) ) return cache.meshes.get( meshCacheKey );
|
|
|
|
|
|
- // Use the correct mode
|
|
|
- if ( mesh.isLineSegments ) {
|
|
|
+ const geometry = mesh.geometry;
|
|
|
+ let mode;
|
|
|
|
|
|
- mode = WEBGL_CONSTANTS.LINES;
|
|
|
+ // Use the correct mode
|
|
|
+ if ( mesh.isLineSegments ) {
|
|
|
|
|
|
- } else if ( mesh.isLineLoop ) {
|
|
|
+ mode = WEBGL_CONSTANTS.LINES;
|
|
|
|
|
|
- mode = WEBGL_CONSTANTS.LINE_LOOP;
|
|
|
+ } else if ( mesh.isLineLoop ) {
|
|
|
|
|
|
- } else if ( mesh.isLine ) {
|
|
|
+ mode = WEBGL_CONSTANTS.LINE_LOOP;
|
|
|
|
|
|
- mode = WEBGL_CONSTANTS.LINE_STRIP;
|
|
|
+ } else if ( mesh.isLine ) {
|
|
|
|
|
|
- } else if ( mesh.isPoints ) {
|
|
|
+ mode = WEBGL_CONSTANTS.LINE_STRIP;
|
|
|
|
|
|
- mode = WEBGL_CONSTANTS.POINTS;
|
|
|
+ } else if ( mesh.isPoints ) {
|
|
|
|
|
|
- } else {
|
|
|
+ mode = WEBGL_CONSTANTS.POINTS;
|
|
|
|
|
|
- mode = mesh.material.wireframe ? WEBGL_CONSTANTS.LINES : WEBGL_CONSTANTS.TRIANGLES;
|
|
|
+ } else {
|
|
|
|
|
|
- }
|
|
|
+ mode = mesh.material.wireframe ? WEBGL_CONSTANTS.LINES : WEBGL_CONSTANTS.TRIANGLES;
|
|
|
|
|
|
- if ( geometry.isBufferGeometry !== true ) {
|
|
|
+ }
|
|
|
|
|
|
- throw new Error( 'THREE.GLTFExporter: Geometry is not of type THREE.BufferGeometry.' );
|
|
|
+ if ( geometry.isBufferGeometry !== true ) {
|
|
|
|
|
|
- }
|
|
|
+ throw new Error( 'THREE.GLTFExporter: Geometry is not of type THREE.BufferGeometry.' );
|
|
|
|
|
|
- var meshDef = {};
|
|
|
- var attributes = {};
|
|
|
- var primitives = [];
|
|
|
- var targets = [];
|
|
|
-
|
|
|
- // Conversion between attributes names in threejs and gltf spec
|
|
|
- var nameConversion = {
|
|
|
- uv: 'TEXCOORD_0',
|
|
|
- uv2: 'TEXCOORD_1',
|
|
|
- color: 'COLOR_0',
|
|
|
- skinWeight: 'WEIGHTS_0',
|
|
|
- skinIndex: 'JOINTS_0'
|
|
|
- };
|
|
|
+ }
|
|
|
|
|
|
- var originalNormal = geometry.getAttribute( 'normal' );
|
|
|
+ const meshDef = {};
|
|
|
+ const attributes = {};
|
|
|
+ const primitives = [];
|
|
|
+ const targets = [];
|
|
|
+
|
|
|
+ // Conversion between attributes names in threejs and gltf spec
|
|
|
+ const nameConversion = {
|
|
|
+ uv: 'TEXCOORD_0',
|
|
|
+ uv2: 'TEXCOORD_1',
|
|
|
+ color: 'COLOR_0',
|
|
|
+ skinWeight: 'WEIGHTS_0',
|
|
|
+ skinIndex: 'JOINTS_0'
|
|
|
+ };
|
|
|
|
|
|
- if ( originalNormal !== undefined && ! this.isNormalizedNormalAttribute( originalNormal ) ) {
|
|
|
+ const originalNormal = geometry.getAttribute( 'normal' );
|
|
|
|
|
|
- console.warn( 'THREE.GLTFExporter: Creating normalized normal attribute from the non-normalized one.' );
|
|
|
+ if ( originalNormal !== undefined && ! this.isNormalizedNormalAttribute( originalNormal ) ) {
|
|
|
|
|
|
- geometry.setAttribute( 'normal', this.createNormalizedNormalAttribute( originalNormal ) );
|
|
|
+ console.warn( 'THREE.GLTFExporter: Creating normalized normal attribute from the non-normalized one.' );
|
|
|
|
|
|
- }
|
|
|
+ geometry.setAttribute( 'normal', this.createNormalizedNormalAttribute( originalNormal ) );
|
|
|
|
|
|
- // @QUESTION Detect if .vertexColors = true?
|
|
|
- // For every attribute create an accessor
|
|
|
- var modifiedAttribute = null;
|
|
|
+ }
|
|
|
|
|
|
- for ( var attributeName in geometry.attributes ) {
|
|
|
+ // @QUESTION Detect if .vertexColors = true?
|
|
|
+ // For every attribute create an accessor
|
|
|
+ let modifiedAttribute = null;
|
|
|
|
|
|
- // Ignore morph target attributes, which are exported later.
|
|
|
- if ( attributeName.substr( 0, 5 ) === 'morph' ) continue;
|
|
|
+ for ( let attributeName in geometry.attributes ) {
|
|
|
|
|
|
- var attribute = geometry.attributes[ attributeName ];
|
|
|
- attributeName = nameConversion[ attributeName ] || attributeName.toUpperCase();
|
|
|
+ // Ignore morph target attributes, which are exported later.
|
|
|
+ if ( attributeName.substr( 0, 5 ) === 'morph' ) continue;
|
|
|
|
|
|
- // Prefix all geometry attributes except the ones specifically
|
|
|
- // listed in the spec; non-spec attributes are considered custom.
|
|
|
- var validVertexAttributes =
|
|
|
- /^(POSITION|NORMAL|TANGENT|TEXCOORD_\d+|COLOR_\d+|JOINTS_\d+|WEIGHTS_\d+)$/;
|
|
|
+ const attribute = geometry.attributes[ attributeName ];
|
|
|
+ attributeName = nameConversion[ attributeName ] || attributeName.toUpperCase();
|
|
|
|
|
|
- if ( ! validVertexAttributes.test( attributeName ) ) attributeName = '_' + attributeName;
|
|
|
+ // Prefix all geometry attributes except the ones specifically
|
|
|
+ // listed in the spec; non-spec attributes are considered custom.
|
|
|
+ const validVertexAttributes =
|
|
|
+ /^(POSITION|NORMAL|TANGENT|TEXCOORD_\d+|COLOR_\d+|JOINTS_\d+|WEIGHTS_\d+)$/;
|
|
|
|
|
|
- if ( cache.attributes.has( this.getUID( attribute ) ) ) {
|
|
|
+ if ( ! validVertexAttributes.test( attributeName ) ) attributeName = '_' + attributeName;
|
|
|
|
|
|
- attributes[ attributeName ] = cache.attributes.get( this.getUID( attribute ) );
|
|
|
- continue;
|
|
|
+ if ( cache.attributes.has( this.getUID( attribute ) ) ) {
|
|
|
|
|
|
- }
|
|
|
+ attributes[ attributeName ] = cache.attributes.get( this.getUID( attribute ) );
|
|
|
+ continue;
|
|
|
|
|
|
- // JOINTS_0 must be UNSIGNED_BYTE or UNSIGNED_SHORT.
|
|
|
- modifiedAttribute = null;
|
|
|
- var array = attribute.array;
|
|
|
+ }
|
|
|
|
|
|
- if ( attributeName === 'JOINTS_0' &&
|
|
|
- ! ( array instanceof Uint16Array ) &&
|
|
|
- ! ( array instanceof Uint8Array ) ) {
|
|
|
+ // JOINTS_0 must be UNSIGNED_BYTE or UNSIGNED_SHORT.
|
|
|
+ modifiedAttribute = null;
|
|
|
+ const array = attribute.array;
|
|
|
|
|
|
- console.warn( 'GLTFExporter: Attribute "skinIndex" converted to type UNSIGNED_SHORT.' );
|
|
|
- modifiedAttribute = new BufferAttribute( new Uint16Array( array ), attribute.itemSize, attribute.normalized );
|
|
|
+ if ( attributeName === 'JOINTS_0' &&
|
|
|
+ ! ( array instanceof Uint16Array ) &&
|
|
|
+ ! ( array instanceof Uint8Array ) ) {
|
|
|
|
|
|
- }
|
|
|
+ console.warn( 'GLTFExporter: Attribute "skinIndex" converted to type UNSIGNED_SHORT.' );
|
|
|
+ modifiedAttribute = new BufferAttribute( new Uint16Array( array ), attribute.itemSize, attribute.normalized );
|
|
|
|
|
|
- var accessor = this.processAccessor( modifiedAttribute || attribute, geometry );
|
|
|
+ }
|
|
|
|
|
|
- if ( accessor !== null ) {
|
|
|
+ const accessor = this.processAccessor( modifiedAttribute || attribute, geometry );
|
|
|
|
|
|
- attributes[ attributeName ] = accessor;
|
|
|
- cache.attributes.set( this.getUID( attribute ), accessor );
|
|
|
+ if ( accessor !== null ) {
|
|
|
|
|
|
- }
|
|
|
+ attributes[ attributeName ] = accessor;
|
|
|
+ cache.attributes.set( this.getUID( attribute ), accessor );
|
|
|
|
|
|
}
|
|
|
|
|
|
- if ( originalNormal !== undefined ) geometry.setAttribute( 'normal', originalNormal );
|
|
|
+ }
|
|
|
|
|
|
- // Skip if no exportable attributes found
|
|
|
- if ( Object.keys( attributes ).length === 0 ) return null;
|
|
|
+ if ( originalNormal !== undefined ) geometry.setAttribute( 'normal', originalNormal );
|
|
|
|
|
|
- // Morph targets
|
|
|
- if ( mesh.morphTargetInfluences !== undefined && mesh.morphTargetInfluences.length > 0 ) {
|
|
|
+ // Skip if no exportable attributes found
|
|
|
+ if ( Object.keys( attributes ).length === 0 ) return null;
|
|
|
|
|
|
- var weights = [];
|
|
|
- var targetNames = [];
|
|
|
- var reverseDictionary = {};
|
|
|
+ // Morph targets
|
|
|
+ if ( mesh.morphTargetInfluences !== undefined && mesh.morphTargetInfluences.length > 0 ) {
|
|
|
|
|
|
- if ( mesh.morphTargetDictionary !== undefined ) {
|
|
|
+ const weights = [];
|
|
|
+ const targetNames = [];
|
|
|
+ const reverseDictionary = {};
|
|
|
|
|
|
- for ( var key in mesh.morphTargetDictionary ) {
|
|
|
+ if ( mesh.morphTargetDictionary !== undefined ) {
|
|
|
|
|
|
- reverseDictionary[ mesh.morphTargetDictionary[ key ] ] = key;
|
|
|
+ for ( const key in mesh.morphTargetDictionary ) {
|
|
|
|
|
|
- }
|
|
|
+ reverseDictionary[ mesh.morphTargetDictionary[ key ] ] = key;
|
|
|
|
|
|
}
|
|
|
|
|
|
- for ( var i = 0; i < mesh.morphTargetInfluences.length; ++ i ) {
|
|
|
-
|
|
|
- var target = {};
|
|
|
- var warned = false;
|
|
|
+ }
|
|
|
|
|
|
- for ( var attributeName in geometry.morphAttributes ) {
|
|
|
+ for ( let i = 0; i < mesh.morphTargetInfluences.length; ++ i ) {
|
|
|
|
|
|
- // glTF 2.0 morph supports only POSITION/NORMAL/TANGENT.
|
|
|
- // Three.js doesn't support TANGENT yet.
|
|
|
+ const target = {};
|
|
|
+ let warned = false;
|
|
|
|
|
|
- if ( attributeName !== 'position' && attributeName !== 'normal' ) {
|
|
|
+ for ( const attributeName in geometry.morphAttributes ) {
|
|
|
|
|
|
- if ( ! warned ) {
|
|
|
+ // glTF 2.0 morph supports only POSITION/NORMAL/TANGENT.
|
|
|
+ // Three.js doesn't support TANGENT yet.
|
|
|
|
|
|
- console.warn( 'GLTFExporter: Only POSITION and NORMAL morph are supported.' );
|
|
|
- warned = true;
|
|
|
+ if ( attributeName !== 'position' && attributeName !== 'normal' ) {
|
|
|
|
|
|
- }
|
|
|
+ if ( ! warned ) {
|
|
|
|
|
|
- continue;
|
|
|
+ console.warn( 'GLTFExporter: Only POSITION and NORMAL morph are supported.' );
|
|
|
+ warned = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
- var attribute = geometry.morphAttributes[ attributeName ][ i ];
|
|
|
- var gltfAttributeName = attributeName.toUpperCase();
|
|
|
+ continue;
|
|
|
|
|
|
- // Three.js morph attribute has absolute values while the one of glTF has relative values.
|
|
|
- //
|
|
|
- // glTF 2.0 Specification:
|
|
|
- // https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#morph-targets
|
|
|
+ }
|
|
|
|
|
|
- var baseAttribute = geometry.attributes[ attributeName ];
|
|
|
+ const attribute = geometry.morphAttributes[ attributeName ][ i ];
|
|
|
+ const gltfAttributeName = attributeName.toUpperCase();
|
|
|
|
|
|
- if ( cache.attributes.has( this.getUID( attribute ) ) ) {
|
|
|
+ // Three.js morph attribute has absolute values while the one of glTF has relative values.
|
|
|
+ //
|
|
|
+ // glTF 2.0 Specification:
|
|
|
+ // https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#morph-targets
|
|
|
|
|
|
- target[ gltfAttributeName ] = cache.attributes.get( this.getUID( attribute ) );
|
|
|
- continue;
|
|
|
+ const baseAttribute = geometry.attributes[ attributeName ];
|
|
|
|
|
|
- }
|
|
|
+ if ( cache.attributes.has( this.getUID( attribute ) ) ) {
|
|
|
|
|
|
- // Clones attribute not to override
|
|
|
- var relativeAttribute = attribute.clone();
|
|
|
+ target[ gltfAttributeName ] = cache.attributes.get( this.getUID( attribute ) );
|
|
|
+ continue;
|
|
|
|
|
|
- if ( ! geometry.morphTargetsRelative ) {
|
|
|
+ }
|
|
|
|
|
|
- for ( var j = 0, jl = attribute.count; j < jl; j ++ ) {
|
|
|
+ // Clones attribute not to override
|
|
|
+ const relativeAttribute = attribute.clone();
|
|
|
|
|
|
- relativeAttribute.setXYZ(
|
|
|
- j,
|
|
|
- attribute.getX( j ) - baseAttribute.getX( j ),
|
|
|
- attribute.getY( j ) - baseAttribute.getY( j ),
|
|
|
- attribute.getZ( j ) - baseAttribute.getZ( j )
|
|
|
- );
|
|
|
+ if ( ! geometry.morphTargetsRelative ) {
|
|
|
|
|
|
- }
|
|
|
+ for ( let j = 0, jl = attribute.count; j < jl; j ++ ) {
|
|
|
|
|
|
- }
|
|
|
+ relativeAttribute.setXYZ(
|
|
|
+ j,
|
|
|
+ attribute.getX( j ) - baseAttribute.getX( j ),
|
|
|
+ attribute.getY( j ) - baseAttribute.getY( j ),
|
|
|
+ attribute.getZ( j ) - baseAttribute.getZ( j )
|
|
|
+ );
|
|
|
|
|
|
- target[ gltfAttributeName ] = this.processAccessor( relativeAttribute, geometry );
|
|
|
- cache.attributes.set( this.getUID( baseAttribute ), target[ gltfAttributeName ] );
|
|
|
+ }
|
|
|
|
|
|
}
|
|
|
|
|
|
- targets.push( target );
|
|
|
-
|
|
|
- weights.push( mesh.morphTargetInfluences[ i ] );
|
|
|
-
|
|
|
- if ( mesh.morphTargetDictionary !== undefined ) targetNames.push( reverseDictionary[ i ] );
|
|
|
+ target[ gltfAttributeName ] = this.processAccessor( relativeAttribute, geometry );
|
|
|
+ cache.attributes.set( this.getUID( baseAttribute ), target[ gltfAttributeName ] );
|
|
|
|
|
|
}
|
|
|
|
|
|
- meshDef.weights = weights;
|
|
|
-
|
|
|
- if ( targetNames.length > 0 ) {
|
|
|
+ targets.push( target );
|
|
|
|
|
|
- meshDef.extras = {};
|
|
|
- meshDef.extras.targetNames = targetNames;
|
|
|
+ weights.push( mesh.morphTargetInfluences[ i ] );
|
|
|
|
|
|
- }
|
|
|
+ if ( mesh.morphTargetDictionary !== undefined ) targetNames.push( reverseDictionary[ i ] );
|
|
|
|
|
|
}
|
|
|
|
|
|
- var isMultiMaterial = Array.isArray( mesh.material );
|
|
|
+ meshDef.weights = weights;
|
|
|
|
|
|
- if ( isMultiMaterial && geometry.groups.length === 0 ) return null;
|
|
|
+ if ( targetNames.length > 0 ) {
|
|
|
|
|
|
- var materials = isMultiMaterial ? mesh.material : [ mesh.material ];
|
|
|
- var groups = isMultiMaterial ? geometry.groups : [ { materialIndex: 0, start: undefined, count: undefined } ];
|
|
|
+ meshDef.extras = {};
|
|
|
+ meshDef.extras.targetNames = targetNames;
|
|
|
|
|
|
- for ( var i = 0, il = groups.length; i < il; i ++ ) {
|
|
|
+ }
|
|
|
|
|
|
- var primitive = {
|
|
|
- mode: mode,
|
|
|
- attributes: attributes,
|
|
|
- };
|
|
|
+ }
|
|
|
+
|
|
|
+ const isMultiMaterial = Array.isArray( mesh.material );
|
|
|
|
|
|
- this.serializeUserData( geometry, primitive );
|
|
|
+ if ( isMultiMaterial && geometry.groups.length === 0 ) return null;
|
|
|
|
|
|
- if ( targets.length > 0 ) primitive.targets = targets;
|
|
|
+ const materials = isMultiMaterial ? mesh.material : [ mesh.material ];
|
|
|
+ const groups = isMultiMaterial ? geometry.groups : [ { materialIndex: 0, start: undefined, count: undefined } ];
|
|
|
|
|
|
- if ( geometry.index !== null ) {
|
|
|
+ for ( let i = 0, il = groups.length; i < il; i ++ ) {
|
|
|
|
|
|
- var cacheKey = this.getUID( geometry.index );
|
|
|
+ const primitive = {
|
|
|
+ mode: mode,
|
|
|
+ attributes: attributes,
|
|
|
+ };
|
|
|
|
|
|
- if ( groups[ i ].start !== undefined || groups[ i ].count !== undefined ) {
|
|
|
+ this.serializeUserData( geometry, primitive );
|
|
|
|
|
|
- cacheKey += ':' + groups[ i ].start + ':' + groups[ i ].count;
|
|
|
+ if ( targets.length > 0 ) primitive.targets = targets;
|
|
|
|
|
|
- }
|
|
|
+ if ( geometry.index !== null ) {
|
|
|
|
|
|
- if ( cache.attributes.has( cacheKey ) ) {
|
|
|
+ let cacheKey = this.getUID( geometry.index );
|
|
|
|
|
|
- primitive.indices = cache.attributes.get( cacheKey );
|
|
|
+ if ( groups[ i ].start !== undefined || groups[ i ].count !== undefined ) {
|
|
|
|
|
|
- } else {
|
|
|
+ cacheKey += ':' + groups[ i ].start + ':' + groups[ i ].count;
|
|
|
|
|
|
- primitive.indices = this.processAccessor( geometry.index, geometry, groups[ i ].start, groups[ i ].count );
|
|
|
- cache.attributes.set( cacheKey, primitive.indices );
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
+ if ( cache.attributes.has( cacheKey ) ) {
|
|
|
|
|
|
- if ( primitive.indices === null ) delete primitive.indices;
|
|
|
+ primitive.indices = cache.attributes.get( cacheKey );
|
|
|
|
|
|
- }
|
|
|
+ } else {
|
|
|
|
|
|
- var material = this.processMaterial( materials[ groups[ i ].materialIndex ] );
|
|
|
+ primitive.indices = this.processAccessor( geometry.index, geometry, groups[ i ].start, groups[ i ].count );
|
|
|
+ cache.attributes.set( cacheKey, primitive.indices );
|
|
|
|
|
|
- if ( material !== null ) primitive.material = material;
|
|
|
+ }
|
|
|
|
|
|
- primitives.push( primitive );
|
|
|
+ if ( primitive.indices === null ) delete primitive.indices;
|
|
|
|
|
|
}
|
|
|
|
|
|
- meshDef.primitives = primitives;
|
|
|
+ const material = this.processMaterial( materials[ groups[ i ].materialIndex ] );
|
|
|
|
|
|
- if ( ! json.meshes ) json.meshes = [];
|
|
|
+ if ( material !== null ) primitive.material = material;
|
|
|
|
|
|
- this._invokeAll( function ( ext ) {
|
|
|
+ primitives.push( primitive );
|
|
|
|
|
|
- ext.writeMesh && ext.writeMesh( mesh, meshDef );
|
|
|
+ }
|
|
|
|
|
|
- } );
|
|
|
+ meshDef.primitives = primitives;
|
|
|
|
|
|
- var index = json.meshes.push( meshDef ) - 1;
|
|
|
- cache.meshes.set( meshCacheKey, index );
|
|
|
- return index;
|
|
|
+ if ( ! json.meshes ) json.meshes = [];
|
|
|
|
|
|
- },
|
|
|
+ this._invokeAll( function ( ext ) {
|
|
|
|
|
|
- /**
|
|
|
- * Process camera
|
|
|
- * @param {THREE.Camera} camera Camera to process
|
|
|
- * @return {Integer} Index of the processed mesh in the "camera" array
|
|
|
- */
|
|
|
- processCamera: function ( camera ) {
|
|
|
+ ext.writeMesh && ext.writeMesh( mesh, meshDef );
|
|
|
|
|
|
- var json = this.json;
|
|
|
+ } );
|
|
|
|
|
|
- if ( ! json.cameras ) json.cameras = [];
|
|
|
+ const index = json.meshes.push( meshDef ) - 1;
|
|
|
+ cache.meshes.set( meshCacheKey, index );
|
|
|
+ return index;
|
|
|
|
|
|
- var isOrtho = camera.isOrthographicCamera;
|
|
|
+ }
|
|
|
|
|
|
- var cameraDef = {
|
|
|
- type: isOrtho ? 'orthographic' : 'perspective'
|
|
|
- };
|
|
|
+ /**
|
|
|
+ * Process camera
|
|
|
+ * @param {THREE.Camera} camera Camera to process
|
|
|
+ * @return {Integer} Index of the processed mesh in the "camera" array
|
|
|
+ */
|
|
|
+ processCamera( camera ) {
|
|
|
|
|
|
- if ( isOrtho ) {
|
|
|
+ const json = this.json;
|
|
|
|
|
|
- cameraDef.orthographic = {
|
|
|
- xmag: camera.right * 2,
|
|
|
- ymag: camera.top * 2,
|
|
|
- zfar: camera.far <= 0 ? 0.001 : camera.far,
|
|
|
- znear: camera.near < 0 ? 0 : camera.near
|
|
|
- };
|
|
|
+ if ( ! json.cameras ) json.cameras = [];
|
|
|
|
|
|
- } else {
|
|
|
+ const isOrtho = camera.isOrthographicCamera;
|
|
|
|
|
|
- cameraDef.perspective = {
|
|
|
- aspectRatio: camera.aspect,
|
|
|
- yfov: MathUtils.degToRad( camera.fov ),
|
|
|
- zfar: camera.far <= 0 ? 0.001 : camera.far,
|
|
|
- znear: camera.near < 0 ? 0 : camera.near
|
|
|
- };
|
|
|
+ const cameraDef = {
|
|
|
+ type: isOrtho ? 'orthographic' : 'perspective'
|
|
|
+ };
|
|
|
|
|
|
- }
|
|
|
+ if ( isOrtho ) {
|
|
|
|
|
|
- // Question: Is saving "type" as name intentional?
|
|
|
- if ( camera.name !== '' ) cameraDef.name = camera.type;
|
|
|
+ cameraDef.orthographic = {
|
|
|
+ xmag: camera.right * 2,
|
|
|
+ ymag: camera.top * 2,
|
|
|
+ zfar: camera.far <= 0 ? 0.001 : camera.far,
|
|
|
+ znear: camera.near < 0 ? 0 : camera.near
|
|
|
+ };
|
|
|
|
|
|
- return json.cameras.push( cameraDef ) - 1;
|
|
|
+ } else {
|
|
|
|
|
|
- },
|
|
|
+ cameraDef.perspective = {
|
|
|
+ aspectRatio: camera.aspect,
|
|
|
+ yfov: MathUtils.degToRad( camera.fov ),
|
|
|
+ zfar: camera.far <= 0 ? 0.001 : camera.far,
|
|
|
+ znear: camera.near < 0 ? 0 : camera.near
|
|
|
+ };
|
|
|
|
|
|
- /**
|
|
|
- * Creates glTF animation entry from AnimationClip object.
|
|
|
- *
|
|
|
- * Status:
|
|
|
- * - Only properties listed in PATH_PROPERTIES may be animated.
|
|
|
- *
|
|
|
- * @param {THREE.AnimationClip} clip
|
|
|
- * @param {THREE.Object3D} root
|
|
|
- * @return {number|null}
|
|
|
- */
|
|
|
- processAnimation: function ( clip, root ) {
|
|
|
+ }
|
|
|
|
|
|
- var json = this.json;
|
|
|
- var nodeMap = this.nodeMap;
|
|
|
+ // Question: Is saving "type" as name intentional?
|
|
|
+ if ( camera.name !== '' ) cameraDef.name = camera.type;
|
|
|
|
|
|
- if ( ! json.animations ) json.animations = [];
|
|
|
+ return json.cameras.push( cameraDef ) - 1;
|
|
|
|
|
|
- clip = GLTFExporter.Utils.mergeMorphTargetTracks( clip.clone(), root );
|
|
|
+ }
|
|
|
|
|
|
- var tracks = clip.tracks;
|
|
|
- var channels = [];
|
|
|
- var samplers = [];
|
|
|
+ /**
|
|
|
+ * Creates glTF animation entry from AnimationClip object.
|
|
|
+ *
|
|
|
+ * Status:
|
|
|
+ * - Only properties listed in PATH_PROPERTIES may be animated.
|
|
|
+ *
|
|
|
+ * @param {THREE.AnimationClip} clip
|
|
|
+ * @param {THREE.Object3D} root
|
|
|
+ * @return {number|null}
|
|
|
+ */
|
|
|
+ processAnimation( clip, root ) {
|
|
|
|
|
|
- for ( var i = 0; i < tracks.length; ++ i ) {
|
|
|
+ const json = this.json;
|
|
|
+ const nodeMap = this.nodeMap;
|
|
|
|
|
|
- var track = tracks[ i ];
|
|
|
- var trackBinding = PropertyBinding.parseTrackName( track.name );
|
|
|
- var trackNode = PropertyBinding.findNode( root, trackBinding.nodeName );
|
|
|
- var trackProperty = PATH_PROPERTIES[ trackBinding.propertyName ];
|
|
|
+ if ( ! json.animations ) json.animations = [];
|
|
|
|
|
|
- if ( trackBinding.objectName === 'bones' ) {
|
|
|
+ clip = GLTFExporter.Utils.mergeMorphTargetTracks( clip.clone(), root );
|
|
|
|
|
|
- if ( trackNode.isSkinnedMesh === true ) {
|
|
|
+ const tracks = clip.tracks;
|
|
|
+ const channels = [];
|
|
|
+ const samplers = [];
|
|
|
|
|
|
- trackNode = trackNode.skeleton.getBoneByName( trackBinding.objectIndex );
|
|
|
+ for ( let i = 0; i < tracks.length; ++ i ) {
|
|
|
|
|
|
- } else {
|
|
|
+ const track = tracks[ i ];
|
|
|
+ const trackBinding = PropertyBinding.parseTrackName( track.name );
|
|
|
+ let trackNode = PropertyBinding.findNode( root, trackBinding.nodeName );
|
|
|
+ const trackProperty = PATH_PROPERTIES[ trackBinding.propertyName ];
|
|
|
|
|
|
- trackNode = undefined;
|
|
|
+ if ( trackBinding.objectName === 'bones' ) {
|
|
|
|
|
|
- }
|
|
|
+ if ( trackNode.isSkinnedMesh === true ) {
|
|
|
|
|
|
- }
|
|
|
+ trackNode = trackNode.skeleton.getBoneByName( trackBinding.objectIndex );
|
|
|
|
|
|
- if ( ! trackNode || ! trackProperty ) {
|
|
|
+ } else {
|
|
|
|
|
|
- console.warn( 'THREE.GLTFExporter: Could not export animation track "%s".', track.name );
|
|
|
- return null;
|
|
|
+ trackNode = undefined;
|
|
|
|
|
|
}
|
|
|
|
|
|
- var inputItemSize = 1;
|
|
|
- var outputItemSize = track.values.length / track.times.length;
|
|
|
+ }
|
|
|
|
|
|
- if ( trackProperty === PATH_PROPERTIES.morphTargetInfluences ) {
|
|
|
+ if ( ! trackNode || ! trackProperty ) {
|
|
|
|
|
|
- outputItemSize /= trackNode.morphTargetInfluences.length;
|
|
|
+ console.warn( 'THREE.GLTFExporter: Could not export animation track "%s".', track.name );
|
|
|
+ return null;
|
|
|
|
|
|
- }
|
|
|
+ }
|
|
|
|
|
|
- var interpolation;
|
|
|
+ const inputItemSize = 1;
|
|
|
+ let outputItemSize = track.values.length / track.times.length;
|
|
|
|
|
|
- // @TODO export CubicInterpolant(InterpolateSmooth) as CUBICSPLINE
|
|
|
+ if ( trackProperty === PATH_PROPERTIES.morphTargetInfluences ) {
|
|
|
|
|
|
- // Detecting glTF cubic spline interpolant by checking factory method's special property
|
|
|
- // GLTFCubicSplineInterpolant is a custom interpolant and track doesn't return
|
|
|
- // valid value from .getInterpolation().
|
|
|
- if ( track.createInterpolant.isInterpolantFactoryMethodGLTFCubicSpline === true ) {
|
|
|
+ outputItemSize /= trackNode.morphTargetInfluences.length;
|
|
|
|
|
|
- interpolation = 'CUBICSPLINE';
|
|
|
+ }
|
|
|
|
|
|
- // itemSize of CUBICSPLINE keyframe is 9
|
|
|
- // (VEC3 * 3: inTangent, splineVertex, and outTangent)
|
|
|
- // but needs to be stored as VEC3 so dividing by 3 here.
|
|
|
- outputItemSize /= 3;
|
|
|
+ let interpolation;
|
|
|
|
|
|
- } else if ( track.getInterpolation() === InterpolateDiscrete ) {
|
|
|
+ // @TODO export CubicInterpolant(InterpolateSmooth) as CUBICSPLINE
|
|
|
|
|
|
- interpolation = 'STEP';
|
|
|
+ // Detecting glTF cubic spline interpolant by checking factory method's special property
|
|
|
+ // GLTFCubicSplineInterpolant is a custom interpolant and track doesn't return
|
|
|
+ // valid value from .getInterpolation().
|
|
|
+ if ( track.createInterpolant.isInterpolantFactoryMethodGLTFCubicSpline === true ) {
|
|
|
|
|
|
- } else {
|
|
|
+ interpolation = 'CUBICSPLINE';
|
|
|
|
|
|
- interpolation = 'LINEAR';
|
|
|
+ // itemSize of CUBICSPLINE keyframe is 9
|
|
|
+ // (VEC3 * 3: inTangent, splineVertex, and outTangent)
|
|
|
+ // but needs to be stored as VEC3 so dividing by 3 here.
|
|
|
+ outputItemSize /= 3;
|
|
|
|
|
|
- }
|
|
|
+ } else if ( track.getInterpolation() === InterpolateDiscrete ) {
|
|
|
|
|
|
- samplers.push( {
|
|
|
- input: this.processAccessor( new BufferAttribute( track.times, inputItemSize ) ),
|
|
|
- output: this.processAccessor( new BufferAttribute( track.values, outputItemSize ) ),
|
|
|
- interpolation: interpolation
|
|
|
- } );
|
|
|
-
|
|
|
- channels.push( {
|
|
|
- sampler: samplers.length - 1,
|
|
|
- target: {
|
|
|
- node: nodeMap.get( trackNode ),
|
|
|
- path: trackProperty
|
|
|
- }
|
|
|
- } );
|
|
|
+ interpolation = 'STEP';
|
|
|
+
|
|
|
+ } else {
|
|
|
+
|
|
|
+ interpolation = 'LINEAR';
|
|
|
|
|
|
}
|
|
|
|
|
|
- json.animations.push( {
|
|
|
- name: clip.name || 'clip_' + json.animations.length,
|
|
|
- samplers: samplers,
|
|
|
- channels: channels
|
|
|
+ samplers.push( {
|
|
|
+ input: this.processAccessor( new BufferAttribute( track.times, inputItemSize ) ),
|
|
|
+ output: this.processAccessor( new BufferAttribute( track.values, outputItemSize ) ),
|
|
|
+ interpolation: interpolation
|
|
|
} );
|
|
|
|
|
|
- return json.animations.length - 1;
|
|
|
+ channels.push( {
|
|
|
+ sampler: samplers.length - 1,
|
|
|
+ target: {
|
|
|
+ node: nodeMap.get( trackNode ),
|
|
|
+ path: trackProperty
|
|
|
+ }
|
|
|
+ } );
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- /**
|
|
|
- * @param {THREE.Object3D} object
|
|
|
- * @return {number|null}
|
|
|
- */
|
|
|
- processSkin: function ( object ) {
|
|
|
+ json.animations.push( {
|
|
|
+ name: clip.name || 'clip_' + json.animations.length,
|
|
|
+ samplers: samplers,
|
|
|
+ channels: channels
|
|
|
+ } );
|
|
|
|
|
|
- var json = this.json;
|
|
|
- var nodeMap = this.nodeMap;
|
|
|
+ return json.animations.length - 1;
|
|
|
|
|
|
- var node = json.nodes[ nodeMap.get( object ) ];
|
|
|
+ }
|
|
|
|
|
|
- var skeleton = object.skeleton;
|
|
|
+ /**
|
|
|
+ * @param {THREE.Object3D} object
|
|
|
+ * @return {number|null}
|
|
|
+ */
|
|
|
+ processSkin( object ) {
|
|
|
|
|
|
- if ( skeleton === undefined ) return null;
|
|
|
+ const json = this.json;
|
|
|
+ const nodeMap = this.nodeMap;
|
|
|
|
|
|
- var rootJoint = object.skeleton.bones[ 0 ];
|
|
|
+ const node = json.nodes[ nodeMap.get( object ) ];
|
|
|
|
|
|
- if ( rootJoint === undefined ) return null;
|
|
|
+ const skeleton = object.skeleton;
|
|
|
|
|
|
- var joints = [];
|
|
|
- var inverseBindMatrices = new Float32Array( skeleton.bones.length * 16 );
|
|
|
- var temporaryBoneInverse = new Matrix4();
|
|
|
+ if ( skeleton === undefined ) return null;
|
|
|
|
|
|
- for ( var i = 0; i < skeleton.bones.length; ++ i ) {
|
|
|
+ const rootJoint = object.skeleton.bones[ 0 ];
|
|
|
|
|
|
- joints.push( nodeMap.get( skeleton.bones[ i ] ) );
|
|
|
- temporaryBoneInverse.copy( skeleton.boneInverses[ i ] );
|
|
|
- temporaryBoneInverse.multiply( object.bindMatrix ).toArray( inverseBindMatrices, i * 16 );
|
|
|
+ if ( rootJoint === undefined ) return null;
|
|
|
|
|
|
- }
|
|
|
+ const joints = [];
|
|
|
+ const inverseBindMatrices = new Float32Array( skeleton.bones.length * 16 );
|
|
|
+ const temporaryBoneInverse = new Matrix4();
|
|
|
|
|
|
- if ( json.skins === undefined ) json.skins = [];
|
|
|
+ for ( let i = 0; i < skeleton.bones.length; ++ i ) {
|
|
|
|
|
|
- json.skins.push( {
|
|
|
- inverseBindMatrices: this.processAccessor( new BufferAttribute( inverseBindMatrices, 16 ) ),
|
|
|
- joints: joints,
|
|
|
- skeleton: nodeMap.get( rootJoint )
|
|
|
- } );
|
|
|
+ joints.push( nodeMap.get( skeleton.bones[ i ] ) );
|
|
|
+ temporaryBoneInverse.copy( skeleton.boneInverses[ i ] );
|
|
|
+ temporaryBoneInverse.multiply( object.bindMatrix ).toArray( inverseBindMatrices, i * 16 );
|
|
|
|
|
|
- var skinIndex = node.skin = json.skins.length - 1;
|
|
|
+ }
|
|
|
|
|
|
- return skinIndex;
|
|
|
+ if ( json.skins === undefined ) json.skins = [];
|
|
|
|
|
|
- },
|
|
|
+ json.skins.push( {
|
|
|
+ inverseBindMatrices: this.processAccessor( new BufferAttribute( inverseBindMatrices, 16 ) ),
|
|
|
+ joints: joints,
|
|
|
+ skeleton: nodeMap.get( rootJoint )
|
|
|
+ } );
|
|
|
|
|
|
- /**
|
|
|
- * Process Object3D node
|
|
|
- * @param {THREE.Object3D} node Object3D to processNode
|
|
|
- * @return {Integer} Index of the node in the nodes list
|
|
|
- */
|
|
|
- processNode: function ( object ) {
|
|
|
+ const skinIndex = node.skin = json.skins.length - 1;
|
|
|
|
|
|
- var json = this.json;
|
|
|
- var options = this.options;
|
|
|
- var nodeMap = this.nodeMap;
|
|
|
+ return skinIndex;
|
|
|
|
|
|
- if ( ! json.nodes ) json.nodes = [];
|
|
|
+ }
|
|
|
|
|
|
- var nodeDef = {};
|
|
|
+ /**
|
|
|
+ * Process Object3D node
|
|
|
+ * @param {THREE.Object3D} node Object3D to processNode
|
|
|
+ * @return {Integer} Index of the node in the nodes list
|
|
|
+ */
|
|
|
+ processNode( object ) {
|
|
|
|
|
|
- if ( options.trs ) {
|
|
|
+ const json = this.json;
|
|
|
+ const options = this.options;
|
|
|
+ const nodeMap = this.nodeMap;
|
|
|
|
|
|
- var rotation = object.quaternion.toArray();
|
|
|
- var position = object.position.toArray();
|
|
|
- var scale = object.scale.toArray();
|
|
|
+ if ( ! json.nodes ) json.nodes = [];
|
|
|
|
|
|
- if ( ! equalArray( rotation, [ 0, 0, 0, 1 ] ) ) {
|
|
|
+ const nodeDef = {};
|
|
|
|
|
|
- nodeDef.rotation = rotation;
|
|
|
+ if ( options.trs ) {
|
|
|
|
|
|
- }
|
|
|
+ const rotation = object.quaternion.toArray();
|
|
|
+ const position = object.position.toArray();
|
|
|
+ const scale = object.scale.toArray();
|
|
|
|
|
|
- if ( ! equalArray( position, [ 0, 0, 0 ] ) ) {
|
|
|
+ if ( ! equalArray( rotation, [ 0, 0, 0, 1 ] ) ) {
|
|
|
|
|
|
- nodeDef.translation = position;
|
|
|
+ nodeDef.rotation = rotation;
|
|
|
|
|
|
- }
|
|
|
+ }
|
|
|
|
|
|
- if ( ! equalArray( scale, [ 1, 1, 1 ] ) ) {
|
|
|
+ if ( ! equalArray( position, [ 0, 0, 0 ] ) ) {
|
|
|
|
|
|
- nodeDef.scale = scale;
|
|
|
+ nodeDef.translation = position;
|
|
|
|
|
|
- }
|
|
|
+ }
|
|
|
|
|
|
- } else {
|
|
|
+ if ( ! equalArray( scale, [ 1, 1, 1 ] ) ) {
|
|
|
|
|
|
- if ( object.matrixAutoUpdate ) {
|
|
|
+ nodeDef.scale = scale;
|
|
|
|
|
|
- object.updateMatrix();
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
+ } else {
|
|
|
|
|
|
- if ( isIdentityMatrix( object.matrix ) === false ) {
|
|
|
+ if ( object.matrixAutoUpdate ) {
|
|
|
|
|
|
- nodeDef.matrix = object.matrix.elements;
|
|
|
+ object.updateMatrix();
|
|
|
|
|
|
- }
|
|
|
+ }
|
|
|
+
|
|
|
+ if ( isIdentityMatrix( object.matrix ) === false ) {
|
|
|
+
|
|
|
+ nodeDef.matrix = object.matrix.elements;
|
|
|
|
|
|
}
|
|
|
|
|
|
- // We don't export empty strings name because it represents no-name in Three.js.
|
|
|
- if ( object.name !== '' ) nodeDef.name = String( object.name );
|
|
|
+ }
|
|
|
|
|
|
- this.serializeUserData( object, nodeDef );
|
|
|
+ // We don't export empty strings name because it represents no-name in Three.js.
|
|
|
+ if ( object.name !== '' ) nodeDef.name = String( object.name );
|
|
|
|
|
|
- if ( object.isMesh || object.isLine || object.isPoints ) {
|
|
|
+ this.serializeUserData( object, nodeDef );
|
|
|
|
|
|
- var meshIndex = this.processMesh( object );
|
|
|
+ if ( object.isMesh || object.isLine || object.isPoints ) {
|
|
|
|
|
|
- if ( meshIndex !== null ) nodeDef.mesh = meshIndex;
|
|
|
+ const meshIndex = this.processMesh( object );
|
|
|
|
|
|
- } else if ( object.isCamera ) {
|
|
|
+ if ( meshIndex !== null ) nodeDef.mesh = meshIndex;
|
|
|
|
|
|
- nodeDef.camera = this.processCamera( object );
|
|
|
+ } else if ( object.isCamera ) {
|
|
|
|
|
|
- }
|
|
|
+ nodeDef.camera = this.processCamera( object );
|
|
|
|
|
|
- if ( object.isSkinnedMesh ) this.skins.push( object );
|
|
|
+ }
|
|
|
|
|
|
- if ( object.children.length > 0 ) {
|
|
|
+ if ( object.isSkinnedMesh ) this.skins.push( object );
|
|
|
|
|
|
- var children = [];
|
|
|
+ if ( object.children.length > 0 ) {
|
|
|
|
|
|
- for ( var i = 0, l = object.children.length; i < l; i ++ ) {
|
|
|
+ const children = [];
|
|
|
|
|
|
- var child = object.children[ i ];
|
|
|
+ for ( let i = 0, l = object.children.length; i < l; i ++ ) {
|
|
|
|
|
|
- if ( child.visible || options.onlyVisible === false ) {
|
|
|
+ const child = object.children[ i ];
|
|
|
|
|
|
- var nodeIndex = this.processNode( child );
|
|
|
+ if ( child.visible || options.onlyVisible === false ) {
|
|
|
|
|
|
- if ( nodeIndex !== null ) children.push( nodeIndex );
|
|
|
+ const nodeIndex = this.processNode( child );
|
|
|
|
|
|
- }
|
|
|
+ if ( nodeIndex !== null ) children.push( nodeIndex );
|
|
|
|
|
|
}
|
|
|
|
|
|
- if ( children.length > 0 ) nodeDef.children = children;
|
|
|
-
|
|
|
}
|
|
|
|
|
|
- this._invokeAll( function ( ext ) {
|
|
|
+ if ( children.length > 0 ) nodeDef.children = children;
|
|
|
|
|
|
- ext.writeNode && ext.writeNode( object, nodeDef );
|
|
|
+ }
|
|
|
|
|
|
- } );
|
|
|
+ this._invokeAll( function ( ext ) {
|
|
|
|
|
|
- var nodeIndex = json.nodes.push( nodeDef ) - 1;
|
|
|
- nodeMap.set( object, nodeIndex );
|
|
|
- return nodeIndex;
|
|
|
+ ext.writeNode && ext.writeNode( object, nodeDef );
|
|
|
|
|
|
- },
|
|
|
+ } );
|
|
|
|
|
|
- /**
|
|
|
- * Process Scene
|
|
|
- * @param {Scene} node Scene to process
|
|
|
- */
|
|
|
- processScene: function ( scene ) {
|
|
|
+ const nodeIndex = json.nodes.push( nodeDef ) - 1;
|
|
|
+ nodeMap.set( object, nodeIndex );
|
|
|
+ return nodeIndex;
|
|
|
|
|
|
- var json = this.json;
|
|
|
- var options = this.options;
|
|
|
+ }
|
|
|
|
|
|
- if ( ! json.scenes ) {
|
|
|
+ /**
|
|
|
+ * Process Scene
|
|
|
+ * @param {Scene} node Scene to process
|
|
|
+ */
|
|
|
+ processScene( scene ) {
|
|
|
|
|
|
- json.scenes = [];
|
|
|
- json.scene = 0;
|
|
|
+ const json = this.json;
|
|
|
+ const options = this.options;
|
|
|
|
|
|
- }
|
|
|
+ if ( ! json.scenes ) {
|
|
|
|
|
|
- var sceneDef = {};
|
|
|
+ json.scenes = [];
|
|
|
+ json.scene = 0;
|
|
|
|
|
|
- if ( scene.name !== '' ) sceneDef.name = scene.name;
|
|
|
+ }
|
|
|
|
|
|
- json.scenes.push( sceneDef );
|
|
|
+ const sceneDef = {};
|
|
|
|
|
|
- var nodes = [];
|
|
|
+ if ( scene.name !== '' ) sceneDef.name = scene.name;
|
|
|
|
|
|
- for ( var i = 0, l = scene.children.length; i < l; i ++ ) {
|
|
|
+ json.scenes.push( sceneDef );
|
|
|
|
|
|
- var child = scene.children[ i ];
|
|
|
+ const nodes = [];
|
|
|
|
|
|
- if ( child.visible || options.onlyVisible === false ) {
|
|
|
+ for ( let i = 0, l = scene.children.length; i < l; i ++ ) {
|
|
|
|
|
|
- var nodeIndex = this.processNode( child );
|
|
|
+ const child = scene.children[ i ];
|
|
|
|
|
|
- if ( nodeIndex !== null ) nodes.push( nodeIndex );
|
|
|
+ if ( child.visible || options.onlyVisible === false ) {
|
|
|
|
|
|
- }
|
|
|
+ const nodeIndex = this.processNode( child );
|
|
|
+
|
|
|
+ if ( nodeIndex !== null ) nodes.push( nodeIndex );
|
|
|
|
|
|
}
|
|
|
|
|
|
- if ( nodes.length > 0 ) sceneDef.nodes = nodes;
|
|
|
+ }
|
|
|
|
|
|
- this.serializeUserData( scene, sceneDef );
|
|
|
+ if ( nodes.length > 0 ) sceneDef.nodes = nodes;
|
|
|
|
|
|
- },
|
|
|
+ this.serializeUserData( scene, sceneDef );
|
|
|
|
|
|
- /**
|
|
|
- * Creates a Scene to hold a list of objects and parse it
|
|
|
- * @param {Array} objects List of objects to process
|
|
|
- */
|
|
|
- processObjects: function ( objects ) {
|
|
|
+ }
|
|
|
|
|
|
- var scene = new Scene();
|
|
|
- scene.name = 'AuxScene';
|
|
|
+ /**
|
|
|
+ * Creates a Scene to hold a list of objects and parse it
|
|
|
+ * @param {Array} objects List of objects to process
|
|
|
+ */
|
|
|
+ processObjects( objects ) {
|
|
|
|
|
|
- for ( var i = 0; i < objects.length; i ++ ) {
|
|
|
+ const scene = new Scene();
|
|
|
+ scene.name = 'AuxScene';
|
|
|
|
|
|
- // We push directly to children instead of calling `add` to prevent
|
|
|
- // modify the .parent and break its original scene and hierarchy
|
|
|
- scene.children.push( objects[ i ] );
|
|
|
+ for ( let i = 0; i < objects.length; i ++ ) {
|
|
|
|
|
|
- }
|
|
|
+ // We push directly to children instead of calling `add` to prevent
|
|
|
+ // modify the .parent and break its original scene and hierarchy
|
|
|
+ scene.children.push( objects[ i ] );
|
|
|
|
|
|
- this.processScene( scene );
|
|
|
+ }
|
|
|
|
|
|
- },
|
|
|
+ this.processScene( scene );
|
|
|
|
|
|
- /**
|
|
|
- * @param {THREE.Object3D|Array<THREE.Object3D>} input
|
|
|
- */
|
|
|
- processInput: function ( input ) {
|
|
|
+ }
|
|
|
|
|
|
- var options = this.options;
|
|
|
+ /**
|
|
|
+ * @param {THREE.Object3D|Array<THREE.Object3D>} input
|
|
|
+ */
|
|
|
+ processInput( input ) {
|
|
|
|
|
|
- input = input instanceof Array ? input : [ input ];
|
|
|
+ const options = this.options;
|
|
|
|
|
|
- this._invokeAll( function ( ext ) {
|
|
|
+ input = input instanceof Array ? input : [ input ];
|
|
|
|
|
|
- ext.beforeParse && ext.beforeParse( input );
|
|
|
+ this._invokeAll( function ( ext ) {
|
|
|
|
|
|
- } );
|
|
|
+ ext.beforeParse && ext.beforeParse( input );
|
|
|
|
|
|
- var objectsWithoutScene = [];
|
|
|
+ } );
|
|
|
|
|
|
- for ( var i = 0; i < input.length; i ++ ) {
|
|
|
+ const objectsWithoutScene = [];
|
|
|
|
|
|
- if ( input[ i ] instanceof Scene ) {
|
|
|
+ for ( let i = 0; i < input.length; i ++ ) {
|
|
|
|
|
|
- this.processScene( input[ i ] );
|
|
|
+ if ( input[ i ] instanceof Scene ) {
|
|
|
|
|
|
- } else {
|
|
|
+ this.processScene( input[ i ] );
|
|
|
|
|
|
- objectsWithoutScene.push( input[ i ] );
|
|
|
+ } else {
|
|
|
|
|
|
- }
|
|
|
+ objectsWithoutScene.push( input[ i ] );
|
|
|
|
|
|
}
|
|
|
|
|
|
- if ( objectsWithoutScene.length > 0 ) this.processObjects( objectsWithoutScene );
|
|
|
+ }
|
|
|
|
|
|
- for ( var i = 0; i < this.skins.length; ++ i ) {
|
|
|
+ if ( objectsWithoutScene.length > 0 ) this.processObjects( objectsWithoutScene );
|
|
|
|
|
|
- this.processSkin( this.skins[ i ] );
|
|
|
+ for ( let i = 0; i < this.skins.length; ++ i ) {
|
|
|
|
|
|
- }
|
|
|
+ this.processSkin( this.skins[ i ] );
|
|
|
|
|
|
- for ( var i = 0; i < options.animations.length; ++ i ) {
|
|
|
+ }
|
|
|
|
|
|
- this.processAnimation( options.animations[ i ], input[ 0 ] );
|
|
|
+ for ( let i = 0; i < options.animations.length; ++ i ) {
|
|
|
|
|
|
- }
|
|
|
+ this.processAnimation( options.animations[ i ], input[ 0 ] );
|
|
|
|
|
|
- this._invokeAll( function ( ext ) {
|
|
|
+ }
|
|
|
|
|
|
- ext.afterParse && ext.afterParse( input );
|
|
|
+ this._invokeAll( function ( ext ) {
|
|
|
|
|
|
- } );
|
|
|
+ ext.afterParse && ext.afterParse( input );
|
|
|
|
|
|
- },
|
|
|
+ } );
|
|
|
|
|
|
- _invokeAll: function ( func ) {
|
|
|
+ }
|
|
|
|
|
|
- for ( var i = 0, il = this.plugins.length; i < il; i ++ ) {
|
|
|
+ _invokeAll( func ) {
|
|
|
|
|
|
- func( this.plugins[ i ] );
|
|
|
+ for ( let i = 0, il = this.plugins.length; i < il; i ++ ) {
|
|
|
|
|
|
- }
|
|
|
+ func( this.plugins[ i ] );
|
|
|
|
|
|
}
|
|
|
|
|
|
- };
|
|
|
+ }
|
|
|
|
|
|
- /**
|
|
|
- * Punctual Lights Extension
|
|
|
- *
|
|
|
- * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_lights_punctual
|
|
|
- */
|
|
|
- function GLTFLightExtension( writer ) {
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * Punctual Lights Extension
|
|
|
+ *
|
|
|
+ * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_lights_punctual
|
|
|
+ */
|
|
|
+class GLTFLightExtension {
|
|
|
+
|
|
|
+ constructor( writer ) {
|
|
|
|
|
|
this.writer = writer;
|
|
|
this.name = 'KHR_lights_punctual';
|
|
|
|
|
|
}
|
|
|
|
|
|
- GLTFLightExtension.prototype = {
|
|
|
-
|
|
|
- constructor: GLTFLightExtension,
|
|
|
+ writeNode( light, nodeDef ) {
|
|
|
|
|
|
- writeNode: function ( light, nodeDef ) {
|
|
|
+ if ( ! light.isLight ) return;
|
|
|
|
|
|
- if ( ! light.isLight ) return;
|
|
|
+ if ( ! light.isDirectionalLight && ! light.isPointLight && ! light.isSpotLight ) {
|
|
|
|
|
|
- if ( ! light.isDirectionalLight && ! light.isPointLight && ! light.isSpotLight ) {
|
|
|
+ console.warn( 'THREE.GLTFExporter: Only directional, point, and spot lights are supported.', light );
|
|
|
+ return;
|
|
|
|
|
|
- console.warn( 'THREE.GLTFExporter: Only directional, point, and spot lights are supported.', light );
|
|
|
- return;
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
+ const writer = this.writer;
|
|
|
+ const json = writer.json;
|
|
|
+ const extensionsUsed = writer.extensionsUsed;
|
|
|
|
|
|
- var writer = this.writer;
|
|
|
- var json = writer.json;
|
|
|
- var extensionsUsed = writer.extensionsUsed;
|
|
|
+ const lightDef = {};
|
|
|
|
|
|
- var lightDef = {};
|
|
|
+ if ( light.name ) lightDef.name = light.name;
|
|
|
|
|
|
- if ( light.name ) lightDef.name = light.name;
|
|
|
+ lightDef.color = light.color.toArray();
|
|
|
|
|
|
- lightDef.color = light.color.toArray();
|
|
|
+ lightDef.intensity = light.intensity;
|
|
|
|
|
|
- lightDef.intensity = light.intensity;
|
|
|
+ if ( light.isDirectionalLight ) {
|
|
|
|
|
|
- if ( light.isDirectionalLight ) {
|
|
|
+ lightDef.type = 'directional';
|
|
|
|
|
|
- lightDef.type = 'directional';
|
|
|
+ } else if ( light.isPointLight ) {
|
|
|
|
|
|
- } else if ( light.isPointLight ) {
|
|
|
+ lightDef.type = 'point';
|
|
|
|
|
|
- lightDef.type = 'point';
|
|
|
+ if ( light.distance > 0 ) lightDef.range = light.distance;
|
|
|
|
|
|
- if ( light.distance > 0 ) lightDef.range = light.distance;
|
|
|
+ } else if ( light.isSpotLight ) {
|
|
|
|
|
|
- } else if ( light.isSpotLight ) {
|
|
|
+ lightDef.type = 'spot';
|
|
|
|
|
|
- lightDef.type = 'spot';
|
|
|
+ if ( light.distance > 0 ) lightDef.range = light.distance;
|
|
|
|
|
|
- if ( light.distance > 0 ) lightDef.range = light.distance;
|
|
|
+ lightDef.spot = {};
|
|
|
+ lightDef.spot.innerConeAngle = ( light.penumbra - 1.0 ) * light.angle * - 1.0;
|
|
|
+ lightDef.spot.outerConeAngle = light.angle;
|
|
|
|
|
|
- lightDef.spot = {};
|
|
|
- lightDef.spot.innerConeAngle = ( light.penumbra - 1.0 ) * light.angle * - 1.0;
|
|
|
- lightDef.spot.outerConeAngle = light.angle;
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
+ if ( light.decay !== undefined && light.decay !== 2 ) {
|
|
|
|
|
|
- if ( light.decay !== undefined && light.decay !== 2 ) {
|
|
|
+ console.warn( 'THREE.GLTFExporter: Light decay may be lost. glTF is physically-based, '
|
|
|
+ + 'and expects light.decay=2.' );
|
|
|
|
|
|
- console.warn( 'THREE.GLTFExporter: Light decay may be lost. glTF is physically-based, '
|
|
|
- + 'and expects light.decay=2.' );
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
+ if ( light.target
|
|
|
+ && ( light.target.parent !== light
|
|
|
+ || light.target.position.x !== 0
|
|
|
+ || light.target.position.y !== 0
|
|
|
+ || light.target.position.z !== - 1 ) ) {
|
|
|
|
|
|
- if ( light.target
|
|
|
- && ( light.target.parent !== light
|
|
|
- || light.target.position.x !== 0
|
|
|
- || light.target.position.y !== 0
|
|
|
- || light.target.position.z !== - 1 ) ) {
|
|
|
+ console.warn( 'THREE.GLTFExporter: Light direction may be lost. For best results, '
|
|
|
+ + 'make light.target a child of the light with position 0,0,-1.' );
|
|
|
|
|
|
- console.warn( 'THREE.GLTFExporter: Light direction may be lost. For best results, '
|
|
|
- + 'make light.target a child of the light with position 0,0,-1.' );
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
+ if ( ! extensionsUsed[ this.name ] ) {
|
|
|
|
|
|
- if ( ! extensionsUsed[ this.name ] ) {
|
|
|
+ json.extensions = json.extensions || {};
|
|
|
+ json.extensions[ this.name ] = { lights: [] };
|
|
|
+ extensionsUsed[ this.name ] = true;
|
|
|
|
|
|
- json.extensions = json.extensions || {};
|
|
|
- json.extensions[ this.name ] = { lights: [] };
|
|
|
- extensionsUsed[ this.name ] = true;
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
+ const lights = json.extensions[ this.name ].lights;
|
|
|
+ lights.push( lightDef );
|
|
|
|
|
|
- var lights = json.extensions[ this.name ].lights;
|
|
|
- lights.push( lightDef );
|
|
|
+ nodeDef.extensions = nodeDef.extensions || {};
|
|
|
+ nodeDef.extensions[ this.name ] = { light: lights.length - 1 };
|
|
|
|
|
|
- nodeDef.extensions = nodeDef.extensions || {};
|
|
|
- nodeDef.extensions[ this.name ] = { light: lights.length - 1 };
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
+}
|
|
|
|
|
|
- };
|
|
|
+/**
|
|
|
+ * Unlit Materials Extension
|
|
|
+ *
|
|
|
+ * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_unlit
|
|
|
+ */
|
|
|
+class GLTFMaterialsUnlitExtension {
|
|
|
|
|
|
- /**
|
|
|
- * Unlit Materials Extension
|
|
|
- *
|
|
|
- * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_unlit
|
|
|
- */
|
|
|
- function GLTFMaterialsUnlitExtension( writer ) {
|
|
|
+ constructor( writer ) {
|
|
|
|
|
|
this.writer = writer;
|
|
|
this.name = 'KHR_materials_unlit';
|
|
|
|
|
|
}
|
|
|
|
|
|
- GLTFMaterialsUnlitExtension.prototype = {
|
|
|
+ writeMaterial( material, materialDef ) {
|
|
|
|
|
|
- constructor: GLTFMaterialsUnlitExtension,
|
|
|
+ if ( ! material.isMeshBasicMaterial ) return;
|
|
|
|
|
|
- writeMaterial: function ( material, materialDef ) {
|
|
|
+ const writer = this.writer;
|
|
|
+ const extensionsUsed = writer.extensionsUsed;
|
|
|
|
|
|
- if ( ! material.isMeshBasicMaterial ) return;
|
|
|
+ materialDef.extensions = materialDef.extensions || {};
|
|
|
+ materialDef.extensions[ this.name ] = {};
|
|
|
|
|
|
- var writer = this.writer;
|
|
|
- var extensionsUsed = writer.extensionsUsed;
|
|
|
+ extensionsUsed[ this.name ] = true;
|
|
|
|
|
|
- materialDef.extensions = materialDef.extensions || {};
|
|
|
- materialDef.extensions[ this.name ] = {};
|
|
|
+ materialDef.pbrMetallicRoughness.metallicFactor = 0.0;
|
|
|
+ materialDef.pbrMetallicRoughness.roughnessFactor = 0.9;
|
|
|
|
|
|
- extensionsUsed[ this.name ] = true;
|
|
|
-
|
|
|
- materialDef.pbrMetallicRoughness.metallicFactor = 0.0;
|
|
|
- materialDef.pbrMetallicRoughness.roughnessFactor = 0.9;
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
+}
|
|
|
|
|
|
- };
|
|
|
+/**
|
|
|
+ * Specular-Glossiness Extension
|
|
|
+ *
|
|
|
+ * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_pbrSpecularGlossiness
|
|
|
+ */
|
|
|
+class GLTFMaterialsPBRSpecularGlossiness {
|
|
|
|
|
|
- /**
|
|
|
- * Specular-Glossiness Extension
|
|
|
- *
|
|
|
- * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_pbrSpecularGlossiness
|
|
|
- */
|
|
|
- function GLTFMaterialsPBRSpecularGlossiness( writer ) {
|
|
|
+ constructor( writer ) {
|
|
|
|
|
|
this.writer = writer;
|
|
|
this.name = 'KHR_materials_pbrSpecularGlossiness';
|
|
|
|
|
|
}
|
|
|
|
|
|
- GLTFMaterialsPBRSpecularGlossiness.prototype = {
|
|
|
-
|
|
|
- constructor: GLTFMaterialsPBRSpecularGlossiness,
|
|
|
+ writeMaterial( material, materialDef ) {
|
|
|
|
|
|
- writeMaterial: function ( material, materialDef ) {
|
|
|
+ if ( ! material.isGLTFSpecularGlossinessMaterial ) return;
|
|
|
|
|
|
- if ( ! material.isGLTFSpecularGlossinessMaterial ) return;
|
|
|
+ const writer = this.writer;
|
|
|
+ const extensionsUsed = writer.extensionsUsed;
|
|
|
|
|
|
- var writer = this.writer;
|
|
|
- var extensionsUsed = writer.extensionsUsed;
|
|
|
+ const extensionDef = {};
|
|
|
|
|
|
- var extensionDef = {};
|
|
|
+ if ( materialDef.pbrMetallicRoughness.baseColorFactor ) {
|
|
|
|
|
|
- if ( materialDef.pbrMetallicRoughness.baseColorFactor ) {
|
|
|
+ extensionDef.diffuseFactor = materialDef.pbrMetallicRoughness.baseColorFactor;
|
|
|
|
|
|
- extensionDef.diffuseFactor = materialDef.pbrMetallicRoughness.baseColorFactor;
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- var specularFactor = [ 1, 1, 1 ];
|
|
|
- material.specular.toArray( specularFactor, 0 );
|
|
|
- extensionDef.specularFactor = specularFactor;
|
|
|
- extensionDef.glossinessFactor = material.glossiness;
|
|
|
-
|
|
|
- if ( materialDef.pbrMetallicRoughness.baseColorTexture ) {
|
|
|
+ }
|
|
|
|
|
|
- extensionDef.diffuseTexture = materialDef.pbrMetallicRoughness.baseColorTexture;
|
|
|
+ const specularFactor = [ 1, 1, 1 ];
|
|
|
+ material.specular.toArray( specularFactor, 0 );
|
|
|
+ extensionDef.specularFactor = specularFactor;
|
|
|
+ extensionDef.glossinessFactor = material.glossiness;
|
|
|
|
|
|
- }
|
|
|
+ if ( materialDef.pbrMetallicRoughness.baseColorTexture ) {
|
|
|
|
|
|
- if ( material.specularMap ) {
|
|
|
+ extensionDef.diffuseTexture = materialDef.pbrMetallicRoughness.baseColorTexture;
|
|
|
|
|
|
- var specularMapDef = { index: writer.processTexture( material.specularMap ) };
|
|
|
- writer.applyTextureTransform( specularMapDef, material.specularMap );
|
|
|
- extensionDef.specularGlossinessTexture = specularMapDef;
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
+ if ( material.specularMap ) {
|
|
|
|
|
|
- materialDef.extensions = materialDef.extensions || {};
|
|
|
- materialDef.extensions[ this.name ] = extensionDef;
|
|
|
- extensionsUsed[ this.name ] = true;
|
|
|
+ const specularMapDef = { index: writer.processTexture( material.specularMap ) };
|
|
|
+ writer.applyTextureTransform( specularMapDef, material.specularMap );
|
|
|
+ extensionDef.specularGlossinessTexture = specularMapDef;
|
|
|
|
|
|
}
|
|
|
|
|
|
- };
|
|
|
+ materialDef.extensions = materialDef.extensions || {};
|
|
|
+ materialDef.extensions[ this.name ] = extensionDef;
|
|
|
+ extensionsUsed[ this.name ] = true;
|
|
|
|
|
|
- /**
|
|
|
- * Static utility functions
|
|
|
- */
|
|
|
- GLTFExporter.Utils = {
|
|
|
+ }
|
|
|
|
|
|
- insertKeyframe: function ( track, time ) {
|
|
|
+}
|
|
|
|
|
|
- var tolerance = 0.001; // 1ms
|
|
|
- var valueSize = track.getValueSize();
|
|
|
+/**
|
|
|
+ * Static utility functions
|
|
|
+ */
|
|
|
+GLTFExporter.Utils = {
|
|
|
|
|
|
- var times = new track.TimeBufferType( track.times.length + 1 );
|
|
|
- var values = new track.ValueBufferType( track.values.length + valueSize );
|
|
|
- var interpolant = track.createInterpolant( new track.ValueBufferType( valueSize ) );
|
|
|
+ insertKeyframe: function ( track, time ) {
|
|
|
|
|
|
- var index;
|
|
|
+ const tolerance = 0.001; // 1ms
|
|
|
+ const valueSize = track.getValueSize();
|
|
|
|
|
|
- if ( track.times.length === 0 ) {
|
|
|
+ const times = new track.TimeBufferType( track.times.length + 1 );
|
|
|
+ const values = new track.ValueBufferType( track.values.length + valueSize );
|
|
|
+ const interpolant = track.createInterpolant( new track.ValueBufferType( valueSize ) );
|
|
|
|
|
|
- times[ 0 ] = time;
|
|
|
+ let index;
|
|
|
|
|
|
- for ( var i = 0; i < valueSize; i ++ ) {
|
|
|
+ if ( track.times.length === 0 ) {
|
|
|
|
|
|
- values[ i ] = 0;
|
|
|
+ times[ 0 ] = time;
|
|
|
|
|
|
- }
|
|
|
+ for ( let i = 0; i < valueSize; i ++ ) {
|
|
|
|
|
|
- index = 0;
|
|
|
+ values[ i ] = 0;
|
|
|
|
|
|
- } else if ( time < track.times[ 0 ] ) {
|
|
|
+ }
|
|
|
|
|
|
- if ( Math.abs( track.times[ 0 ] - time ) < tolerance ) return 0;
|
|
|
+ index = 0;
|
|
|
|
|
|
- times[ 0 ] = time;
|
|
|
- times.set( track.times, 1 );
|
|
|
+ } else if ( time < track.times[ 0 ] ) {
|
|
|
|
|
|
- values.set( interpolant.evaluate( time ), 0 );
|
|
|
- values.set( track.values, valueSize );
|
|
|
+ if ( Math.abs( track.times[ 0 ] - time ) < tolerance ) return 0;
|
|
|
|
|
|
- index = 0;
|
|
|
+ times[ 0 ] = time;
|
|
|
+ times.set( track.times, 1 );
|
|
|
|
|
|
- } else if ( time > track.times[ track.times.length - 1 ] ) {
|
|
|
+ values.set( interpolant.evaluate( time ), 0 );
|
|
|
+ values.set( track.values, valueSize );
|
|
|
|
|
|
- if ( Math.abs( track.times[ track.times.length - 1 ] - time ) < tolerance ) {
|
|
|
+ index = 0;
|
|
|
|
|
|
- return track.times.length - 1;
|
|
|
+ } else if ( time > track.times[ track.times.length - 1 ] ) {
|
|
|
|
|
|
- }
|
|
|
+ if ( Math.abs( track.times[ track.times.length - 1 ] - time ) < tolerance ) {
|
|
|
|
|
|
- times[ times.length - 1 ] = time;
|
|
|
- times.set( track.times, 0 );
|
|
|
+ return track.times.length - 1;
|
|
|
|
|
|
- values.set( track.values, 0 );
|
|
|
- values.set( interpolant.evaluate( time ), track.values.length );
|
|
|
+ }
|
|
|
|
|
|
- index = times.length - 1;
|
|
|
+ times[ times.length - 1 ] = time;
|
|
|
+ times.set( track.times, 0 );
|
|
|
|
|
|
- } else {
|
|
|
+ values.set( track.values, 0 );
|
|
|
+ values.set( interpolant.evaluate( time ), track.values.length );
|
|
|
|
|
|
- for ( var i = 0; i < track.times.length; i ++ ) {
|
|
|
+ index = times.length - 1;
|
|
|
|
|
|
- if ( Math.abs( track.times[ i ] - time ) < tolerance ) return i;
|
|
|
+ } else {
|
|
|
|
|
|
- if ( track.times[ i ] < time && track.times[ i + 1 ] > time ) {
|
|
|
+ for ( let i = 0; i < track.times.length; i ++ ) {
|
|
|
|
|
|
- times.set( track.times.slice( 0, i + 1 ), 0 );
|
|
|
- times[ i + 1 ] = time;
|
|
|
- times.set( track.times.slice( i + 1 ), i + 2 );
|
|
|
+ if ( Math.abs( track.times[ i ] - time ) < tolerance ) return i;
|
|
|
|
|
|
- values.set( track.values.slice( 0, ( i + 1 ) * valueSize ), 0 );
|
|
|
- values.set( interpolant.evaluate( time ), ( i + 1 ) * valueSize );
|
|
|
- values.set( track.values.slice( ( i + 1 ) * valueSize ), ( i + 2 ) * valueSize );
|
|
|
+ if ( track.times[ i ] < time && track.times[ i + 1 ] > time ) {
|
|
|
|
|
|
- index = i + 1;
|
|
|
+ times.set( track.times.slice( 0, i + 1 ), 0 );
|
|
|
+ times[ i + 1 ] = time;
|
|
|
+ times.set( track.times.slice( i + 1 ), i + 2 );
|
|
|
|
|
|
- break;
|
|
|
+ values.set( track.values.slice( 0, ( i + 1 ) * valueSize ), 0 );
|
|
|
+ values.set( interpolant.evaluate( time ), ( i + 1 ) * valueSize );
|
|
|
+ values.set( track.values.slice( ( i + 1 ) * valueSize ), ( i + 2 ) * valueSize );
|
|
|
|
|
|
- }
|
|
|
+ index = i + 1;
|
|
|
+
|
|
|
+ break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
- track.times = times;
|
|
|
- track.values = values;
|
|
|
-
|
|
|
- return index;
|
|
|
-
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- mergeMorphTargetTracks: function ( clip, root ) {
|
|
|
+ track.times = times;
|
|
|
+ track.values = values;
|
|
|
|
|
|
- var tracks = [];
|
|
|
- var mergedTracks = {};
|
|
|
- var sourceTracks = clip.tracks;
|
|
|
+ return index;
|
|
|
|
|
|
- for ( var i = 0; i < sourceTracks.length; ++ i ) {
|
|
|
+ },
|
|
|
|
|
|
- var sourceTrack = sourceTracks[ i ];
|
|
|
- var sourceTrackBinding = PropertyBinding.parseTrackName( sourceTrack.name );
|
|
|
- var sourceTrackNode = PropertyBinding.findNode( root, sourceTrackBinding.nodeName );
|
|
|
+ mergeMorphTargetTracks: function ( clip, root ) {
|
|
|
|
|
|
- if ( sourceTrackBinding.propertyName !== 'morphTargetInfluences' || sourceTrackBinding.propertyIndex === undefined ) {
|
|
|
+ const tracks = [];
|
|
|
+ const mergedTracks = {};
|
|
|
+ const sourceTracks = clip.tracks;
|
|
|
|
|
|
- // Tracks that don't affect morph targets, or that affect all morph targets together, can be left as-is.
|
|
|
- tracks.push( sourceTrack );
|
|
|
- continue;
|
|
|
+ for ( let i = 0; i < sourceTracks.length; ++ i ) {
|
|
|
|
|
|
- }
|
|
|
+ let sourceTrack = sourceTracks[ i ];
|
|
|
+ const sourceTrackBinding = PropertyBinding.parseTrackName( sourceTrack.name );
|
|
|
+ const sourceTrackNode = PropertyBinding.findNode( root, sourceTrackBinding.nodeName );
|
|
|
|
|
|
- if ( sourceTrack.createInterpolant !== sourceTrack.InterpolantFactoryMethodDiscrete
|
|
|
- && sourceTrack.createInterpolant !== sourceTrack.InterpolantFactoryMethodLinear ) {
|
|
|
+ if ( sourceTrackBinding.propertyName !== 'morphTargetInfluences' || sourceTrackBinding.propertyIndex === undefined ) {
|
|
|
|
|
|
- if ( sourceTrack.createInterpolant.isInterpolantFactoryMethodGLTFCubicSpline ) {
|
|
|
+ // Tracks that don't affect morph targets, or that affect all morph targets together, can be left as-is.
|
|
|
+ tracks.push( sourceTrack );
|
|
|
+ continue;
|
|
|
|
|
|
- // This should never happen, because glTF morph target animations
|
|
|
- // affect all targets already.
|
|
|
- throw new Error( 'THREE.GLTFExporter: Cannot merge tracks with glTF CUBICSPLINE interpolation.' );
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
+ if ( sourceTrack.createInterpolant !== sourceTrack.InterpolantFactoryMethodDiscrete
|
|
|
+ && sourceTrack.createInterpolant !== sourceTrack.InterpolantFactoryMethodLinear ) {
|
|
|
|
|
|
- console.warn( 'THREE.GLTFExporter: Morph target interpolation mode not yet supported. Using LINEAR instead.' );
|
|
|
+ if ( sourceTrack.createInterpolant.isInterpolantFactoryMethodGLTFCubicSpline ) {
|
|
|
|
|
|
- sourceTrack = sourceTrack.clone();
|
|
|
- sourceTrack.setInterpolation( InterpolateLinear );
|
|
|
+ // This should never happen, because glTF morph target animations
|
|
|
+ // affect all targets already.
|
|
|
+ throw new Error( 'THREE.GLTFExporter: Cannot merge tracks with glTF CUBICSPLINE interpolation.' );
|
|
|
|
|
|
}
|
|
|
|
|
|
- var targetCount = sourceTrackNode.morphTargetInfluences.length;
|
|
|
- var targetIndex = sourceTrackNode.morphTargetDictionary[ sourceTrackBinding.propertyIndex ];
|
|
|
+ console.warn( 'THREE.GLTFExporter: Morph target interpolation mode not yet supported. Using LINEAR instead.' );
|
|
|
|
|
|
- if ( targetIndex === undefined ) {
|
|
|
+ sourceTrack = sourceTrack.clone();
|
|
|
+ sourceTrack.setInterpolation( InterpolateLinear );
|
|
|
|
|
|
- throw new Error( 'THREE.GLTFExporter: Morph target name not found: ' + sourceTrackBinding.propertyIndex );
|
|
|
-
|
|
|
- }
|
|
|
+ }
|
|
|
|
|
|
- var mergedTrack;
|
|
|
+ const targetCount = sourceTrackNode.morphTargetInfluences.length;
|
|
|
+ const targetIndex = sourceTrackNode.morphTargetDictionary[ sourceTrackBinding.propertyIndex ];
|
|
|
|
|
|
- // If this is the first time we've seen this object, create a new
|
|
|
- // track to store merged keyframe data for each morph target.
|
|
|
- if ( mergedTracks[ sourceTrackNode.uuid ] === undefined ) {
|
|
|
+ if ( targetIndex === undefined ) {
|
|
|
|
|
|
- mergedTrack = sourceTrack.clone();
|
|
|
+ throw new Error( 'THREE.GLTFExporter: Morph target name not found: ' + sourceTrackBinding.propertyIndex );
|
|
|
|
|
|
- var values = new mergedTrack.ValueBufferType( targetCount * mergedTrack.times.length );
|
|
|
+ }
|
|
|
|
|
|
- for ( var j = 0; j < mergedTrack.times.length; j ++ ) {
|
|
|
+ let mergedTrack;
|
|
|
|
|
|
- values[ j * targetCount + targetIndex ] = mergedTrack.values[ j ];
|
|
|
+ // If this is the first time we've seen this object, create a new
|
|
|
+ // track to store merged keyframe data for each morph target.
|
|
|
+ if ( mergedTracks[ sourceTrackNode.uuid ] === undefined ) {
|
|
|
|
|
|
- }
|
|
|
+ mergedTrack = sourceTrack.clone();
|
|
|
|
|
|
- // We need to take into consideration the intended target node
|
|
|
- // of our original un-merged morphTarget animation.
|
|
|
- mergedTrack.name = ( sourceTrackBinding.nodeName || '' ) + '.morphTargetInfluences';
|
|
|
- mergedTrack.values = values;
|
|
|
+ const values = new mergedTrack.ValueBufferType( targetCount * mergedTrack.times.length );
|
|
|
|
|
|
- mergedTracks[ sourceTrackNode.uuid ] = mergedTrack;
|
|
|
- tracks.push( mergedTrack );
|
|
|
+ for ( let j = 0; j < mergedTrack.times.length; j ++ ) {
|
|
|
|
|
|
- continue;
|
|
|
+ values[ j * targetCount + targetIndex ] = mergedTrack.values[ j ];
|
|
|
|
|
|
}
|
|
|
|
|
|
- var sourceInterpolant = sourceTrack.createInterpolant( new sourceTrack.ValueBufferType( 1 ) );
|
|
|
+ // We need to take into consideration the intended target node
|
|
|
+ // of our original un-merged morphTarget animation.
|
|
|
+ mergedTrack.name = ( sourceTrackBinding.nodeName || '' ) + '.morphTargetInfluences';
|
|
|
+ mergedTrack.values = values;
|
|
|
|
|
|
- mergedTrack = mergedTracks[ sourceTrackNode.uuid ];
|
|
|
+ mergedTracks[ sourceTrackNode.uuid ] = mergedTrack;
|
|
|
+ tracks.push( mergedTrack );
|
|
|
|
|
|
- // For every existing keyframe of the merged track, write a (possibly
|
|
|
- // interpolated) value from the source track.
|
|
|
- for ( var j = 0; j < mergedTrack.times.length; j ++ ) {
|
|
|
+ continue;
|
|
|
|
|
|
- mergedTrack.values[ j * targetCount + targetIndex ] = sourceInterpolant.evaluate( mergedTrack.times[ j ] );
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
+ const sourceInterpolant = sourceTrack.createInterpolant( new sourceTrack.ValueBufferType( 1 ) );
|
|
|
|
|
|
- // For every existing keyframe of the source track, write a (possibly
|
|
|
- // new) keyframe to the merged track. Values from the previous loop may
|
|
|
- // be written again, but keyframes are de-duplicated.
|
|
|
- for ( var j = 0; j < sourceTrack.times.length; j ++ ) {
|
|
|
+ mergedTrack = mergedTracks[ sourceTrackNode.uuid ];
|
|
|
|
|
|
- var keyframeIndex = this.insertKeyframe( mergedTrack, sourceTrack.times[ j ] );
|
|
|
- mergedTrack.values[ keyframeIndex * targetCount + targetIndex ] = sourceTrack.values[ j ];
|
|
|
+ // For every existing keyframe of the merged track, write a (possibly
|
|
|
+ // interpolated) value from the source track.
|
|
|
+ for ( let j = 0; j < mergedTrack.times.length; j ++ ) {
|
|
|
|
|
|
- }
|
|
|
+ mergedTrack.values[ j * targetCount + targetIndex ] = sourceInterpolant.evaluate( mergedTrack.times[ j ] );
|
|
|
|
|
|
}
|
|
|
|
|
|
- clip.tracks = tracks;
|
|
|
+ // For every existing keyframe of the source track, write a (possibly
|
|
|
+ // new) keyframe to the merged track. Values from the previous loop may
|
|
|
+ // be written again, but keyframes are de-duplicated.
|
|
|
+ for ( let j = 0; j < sourceTrack.times.length; j ++ ) {
|
|
|
|
|
|
- return clip;
|
|
|
+ const keyframeIndex = this.insertKeyframe( mergedTrack, sourceTrack.times[ j ] );
|
|
|
+ mergedTrack.values[ keyframeIndex * targetCount + targetIndex ] = sourceTrack.values[ j ];
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
}
|
|
|
|
|
|
- };
|
|
|
+ clip.tracks = tracks;
|
|
|
+
|
|
|
+ return clip;
|
|
|
|
|
|
- return GLTFExporter;
|
|
|
+ }
|
|
|
|
|
|
-} )();
|
|
|
+};
|
|
|
|
|
|
export { GLTFExporter };
|