Skip to content

Commit

Permalink
Merge pull request #529 from KhronosGroup/fix/bounding_box
Browse files Browse the repository at this point in the history
Fix calculation of bounding box [518]
  • Loading branch information
UX3D-kanzler authored Jan 17, 2024
2 parents 831cf2b + 1085bf6 commit 2eceb91
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
1 change: 1 addition & 0 deletions source/gltf/accessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ class gltfAccessor extends GltfObject
}

// dequantize can be used to perform the normalization from WebGL2 vertexAttribPointer explicitly
// https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Khronos/KHR_mesh_quantization/README.md#encoding-quantized-data
static dequantize(typedArray, componentType)
{
switch (componentType)
Expand Down
42 changes: 33 additions & 9 deletions source/gltf/gltf_utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { vec3 } from 'gl-matrix';
import { jsToGl } from './utils.js';
import { gltfAccessor } from './accessor.js';

function getSceneExtents(gltf, sceneIndex, outMin, outMax)
{
Expand Down Expand Up @@ -52,19 +53,42 @@ function getSceneExtents(gltf, sceneIndex, outMin, outMax)

function getExtentsFromAccessor(accessor, worldTransform, outMin, outMax)
{
const boxMin = vec3.create();
let min = jsToGl(accessor.min);
if (accessor.normalized){
vec3.normalize(min, min);
let max = jsToGl(accessor.max);

if (accessor.normalized) {
min = gltfAccessor.dequantize(min, accessor.componentType)
max = gltfAccessor.dequantize(max, accessor.componentType)
}
vec3.transformMat4(boxMin, min, worldTransform);

const boxMax = vec3.create();
let max = jsToGl(accessor.max);
if (accessor.normalized){
vec3.normalize(max, max);
// Construct all eight corners from min and max values
let boxVertices = [
vec3.fromValues(min[0], min[1], min[2]),
vec3.fromValues(min[0], min[1], max[2]),
vec3.fromValues(min[0], max[1], min[2]),
vec3.fromValues(min[0], max[1], max[2]),

vec3.fromValues(max[0], min[1], min[2]),
vec3.fromValues(max[0], min[1], max[2]),
vec3.fromValues(max[0], max[1], min[2]),
vec3.fromValues(max[0], max[1], max[2])]


// Transform all bounding box vertices
for(let i in boxVertices) {
vec3.transformMat4(boxVertices[i], boxVertices[i], worldTransform);
}

// Create new (axis-aligned) bounding box out of transformed bounding box
const boxMin = vec3.clone(boxVertices[0]); // initialize
const boxMax = vec3.clone(boxVertices[0]);

for(let i in boxVertices) {
for (const component of [0, 1, 2]) {
boxMin[component] = Math.min(boxMin[component], boxVertices[i][component]);
boxMax[component] = Math.max(boxMax[component], boxVertices[i][component]);
}
}
vec3.transformMat4(boxMax, max, worldTransform);

const center = vec3.create();
vec3.add(center, boxMax, boxMin);
Expand Down

0 comments on commit 2eceb91

Please sign in to comment.