From 3c0f915fc4cf86c7396adc730f8b261b3c9af42f Mon Sep 17 00:00:00 2001 From: Avaer Kazmer Date: Thu, 21 Nov 2019 05:19:48 -0500 Subject: [PATCH] Update GLTFLoader/GLTFExporter --- GLTFExporter.js | 20 +++-- GLTFLoader.js | 206 +++++++++++++++++++++--------------------------- 2 files changed, 102 insertions(+), 124 deletions(-) diff --git a/GLTFExporter.js b/GLTFExporter.js index c996cf1..15a41d7 100644 --- a/GLTFExporter.js +++ b/GLTFExporter.js @@ -1339,14 +1339,18 @@ THREE.GLTFExporter.prototype = { // Clones attribute not to override var relativeAttribute = attribute.clone(); - for ( var 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 ) - ); + if ( ! geometry.morphTargetsRelative ) { + + for ( var 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 ) + ); + + } } diff --git a/GLTFLoader.js b/GLTFLoader.js index 8c31d19..5b92a19 100644 --- a/GLTFLoader.js +++ b/GLTFLoader.js @@ -181,11 +181,15 @@ THREE.GLTFLoader = ( function () { break; case EXTENSIONS.MSFT_TEXTURE_DDS: - extensions[ EXTENSIONS.MSFT_TEXTURE_DDS ] = new GLTFTextureDDSExtension( this.ddsLoader ); + extensions[ extensionName ] = new GLTFTextureDDSExtension( this.ddsLoader ); break; case EXTENSIONS.KHR_TEXTURE_TRANSFORM: - extensions[ EXTENSIONS.KHR_TEXTURE_TRANSFORM ] = new GLTFTextureTransformExtension(); + extensions[ extensionName ] = new GLTFTextureTransformExtension(); + break; + + case EXTENSIONS.KHR_MESH_QUANTIZATION: + extensions[ extensionName ] = new GLTFMeshQuantizationExtension(); break; default: @@ -263,6 +267,7 @@ THREE.GLTFLoader = ( function () { KHR_MATERIALS_PBR_SPECULAR_GLOSSINESS: 'KHR_materials_pbrSpecularGlossiness', KHR_MATERIALS_UNLIT: 'KHR_materials_unlit', KHR_TEXTURE_TRANSFORM: 'KHR_texture_transform', + KHR_MESH_QUANTIZATION: 'KHR_mesh_quantization', MSFT_TEXTURE_DDS: 'MSFT_texture_dds' }; @@ -981,6 +986,17 @@ THREE.GLTFLoader = ( function () { } + /** + * Mesh Quantization Extension + * + * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_mesh_quantization + */ + function GLTFMeshQuantizationExtension() { + + this.name = EXTENSIONS.KHR_MESH_QUANTIZATION; + + } + /*********************************/ /********** INTERPOLATION ********/ /*********************************/ @@ -1309,95 +1325,9 @@ THREE.GLTFLoader = ( function () { var morphPositions = accessors[ 0 ]; var morphNormals = accessors[ 1 ]; - // Clone morph target accessors before modifying them. - - for ( var i = 0, il = morphPositions.length; i < il; i ++ ) { - - if ( geometry.attributes.position === morphPositions[ i ] ) continue; - - morphPositions[ i ] = cloneBufferAttribute( morphPositions[ i ] ); - - } - - for ( var i = 0, il = morphNormals.length; i < il; i ++ ) { - - if ( geometry.attributes.normal === morphNormals[ i ] ) continue; - - morphNormals[ i ] = cloneBufferAttribute( morphNormals[ i ] ); - - } - - for ( var i = 0, il = targets.length; i < il; i ++ ) { - - var target = targets[ i ]; - var attributeName = 'morphTarget' + i; - - if ( hasMorphPosition ) { - - // Three.js morph position is absolute value. The formula is - // basePosition - // + weight0 * ( morphPosition0 - basePosition ) - // + weight1 * ( morphPosition1 - basePosition ) - // ... - // while the glTF one is relative - // basePosition - // + weight0 * glTFmorphPosition0 - // + weight1 * glTFmorphPosition1 - // ... - // then we need to convert from relative to absolute here. - - if ( target.POSITION !== undefined ) { - - var positionAttribute = morphPositions[ i ]; - positionAttribute.name = attributeName; - - var position = geometry.attributes.position; - - for ( var j = 0, jl = positionAttribute.count; j < jl; j ++ ) { - - positionAttribute.setXYZ( - j, - positionAttribute.getX( j ) + position.getX( j ), - positionAttribute.getY( j ) + position.getY( j ), - positionAttribute.getZ( j ) + position.getZ( j ) - ); - - } - - } - - } - - if ( hasMorphNormal ) { - - // see target.POSITION's comment - - if ( target.NORMAL !== undefined ) { - - var normalAttribute = morphNormals[ i ]; - normalAttribute.name = attributeName; - - var normal = geometry.attributes.normal; - - for ( var j = 0, jl = normalAttribute.count; j < jl; j ++ ) { - - normalAttribute.setXYZ( - j, - normalAttribute.getX( j ) + normal.getX( j ), - normalAttribute.getY( j ) + normal.getY( j ), - normalAttribute.getZ( j ) + normal.getZ( j ) - ); - - } - - } - - } - - } - if ( hasMorphPosition ) geometry.morphAttributes.position = morphPositions; if ( hasMorphNormal ) geometry.morphAttributes.normal = morphNormals; + geometry.morphTargetsRelative = true; return geometry; @@ -1485,31 +1415,6 @@ THREE.GLTFLoader = ( function () { } - function cloneBufferAttribute( attribute ) { - - if ( attribute.isInterleavedBufferAttribute ) { - - var count = attribute.count; - var itemSize = attribute.itemSize; - var array = attribute.array.slice( 0, count * itemSize ); - - for ( var i = 0, j = 0; i < count; ++ i ) { - - array[ j ++ ] = attribute.getX( i ); - if ( itemSize >= 2 ) array[ j ++ ] = attribute.getY( i ); - if ( itemSize >= 3 ) array[ j ++ ] = attribute.getZ( i ); - if ( itemSize >= 4 ) array[ j ++ ] = attribute.getW( i ); - - } - - return new THREE.BufferAttribute( array, itemSize, attribute.normalized ); - - } - - return attribute.clone(); - - } - /* GLTF PARSER */ function GLTFParser( json, extensions, options ) { @@ -2185,7 +2090,7 @@ THREE.GLTFLoader = ( function () { if ( material.aoMap && geometry.attributes.uv2 === undefined && geometry.attributes.uv !== undefined ) { console.log( 'THREE.GLTFLoader: Duplicating UVs to support aoMap.' ); - geometry.addAttribute( 'uv2', new THREE.BufferAttribute( geometry.attributes.uv.array, 2 ) ); + geometry.setAttribute( 'uv2', new THREE.BufferAttribute( geometry.attributes.uv.array, 2 ) ); } @@ -2362,6 +2267,73 @@ THREE.GLTFLoader = ( function () { }; + /** + * @param {THREE.BufferGeometry} geometry + * @param {GLTF.Primitive} primitiveDef + * @param {GLTFParser} parser + */ + function computeBounds( geometry, primitiveDef, parser ) { + + var attributes = primitiveDef.attributes; + + var box = new THREE.Box3(); + + if ( attributes.POSITION !== undefined ) { + + var accessor = parser.json.accessors[ attributes.POSITION ]; + var min = accessor.min; + var max = accessor.max; + + box.set( + new THREE.Vector3( min[ 0 ], min[ 1 ], min[ 2 ] ), + new THREE.Vector3( max[ 0 ], max[ 1 ], max[ 2 ] ) ); + + } else { + + return; + + } + + var targets = primitiveDef.targets; + + if ( targets !== undefined ) { + + var vector = new THREE.Vector3(); + + for ( var i = 0, il = targets.length; i < il; i ++ ) { + + var target = targets[ i ]; + + if ( target.POSITION !== undefined ) { + + var accessor = parser.json.accessors[ target.POSITION ]; + var min = accessor.min; + var max = accessor.max; + + // we need to get max of absolute components because target weight is [-1,1] + vector.setX( Math.max( Math.abs( min[ 0 ] ), Math.abs( max[ 0 ] ) ) ); + vector.setY( Math.max( Math.abs( min[ 1 ] ), Math.abs( max[ 1 ] ) ) ); + vector.setZ( Math.max( Math.abs( min[ 2 ] ), Math.abs( max[ 2 ] ) ) ); + + box.expandByVector( vector ); + + } + + } + + } + + geometry.boundingBox = box; + + var sphere = new THREE.Sphere(); + + box.getCenter( sphere.center ); + sphere.radius = box.min.distanceTo( box.max ) / 2; + + geometry.boundingSphere = sphere; + + } + /** * @param {THREE.BufferGeometry} geometry * @param {GLTF.Primitive} primitiveDef @@ -2379,7 +2351,7 @@ THREE.GLTFLoader = ( function () { return parser.getDependency( 'accessor', accessorIndex ) .then( function ( accessor ) { - geometry.addAttribute( attributeName, accessor ); + geometry.setAttribute( attributeName, accessor ); } ); @@ -2410,6 +2382,8 @@ THREE.GLTFLoader = ( function () { assignExtrasToUserData( geometry, primitiveDef ); + computeBounds( geometry, primitiveDef, parser ); + return Promise.all( pending ).then( function () { return primitiveDef.targets !== undefined