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
70 changes: 45 additions & 25 deletions examples/js/loaders/GLTFLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -1805,6 +1805,32 @@ THREE.GLTFLoader = ( function () {

}

function getNormalizedComponentScale( constructor ) {

// Reference:
// https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_mesh_quantization#encoding-quantized-data

switch ( constructor ) {

case Int8Array:
return 1 / 127;

case Uint8Array:
return 1 / 255;

case Int16Array:
return 1 / 32767;

case Uint16Array:
return 1 / 65535;

default:
throw new Error( 'THREE.GLTFLoader: Unsupported normalized accessor component type.' );

}

}

/* GLTF PARSER */

function GLTFParser( json, options ) {
Expand Down Expand Up @@ -2870,7 +2896,16 @@ THREE.GLTFLoader = ( function () {

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

if ( accessor.normalized ) {

var boxScale = getNormalizedComponentScale( WEBGL_COMPONENT_TYPES[ accessor.componentType ] );
box.min.multiplyScalar( boxScale );
box.max.multiplyScalar( boxScale );

}

} else {

Expand Down Expand Up @@ -2912,6 +2947,14 @@ THREE.GLTFLoader = ( function () {
vector.setY( Math.max( Math.abs( min[ 1 ] ), Math.abs( max[ 1 ] ) ) );
vector.setZ( Math.max( Math.abs( min[ 2 ] ), Math.abs( max[ 2 ] ) ) );


if ( accessor.normalized ) {

var boxScale = getNormalizedComponentScale( WEBGL_COMPONENT_TYPES[ accessor.componentType ] );
vector.multiplyScalar( boxScale );

}

// Note: this assumes that the sum of all weights is at most 1. This isn't quite correct - it's more conservative
// to assume that each target can have a max weight of 1. However, for some use cases - notably, when morph targets
// are used to implement key-frame animations and as such only two are active at a time - this results in very large
Expand Down Expand Up @@ -3482,30 +3525,7 @@ THREE.GLTFLoader = ( function () {

if ( outputAccessor.normalized ) {

var scale;

if ( outputArray.constructor === Int8Array ) {

scale = 1 / 127;

} else if ( outputArray.constructor === Uint8Array ) {

scale = 1 / 255;

} else if ( outputArray.constructor == Int16Array ) {

scale = 1 / 32767;

} else if ( outputArray.constructor === Uint16Array ) {

scale = 1 / 65535;

} else {

throw new Error( 'THREE.GLTFLoader: Unsupported output accessor component type.' );

}

var scale = getNormalizedComponentScale( outputArray.constructor );
var scaled = new Float32Array( outputArray.length );

for ( var j = 0, jl = outputArray.length; j < jl; j ++ ) {
Expand Down
70 changes: 45 additions & 25 deletions examples/jsm/loaders/GLTFLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -1870,6 +1870,32 @@ var GLTFLoader = ( function () {

}

function getNormalizedComponentScale( constructor ) {

// Reference:
// https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_mesh_quantization#encoding-quantized-data

switch ( constructor ) {

case Int8Array:
return 1 / 127;

case Uint8Array:
return 1 / 255;

case Int16Array:
return 1 / 32767;

case Uint16Array:
return 1 / 65535;

default:
throw new Error( 'THREE.GLTFLoader: Unsupported normalized accessor component type.' );

}

}

/* GLTF PARSER */

function GLTFParser( json, options ) {
Expand Down Expand Up @@ -2935,7 +2961,16 @@ var GLTFLoader = ( function () {

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

if ( accessor.normalized ) {

var boxScale = getNormalizedComponentScale( WEBGL_COMPONENT_TYPES[ accessor.componentType ] );
box.min.multiplyScalar( boxScale );
box.max.multiplyScalar( boxScale );

}

} else {

Expand Down Expand Up @@ -2977,6 +3012,14 @@ var GLTFLoader = ( function () {
vector.setY( Math.max( Math.abs( min[ 1 ] ), Math.abs( max[ 1 ] ) ) );
vector.setZ( Math.max( Math.abs( min[ 2 ] ), Math.abs( max[ 2 ] ) ) );


if ( accessor.normalized ) {

var boxScale = getNormalizedComponentScale( WEBGL_COMPONENT_TYPES[ accessor.componentType ] );
vector.multiplyScalar( boxScale );

}

// Note: this assumes that the sum of all weights is at most 1. This isn't quite correct - it's more conservative
// to assume that each target can have a max weight of 1. However, for some use cases - notably, when morph targets
// are used to implement key-frame animations and as such only two are active at a time - this results in very large
Expand Down Expand Up @@ -3547,30 +3590,7 @@ var GLTFLoader = ( function () {

if ( outputAccessor.normalized ) {

var scale;

if ( outputArray.constructor === Int8Array ) {

scale = 1 / 127;

} else if ( outputArray.constructor === Uint8Array ) {

scale = 1 / 255;

} else if ( outputArray.constructor == Int16Array ) {

scale = 1 / 32767;

} else if ( outputArray.constructor === Uint16Array ) {

scale = 1 / 65535;

} else {

throw new Error( 'THREE.GLTFLoader: Unsupported output accessor component type.' );

}

var scale = getNormalizedComponentScale( outputArray.constructor );
var scaled = new Float32Array( outputArray.length );

for ( var j = 0, jl = outputArray.length; j < jl; j ++ ) {
Expand Down