Files
hyperframes/docs/public/catalog/vendor/BufferGeometryUtils.json
T
Miguel Ángel f24ff83834 feat(catalog): add the seven 3D-motion pieces as installable registry blocks (#4056)
* feat(catalog): add the seven 3D-motion pieces as installable registry blocks

Each piece ships as a registry block with a CDN poster and preview, a live
catalog detail page and its source. Scripts travel inside the catalog payload
(shared vendor libraries fetched once, item scripts inline, the one HDR as a
data URI) because the docs host only serves fonts, images and JSON.

NO_ESSAY_COMMENTS_OK: the long comment blocks are in vendored libraries and
the ported frost source under registry/blocks, not hand-written here.
HF_MAX_NONLFS_KB=6000: four hosted texture PNGs under docs/public/catalog/items
are runtime assets the docs host must serve as files.
fallow skipped locally: it audits against origin/main and this branch is
stacked on an older main.

* chore(catalog): regenerate gallery data and nav after rebasing onto main

* fix(catalog): read the install manifest through the bounded project reader

* fix(catalog): a script the inliner missed fails the payload instead of shipping a dead URL

* refactor(catalog): the leftover-script check reuses the reference finder

* fix(catalog): frost and glass textures load from the CDN and the iteration presets are gone

* chore(catalog): regenerate the frost and glass payloads and nav

* fix(catalog): frost and glass textures are hosted through the registry url field, not hardcoded

* chore(catalog): regenerate the frost and glass payloads against the hosted texture URLs

* fix(catalog): the hosted-reference rewrite refuses a bare name it cannot safely replace

* ci(catalog): verify changed catalog payloads in headless Chrome and check the docs catalog

* feat(catalog): every item page follows the shared layout with a Tune panel and tabs

* fix(catalog): let the texture-mask item host its masks so its tile plays live

* chore(catalog): regenerate item pages, gallery data and the texture-mask payload

* chore(catalog): regenerate item pages after merging main

* fix(catalog): declared-only variables get the Tune panel, Code and Docs tabs capped

* feat(catalog): give the blend-difference caption a demo scene so its tile plays live

* chore(catalog): live payload for the blend-difference caption

* perf(catalog): heavy 3d pieces compile and draw before ready, not on the first visible frame

* feat(catalog): the avatar promo card hosts its videos, audio and fonts on the CDN

* feat(catalog): the avatar promo card tile plays live from its CDN-hosted media

* chore(catalog): the avatar promo card plays live, no allowlist left

* chore(catalog): shorten two comment blocks

* chore(catalog): trim first-party comment blocks to the comment-check limits

* chore(catalog): shorten two frost source comments

* fix(catalog): the Docs tab renders as prose again, and the detail page honours reduced motion

Frame-mode pages carry no prose wrapper, and the detail component sat inside
not-prose, so the Docs slot's headings, paragraphs, links and lists had no
styling at all. The slots now carry their own prose classes and the chrome
around them keeps not-prose.

The recorded clip also gets the reduced-motion guard the docs-snippet check
requires, and the generator reads stability from the source metadata type so
the scripts typecheck passes.

* fix(catalog): docs-tab paragraphs render as blocks

Mintlify emits paragraphs as inline spans, so two paragraphs in the Docs slot ran together.

* fix(ci): drop the duplicate docs_catalog job output

git's line-based auto-merge silently duplicated the outputs key when
main and this branch each added it on non-adjacent lines during the
rebase; oxfmt's yaml parser rejects the repeated map key.

* fix(catalog): close four CodeQL alerts in the frost source

* chore(catalog): allowlist the avatar-promo-card CDN asset deletions, format sync-docs-catalog

check-no-main-deletions.mjs was rejecting 11 local mp4/font files this branch
already deleted in favor of CDN hosting; scripts/sync-docs-catalog.mjs had one
line over the 100-col print width from an earlier edit in this branch.

* fix(catalog): detail tabs use docs prose spacing, Install first; previews skip no-WebGPU runners

* fix(catalog): the docs tab scrolls with the page instead of a capped inner box

* refactor(catalog): split the payload asset and verify scripts into single-purpose helpers

* refactor(catalog): split the preview generator into single-purpose helpers

* ci(catalog): fail when the committed catalog differs from generator output

The docs host serves the committed catalog and nothing regenerates it, so a stale
copy shipped silently. The catalog payloads job now generates into a temp dir and
compares it with the committed tree.

* chore(catalog): regenerate docs/public/catalog with the refactored scripts

One full regeneration from an empty output tree with the current scripts. The tree grows
from 58.4 MB to 65.1 MB. Two hosted assets are about 2 MB each (an avatar png and a wav);
the same bytes are already committed under assets/, so HF_MAX_NONLFS_KB is raised for
this commit only.

* fix(catalog): run the catalog script tests under the runner they are written for

The rebase put three node:test files in the vitest half of test:scripts, where they report
"No test suite found". They now sit in the node --test list.

* fix(catalog): a WebGPU item without an adapter shows its recorded clip and says why

* ci(catalog): the drift check runs from a committed fetch mirror with no network

Generation now serves every font and CDN script fetch from scripts/catalog-fetch-mirror and fails
when a fetch is not in it, even if the caller swallowed the error. CATALOG_FETCH_MIRROR=record
refreshes the mirror from the live network. The docs_catalog filter also watches packages/core,
packages/producer and the mirror, and the 13 re-hashed font files are listed in ALLOWED_DELETIONS.

* chore(catalog): clean up the temp font cache and sort the mirror index without a locale

* fix(catalog): hide Tune when the clip stands in, time out the adapter request, share one check

* fix(catalog): the adapter probe survives a sync throw and a hang; test the clip guard

* fix(catalog): keep the adapter probe inside the component, Mintlify evaluates only exports

* fix(catalog): run the detail and page generator tests in CI; no Tune flash while probing WebGPU

* docs(catalog): credit vendored libraries and make each SKILL.md list what the block installs

CREDITS.md names every vendored dependency with its upstream and licence, and flags the two leaf
textures whose origin is still to be confirmed. Each SKILL.md Files table now comes from the block
manifest and its install and render paths point at compositions/<name>/. A test checks that every
path a SKILL.md names is a manifest file that exists.

* fix(catalog): a declared WebGPU item passes verify by its manifest tag, not by console wording

verify-catalog-payloads forgave a missing adapter only by matching the markup and the message, which
library-reported errors and widgets never satisfy. The webgpu tag in the item manifest is now the
owner: a tagged item ignores its page and console errors, an untagged one gets no forgiveness, and a
marker payload with no html is listed instead of crashing the run.

* docs(catalog): correct the credits per block and guard the mount and render paths

CREDITS.md states the three.js version and vendored-or-CDN status per block, adds d3-delaunay,
Clipper on the CDN and the Google Fonts loads, and lists every texture with no known origin.
The SKILL.md test now also checks the mount and render paths against the manifest composition.

* test(catalog): read each block manifest once in the SKILL.md file test

The two checks shared a copied manifest read, which fallow flagged as a clone with a high score.

* fix(catalog): commit the hosted item files a gitignore rule kept out, and check git not the disk

The compositions/ and *-ad/ ignore rules matched folder names under docs/public/catalog/items,
so 33 hosted fonts, textures and marks were generated but never committed and would 404 on
the docs host. The rules now have a negation for that tree and the files are added.

The drift check read the checkout, where git-ignored files still exist, so it passed locally
and failed only in CI. It now unpacks HEAD with git archive and compares that.

* fix(catalog): a tile keeps its poster until the composition has painted

* fix(catalog): a tagged WebGPU item forgives only the adapter-absent error, and test the wiring

The manifest webgpu tag now decides whether an item may lack an adapter, and the error wording
decides which lines are that absence. Any other page or console error on the item, and every
failed request, still fails. The per-item decision is one exported function the tests drive,
marker payloads are parsed by a tested helper, and the SKILL.md variable counts are checked
against their tables (cuboid-carousel said forty-nine for forty).

* fix(catalog): the pinned group's posters load eagerly at high priority

* fix(catalog): type the tile reveal test so the scripts typecheck passes

* fix(catalog): stop the adapter signature matching an unrelated failed adapter request

failed to request .*adapter also matched errors about other adapters. It now matches only a
request for the WebGPU adapter, so a tagged item cannot hide an unrelated failure behind it.

* test(catalog): a tagged item still fails on an error about some other adapter

* feat(catalog): the install command sits above the preview, as on the live catalog

* fix(catalog): find the install slot through Mintlify's child wrapper

* fix(docs): catalog copy buttons keep their size when the label swaps to Copied

* refactor(docs): name the shown copy label once in the catalog copy button

* fix(docs): primary catalog button keeps a readable label on hover and after copy

* fix(docs): inset the catalog tab strip and tab content to match the boxed panels
2026-09-19 16:53:29 -04:00

1 line
38 KiB
JSON

{"text":"import {\n BufferAttribute,\n BufferGeometry,\n Float32BufferAttribute,\n InstancedBufferAttribute,\n InterleavedBuffer,\n InterleavedBufferAttribute,\n TriangleFanDrawMode,\n TriangleStripDrawMode,\n TrianglesDrawMode,\n Vector3,\n} from \"three\";\n\n/**\n * @module BufferGeometryUtils\n * @three_import import * as BufferGeometryUtils from 'three/addons/utils/BufferGeometryUtils.js';\n */\n\n/**\n * Computes vertex tangents using the MikkTSpace algorithm. MikkTSpace generates the same tangents consistently,\n * and is used in most modelling tools and normal map bakers. Use MikkTSpace for materials with normal maps,\n * because inconsistent tangents may lead to subtle visual issues in the normal map, particularly around mirrored\n * UV seams.\n *\n * In comparison to this method, {@link BufferGeometry#computeTangents} (a custom algorithm) generates tangents that\n * probably will not match the tangents in other software. The custom algorithm is sufficient for general use with a\n * custom material, and may be faster than MikkTSpace.\n *\n * Returns the original BufferGeometry. Indexed geometries will be de-indexed. Requires position, normal, and uv attributes.\n *\n * @param {BufferGeometry} geometry - The geometry to compute tangents for.\n * @param {Object} MikkTSpace - Instance of `examples/jsm/libs/mikktspace.module.js`, or `mikktspace` npm package.\n * Await `MikkTSpace.ready` before use.\n * @param {boolean} [negateSign=true] - Whether to negate the sign component (.w) of each tangent.\n * Required for normal map conventions in some formats, including glTF.\n * @return {BufferGeometry} The updated geometry.\n */\nfunction computeMikkTSpaceTangents(geometry, MikkTSpace, negateSign = true) {\n if (!MikkTSpace || !MikkTSpace.isReady) {\n throw new Error(\"BufferGeometryUtils: Initialized MikkTSpace library required.\");\n }\n\n if (\n !geometry.hasAttribute(\"position\") ||\n !geometry.hasAttribute(\"normal\") ||\n !geometry.hasAttribute(\"uv\")\n ) {\n throw new Error(\n 'BufferGeometryUtils: Tangents require \"position\", \"normal\", and \"uv\" attributes.',\n );\n }\n\n function getAttributeArray(attribute) {\n if (attribute.normalized || attribute.isInterleavedBufferAttribute) {\n const dstArray = new Float32Array(attribute.count * attribute.itemSize);\n\n for (let i = 0, j = 0; i < attribute.count; i++) {\n dstArray[j++] = attribute.getX(i);\n dstArray[j++] = attribute.getY(i);\n\n if (attribute.itemSize > 2) {\n dstArray[j++] = attribute.getZ(i);\n }\n }\n\n return dstArray;\n }\n\n if (attribute.array instanceof Float32Array) {\n return attribute.array;\n }\n\n return new Float32Array(attribute.array);\n }\n\n // MikkTSpace algorithm requires non-indexed input.\n\n const _geometry = geometry.index ? geometry.toNonIndexed() : geometry;\n\n // Compute vertex tangents.\n\n const tangents = MikkTSpace.generateTangents(\n getAttributeArray(_geometry.attributes.position),\n getAttributeArray(_geometry.attributes.normal),\n getAttributeArray(_geometry.attributes.uv),\n );\n\n // Texture coordinate convention of glTF differs from the apparent\n // default of the MikkTSpace library; .w component must be flipped.\n\n if (negateSign) {\n for (let i = 3; i < tangents.length; i += 4) {\n tangents[i] *= -1;\n }\n }\n\n //\n\n _geometry.setAttribute(\"tangent\", new BufferAttribute(tangents, 4));\n\n if (geometry !== _geometry) {\n geometry.copy(_geometry);\n }\n\n return geometry;\n}\n\n/**\n * Merges a set of geometries into a single instance. All geometries must have compatible attributes.\n *\n * @param {Array<BufferGeometry>} geometries - The geometries to merge.\n * @param {boolean} [useGroups=false] - Whether to use groups or not.\n * @return {?BufferGeometry} The merged geometry. Returns `null` if the merge does not succeed.\n */\nfunction mergeGeometries(geometries, useGroups = false) {\n const isIndexed = geometries[0].index !== null;\n\n const attributesUsed = new Set(Object.keys(geometries[0].attributes));\n const morphAttributesUsed = new Set(Object.keys(geometries[0].morphAttributes));\n\n const attributes = {};\n const morphAttributes = {};\n\n const morphTargetsRelative = geometries[0].morphTargetsRelative;\n\n const mergedGeometry = new BufferGeometry();\n\n let offset = 0;\n\n for (let i = 0; i < geometries.length; ++i) {\n const geometry = geometries[i];\n let attributesCount = 0;\n\n // ensure that all geometries are indexed, or none\n\n if (isIndexed !== (geometry.index !== null)) {\n console.error(\n \"THREE.BufferGeometryUtils: .mergeGeometries() failed with geometry at index \" +\n i +\n \". All geometries must have compatible attributes; make sure index attribute exists among all geometries, or in none of them.\",\n );\n return null;\n }\n\n // gather attributes, exit early if they're different\n\n for (const name in geometry.attributes) {\n if (!attributesUsed.has(name)) {\n console.error(\n \"THREE.BufferGeometryUtils: .mergeGeometries() failed with geometry at index \" +\n i +\n '. All geometries must have compatible attributes; make sure \"' +\n name +\n '\" attribute exists among all geometries, or in none of them.',\n );\n return null;\n }\n\n if (attributes[name] === undefined) attributes[name] = [];\n\n attributes[name].push(geometry.attributes[name]);\n\n attributesCount++;\n }\n\n // ensure geometries have the same number of attributes\n\n if (attributesCount !== attributesUsed.size) {\n console.error(\n \"THREE.BufferGeometryUtils: .mergeGeometries() failed with geometry at index \" +\n i +\n \". Make sure all geometries have the same number of attributes.\",\n );\n return null;\n }\n\n // gather morph attributes, exit early if they're different\n\n if (morphTargetsRelative !== geometry.morphTargetsRelative) {\n console.error(\n \"THREE.BufferGeometryUtils: .mergeGeometries() failed with geometry at index \" +\n i +\n \". .morphTargetsRelative must be consistent throughout all geometries.\",\n );\n return null;\n }\n\n for (const name in geometry.morphAttributes) {\n if (!morphAttributesUsed.has(name)) {\n console.error(\n \"THREE.BufferGeometryUtils: .mergeGeometries() failed with geometry at index \" +\n i +\n \". .morphAttributes must be consistent throughout all geometries.\",\n );\n return null;\n }\n\n if (morphAttributes[name] === undefined) morphAttributes[name] = [];\n\n morphAttributes[name].push(geometry.morphAttributes[name]);\n }\n\n if (useGroups) {\n let count;\n\n if (isIndexed) {\n count = geometry.index.count;\n } else if (geometry.attributes.position !== undefined) {\n count = geometry.attributes.position.count;\n } else {\n console.error(\n \"THREE.BufferGeometryUtils: .mergeGeometries() failed with geometry at index \" +\n i +\n \". The geometry must have either an index or a position attribute\",\n );\n return null;\n }\n\n mergedGeometry.addGroup(offset, count, i);\n\n offset += count;\n }\n }\n\n // merge indices\n\n if (isIndexed) {\n let indexOffset = 0;\n const mergedIndex = [];\n\n for (let i = 0; i < geometries.length; ++i) {\n const index = geometries[i].index;\n\n for (let j = 0; j < index.count; ++j) {\n mergedIndex.push(index.getX(j) + indexOffset);\n }\n\n indexOffset += geometries[i].attributes.position.count;\n }\n\n mergedGeometry.setIndex(mergedIndex);\n }\n\n // merge attributes\n\n for (const name in attributes) {\n const mergedAttribute = mergeAttributes(attributes[name]);\n\n if (!mergedAttribute) {\n console.error(\n \"THREE.BufferGeometryUtils: .mergeGeometries() failed while trying to merge the \" +\n name +\n \" attribute.\",\n );\n return null;\n }\n\n mergedGeometry.setAttribute(name, mergedAttribute);\n }\n\n // merge morph attributes\n\n for (const name in morphAttributes) {\n const numMorphTargets = morphAttributes[name][0].length;\n\n if (numMorphTargets === 0) break;\n\n mergedGeometry.morphAttributes = mergedGeometry.morphAttributes || {};\n mergedGeometry.morphAttributes[name] = [];\n\n for (let i = 0; i < numMorphTargets; ++i) {\n const morphAttributesToMerge = [];\n\n for (let j = 0; j < morphAttributes[name].length; ++j) {\n morphAttributesToMerge.push(morphAttributes[name][j][i]);\n }\n\n const mergedMorphAttribute = mergeAttributes(morphAttributesToMerge);\n\n if (!mergedMorphAttribute) {\n console.error(\n \"THREE.BufferGeometryUtils: .mergeGeometries() failed while trying to merge the \" +\n name +\n \" morphAttribute.\",\n );\n return null;\n }\n\n mergedGeometry.morphAttributes[name].push(mergedMorphAttribute);\n }\n }\n\n return mergedGeometry;\n}\n\n/**\n * Merges a set of attributes into a single instance. All attributes must have compatible properties and types.\n * Instances of {@link InterleavedBufferAttribute} are not supported.\n *\n * @param {Array<BufferAttribute>} attributes - The attributes to merge.\n * @return {?BufferAttribute} The merged attribute. Returns `null` if the merge does not succeed.\n */\nfunction mergeAttributes(attributes) {\n let TypedArray;\n let itemSize;\n let normalized;\n let gpuType = -1;\n let arrayLength = 0;\n\n for (let i = 0; i < attributes.length; ++i) {\n const attribute = attributes[i];\n\n if (TypedArray === undefined) TypedArray = attribute.array.constructor;\n if (TypedArray !== attribute.array.constructor) {\n console.error(\n \"THREE.BufferGeometryUtils: .mergeAttributes() failed. BufferAttribute.array must be of consistent array types across matching attributes.\",\n );\n return null;\n }\n\n if (itemSize === undefined) itemSize = attribute.itemSize;\n if (itemSize !== attribute.itemSize) {\n console.error(\n \"THREE.BufferGeometryUtils: .mergeAttributes() failed. BufferAttribute.itemSize must be consistent across matching attributes.\",\n );\n return null;\n }\n\n if (normalized === undefined) normalized = attribute.normalized;\n if (normalized !== attribute.normalized) {\n console.error(\n \"THREE.BufferGeometryUtils: .mergeAttributes() failed. BufferAttribute.normalized must be consistent across matching attributes.\",\n );\n return null;\n }\n\n if (gpuType === -1) gpuType = attribute.gpuType;\n if (gpuType !== attribute.gpuType) {\n console.error(\n \"THREE.BufferGeometryUtils: .mergeAttributes() failed. BufferAttribute.gpuType must be consistent across matching attributes.\",\n );\n return null;\n }\n\n arrayLength += attribute.count * itemSize;\n }\n\n const array = new TypedArray(arrayLength);\n const result = new BufferAttribute(array, itemSize, normalized);\n let offset = 0;\n\n for (let i = 0; i < attributes.length; ++i) {\n const attribute = attributes[i];\n if (attribute.isInterleavedBufferAttribute) {\n const tupleOffset = offset / itemSize;\n for (let j = 0, l = attribute.count; j < l; j++) {\n for (let c = 0; c < itemSize; c++) {\n const value = attribute.getComponent(j, c);\n result.setComponent(j + tupleOffset, c, value);\n }\n }\n } else {\n array.set(attribute.array, offset);\n }\n\n offset += attribute.count * itemSize;\n }\n\n if (gpuType !== undefined) {\n result.gpuType = gpuType;\n }\n\n return result;\n}\n\n/**\n * Performs a deep clone of the given buffer attribute.\n *\n * @param {BufferAttribute} attribute - The attribute to clone.\n * @return {BufferAttribute} The cloned attribute.\n */\nfunction deepCloneAttribute(attribute) {\n if (attribute.isInstancedInterleavedBufferAttribute || attribute.isInterleavedBufferAttribute) {\n return deinterleaveAttribute(attribute);\n }\n\n if (attribute.isInstancedBufferAttribute) {\n return new InstancedBufferAttribute().copy(attribute);\n }\n\n return new BufferAttribute().copy(attribute);\n}\n\n/**\n * Interleaves a set of attributes and returns a new array of corresponding attributes that share a\n * single {@link InterleavedBuffer} instance. All attributes must have compatible types.\n *\n * @param {Array<BufferAttribute>} attributes - The attributes to interleave.\n * @return {?Array<InterleavedBufferAttribute>} An array of interleaved attributes. If interleave does not succeed, the method returns `null`.\n */\nfunction interleaveAttributes(attributes) {\n // Interleaves the provided attributes into an InterleavedBuffer and returns\n // a set of InterleavedBufferAttributes for each attribute\n let TypedArray;\n let arrayLength = 0;\n let stride = 0;\n\n // calculate the length and type of the interleavedBuffer\n for (let i = 0, l = attributes.length; i < l; ++i) {\n const attribute = attributes[i];\n\n if (TypedArray === undefined) TypedArray = attribute.array.constructor;\n if (TypedArray !== attribute.array.constructor) {\n console.error(\"AttributeBuffers of different types cannot be interleaved\");\n return null;\n }\n\n arrayLength += attribute.array.length;\n stride += attribute.itemSize;\n }\n\n // Create the set of buffer attributes\n const interleavedBuffer = new InterleavedBuffer(new TypedArray(arrayLength), stride);\n let offset = 0;\n const res = [];\n const getters = [\"getX\", \"getY\", \"getZ\", \"getW\"];\n const setters = [\"setX\", \"setY\", \"setZ\", \"setW\"];\n\n for (let j = 0, l = attributes.length; j < l; j++) {\n const attribute = attributes[j];\n const itemSize = attribute.itemSize;\n const count = attribute.count;\n const iba = new InterleavedBufferAttribute(\n interleavedBuffer,\n itemSize,\n offset,\n attribute.normalized,\n );\n res.push(iba);\n\n offset += itemSize;\n\n // Move the data for each attribute into the new interleavedBuffer\n // at the appropriate offset\n for (let c = 0; c < count; c++) {\n for (let k = 0; k < itemSize; k++) {\n iba[setters[k]](c, attribute[getters[k]](c));\n }\n }\n }\n\n return res;\n}\n\n/**\n * Returns a new, non-interleaved version of the given attribute.\n *\n * @param {InterleavedBufferAttribute} attribute - The interleaved attribute.\n * @return {BufferAttribute} The non-interleaved attribute.\n */\nfunction deinterleaveAttribute(attribute) {\n const cons = attribute.data.array.constructor;\n const count = attribute.count;\n const itemSize = attribute.itemSize;\n const normalized = attribute.normalized;\n\n const array = new cons(count * itemSize);\n let newAttribute;\n if (attribute.isInstancedInterleavedBufferAttribute) {\n newAttribute = new InstancedBufferAttribute(\n array,\n itemSize,\n normalized,\n attribute.meshPerAttribute,\n );\n } else {\n newAttribute = new BufferAttribute(array, itemSize, normalized);\n }\n\n for (let i = 0; i < count; i++) {\n newAttribute.setX(i, attribute.getX(i));\n\n if (itemSize >= 2) {\n newAttribute.setY(i, attribute.getY(i));\n }\n\n if (itemSize >= 3) {\n newAttribute.setZ(i, attribute.getZ(i));\n }\n\n if (itemSize >= 4) {\n newAttribute.setW(i, attribute.getW(i));\n }\n }\n\n return newAttribute;\n}\n\n/**\n * Deinterleaves all attributes on the given geometry.\n *\n * @param {BufferGeometry} geometry - The geometry to deinterleave.\n */\nfunction deinterleaveGeometry(geometry) {\n const attributes = geometry.attributes;\n const morphTargets = geometry.morphTargets;\n const attrMap = new Map();\n\n for (const key in attributes) {\n const attr = attributes[key];\n if (attr.isInterleavedBufferAttribute) {\n if (!attrMap.has(attr)) {\n attrMap.set(attr, deinterleaveAttribute(attr));\n }\n\n attributes[key] = attrMap.get(attr);\n }\n }\n\n for (const key in morphTargets) {\n const attr = morphTargets[key];\n if (attr.isInterleavedBufferAttribute) {\n if (!attrMap.has(attr)) {\n attrMap.set(attr, deinterleaveAttribute(attr));\n }\n\n morphTargets[key] = attrMap.get(attr);\n }\n }\n}\n\n/**\n * Returns the amount of bytes used by all attributes to represent the geometry.\n *\n * @param {BufferGeometry} geometry - The geometry.\n * @return {number} The estimate bytes used.\n */\nfunction estimateBytesUsed(geometry) {\n // Return the estimated memory used by this geometry in bytes\n // Calculate using itemSize, count, and BYTES_PER_ELEMENT to account\n // for InterleavedBufferAttributes.\n let mem = 0;\n for (const name in geometry.attributes) {\n const attr = geometry.getAttribute(name);\n mem += attr.count * attr.itemSize * attr.array.BYTES_PER_ELEMENT;\n }\n\n const indices = geometry.getIndex();\n mem += indices ? indices.count * indices.itemSize * indices.array.BYTES_PER_ELEMENT : 0;\n return mem;\n}\n\n/**\n * Returns a new geometry with vertices for which all similar vertex attributes (within tolerance) are merged.\n *\n * @param {BufferGeometry} geometry - The geometry to merge vertices for.\n * @param {number} [tolerance=1e-4] - The tolerance value.\n * @return {BufferGeometry} - The new geometry with merged vertices.\n */\nfunction mergeVertices(geometry, tolerance = 1e-4) {\n tolerance = Math.max(tolerance, Number.EPSILON);\n\n // Generate an index buffer if the geometry doesn't have one, or optimize it\n // if it's already available.\n const hashToIndex = {};\n const indices = geometry.getIndex();\n const positions = geometry.getAttribute(\"position\");\n const vertexCount = indices ? indices.count : positions.count;\n\n // next value for triangle indices\n let nextIndex = 0;\n\n // attributes and new attribute arrays\n const attributeNames = Object.keys(geometry.attributes);\n const tmpAttributes = {};\n const tmpMorphAttributes = {};\n const newIndices = [];\n const getters = [\"getX\", \"getY\", \"getZ\", \"getW\"];\n const setters = [\"setX\", \"setY\", \"setZ\", \"setW\"];\n\n // Initialize the arrays, allocating space conservatively. Extra\n // space will be trimmed in the last step.\n for (let i = 0, l = attributeNames.length; i < l; i++) {\n const name = attributeNames[i];\n const attr = geometry.attributes[name];\n\n tmpAttributes[name] = new attr.constructor(\n new attr.array.constructor(attr.count * attr.itemSize),\n attr.itemSize,\n attr.normalized,\n );\n\n const morphAttributes = geometry.morphAttributes[name];\n if (morphAttributes) {\n if (!tmpMorphAttributes[name]) tmpMorphAttributes[name] = [];\n morphAttributes.forEach((morphAttr, i) => {\n const array = new morphAttr.array.constructor(morphAttr.count * morphAttr.itemSize);\n tmpMorphAttributes[name][i] = new morphAttr.constructor(\n array,\n morphAttr.itemSize,\n morphAttr.normalized,\n );\n });\n }\n }\n\n // convert the error tolerance to an amount of decimal places to truncate to\n const halfTolerance = tolerance * 0.5;\n const exponent = Math.log10(1 / tolerance);\n const hashMultiplier = Math.pow(10, exponent);\n const hashAdditive = halfTolerance * hashMultiplier;\n for (let i = 0; i < vertexCount; i++) {\n const index = indices ? indices.getX(i) : i;\n\n // Generate a hash for the vertex attributes at the current index 'i'\n let hash = \"\";\n for (let j = 0, l = attributeNames.length; j < l; j++) {\n const name = attributeNames[j];\n const attribute = geometry.getAttribute(name);\n const itemSize = attribute.itemSize;\n\n for (let k = 0; k < itemSize; k++) {\n // double tilde truncates the decimal value\n hash += `${~~(attribute[getters[k]](index) * hashMultiplier + hashAdditive)},`;\n }\n }\n\n // Add another reference to the vertex if it's already\n // used by another index\n if (hash in hashToIndex) {\n newIndices.push(hashToIndex[hash]);\n } else {\n // copy data to the new index in the temporary attributes\n for (let j = 0, l = attributeNames.length; j < l; j++) {\n const name = attributeNames[j];\n const attribute = geometry.getAttribute(name);\n const morphAttributes = geometry.morphAttributes[name];\n const itemSize = attribute.itemSize;\n const newArray = tmpAttributes[name];\n const newMorphArrays = tmpMorphAttributes[name];\n\n for (let k = 0; k < itemSize; k++) {\n const getterFunc = getters[k];\n const setterFunc = setters[k];\n newArray[setterFunc](nextIndex, attribute[getterFunc](index));\n\n if (morphAttributes) {\n for (let m = 0, ml = morphAttributes.length; m < ml; m++) {\n newMorphArrays[m][setterFunc](nextIndex, morphAttributes[m][getterFunc](index));\n }\n }\n }\n }\n\n hashToIndex[hash] = nextIndex;\n newIndices.push(nextIndex);\n nextIndex++;\n }\n }\n\n // generate result BufferGeometry\n const result = geometry.clone();\n for (const name in geometry.attributes) {\n const tmpAttribute = tmpAttributes[name];\n\n result.setAttribute(\n name,\n new tmpAttribute.constructor(\n tmpAttribute.array.slice(0, nextIndex * tmpAttribute.itemSize),\n tmpAttribute.itemSize,\n tmpAttribute.normalized,\n ),\n );\n\n if (!(name in tmpMorphAttributes)) continue;\n\n for (let j = 0; j < tmpMorphAttributes[name].length; j++) {\n const tmpMorphAttribute = tmpMorphAttributes[name][j];\n\n result.morphAttributes[name][j] = new tmpMorphAttribute.constructor(\n tmpMorphAttribute.array.slice(0, nextIndex * tmpMorphAttribute.itemSize),\n tmpMorphAttribute.itemSize,\n tmpMorphAttribute.normalized,\n );\n }\n }\n\n // indices\n\n result.setIndex(newIndices);\n\n return result;\n}\n\n/**\n * Returns a new indexed geometry based on `TrianglesDrawMode` draw mode.\n * This mode corresponds to the `gl.TRIANGLES` primitive in WebGL.\n *\n * @param {BufferGeometry} geometry - The geometry to convert.\n * @param {number} drawMode - The current draw mode.\n * @return {BufferGeometry} The new geometry using `TrianglesDrawMode`.\n */\nfunction toTrianglesDrawMode(geometry, drawMode) {\n if (drawMode === TrianglesDrawMode) {\n console.warn(\n \"THREE.BufferGeometryUtils.toTrianglesDrawMode(): Geometry already defined as triangles.\",\n );\n return geometry;\n }\n\n if (drawMode === TriangleFanDrawMode || drawMode === TriangleStripDrawMode) {\n let index = geometry.getIndex();\n\n // generate index if not present\n\n if (index === null) {\n const indices = [];\n\n const position = geometry.getAttribute(\"position\");\n\n if (position !== undefined) {\n for (let i = 0; i < position.count; i++) {\n indices.push(i);\n }\n\n geometry.setIndex(indices);\n index = geometry.getIndex();\n } else {\n console.error(\n \"THREE.BufferGeometryUtils.toTrianglesDrawMode(): Undefined position attribute. Processing not possible.\",\n );\n return geometry;\n }\n }\n\n //\n\n const numberOfTriangles = index.count - 2;\n const newIndices = [];\n\n if (drawMode === TriangleFanDrawMode) {\n // gl.TRIANGLE_FAN\n\n for (let i = 1; i <= numberOfTriangles; i++) {\n newIndices.push(index.getX(0));\n newIndices.push(index.getX(i));\n newIndices.push(index.getX(i + 1));\n }\n } else {\n // gl.TRIANGLE_STRIP\n\n for (let i = 0; i < numberOfTriangles; i++) {\n if (i % 2 === 0) {\n newIndices.push(index.getX(i));\n newIndices.push(index.getX(i + 1));\n newIndices.push(index.getX(i + 2));\n } else {\n newIndices.push(index.getX(i + 2));\n newIndices.push(index.getX(i + 1));\n newIndices.push(index.getX(i));\n }\n }\n }\n\n if (newIndices.length / 3 !== numberOfTriangles) {\n console.error(\n \"THREE.BufferGeometryUtils.toTrianglesDrawMode(): Unable to generate correct amount of triangles.\",\n );\n }\n\n // build final geometry\n\n const newGeometry = geometry.clone();\n newGeometry.setIndex(newIndices);\n newGeometry.clearGroups();\n\n return newGeometry;\n } else {\n console.error(\"THREE.BufferGeometryUtils.toTrianglesDrawMode(): Unknown draw mode:\", drawMode);\n return geometry;\n }\n}\n\n/**\n * Calculates the morphed attributes of a morphed/skinned BufferGeometry.\n *\n * Helpful for Raytracing or Decals (i.e. a `DecalGeometry` applied to a morphed Object with a `BufferGeometry`\n * will use the original `BufferGeometry`, not the morphed/skinned one, generating an incorrect result.\n * Using this function to create a shadow `Object3`D the `DecalGeometry` can be correctly generated).\n *\n * @param {Mesh|Line|Points} object - The 3D object to compute morph attributes for.\n * @return {Object} An object with original position/normal attributes and morphed ones.\n */\nfunction computeMorphedAttributes(object) {\n const _vA = new Vector3();\n const _vB = new Vector3();\n const _vC = new Vector3();\n\n const _tempA = new Vector3();\n const _tempB = new Vector3();\n const _tempC = new Vector3();\n\n const _morphA = new Vector3();\n const _morphB = new Vector3();\n const _morphC = new Vector3();\n\n function _calculateMorphedAttributeData(\n object,\n attribute,\n morphAttribute,\n morphTargetsRelative,\n a,\n b,\n c,\n modifiedAttributeArray,\n ) {\n _vA.fromBufferAttribute(attribute, a);\n _vB.fromBufferAttribute(attribute, b);\n _vC.fromBufferAttribute(attribute, c);\n\n const morphInfluences = object.morphTargetInfluences;\n\n if (morphAttribute && morphInfluences) {\n _morphA.set(0, 0, 0);\n _morphB.set(0, 0, 0);\n _morphC.set(0, 0, 0);\n\n for (let i = 0, il = morphAttribute.length; i < il; i++) {\n const influence = morphInfluences[i];\n const morph = morphAttribute[i];\n\n if (influence === 0) continue;\n\n _tempA.fromBufferAttribute(morph, a);\n _tempB.fromBufferAttribute(morph, b);\n _tempC.fromBufferAttribute(morph, c);\n\n if (morphTargetsRelative) {\n _morphA.addScaledVector(_tempA, influence);\n _morphB.addScaledVector(_tempB, influence);\n _morphC.addScaledVector(_tempC, influence);\n } else {\n _morphA.addScaledVector(_tempA.sub(_vA), influence);\n _morphB.addScaledVector(_tempB.sub(_vB), influence);\n _morphC.addScaledVector(_tempC.sub(_vC), influence);\n }\n }\n\n _vA.add(_morphA);\n _vB.add(_morphB);\n _vC.add(_morphC);\n }\n\n if (object.isSkinnedMesh) {\n object.applyBoneTransform(a, _vA);\n object.applyBoneTransform(b, _vB);\n object.applyBoneTransform(c, _vC);\n }\n\n modifiedAttributeArray[a * 3 + 0] = _vA.x;\n modifiedAttributeArray[a * 3 + 1] = _vA.y;\n modifiedAttributeArray[a * 3 + 2] = _vA.z;\n modifiedAttributeArray[b * 3 + 0] = _vB.x;\n modifiedAttributeArray[b * 3 + 1] = _vB.y;\n modifiedAttributeArray[b * 3 + 2] = _vB.z;\n modifiedAttributeArray[c * 3 + 0] = _vC.x;\n modifiedAttributeArray[c * 3 + 1] = _vC.y;\n modifiedAttributeArray[c * 3 + 2] = _vC.z;\n }\n\n const geometry = object.geometry;\n const material = object.material;\n\n let a, b, c;\n const index = geometry.index;\n const positionAttribute = geometry.attributes.position;\n const morphPosition = geometry.morphAttributes.position;\n const morphTargetsRelative = geometry.morphTargetsRelative;\n const normalAttribute = geometry.attributes.normal;\n const morphNormal = geometry.morphAttributes.position;\n\n const groups = geometry.groups;\n const drawRange = geometry.drawRange;\n let i, j, il, jl;\n let group;\n let start, end;\n\n const modifiedPosition = new Float32Array(positionAttribute.count * positionAttribute.itemSize);\n const modifiedNormal = new Float32Array(normalAttribute.count * normalAttribute.itemSize);\n\n if (index !== null) {\n // indexed buffer geometry\n\n if (Array.isArray(material)) {\n for (i = 0, il = groups.length; i < il; i++) {\n group = groups[i];\n\n start = Math.max(group.start, drawRange.start);\n end = Math.min(group.start + group.count, drawRange.start + drawRange.count);\n\n for (j = start, jl = end; j < jl; j += 3) {\n a = index.getX(j);\n b = index.getX(j + 1);\n c = index.getX(j + 2);\n\n _calculateMorphedAttributeData(\n object,\n positionAttribute,\n morphPosition,\n morphTargetsRelative,\n a,\n b,\n c,\n modifiedPosition,\n );\n\n _calculateMorphedAttributeData(\n object,\n normalAttribute,\n morphNormal,\n morphTargetsRelative,\n a,\n b,\n c,\n modifiedNormal,\n );\n }\n }\n } else {\n start = Math.max(0, drawRange.start);\n end = Math.min(index.count, drawRange.start + drawRange.count);\n\n for (i = start, il = end; i < il; i += 3) {\n a = index.getX(i);\n b = index.getX(i + 1);\n c = index.getX(i + 2);\n\n _calculateMorphedAttributeData(\n object,\n positionAttribute,\n morphPosition,\n morphTargetsRelative,\n a,\n b,\n c,\n modifiedPosition,\n );\n\n _calculateMorphedAttributeData(\n object,\n normalAttribute,\n morphNormal,\n morphTargetsRelative,\n a,\n b,\n c,\n modifiedNormal,\n );\n }\n }\n } else {\n // non-indexed buffer geometry\n\n if (Array.isArray(material)) {\n for (i = 0, il = groups.length; i < il; i++) {\n group = groups[i];\n\n start = Math.max(group.start, drawRange.start);\n end = Math.min(group.start + group.count, drawRange.start + drawRange.count);\n\n for (j = start, jl = end; j < jl; j += 3) {\n a = j;\n b = j + 1;\n c = j + 2;\n\n _calculateMorphedAttributeData(\n object,\n positionAttribute,\n morphPosition,\n morphTargetsRelative,\n a,\n b,\n c,\n modifiedPosition,\n );\n\n _calculateMorphedAttributeData(\n object,\n normalAttribute,\n morphNormal,\n morphTargetsRelative,\n a,\n b,\n c,\n modifiedNormal,\n );\n }\n }\n } else {\n start = Math.max(0, drawRange.start);\n end = Math.min(positionAttribute.count, drawRange.start + drawRange.count);\n\n for (i = start, il = end; i < il; i += 3) {\n a = i;\n b = i + 1;\n c = i + 2;\n\n _calculateMorphedAttributeData(\n object,\n positionAttribute,\n morphPosition,\n morphTargetsRelative,\n a,\n b,\n c,\n modifiedPosition,\n );\n\n _calculateMorphedAttributeData(\n object,\n normalAttribute,\n morphNormal,\n morphTargetsRelative,\n a,\n b,\n c,\n modifiedNormal,\n );\n }\n }\n }\n\n const morphedPositionAttribute = new Float32BufferAttribute(modifiedPosition, 3);\n const morphedNormalAttribute = new Float32BufferAttribute(modifiedNormal, 3);\n\n return {\n positionAttribute: positionAttribute,\n normalAttribute: normalAttribute,\n morphedPositionAttribute: morphedPositionAttribute,\n morphedNormalAttribute: morphedNormalAttribute,\n };\n}\n\n/**\n * Merges the {@link BufferGeometry#groups} for the given geometry.\n *\n * @param {BufferGeometry} geometry - The geometry to modify.\n * @return {BufferGeometry} - The updated geometry\n */\nfunction mergeGroups(geometry) {\n if (geometry.groups.length === 0) {\n console.warn(\n \"THREE.BufferGeometryUtils.mergeGroups(): No groups are defined. Nothing to merge.\",\n );\n return geometry;\n }\n\n let groups = geometry.groups;\n\n // sort groups by material index\n\n groups = groups.sort((a, b) => {\n if (a.materialIndex !== b.materialIndex) return a.materialIndex - b.materialIndex;\n\n return a.start - b.start;\n });\n\n // create index for non-indexed geometries\n\n if (geometry.getIndex() === null) {\n const positionAttribute = geometry.getAttribute(\"position\");\n const indices = [];\n\n for (let i = 0; i < positionAttribute.count; i += 3) {\n indices.push(i, i + 1, i + 2);\n }\n\n geometry.setIndex(indices);\n }\n\n // sort index\n\n const index = geometry.getIndex();\n\n const newIndices = [];\n\n for (let i = 0; i < groups.length; i++) {\n const group = groups[i];\n\n const groupStart = group.start;\n const groupLength = groupStart + group.count;\n\n for (let j = groupStart; j < groupLength; j++) {\n newIndices.push(index.getX(j));\n }\n }\n\n geometry.dispose(); // Required to force buffer recreation\n geometry.setIndex(newIndices);\n\n // update groups indices\n\n let start = 0;\n\n for (let i = 0; i < groups.length; i++) {\n const group = groups[i];\n\n group.start = start;\n start += group.count;\n }\n\n // merge groups\n\n let currentGroup = groups[0];\n\n geometry.groups = [currentGroup];\n\n for (let i = 1; i < groups.length; i++) {\n const group = groups[i];\n\n if (currentGroup.materialIndex === group.materialIndex) {\n currentGroup.count += group.count;\n } else {\n currentGroup = group;\n geometry.groups.push(currentGroup);\n }\n }\n\n return geometry;\n}\n\n/**\n * Modifies the supplied geometry if it is non-indexed, otherwise creates a new,\n * non-indexed geometry. Returns the geometry with smooth normals everywhere except\n * faces that meet at an angle greater than the crease angle.\n *\n * @param {BufferGeometry} geometry - The geometry to modify.\n * @param {number} [creaseAngle=Math.PI/3] - The crease angle in radians.\n * @return {BufferGeometry} - The updated geometry\n */\nfunction toCreasedNormals(geometry, creaseAngle = Math.PI / 3 /* 60 degrees */) {\n const creaseDot = Math.cos(creaseAngle);\n const hashMultiplier = (1 + 1e-10) * 1e2;\n\n // reusable vectors\n const verts = [new Vector3(), new Vector3(), new Vector3()];\n const tempVec1 = new Vector3();\n const tempVec2 = new Vector3();\n const tempNorm = new Vector3();\n const tempNorm2 = new Vector3();\n\n // hashes a vector\n function hashVertex(v) {\n const x = ~~(v.x * hashMultiplier);\n const y = ~~(v.y * hashMultiplier);\n const z = ~~(v.z * hashMultiplier);\n return `${x},${y},${z}`;\n }\n\n // BufferGeometry.toNonIndexed() warns if the geometry is non-indexed\n // and returns the original geometry\n const resultGeometry = geometry.index ? geometry.toNonIndexed() : geometry;\n const posAttr = resultGeometry.attributes.position;\n const vertexMap = {};\n\n // find all the normals shared by commonly located vertices\n for (let i = 0, l = posAttr.count / 3; i < l; i++) {\n const i3 = 3 * i;\n const a = verts[0].fromBufferAttribute(posAttr, i3 + 0);\n const b = verts[1].fromBufferAttribute(posAttr, i3 + 1);\n const c = verts[2].fromBufferAttribute(posAttr, i3 + 2);\n\n tempVec1.subVectors(c, b);\n tempVec2.subVectors(a, b);\n\n // add the normal to the map for all vertices\n const normal = new Vector3().crossVectors(tempVec1, tempVec2).normalize();\n for (let n = 0; n < 3; n++) {\n const vert = verts[n];\n const hash = hashVertex(vert);\n if (!(hash in vertexMap)) {\n vertexMap[hash] = [];\n }\n\n vertexMap[hash].push(normal);\n }\n }\n\n // average normals from all vertices that share a common location if they are within the\n // provided crease threshold\n const normalArray = new Float32Array(posAttr.count * 3);\n const normAttr = new BufferAttribute(normalArray, 3, false);\n for (let i = 0, l = posAttr.count / 3; i < l; i++) {\n // get the face normal for this vertex\n const i3 = 3 * i;\n const a = verts[0].fromBufferAttribute(posAttr, i3 + 0);\n const b = verts[1].fromBufferAttribute(posAttr, i3 + 1);\n const c = verts[2].fromBufferAttribute(posAttr, i3 + 2);\n\n tempVec1.subVectors(c, b);\n tempVec2.subVectors(a, b);\n\n tempNorm.crossVectors(tempVec1, tempVec2).normalize();\n\n // average all normals that meet the threshold and set the normal value\n for (let n = 0; n < 3; n++) {\n const vert = verts[n];\n const hash = hashVertex(vert);\n const otherNormals = vertexMap[hash];\n tempNorm2.set(0, 0, 0);\n\n for (let k = 0, lk = otherNormals.length; k < lk; k++) {\n const otherNorm = otherNormals[k];\n if (tempNorm.dot(otherNorm) > creaseDot) {\n tempNorm2.add(otherNorm);\n }\n }\n\n tempNorm2.normalize();\n normAttr.setXYZ(i3 + n, tempNorm2.x, tempNorm2.y, tempNorm2.z);\n }\n }\n\n resultGeometry.setAttribute(\"normal\", normAttr);\n return resultGeometry;\n}\n\nexport {\n computeMikkTSpaceTangents,\n mergeGeometries,\n mergeAttributes,\n deepCloneAttribute,\n deinterleaveAttribute,\n deinterleaveGeometry,\n interleaveAttributes,\n estimateBytesUsed,\n mergeVertices,\n toTrianglesDrawMode,\n computeMorphedAttributes,\n mergeGroups,\n toCreasedNormals,\n};\n"}