Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions examples/js/loaders/GLTFLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -2285,12 +2285,13 @@ THREE.GLTFLoader = ( function () {
var max = accessor.max;

box.set(
new THREE.Vector3( min[0], min[1], min[2] ),
new THREE.Vector3( max[0], max[1], max[2] ) );
new THREE.Vector3( min[ 0 ], min[ 1 ], min[ 2 ] ),
new THREE.Vector3( max[ 0 ], max[ 1 ], max[ 2 ] ) );

} else {

return;

}

var targets = primitiveDef.targets;
Expand All @@ -2310,9 +2311,9 @@ THREE.GLTFLoader = ( function () {
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] ) ) );
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 );

Expand Down
72 changes: 72 additions & 0 deletions examples/jsm/loaders/GLTFLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import {
AnimationClip,
Bone,
Box3,
BufferAttribute,
BufferGeometry,
ClampToEdgeWrapping,
Expand Down Expand Up @@ -59,12 +60,14 @@ import {
ShaderMaterial,
Skeleton,
SkinnedMesh,
Sphere,
SpotLight,
TextureLoader,
TriangleFanDrawMode,
TriangleStripDrawMode,
UniformsUtils,
Vector2,
Vector3,
VectorKeyframeTrack,
VertexColors,
sRGBEncoding
Expand Down Expand Up @@ -2331,6 +2334,73 @@ var GLTFLoader = ( function () {

};

/**
* @param {BufferGeometry} geometry
* @param {GLTF.Primitive} primitiveDef
* @param {GLTFParser} parser
*/
function computeBounds( geometry, primitiveDef, parser ) {

var attributes = primitiveDef.attributes;

var box = new Box3();

if ( attributes.POSITION !== undefined ) {

var accessor = parser.json.accessors[ attributes.POSITION ];
var min = accessor.min;
var max = accessor.max;

box.set(
new Vector3( min[ 0 ], min[ 1 ], min[ 2 ] ),
new Vector3( max[ 0 ], max[ 1 ], max[ 2 ] ) );

} else {

return;

}

var targets = primitiveDef.targets;

if ( targets !== undefined ) {

var vector = new 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 Sphere();

box.getCenter( sphere.center );
sphere.radius = box.min.distanceTo( box.max ) / 2;

geometry.boundingSphere = sphere;

}

/**
* @param {BufferGeometry} geometry
* @param {GLTF.Primitive} primitiveDef
Expand Down Expand Up @@ -2379,6 +2449,8 @@ var GLTFLoader = ( function () {

assignExtrasToUserData( geometry, primitiveDef );

computeBounds( geometry, primitiveDef, parser );

return Promise.all( pending ).then( function () {

return primitiveDef.targets !== undefined
Expand Down