@gltf-transform/functions
Advanced tools
Comparing version 4.0.0-alpha.4 to 4.0.0-alpha.5
@@ -50,4 +50,6 @@ import type { NdArray } from 'ndarray'; | ||
/** @hidden */ | ||
export declare function remapAttribute(attribute: Accessor, remap: TypedArray, dstCount: number): Accessor; | ||
export declare function remapAttribute(srcAttribute: Accessor, remap: TypedArray, dstCount: number, dstAttribute?: Accessor): Accessor; | ||
/** @hidden */ | ||
export declare function remapIndices(srcIndices: Accessor, remap: TypedArray, dstOffset: number, dstCount: number, dstIndices?: Accessor): Accessor; | ||
/** @hidden */ | ||
export declare function createIndices(count: number, maxIndex?: number): Uint16Array | Uint32Array; | ||
@@ -54,0 +56,0 @@ /** @hidden */ |
{ | ||
"name": "@gltf-transform/functions", | ||
"version": "4.0.0-alpha.4", | ||
"version": "4.0.0-alpha.5", | ||
"repository": "github:donmccurdy/glTF-Transform", | ||
@@ -38,4 +38,4 @@ "homepage": "https://gltf-transform.dev/functions.html", | ||
"dependencies": { | ||
"@gltf-transform/core": "^4.0.0-alpha.4", | ||
"@gltf-transform/extensions": "^4.0.0-alpha.4", | ||
"@gltf-transform/core": "^4.0.0-alpha.5", | ||
"@gltf-transform/extensions": "^4.0.0-alpha.5", | ||
"ktx-parse": "^0.6.0", | ||
@@ -56,3 +56,3 @@ "ndarray": "^1.0.19", | ||
}, | ||
"gitHead": "a062d5e78cec994d5ea01299fcab6357cd0a6452" | ||
"gitHead": "20a2e5c119208c7df9f041e1e8e075d921b8f95b" | ||
} |
import { Document, Primitive, ComponentTypeToTypedArray } from '@gltf-transform/core'; | ||
import { createIndices, createPrimGroupKey, shallowCloneAccessor } from './utils.js'; | ||
import { createIndices, createPrimGroupKey, remapAttribute, remapIndices, shallowCloneAccessor } from './utils.js'; | ||
@@ -12,2 +12,4 @@ interface JoinPrimitiveOptions { | ||
const EMPTY_U32 = 2 ** 32 - 1; | ||
/** | ||
@@ -48,5 +50,4 @@ * Given a list of compatible Mesh {@link Primitive Primitives}, returns new Primitive | ||
const remapList = [] as Uint32Array[]; // remap[srcIndex] → dstIndex, by prim | ||
const countList = [] as number[]; // vertex count, by prim | ||
const indicesList = [] as (Uint32Array | Uint16Array)[]; // indices, by prim | ||
const primRemaps = [] as Uint32Array[]; // remap[srcIndex] → dstIndex, by prim | ||
const primVertexCounts = new Uint32Array(prims.length); // vertex count, by prim | ||
@@ -57,17 +58,21 @@ let dstVertexCount = 0; | ||
// (2) Build remap lists. | ||
for (const srcPrim of prims) { | ||
const indices = _getOrCreateIndices(srcPrim); | ||
const remap = []; | ||
let count = 0; | ||
for (let i = 0; i < indices.length; i++) { | ||
const index = indices[i]; | ||
if (remap[index] === undefined) { | ||
for (let primIndex = 0; primIndex < prims.length; primIndex++) { | ||
const srcPrim = prims[primIndex]; | ||
const srcIndices = srcPrim.getIndices(); | ||
const srcVertexCount = srcPrim.getAttribute('POSITION')!.getCount(); | ||
const srcIndicesArray = srcIndices ? srcIndices.getArray() : null; | ||
const srcIndicesCount = srcIndices ? srcIndices.getCount() : srcVertexCount; | ||
const remap = new Uint32Array(getIndicesMax(srcPrim) + 1).fill(EMPTY_U32); | ||
for (let i = 0; i < srcIndicesCount; i++) { | ||
const index = srcIndicesArray ? srcIndicesArray[i] : i; | ||
if (remap[index] === EMPTY_U32) { | ||
remap[index] = dstVertexCount++; | ||
count++; | ||
primVertexCounts[primIndex]++; | ||
} | ||
dstIndicesCount++; | ||
} | ||
remapList.push(new Uint32Array(remap)); | ||
countList.push(count); | ||
indicesList.push(indices); | ||
primRemaps.push(new Uint32Array(remap)); | ||
dstIndicesCount += srcIndicesCount; | ||
} | ||
@@ -94,28 +99,22 @@ | ||
// (5) Remap attributes into joined Primitive. | ||
let dstNextIndex = 0; | ||
for (let primIndex = 0; primIndex < remapList.length; primIndex++) { | ||
let dstIndicesOffset = 0; | ||
for (let primIndex = 0; primIndex < primRemaps.length; primIndex++) { | ||
const srcPrim = prims[primIndex]; | ||
const remap = remapList[primIndex]; | ||
const indicesArray = indicesList[primIndex]; | ||
const srcVertexCount = srcPrim.getAttribute('POSITION')!.getCount(); | ||
const srcIndices = srcPrim.getIndices(); | ||
const srcIndicesCount = srcIndices ? srcIndices.getCount() : -1; | ||
const primStartIndex = dstNextIndex; | ||
let primNextIndex = primStartIndex; | ||
const remap = primRemaps[primIndex]; | ||
if (srcIndices && dstIndices) { | ||
remapIndices(srcIndices, remap, dstIndicesOffset, srcIndicesCount, dstIndices); | ||
} | ||
for (const semantic of dstPrim.listSemantics()) { | ||
const srcAttribute = srcPrim.getAttribute(semantic)!; | ||
const dstAttribute = dstPrim.getAttribute(semantic)!; | ||
const el = [] as number[]; | ||
primNextIndex = primStartIndex; | ||
for (let i = 0; i < indicesArray.length; i++) { | ||
const index = indicesArray[i]; | ||
srcAttribute.getElement(index, el); | ||
dstAttribute.setElement(remap[index], el); | ||
if (dstIndices) { | ||
dstIndices.setScalar(primNextIndex++, remap[index]); | ||
} | ||
} | ||
remapAttribute(srcAttribute, remap, srcVertexCount, dstAttribute); | ||
} | ||
dstNextIndex = primNextIndex; | ||
dstIndicesOffset += srcIndicesCount; | ||
} | ||
@@ -126,7 +125,15 @@ | ||
function _getOrCreateIndices(prim: Primitive): Uint16Array | Uint32Array { | ||
function getIndicesMax(prim: Primitive): number { | ||
const indices = prim.getIndices(); | ||
if (indices) return indices.getArray() as Uint32Array | Uint16Array; | ||
const position = prim.getAttribute('POSITION')!; | ||
return createIndices(position.getCount()); | ||
if (!indices) return position.getCount() - 1; | ||
const indicesArray = indices.getArray()!; | ||
const indicesCount = indices.getCount(); | ||
let indicesMax = -1; | ||
for (let i = 0; i < indicesCount; i++) { | ||
indicesMax = Math.max(indicesMax, indicesArray[i]); | ||
} | ||
return indicesMax; | ||
} |
@@ -240,7 +240,15 @@ import type { NdArray } from 'ndarray'; | ||
/** @hidden */ | ||
export function remapAttribute(attribute: Accessor, remap: TypedArray, dstCount: number): Accessor { | ||
const elementSize = attribute.getElementSize(); | ||
const srcCount = attribute.getCount(); | ||
const srcArray = attribute.getArray()!; | ||
const dstArray = srcArray.slice(0, dstCount * elementSize); | ||
export function remapAttribute( | ||
srcAttribute: Accessor, | ||
remap: TypedArray, | ||
dstCount: number, | ||
dstAttribute = srcAttribute, | ||
): Accessor { | ||
const elementSize = srcAttribute.getElementSize(); | ||
const srcCount = srcAttribute.getCount(); | ||
const srcArray = srcAttribute.getArray()!; | ||
// prettier-ignore | ||
const dstArray = dstAttribute === srcAttribute | ||
? srcArray.slice(0, dstCount * elementSize) | ||
: dstAttribute.getArray()!; | ||
const done = new Uint8Array(dstCount); | ||
@@ -257,6 +265,27 @@ | ||
return attribute.setArray(dstArray); | ||
return dstAttribute.setArray(dstArray); | ||
} | ||
/** @hidden */ | ||
export function remapIndices( | ||
srcIndices: Accessor, | ||
remap: TypedArray, | ||
dstOffset: number, | ||
dstCount: number, | ||
dstIndices = srcIndices, | ||
): Accessor { | ||
const srcCount = srcIndices.getCount(); | ||
const srcArray = srcIndices.getArray()!; | ||
const dstArray = dstIndices === srcIndices ? srcArray.slice(0, dstCount) : dstIndices.getArray()!; | ||
for (let i = 0; i < srcCount; i++) { | ||
const srcIndex = srcArray[i]; | ||
const dstIndex = remap[srcIndex]; | ||
dstArray[dstOffset + i] = dstIndex; | ||
} | ||
return dstIndices.setArray(dstArray); | ||
} | ||
/** @hidden */ | ||
export function createIndices(count: number, maxIndex = count): Uint16Array | Uint32Array { | ||
@@ -263,0 +292,0 @@ const array = maxIndex <= 65534 ? new Uint16Array(count) : new Uint32Array(count); |
@@ -55,3 +55,3 @@ import { Accessor, BufferUtils, Document, Primitive, PropertyType, Transform, vec3 } from '@gltf-transform/core'; | ||
/** Flags 'empty' values in a Uint32Array index. */ | ||
const EMPTY = 2 ** 32 - 1; | ||
const EMPTY_U32 = 2 ** 32 - 1; | ||
@@ -203,4 +203,4 @@ const Tolerance = { | ||
const tableSize = ceilPowerOfTwo(srcVertexCount + srcVertexCount / 4); | ||
const table = new Uint32Array(tableSize).fill(EMPTY); | ||
const writeMap = new Uint32Array(srcVertexCount).fill(EMPTY); // oldIndex → newIndex | ||
const table = new Uint32Array(tableSize).fill(EMPTY_U32); | ||
const writeMap = new Uint32Array(srcVertexCount).fill(EMPTY_U32); // oldIndex → newIndex | ||
@@ -213,8 +213,8 @@ // (1) Compare and identify indices to weld. | ||
const srcIndex = srcIndicesArray ? srcIndicesArray[i] : i; | ||
if (writeMap[srcIndex] !== EMPTY) continue; | ||
if (writeMap[srcIndex] !== EMPTY_U32) continue; | ||
const hashIndex = hashLookup(table, tableSize, hash, srcIndex, EMPTY); | ||
const hashIndex = hashLookup(table, tableSize, hash, srcIndex, EMPTY_U32); | ||
const dstIndex = table[hashIndex]; | ||
if (dstIndex === EMPTY) { | ||
if (dstIndex === EMPTY_U32) { | ||
table[hashIndex] = srcIndex; | ||
@@ -269,3 +269,3 @@ writeMap[srcIndex] = dstVertexCount++; | ||
const weldMap = createIndices(srcMaxIndex + 1); // oldIndex → oldCommonIndex | ||
const writeMap = new Uint32Array(uniqueIndices.length).fill(EMPTY); // oldIndex → newIndex | ||
const writeMap = new Uint32Array(uniqueIndices.length).fill(EMPTY_U32); // oldIndex → newIndex | ||
@@ -505,3 +505,3 @@ const srcVertexCount = srcPosition.getCount(); | ||
function hashLookup(table: Uint32Array, buckets: number, hash: HashTable, key: number, empty = EMPTY): number { | ||
function hashLookup(table: Uint32Array, buckets: number, hash: HashTable, key: number, empty = EMPTY_U32): number { | ||
const hashmod = buckets - 1; | ||
@@ -508,0 +508,0 @@ const hashval = hash.hash(key); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
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
2142712
25242