gltf-bounding-box
Advanced tools
Comparing version 0.1.5 to 0.1.6
{ | ||
"name": "gltf-bounding-box", | ||
"version": "0.1.5", | ||
"version": "0.1.6", | ||
"description": "Computes the global bounding box of a gltf model", | ||
@@ -32,28 +32,28 @@ "main": "dist/gltf-bounding-box.js", | ||
"devDependencies": { | ||
"babel-core": "6.14.0", | ||
"babel-loader": "6.2.5", | ||
"babel-core": "6.26.0", | ||
"babel-loader": "7.1.2", | ||
"babel-plugin-add-module-exports": "^0.2.1", | ||
"babel-preset-latest": "6.14.0", | ||
"babel-register": "6.14.0", | ||
"chai": "3.5.0", | ||
"del": "2.2.2", | ||
"glob": "7.0.6", | ||
"babel-preset-latest": "6.24.1", | ||
"babel-register": "6.26.0", | ||
"chai": "4.1.2", | ||
"del": "3.0.0", | ||
"glob": "7.1.2", | ||
"gulp": "3.9.1", | ||
"gulp-eslint": "3.0.1", | ||
"gulp-filter": "4.0.0", | ||
"gulp-istanbul": "1.1.1", | ||
"gulp-eslint": "4.0.0", | ||
"gulp-filter": "5.0.1", | ||
"gulp-istanbul": "1.1.2", | ||
"gulp-livereload": "3.8.1", | ||
"gulp-load-plugins": "1.2.4", | ||
"gulp-mocha": "3.0.1", | ||
"gulp-load-plugins": "1.5.0", | ||
"gulp-mocha": "4.3.1", | ||
"gulp-plumber": "1.1.0", | ||
"gulp-rename": "1.2.2", | ||
"gulp-sourcemaps": "1.6.0", | ||
"gulp-uglify": "2.0.0", | ||
"gulp-sourcemaps": "2.6.1", | ||
"gulp-uglify": "3.0.0", | ||
"isparta": "4.0.0", | ||
"json-loader": "0.5.4", | ||
"mocha": "3.0.2", | ||
"sinon": "1.17.5", | ||
"sinon-chai": "2.8.0", | ||
"webpack": "1.13.2", | ||
"webpack-stream": "3.2.0" | ||
"json-loader": "0.5.7", | ||
"mocha": "3.5.3", | ||
"sinon": "3.3.0", | ||
"sinon-chai": "2.13.0", | ||
"webpack": "3.6.0", | ||
"webpack-stream": "4.0.0" | ||
}, | ||
@@ -60,0 +60,0 @@ "babelBoilerplateOptions": { |
import { Matrix } from 'matrixmath'; | ||
import { flattenDeep, includes } from 'lodash'; | ||
import { loadPositions } from './gltf-reader'; | ||
@@ -7,37 +8,13 @@ const gltfBoundingBox = { | ||
computeBoundings(gltf) { | ||
const meshesTransformMatrices = this.getMeshesTransformMatrices(gltf.nodes); | ||
gltf.loadedBuffers = {}; | ||
// Loop over each mesh of the model to get the position accessors of each of | ||
// its primitives | ||
const boundings = flattenDeep( | ||
Object.keys(gltf.meshes).map( | ||
meshName => gltf.meshes[meshName].primitives.map( | ||
primitive => | ||
// get all the points and retrieve min max | ||
const boundings = this.getMeshesTransformMatrices(gltf.nodes, gltf).reduce((acc, point) => { | ||
acc.min = acc.min.map((elt, i) => elt < point[i] ? elt : point[i]); | ||
acc.max = acc.max.map((elt, i) => elt > point[i] ? elt : point[i]); | ||
return acc; | ||
},{min: [Infinity, Infinity, Infinity], max: [-Infinity, -Infinity, -Infinity]}); | ||
// Get the points from the bounding box of each position accessor | ||
this.getPointsFromBoundings( | ||
gltf.accessors[primitive.attributes.POSITION].min, | ||
gltf.accessors[primitive.attributes.POSITION].max | ||
) | ||
// Multiply by the transform matrix of the mesh | ||
.map(point => Matrix.multiply(point, meshesTransformMatrices[meshName])) | ||
) | ||
) | ||
).reduce((boundings, point) => { | ||
boundings.min[0] = Math.min(boundings.min[0], point[0]); | ||
boundings.min[1] = Math.min(boundings.min[1], point[1]); | ||
boundings.min[2] = Math.min(boundings.min[2], point[2]); | ||
boundings.max[0] = Math.max(boundings.max[0], point[0]); | ||
boundings.max[1] = Math.max(boundings.max[1], point[1]); | ||
boundings.max[2] = Math.max(boundings.max[2], point[2]); | ||
return boundings; | ||
}, { | ||
min: [Infinity, Infinity, Infinity], | ||
max: [-Infinity, -Infinity, -Infinity], | ||
}) | ||
// Return the dimensions of the bounding box | ||
return { | ||
const res = { | ||
dimensions: { | ||
@@ -54,5 +31,7 @@ width: Math.round(boundings.max[0] - boundings.min[0]), | ||
}; | ||
return res; | ||
}, | ||
getMeshesTransformMatrices(nodes) { | ||
getMeshesTransformMatrices(nodes, gltf) { | ||
return Object.keys(nodes) | ||
@@ -70,3 +49,3 @@ | ||
.reduce((tree, { mesh, nodeName }) => { | ||
.reduce((acc, { mesh, nodeName }) => { | ||
@@ -78,6 +57,11 @@ // Climb up the tree to retrieve all the transform matrices | ||
// Compute the global transform matrix | ||
tree[mesh] = Matrix.multiply(...matrices); | ||
const matrix = Matrix.multiply(...matrices); | ||
const positions = this.getPointsFromArray(loadPositions(gltf, mesh)); | ||
return tree; | ||
}, {}) | ||
const transformedPoints = positions.map(point => Matrix.multiply(point, matrix)); | ||
acc.push(...transformedPoints); | ||
return acc; | ||
}, []); | ||
}, | ||
@@ -106,32 +90,13 @@ | ||
getPointsFromBoundings(min, max) { | ||
return [ | ||
new Matrix(1, 4, false).setData([ | ||
min[0], min[1], min[2], 1, | ||
]), | ||
new Matrix(1, 4, false).setData([ | ||
min[0], min[1], max[2], 1, | ||
]), | ||
new Matrix(1, 4, false).setData([ | ||
min[0], max[1], min[2], 1, | ||
]), | ||
new Matrix(1, 4, false).setData([ | ||
min[0], max[1], max[2], 1, | ||
]), | ||
new Matrix(1, 4, false).setData([ | ||
max[0], min[1], min[2], 1, | ||
]), | ||
new Matrix(1, 4, false).setData([ | ||
max[0], min[1], max[2], 1, | ||
]), | ||
new Matrix(1, 4, false).setData([ | ||
max[0], max[1], min[2], 1, | ||
]), | ||
new Matrix(1, 4, false).setData([ | ||
max[0], max[1], max[2], 1, | ||
]), | ||
] | ||
getPointsFromArray(array) { | ||
const res = []; | ||
for(let i = 0; i< array.length ; i+=3) { | ||
res.push(new Matrix(1,4,false).setData([array[i], array[i+1], array[i+2], 1])); | ||
} | ||
return res; | ||
}, | ||
}; | ||
export default gltfBoundingBox; |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2381104
15
19669
1