diff --git a/packages/core/src/core.ts b/packages/core/src/core.ts index 57c86d498..02accb299 100644 --- a/packages/core/src/core.ts +++ b/packages/core/src/core.ts @@ -38,6 +38,7 @@ export { Logger, MathUtils, Verbosity, + getBounds, bounds, uuid, } from './utils'; diff --git a/packages/core/src/utils/bounds.ts b/packages/core/src/utils/get-bounds.ts similarity index 91% rename from packages/core/src/utils/bounds.ts rename to packages/core/src/utils/get-bounds.ts index 12b0cbeeb..fd23fe000 100644 --- a/packages/core/src/utils/bounds.ts +++ b/packages/core/src/utils/get-bounds.ts @@ -8,10 +8,10 @@ import type { Mesh, Node, Scene } from '../properties'; * Example: * * ```ts - * const {min, max} = bounds(scene); + * const {min, max} = getBounds(scene); * ``` */ -export function bounds(node: Node | Scene): bbox { +export function getBounds(node: Node | Scene): bbox { const resultBounds = createBounds(); const parents = node.propertyType === PropertyType.NODE ? [node] : node.listChildren(); @@ -30,6 +30,12 @@ export function bounds(node: Node | Scene): bbox { return resultBounds; } +/** + * @deprecated Renamed to {@link getBounds}. + * @hidden + */ +export const bounds = getBounds; + /** Computes mesh bounds in local space. */ function getMeshBounds(mesh: Mesh, worldMatrix: mat4): bbox { const meshBounds = createBounds(); diff --git a/packages/core/src/utils/index.ts b/packages/core/src/utils/index.ts index a9a5de9fa..1649de37f 100644 --- a/packages/core/src/utils/index.ts +++ b/packages/core/src/utils/index.ts @@ -1,4 +1,4 @@ -export * from './bounds'; +export * from './get-bounds'; export * from './buffer-utils'; export * from './color-utils'; export * from './file-utils'; diff --git a/packages/core/test/utils/bounds.test.ts b/packages/core/test/utils/get-bounds.test.ts similarity index 83% rename from packages/core/test/utils/bounds.test.ts rename to packages/core/test/utils/get-bounds.test.ts index 51cef91d8..5e618c204 100644 --- a/packages/core/test/utils/bounds.test.ts +++ b/packages/core/test/utils/get-bounds.test.ts @@ -1,8 +1,8 @@ import test from 'tape'; import { Accessor, Document } from '@gltf-transform/core'; -import { bounds } from '@gltf-transform/core'; +import { getBounds } from '@gltf-transform/core'; -test('@gltf-transform/functions::bounds', (t) => { +test('@gltf-transform/functions::getBounds', (t) => { const doc = new Document(); const position = doc .createAccessor() @@ -14,7 +14,7 @@ test('@gltf-transform/functions::bounds', (t) => { const scene = doc.createScene().addChild(node); t.deepEquals( - bounds(scene), + getBounds(scene), { min: [100, 100, 100], max: [105, 105, 105], diff --git a/packages/extensions/src/khr-draco-mesh-compression/draco-mesh-compression.ts b/packages/extensions/src/khr-draco-mesh-compression/draco-mesh-compression.ts index fb4d1107d..58193b28f 100644 --- a/packages/extensions/src/khr-draco-mesh-compression/draco-mesh-compression.ts +++ b/packages/extensions/src/khr-draco-mesh-compression/draco-mesh-compression.ts @@ -1,7 +1,7 @@ import { Accessor, bbox, - bounds, + getBounds, BufferUtils, Document, Extension, @@ -244,7 +244,7 @@ export class DracoMeshCompression extends Extension { if (this.document.getRoot().listScenes().length !== 1) { logger.warn(`[${NAME}]: quantizationVolume=scene requires exactly 1 scene.`); } else { - quantizationVolume = bounds(this.document.getRoot().listScenes().pop()!); + quantizationVolume = getBounds(this.document.getRoot().listScenes().pop()!); } } diff --git a/packages/extensions/test/draco-mesh-compression.test.ts b/packages/extensions/test/draco-mesh-compression.test.ts index 9db59d858..33f278794 100644 --- a/packages/extensions/test/draco-mesh-compression.test.ts +++ b/packages/extensions/test/draco-mesh-compression.test.ts @@ -3,8 +3,7 @@ require('source-map-support').install(); import path from 'path'; import { createDecoderModule, createEncoderModule } from 'draco3dgltf'; import test from 'tape'; -import { Accessor, Buffer, Document, Format, NodeIO, Primitive } from '@gltf-transform/core'; -import { bounds } from '@gltf-transform/functions'; +import { Accessor, Buffer, Document, Format, NodeIO, Primitive, getBounds } from '@gltf-transform/core'; import { DracoMeshCompression } from '../'; const throwsAsync = async (t: test.Test, fn: () => Promise, re: RegExp, msg: string): Promise => { @@ -19,7 +18,7 @@ const throwsAsync = async (t: test.Test, fn: () => Promise, re: RegExp, test('@gltf-transform/extensions::draco-mesh-compression | decoding', async (t) => { const io = await createDecoderIO(); const doc = await io.read(path.join(__dirname, 'in', 'BoxDraco.gltf')); - const bbox = bounds(doc.getRoot().listScenes()[0]); + const bbox = getBounds(doc.getRoot().listScenes()[0]); t.deepEquals( bbox.min.map((v) => +v.toFixed(3)), [-0.5, -0.5, -0.5], @@ -123,7 +122,7 @@ test('@gltf-transform/extensions::draco-mesh-compression | encoding complete', a io = await createDecoderIO(); const roundtripDoc = await io.readJSON(jsonDoc); const roundtripNode = roundtripDoc.getRoot().listNodes()[0]; - const bbox = bounds(roundtripNode); + const bbox = getBounds(roundtripNode); t.deepEquals( bbox.min.map((v) => +v.toFixed(3)), [0, -1, -1], diff --git a/packages/extensions/test/meshopt-compression.test.ts b/packages/extensions/test/meshopt-compression.test.ts index 5704fcf19..357d0f9d5 100644 --- a/packages/extensions/test/meshopt-compression.test.ts +++ b/packages/extensions/test/meshopt-compression.test.ts @@ -2,8 +2,7 @@ require('source-map-support').install(); import path from 'path'; import test from 'tape'; -import { NodeIO } from '@gltf-transform/core'; -import { bounds } from '@gltf-transform/functions'; +import { NodeIO, getBounds } from '@gltf-transform/core'; import { MeshoptCompression, MeshQuantization } from '../'; import { MeshoptDecoder, MeshoptEncoder } from 'meshoptimizer'; @@ -18,7 +17,7 @@ test('@gltf-transform/extensions::draco-mesh-compression | decoding', async (t) for (const input of INPUTS) { const doc = await io.read(path.join(__dirname, 'in', input)); - const bbox = bounds(doc.getRoot().listScenes()[0]); + const bbox = getBounds(doc.getRoot().listScenes()[0]); t.deepEquals( bbox.min.map((v) => +v.toFixed(3)), [-0.5, -0.5, -0.5], @@ -50,7 +49,7 @@ test('@gltf-transform/extensions::draco-mesh-compression | encoding', async (t) .getRoot() .listExtensionsRequired() .map((ext) => ext.extensionName); - const bbox = bounds(doc.getRoot().listScenes()[0]); + const bbox = getBounds(doc.getRoot().listScenes()[0]); t.ok(extensionsRequired.includes('EXT_meshopt_compression'), 'retains EXT_meshopt_compression'); t.deepEquals( diff --git a/packages/functions/src/center.ts b/packages/functions/src/center.ts index f169569af..4d5862333 100644 --- a/packages/functions/src/center.ts +++ b/packages/functions/src/center.ts @@ -1,5 +1,5 @@ import type { Document, Transform, vec3 } from '@gltf-transform/core'; -import { bounds } from '@gltf-transform/core'; +import { getBounds } from '@gltf-transform/core'; import { createTransform } from './utils'; const NAME = 'center'; @@ -37,7 +37,7 @@ export function center(_options: CenterOptions = CENTER_DEFAULTS): Transform { let pivot: vec3; if (typeof options.pivot === 'string') { - const bbox = bounds(scene); + const bbox = getBounds(scene); pivot = [ (bbox.max[0] - bbox.min[0]) / 2 + bbox.min[0], (bbox.max[1] - bbox.min[1]) / 2 + bbox.min[1], diff --git a/packages/functions/src/index.ts b/packages/functions/src/index.ts index 2fa6917c9..4e991dd63 100644 --- a/packages/functions/src/index.ts +++ b/packages/functions/src/index.ts @@ -37,7 +37,6 @@ * @module functions */ -export { bounds } from '@gltf-transform/core'; // backwards compatibility, remove in v0.12 export * from './center'; export * from './colorspace'; export * from './dedup'; diff --git a/packages/functions/src/inspect.ts b/packages/functions/src/inspect.ts index a730c1065..5e380aaeb 100644 --- a/packages/functions/src/inspect.ts +++ b/packages/functions/src/inspect.ts @@ -5,7 +5,7 @@ import { GLTF, ImageUtils, Texture, - bounds, + getBounds, PropertyType, } from '@gltf-transform/core'; import { getGLPrimitiveCount } from './utils'; @@ -29,7 +29,7 @@ function listScenes(doc: Document): InspectPropertyReport { .listScenes() .map((scene) => { const root = scene.listChildren()[0]; - const sceneBounds = bounds(scene); + const sceneBounds = getBounds(scene); return { name: scene.getName(), rootName: root ? root.getName() : '', diff --git a/packages/functions/test/center.test.ts b/packages/functions/test/center.test.ts index dbfbb7357..b19b211d2 100644 --- a/packages/functions/test/center.test.ts +++ b/packages/functions/test/center.test.ts @@ -1,8 +1,8 @@ require('source-map-support').install(); import test from 'tape'; -import { Accessor, Document } from '@gltf-transform/core'; -import { bounds, center } from '../'; +import { Accessor, Document, getBounds } from '@gltf-transform/core'; +import { center } from '../'; test('@gltf-transform/functions::center', async (t) => { const doc = new Document(); @@ -18,7 +18,7 @@ test('@gltf-transform/functions::center', async (t) => { await doc.transform(center({ pivot: 'center' })); t.deepEquals( - bounds(scene), + getBounds(scene), { min: [-2.5, -2.5, -2.5], max: [2.5, 2.5, 2.5], @@ -29,7 +29,7 @@ test('@gltf-transform/functions::center', async (t) => { await doc.transform(center({ pivot: 'above' })); t.deepEquals( - bounds(scene), + getBounds(scene), { min: [-2.5, -5.0, -2.5], max: [2.5, 0.0, 2.5], @@ -40,7 +40,7 @@ test('@gltf-transform/functions::center', async (t) => { await doc.transform(center({ pivot: 'below' })); t.deepEquals( - bounds(scene), + getBounds(scene), { min: [-2.5, 0.0, -2.5], max: [2.5, 5.0, 2.5], diff --git a/packages/functions/test/dequantize.test.ts b/packages/functions/test/dequantize.test.ts index cf1c93e07..cf7badce7 100644 --- a/packages/functions/test/dequantize.test.ts +++ b/packages/functions/test/dequantize.test.ts @@ -1,7 +1,7 @@ require('source-map-support').install(); import test from 'tape'; -import { bbox, bounds, Document, Logger, Primitive, PrimitiveTarget, Scene, vec3 } from '@gltf-transform/core'; +import { bbox, getBounds, Document, Logger, Primitive, PrimitiveTarget, Scene, vec3 } from '@gltf-transform/core'; import { dequantize } from '../'; const logger = new Logger(Logger.Verbosity.WARN); @@ -12,8 +12,8 @@ test('@gltf-transform/functions::dequantize', async (t) => { const node = doc.getRoot().listNodes()[0]; const prim = doc.getRoot().listMeshes()[0].listPrimitives()[0]; - const bboxScenePrev = bounds(scene); - const bboxNodePrev = bounds(node); + const bboxScenePrev = getBounds(scene); + const bboxNodePrev = getBounds(node); const bboxMeshPrev = primBounds(prim); t.deepEquals(bboxScenePrev, { min: [0, 0, 0], max: [50, 50, 50] }, 'original bbox - scene'); @@ -22,8 +22,8 @@ test('@gltf-transform/functions::dequantize', async (t) => { await doc.transform(dequantize()); - const bboxScene = bounds(scene); - const bboxNode = bounds(node); + const bboxScene = getBounds(scene); + const bboxNode = getBounds(node); const bboxMesh = primBounds(prim); t.ok(prim.getAttribute('POSITION').getArray() instanceof Float32Array, 'position - float32'); diff --git a/packages/functions/test/quantize.test.ts b/packages/functions/test/quantize.test.ts index 3a51da7ff..689d9be90 100644 --- a/packages/functions/test/quantize.test.ts +++ b/packages/functions/test/quantize.test.ts @@ -5,7 +5,7 @@ import { Accessor, AnimationChannel, bbox, - bounds, + getBounds, Document, Logger, Mesh, @@ -45,9 +45,9 @@ test('@gltf-transform/functions::quantize | mesh volume', async (t) => { const primA = doc.getRoot().listMeshes()[0].listPrimitives()[0]; const primB = doc.getRoot().listMeshes()[1].listPrimitives()[0]; - const bboxSceneCopy = bounds(scene); - const bboxNodeACopy = bounds(nodeA); - const bboxNodeBCopy = bounds(nodeB); + const bboxSceneCopy = getBounds(scene); + const bboxNodeACopy = getBounds(nodeA); + const bboxNodeBCopy = getBounds(nodeB); const bboxMeshACopy = primBounds(primA); const bboxMeshBCopy = primBounds(primB); @@ -59,9 +59,9 @@ test('@gltf-transform/functions::quantize | mesh volume', async (t) => { await doc.transform(quantize({ quantizePosition: 14 })); - const bboxScene = bounds(scene); - const bboxNodeA = bounds(nodeA); - const bboxNodeB = bounds(nodeB); + const bboxScene = getBounds(scene); + const bboxNodeA = getBounds(nodeA); + const bboxNodeB = getBounds(nodeB); const bboxMeshA = primBounds(primA); const bboxMeshB = primBounds(primB); @@ -84,9 +84,9 @@ test('@gltf-transform/functions::quantize | scene volume', async (t) => { const primA = doc.getRoot().listMeshes()[0].listPrimitives()[0]; const primB = doc.getRoot().listMeshes()[1].listPrimitives()[0]; - const bboxSceneCopy = bounds(scene); - const bboxNodeACopy = bounds(nodeA); - const bboxNodeBCopy = bounds(nodeB); + const bboxSceneCopy = getBounds(scene); + const bboxNodeACopy = getBounds(nodeA); + const bboxNodeBCopy = getBounds(nodeB); const bboxMeshACopy = primBounds(primA); const bboxMeshBCopy = primBounds(primB); @@ -103,9 +103,9 @@ test('@gltf-transform/functions::quantize | scene volume', async (t) => { }) ); - const bboxScene = roundBbox(bounds(scene), 3); - const bboxNodeA = roundBbox(bounds(nodeA), 2); - const bboxNodeB = roundBbox(bounds(nodeB), 3); + const bboxScene = roundBbox(getBounds(scene), 3); + const bboxNodeA = roundBbox(getBounds(nodeA), 2); + const bboxNodeB = roundBbox(getBounds(nodeB), 3); const bboxMeshA = roundBbox(primBounds(primA), 3); const bboxMeshB = roundBbox(primBounds(primB), 3); @@ -128,9 +128,9 @@ test('@gltf-transform/functions::quantize | skinned mesh', async (t) => { const primA = doc.getRoot().listMeshes()[0].listPrimitives()[0]; const primB = doc.getRoot().listMeshes()[1].listPrimitives()[0]; - const bboxSceneCopy = bounds(scene); - const bboxNodeACopy = bounds(nodeA); - const bboxNodeBCopy = bounds(nodeB); + const bboxSceneCopy = getBounds(scene); + const bboxNodeACopy = getBounds(nodeA); + const bboxNodeBCopy = getBounds(nodeB); const bboxMeshACopy = primBounds(primA); const bboxMeshBCopy = primBounds(primB); @@ -152,13 +152,13 @@ test('@gltf-transform/functions::quantize | skinned mesh', async (t) => { await doc.transform(quantize({ quantizePosition: 14 })); - const bboxScene = bounds(scene); - const bboxNodeA = bounds(nodeA); - const bboxNodeB = bounds(nodeB); + const bboxScene = getBounds(scene); + const bboxNodeA = getBounds(nodeA); + const bboxNodeB = getBounds(nodeB); const bboxMeshA = primBounds(primA); const bboxMeshB = primBounds(primB); - // NodeA now affects scene bounds, because bounds() does not check IBMs. + // NodeA now affects scene bounds, because getBounds() does not check IBMs. t.deepEquals(bboxScene, { min: [-0.5, -0.5, -0.5], max: [25, 15, 0.5] }, 'bbox - scene'); t.deepEquals(bboxNodeA, { min: [20, 10, 0], max: [25, 15, 0] }, 'bbox - nodeA'); t.deepEquals(bboxNodeB, { min: [-0.5, -0.5, -0.5], max: [0.5, 0.5, 0.5] }, 'bbox - nodeB'); @@ -192,7 +192,7 @@ test('@gltf-transform/functions::quantize | morph targets', async (t) => { const scene = doc.getRoot().listScenes().pop(); - const bboxSceneCopy = bounds(scene); + const bboxSceneCopy = getBounds(scene); const bboxMeshCopy = primBounds(prim); const bboxTargetCopy = primBounds(target); @@ -202,7 +202,7 @@ test('@gltf-transform/functions::quantize | morph targets', async (t) => { await doc.transform(quantize({ quantizePosition: 14 })); - const bboxScene = roundBbox(bounds(scene), 2); + const bboxScene = roundBbox(getBounds(scene), 2); const bboxMesh = roundBbox(primBounds(prim), 3); const bboxTarget = roundBbox(primBounds(target), 3); diff --git a/packages/functions/test/simplify.test.ts b/packages/functions/test/simplify.test.ts index 96b1aaf90..f73c8b1d6 100644 --- a/packages/functions/test/simplify.test.ts +++ b/packages/functions/test/simplify.test.ts @@ -2,7 +2,7 @@ require('source-map-support').install(); import test from 'tape'; import path from 'path'; -import { bbox, bounds, Document, Logger, NodeIO, Primitive, vec3 } from '@gltf-transform/core'; +import { bbox, getBounds, Document, Logger, NodeIO, Primitive, vec3 } from '@gltf-transform/core'; import { DracoMeshCompression, MeshQuantization } from '@gltf-transform/extensions'; import { weld, unweld, simplify } from '../'; import { MeshoptSimplifier } from 'meshoptimizer'; @@ -25,12 +25,12 @@ test('@gltf-transform/functions::simplify | welded', async (t) => { const scene = document.getRoot().getDefaultScene()!; const srcCount = getVertexCount(document); - const srcBounds = roundBbox(bounds(scene), 2); + const srcBounds = roundBbox(getBounds(scene), 2); await document.transform(weld(), simplify({ simplifier: MeshoptSimplifier, ratio: 0.5 })); const dstCount = getVertexCount(document); - const dstBounds = roundBbox(bounds(scene), 2); + const dstBounds = roundBbox(getBounds(scene), 2); t.ok((srcCount - dstCount) / srcCount > 0.5, '≥50% reduction'); t.ok(srcCount > dstCount, 'src.count > dst.count'); @@ -44,12 +44,12 @@ test('@gltf-transform/functions::simplify | unwelded', async (t) => { const scene = document.getRoot().getDefaultScene()!; const srcCount = getVertexCount(document); - const srcBounds = roundBbox(bounds(scene), 2); + const srcBounds = roundBbox(getBounds(scene), 2); await document.transform(unweld(), simplify({ simplifier: MeshoptSimplifier, ratio: 0.5 })); const dstCount = getVertexCount(document); - const dstBounds = roundBbox(bounds(scene), 2); + const dstBounds = roundBbox(getBounds(scene), 2); t.ok((srcCount - dstCount) / srcCount > 0.5, '≥50% reduction'); t.ok(srcCount > dstCount, 'src.count > dst.count'); @@ -80,12 +80,12 @@ test('@gltf-transform/functions::simplify | shared accessors', async (t) => { scene.addChild(nodeA).addChild(nodeB); const srcCount = getVertexCount(document); - const srcBounds = roundBbox(bounds(scene), 2); + const srcBounds = roundBbox(getBounds(scene), 2); await document.transform(unweld(), simplify({ simplifier: MeshoptSimplifier, ratio: 0.5 })); const dstCount = getVertexCount(document); - const dstBounds = roundBbox(bounds(scene), 2); + const dstBounds = roundBbox(getBounds(scene), 2); t.ok((srcCount - dstCount) / srcCount > 0.5, '≥50% reduction'); t.ok(srcCount > dstCount, 'src.count > dst.count');