cuboid-carousel
by HeyGen · official repository
A chain of bevelled cuboids rides a travelling wave as a content carousel. HyperFrames block, 1920×1080, 6.666666666666667s, 40 variables.
Labels from its files: reaches the network, runs commands. 11 files, read Oct 21, 2026. Nothing was run.
npx skills add https://github.com/heygen-com/hyperframes --skill cuboid-carouselWhat it lets an agent do
Each label, and the line in the skill's files that set it.
- Reaches the network
Its scripts or instructions fetch from or send to the network.
- assets/addons/environments/RoomEnvironment.js makes network calls.
- Runs commands
Allows a shell or ships scripts the agent runs.
- It ships 6 scripts the agent can run.
Findings
Matches of fixed checks, with the text they matched.
No check matched anything in this skill's files.
Not a safety rating, and no finding is not a safety guarantee. These are fixed checks of the files as read on Oct 21, 2026. Nothing was run. How skills are checked.
SKILL.md
As the publisher wrote it. Shown as text, never rendered.
---
name: cuboid-carousel
description: A chain of bevelled cuboids rides a travelling wave as a content carousel. HyperFrames block, 1920×1080, 6.666666666666667s, 40 variables.
---
# Cuboid Carousel
Rounded, bevelled cuboids carrying card content fly in from the right as one rigid group, cruise with a per-cuboid rotation stagger, decelerate to a hero hold, then exit left. Spacing is derived from the cuboid dimensions and the largest reachable X half-extent, so cuboids can never overlap. Forty variables carry the original DialKit schema: geometry, materials, lights, shadows, backdrop, camera and every segment duration and easing. Cards come from the built-in list or a JSON variable.
Composition id: `cuboid-carousel`. Duration 6.666666666666667 s at 30 fps, 1920×1080.
## Files
- `cuboid-carousel.html` (38 KB)
- `assets/Three-LICENSE.txt` (1 KB)
- `assets/addons/environments/RoomEnvironment.js` (5 KB)
- `assets/addons/utils/BufferGeometryUtils.js` (36 KB)
- `assets/cuboid-motion.js` (593 KB)
- `assets/gsap-3.14.2.min.js` (128 KB)
- `assets/GSAP-NOTICE.txt` (1 KB)
- `assets/three.core.min.js` (554 KB)
- `assets/three.module.min.js` (468 KB)
## Install
Install with `npx hyperframes add cuboid-carousel`; by default the files above land under `compositions/cuboid-carousel/`. Then mount the block from the host `index.html`:
```html
<div
data-composition-id="cuboid-carousel"
data-composition-src="compositions/cuboid-carousel/cuboid-carousel.html"
data-start="0"
data-duration="6.666666666666667"
data-track-index="1"
data-width="1920"
data-height="1080"
></div>
```
Render with custom values by targeting the composition file directly:
```sh
npx --yes hyperframes@0.8.12 render 'compositions/cuboid-carousel/cuboid-carousel.html' --variables '{"cardsJson":"","heroCard":4}'
```
## Variables
Read at runtime via `window.__hyperframes.getVariables()`; declared on the composition root as `data-composition-variables` (single-quoted attribute, plain JSON).
| id | type | default | label / range |
| ------------------- | ------ | ----------- | ---------------------------------------------------- |
| `cardsJson` | string | `""` | Content · Cards JSON (optional, see CARDS in source) |
| `heroCard` | number | `4` | Content · Hero card (1-based) 1–12 step 1 |
| `count` | number | `8` | Content · Repeating card count 1–12 step 1 |
| `gap` | number | `0.01` | Layout · Extra gap between cuboids 0–1.5 step 0.005 |
| `cuboidWidth` | number | `1.7` | Cuboid · Width 0.3–5 step 0.01 |
| `cuboidHeight` | number | `2.4` | Cuboid · Height 0.3–6 step 0.01 |
| `cuboidDepth` | number | `0.14` | Cuboid · Thickness (depth) 0.02–2 step 0.01 |
| `cornerRadius` | number | `0.105` | Cuboid · Corner radius 0–0.6 step 0.005 |
| `bevelSize` | number | `0.03` | Cuboid · Bevel size (edge chamfer) 0–0.3 step 0.001 |
| `bevelThickness` | number | `0.04` | Cuboid · Bevel thickness 0–0.3 step 0.001 |
| `smoothness` | number | `32` | Cuboid · Smoothness (segments) 1–32 step 1 |
| `bodyColor` | color | `"#242424"` | Cuboid · Body colour |
| `roughness` | number | `0.6` | Cuboid · Roughness 0–1 step 0.01 |
| `metalness` | number | `0.4` | Cuboid · Metalness 0–1 step 0.01 |
| `heroScale` | number | `1` | Hero · Isolated scale 1–1.6 step 0.01 |
| `heroFillIntensity` | number | `0.55` | Hero · Face light intensity 0–2 step 0.05 |
| `heroExitX` | number | `288` | Hero · Exit X rotation degrees -720–720 step 1 |
| `heroExitY` | number | `-234` | Hero · Exit Y rotation degrees -720–720 step 1 |
| `heroExitZ` | number | `198` | Hero · Exit Z rotation degrees -720–720 step 1 |
| `ambientIntensity` | number | `0.35` | Lights · Ambient intensity 0–4 step 0.01 |
| `ambientColor` | color | `"#ffffff"` | Lights · Ambient colour |
| `topIntensity` | number | `3.1` | Lights · Top rig intensity 0–12 step 0.05 |
| `topColor` | color | `"#ffffff"` | Lights · Top rig colour |
| `bottomIntensity` | number | `2.15` | Lights · Bottom rig intensity 0–12 step 0.05 |
| `bottomColor` | color | `"#ffcaad"` | Lights · Bottom rig colour |
| `keyIntensity` | number | `5` | Lights · Key rig intensity 0–12 step 0.05 |
| `keyColor` | color | `"#ffe9d6"` | Lights · Key rig colour |
| `shadowRadius` | number | `44.5` | Shadows · Softness at map size 2048 0–50 step 0.5 |
| `shadowMapSize` | number | `2048` | Shadows · Map size 512–4096 step 256 |
| `backdrop` | color | `"#0b0d13"` | Background · Colour |
| `cameraFov` | number | `11` | Camera · Initial Field of view 10–90 step 0.5 |
| `cameraX` | number | `-1.65` | Camera · Initial X -20–20 step 0.05 |
| `cameraY` | number | `8.5` | Camera · Initial Y -20–20 step 0.05 |
| `cameraZ` | number | `-8.508708` | Camera · Initial Z (distance) -20–50 step 0.1 |
| `cameraSettleFrame` | number | `70` | Camera · Settle frame (30 fps) 20–90 step 1 |
| `handheldStrength` | number | `1` | Camera · Handheld hold strength 0–1 step 0.05 |
| `cameraTargetFov` | number | `34` | Camera target · Field of view 10–90 step 0.5 |
| `cameraTargetX` | number | `-7.25` | Camera target · X -20–20 step 0.05 |
| `cameraTargetY` | number | `1.15` | Camera target · Y -20–20 step 0.05 |
| `cameraTargetZ` | number | `6.2` | Camera target · Z 2–50 step 0.1 |
## Runtime contract
- One paused GSAP timeline registered as `window.__timelines["cuboid-carousel"]`.
- Re-syncs on the `hf-seek` CustomEvent; every frame is a closed-form function of time (seeded PRNG only, no rAF loops, no Date.now).
- Renderer: WebGL, GSAP, Canvas 2D, PMREM, Shadow maps. Budget roughly 350 MB per live instance; run one at a time.
- External runtime dependencies: `./assets/three.module.min.js`, `./assets/addons/`.
- Web fonts from Google Fonts: Inter.
## Editing rules (from the source project)
1. Keep `data-composition-variables` a single-quoted attribute with plain `"` JSON. Never save it through Studio's Design panel.
2. Do not put `<canvas>` in static markup; create it at runtime.
3. Keep every visual state a function of t; seek-safety is what makes the block renderable.
Scripts (2)
Read as text. anlyon.dev never runs them.
assets/addons/environments/RoomEnvironment.jsShowHide
import {
BackSide,
BoxGeometry,
InstancedMesh,
Mesh,
MeshLambertMaterial,
MeshStandardMaterial,
PointLight,
Scene,
Object3D,
} from "three";
/**
* This class represents a scene with a basic room setup that can be used as
* input for {@link PMREMGenerator#fromScene}. The resulting PMREM represents the room's
* lighting and can be used for Image Based Lighting by assigning it to {@link Scene#environment}
* or directly as an environment map to PBR materials.
*
* The implementation is based on the [EnvironmentScene](https://github.com/google/model-viewer/blob/master/packages/model-viewer/src/three-components/EnvironmentScene.ts)
* component from the `model-viewer` project.
*
* ```js
* const environment = new RoomEnvironment();
* const pmremGenerator = new THREE.PMREMGenerator( renderer );
*
* const envMap = pmremGenerator.fromScene( environment ).texture;
* scene.environment = envMap;
* ```
*
* @augments Scene
* @three_import import { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.js';
*/
class RoomEnvironment extends Scene {
constructor() {
super();
const geometry = new BoxGeometry();
geometry.deleteAttribute("uv");
const roomMaterial = new MeshStandardMaterial({ side: BackSide });
const boxMaterial = new MeshStandardMaterial();
const mainLight = new PointLight(0xffffff, 900, 28, 2);
mainLight.position.set(0.418, 16.199, 0.3);
this.add(mainLight);
const room = new Mesh(geometry, roomMaterial);
room.position.set(-0.757, 13.219, 0.717);
room.scale.set(31.713, 28.305, 28.591);
this.add(room);
const boxes = new InstancedMesh(geometry, boxMaterial, 6);
const transform = new Object3D();
// box1
transform.position.set(-10.906, 2.009, 1.846);
transform.rotation.set(0, -0.195, 0);
transform.scale.set(2.328, 7.905, 4.651);
transform.updateMatrix();
boxes.setMatrixAt(0, transform.matrix);
// box2
transform.position.set(-5.607, -0.754, -0.758);
transform.rotation.set(0, 0.994, 0);
transform.scale.set(1.97, 1.534, 3.955);
transform.updateMatrix();
boxes.setMatrixAt(1, transform.matrix);
// box3
transform.position.set(6.167, 0.857, 7.803);
transform.rotation.set(0, 0.561, 0);
transform.scale.set(3.927, 6.285, 3.687);
transform.updateMatrix();
boxes.setMatrixAt(2, transform.matrix);
// box4
transform.position.set(-2.017, 0.018, 6.124);
transform.rotation.set(0, 0.333, 0);
transform.scale.set(2.002, 4.566, 2.064);
transform.updateMatrix();
boxes.setMatrixAt(3, transform.matrix);
// box5
transform.position.set(2.291, -0.756, -2.621);
transform.rotation.set(0, -0.286, 0);
transform.scale.set(1.546, 1.552, 1.496);
transform.updateMatrix();
boxes.setMatrixAt(4, transform.matrix);
// box6
transform.position.set(-2.193, -0.369, -5.547);
transform.rotation.set(0, 0.516, 0);
transform.scale.set(3.875, 3.487, 2.986);
transform.updateMatrix();
boxes.setMatrixAt(5, transform.matrix);
this.add(boxes);
// -x right
const light1 = new Mesh(geometry, createAreaLightMaterial(50));
light1.position.set(-16.116, 14.37, 8.208);
light1.scale.set(0.1, 2.428, 2.739);
this.add(light1);
// -x left
const light2 = new Mesh(geometry, createAreaLightMaterial(50));
light2.position.set(-16.109, 18.021, -8.207);
light2.scale.set(0.1, 2.425, 2.751);
this.add(light2);
// +x
const light3 = new Mesh(geometry, createAreaLightMaterial(17));
light3.position.set(14.904, 12.198, -1.832);
light3.scale.set(0.15, 4.265, 6.331);
this.add(light3);
// +z
const light4 = new Mesh(geometry, createAreaLightMaterial(43));
light4.position.set(-0.462, 8.89, 14.52);
light4.scale.set(4.38, 5.441, 0.088);
this.add(light4);
// -z
const light5 = new Mesh(geometry, createAreaLightMaterial(20));
light5.position.set(3.235, 11.486, -12.541);
light5.scale.set(2.5, 2.0, 0.1);
this.add(light5);
// +y
const light6 = new Mesh(geometry, createAreaLightMaterial(100));
light6.position.set(0.0, 20.0, 0.0);
light6.scale.set(1.0, 0.1, 1.0);
this.add(light6);
}
/**
* Frees internal resources. This method should be called
* when the environment is no longer required.
*/
dispose() {
const resources = new Set();
this.traverse((object) => {
if (object.isMesh) {
resources.add(object.geometry);
resources.add(object.material);
}
});
for (const resource of resources) {
resource.dispose();
}
}
}
function createAreaLightMaterial(intensity) {
// create an emissive-only material. see #31348
const material = new MeshLambertMaterial({
color: 0x000000,
emissive: 0xffffff,
emissiveIntensity: intensity,
});
return material;
}
export { RoomEnvironment };
assets/addons/utils/BufferGeometryUtils.jsShowHide
import {
BufferAttribute,
BufferGeometry,
Float32BufferAttribute,
InstancedBufferAttribute,
InterleavedBuffer,
InterleavedBufferAttribute,
TriangleFanDrawMode,
TriangleStripDrawMode,
TrianglesDrawMode,
Vector3,
} from "three";
/**
* @module BufferGeometryUtils
* @three_import import * as BufferGeometryUtils from 'three/addons/utils/BufferGeometryUtils.js';
*/
/**
* Computes vertex tangents using the MikkTSpace algorithm. MikkTSpace generates the same tangents consistently,
* and is used in most modelling tools and normal map bakers. Use MikkTSpace for materials with normal maps,
* because inconsistent tangents may lead to subtle visual issues in the normal map, particularly around mirrored
* UV seams.
*
* In comparison to this method, {@link BufferGeometry#computeTangents} (a custom algorithm) generates tangents that
* probably will not match the tangents in other software. The custom algorithm is sufficient for general use with a
* custom material, and may be faster than MikkTSpace.
*
* Returns the original BufferGeometry. Indexed geometries will be de-indexed. Requires position, normal, and uv attributes.
*
* @param {BufferGeometry} geometry - The geometry to compute tangents for.
* @param {Object} MikkTSpace - Instance of `examples/jsm/libs/mikktspace.module.js`, or `mikktspace` npm package.
* Await `MikkTSpace.ready` before use.
* @param {boolean} [negateSign=true] - Whether to negate the sign component (.w) of each tangent.
* Required for normal map conventions in some formats, including glTF.
* @return {BufferGeometry} The updated geometry.
*/
function computeMikkTSpaceTangents(geometry, MikkTSpace, negateSign = true) {
if (!MikkTSpace || !MikkTSpace.isReady) {
throw new Error("BufferGeometryUtils: Initialized MikkTSpace library required.");
}
if (
!geometry.hasAttribute("position") ||
!geometry.hasAttribute("normal") ||
!geometry.hasAttribute("uv")
) {
throw new Error(
'BufferGeometryUtils: Tangents require "position", "normal", and "uv" attributes.',
);
}
function getAttributeArray(attribute) {
if (attribute.normalized || attribute.isInterleavedBufferAttribute) {
const dstArray = new Float32Array(attribute.count * attribute.itemSize);
for (let i = 0, j = 0; i < attribute.count; i++) {
dstArray[j++] = attribute.getX(i);
dstArray[j++] = attribute.getY(i);
if (attribute.itemSize > 2) {
dstArray[j++] = attribute.getZ(i);
}
}
return dstArray;
}
if (attribute.array instanceof Float32Array) {
return attribute.array;
}
return new Float32Array(attribute.array);
}
// MikkTSpace algorithm requires non-indexed input.
const _geometry = geometry.index ? geometry.toNonIndexed() : geometry;
// Compute vertex tangents.
const tangents = MikkTSpace.generateTangents(
getAttributeArray(_geometry.attributes.position),
getAttributeArray(_geometry.attributes.normal),
getAttributeArray(_geometry.attributes.uv),
);
// Texture coordinate convention of glTF differs from the apparent
// default of the MikkTSpace library; .w component must be flipped.
if (negateSign) {
for (let i = 3; i < tangents.length; i += 4) {
tangents[i] *= -1;
}
}
//
_geometry.setAttribute("tangent", new BufferAttribute(tangents, 4));
if (geometry !== _geometry) {
geometry.copy(_geometry);
}
return geometry;
}
/**
* Merges a set of geometries into a single instance. All geometries must have compatible attributes.
*
* @param {Array<BufferGeometry>} geometries - The geometries to merge.
* @param {boolean} [useGroups=false] - Whether to use groups or not.
* @return {?BufferGeometry} The merged geometry. Returns `null` if the merge does not succeed.
*/
function mergeGeometries(geometries, useGroups = false) {
const isIndexed = geometries[0].index !== null;
const attributesUsed = new Set(Object.keys(geometries[0].attributes));
const morphAttributesUsed = new Set(Object.keys(geometries[0].morphAttributes));
const attributes = {};
const morphAttributes = {};
const morphTargetsRelative = geometries[0].morphTargetsRelative;
const mergedGeometry = new BufferGeometry();
let offset = 0;
for (let i = 0; i < geometries.length; ++i) {
const geometry = geometries[i];
let attributesCount = 0;
// ensure that all geometries are indexed, or none
if (isIndexed !== (geometry.index !== null)) {
console.error(
"THREE.BufferGeometryUtils: .mergeGeometries() failed with geometry at index " +
i +
". All geometries must have compatible attributes; make sure index attribute exists among all geometries, or in none of them.",
);
return null;
}
// gather attributes, exit early if they're different
for (const name in geometry.attributes) {
if (!attributesUsed.has(name)) {
console.error(
"THREE.BufferGeometryUtils: .mergeGeometries() failed with geometry at index " +
i +
'. All geometries must have compatible attributes; make sure "' +
name +
'" attribute exists among all geometries, or in none of them.',
);
return null;
}
if (attributes[name] === undefined) attributes[name] = [];
attributes[name].push(geometry.attributes[name]);
attributesCount++;
}
// ensure geometries have the same number of attributes
if (attributesCount !== attributesUsed.size) {
console.error(
"THREE.BufferGeometryUtils: .mergeGeometries() failed with geometry at index " +
i +
". Make sure all geometries have the same number of attributes.",
);
return null;
}
// gather morph attributes, exit early if they're different
if (morphTargetsRelative !== geometry.morphTargetsRelative) {
console.error(
"THREE.BufferGeometryUtils: .mergeGeometries() failed with geometry at index " +
i +
". .morphTargetsRelative must be consistent throughout all geometries.",
);
return null;
}
for (const name in geometry.morphAttributes) {
if (!morphAttributesUsed.has(name)) {
console.error(
"THREE.BufferGeometryUtils: .mergeGeometries() failed with geometry at index " +
i +
". .morphAttributes must be consistent throughout all geometries.",
);
return null;
}
if (morphAttributes[name] === undefined) morphAttributes[name] = [];
morphAttributes[name].push(geometry.morphAttributes[name]);
}
if (useGroups) {
let count;
if (isIndexed) {
count = geometry.index.count;
} else if (geometry.attributes.position !== undefined) {
count = geometry.attributes.position.count;
} else {
console.error(
"THREE.BufferGeometryUtils: .mergeGeometries() failed with geometry at index " +
i +
". The geometry must have either an index or a position attribute",
);
return null;
}
mergedGeometry.addGroup(offset, count, i);
offset += count;
}
}
// merge indices
if (isIndexed) {
let indexOffset = 0;
const mergedIndex = [];
for (let i = 0; i < geometries.length; ++i) {
const index = geometries[i].index;
for (let j = 0; j < index.count; ++j) {
mergedIndex.push(index.getX(j) + indexOffset);
}
indexOffset += geometries[i].attributes.position.count;
}
mergedGeometry.setIndex(mergedIndex);
}
// merge attributes
for (const name in attributes) {
const mergedAttribute = mergeAttributes(attributes[name]);
if (!mergedAttribute) {
console.error(
"THREE.BufferGeometryUtils: .mergeGeometries() failed while trying to merge the " +
name +
" attribute.",
);
return null;
}
mergedGeometry.setAttribute(name, mergedAttribute);
}
// merge morph attributes
for (const name in morphAttributes) {
const numMorphTargets = morphAttributes[name][0].length;
if (numMorphTargets === 0) break;
mergedGeometry.morphAttributes = mergedGeometry.morphAttributes || {};
mergedGeometry.morphAttributes[name] = [];
for (let i = 0; i < numMorphTargets; ++i) {
const morphAttributesToMerge = [];
for (let j = 0; j < morphAttributes[name].length; ++j) {
morphAttributesToMerge.push(morphAttributes[name][j][i]);
}
const mergedMorphAttribute = mergeAttributes(morphAttributesToMerge);
if (!mergedMorphAttribute) {
console.error(
"THREE.BufferGeometryUtils: .mergeGeometries() failed while trying to merge the " +
name +
" morphAttribute.",
);
return null;
}
mergedGeometry.morphAttributes[name].push(mergedMorphAttribute);
}
}
return mergedGeometry;
}
/**
* Merges a set of attributes into a single instance. All attributes must have compatible properties and types.
* Instances of {@link InterleavedBufferAttribute} are not supported.
*
* @param {Array<BufferAttribute>} attributes - The attributes to merge.
* @return {?BufferAttribute} The merged attribute. Returns `null` if the merge does not succeed.
*/
function mergeAttributes(attributes) {
let TypedArray;
let itemSize;
let normalized;
let gpuType = -1;
let arrayLength = 0;
for (let i = 0; i < attributes.length; ++i) {
const attribute = attributes[i];
if (TypedArray === undefined) TypedArray = attribute.array.constructor;
if (TypedArray !== attribute.array.constructor) {
console.error(
"THREE.BufferGeometryUtils: .mergeAttributes() failed. BufferAttribute.array must be of consistent array types across matching attributes.",
);
return null;
}
if (itemSize === undefined) itemSize = attribute.itemSize;
if (itemSize !== attribute.itemSize) {
console.error(
"THREE.BufferGeometryUtils: .mergeAttributes() failed. BufferAttribute.itemSize must be consistent across matching attributes.",
);
return null;
}
if (normalized === undefined) normalized = attribute.normalized;
if (normalized !== attribute.normalized) {
console.error(
"THREE.BufferGeometryUtils: .mergeAttributes() failed. BufferAttribute.normalized must be consistent across matching attributes.",
);
return null;
}
if (gpuType === -1) gpuType = attribute.gpuType;
if (gpuType !== attribute.gpuType) {
console.error(
"THREE.BufferGeometryUtils: .mergeAttributes() failed. BufferAttribute.gpuType must be consistent across matching attributes.",
);
return null;
}
arrayLength += attribute.count * itemSize;
}
const array = new TypedArray(arrayLength);
const result = new BufferAttribute(array, itemSize, normalized);
let offset = 0;
for (let i = 0; i < attributes.length; ++i) {
const attribute = attributes[i];
if (attribute.isInterleavedBufferAttribute) {
const tupleOffset = offset / itemSize;
for (let j = 0, l = attribute.count; j < l; j++) {
for (let c = 0; c < itemSize; c++) {
const value = attribute.getComponent(j, c);
result.setComponent(j + tupleOffset, c, value);
}
}
} else {
array.set(attribute.array, offset);
}
offset += attribute.count * itemSize;
}
if (gpuType !== undefined) {
result.gpuType = gpuType;
}
return result;
}
/**
* Performs a deep clone of the given buffer attribute.
*
* @param {BufferAttribute} attribute - The attribute to clone.
* @return {BufferAttribute} The cloned attribute.
*/
function deepCloneAttribute(attribute) {
if (attribute.isInstancedInterleavedBufferAttribute || attribute.isInterleavedBufferAttribute) {
return deinterleaveAttribute(attribute);
}
if (attribute.isInstancedBufferAttribute) {
return new InstancedBufferAttribute().copy(attribute);
}
return new BufferAttribute().copy(attribute);
}
/**
* Interleaves a set of attributes and returns a new array of corresponding attributes that share a
* single {@link InterleavedBuffer} instance. All attributes must have compatible types.
*
* @param {Array<BufferAttribute>} attributes - The attributes to interleave.
* @return {?Array<InterleavedBufferAttribute>} An array of interleaved attributes. If interleave does not succeed, the method returns `null`.
*/
function interleaveAttributes(attributes) {
// Interleaves the provided attributes into an InterleavedBuffer and returns
// a set of InterleavedBufferAttributes for each attribute
let TypedArray;
let arrayLength = 0;
let stride = 0;
// calculate the length and type of the interleavedBuffer
for (let i = 0, l = attributes.length; i < l; ++i) {
const attribute = attributes[i];
if (TypedArray === undefined) TypedArray = attribute.array.constructor;
if (TypedArray !== attribute.array.constructor) {
console.error("AttributeBuffers of different types cannot be interleaved");
return null;
}
arrayLength += attribute.array.length;
stride += attribute.itemSize;
}
// Create the set of buffer attributes
const interleavedBuffer = new InterleavedBuffer(new TypedArray(arrayLength), stride);
let offset = 0;
const res = [];
const getters = ["getX", "getY", "getZ", "getW"];
const setters = ["setX", "setY", "setZ", "setW"];
for (let j = 0, l = attributes.length; j < l; j++) {
const attribute = attributes[j];
const itemSize = attribute.itemSize;
const count = attribute.count;
const iba = new InterleavedBufferAttribute(
interleavedBuffer,
itemSize,
offset,
attribute.normalized,
);
res.push(iba);
offset += itemSize;
// Move the data for each attribute into the new interleavedBuffer
// at the appropriate offset
for (let c = 0; c < count; c++) {
for (let k = 0; k < itemSize; k++) {
iba[setters[k]](c, attribute[getters[k]](c));
}
}
}
return res;
}
/**
* Returns a new, non-interleaved version of the given attribute.
*
* @param {InterleavedBufferAttribute} attribute - The interleaved attribute.
* @return {BufferAttribute} The non-interleaved attribute.
*/
function deinterleaveAttribute(attribute) {
const cons = attribute.data.array.constructor;
const count = attribute.count;
const itemSize = attribute.itemSize;
const normalized = attribute.normalized;
const array = new cons(count * itemSize);
let newAttribute;
if (attribute.isInstancedInterleavedBufferAttribute) {
newAttribute = new InstancedBufferAttribute(
array,
itemSize,
normalized,
attribute.meshPerAttribute,
);
} else {
newAttribute = new BufferAttribute(array, itemSize, normalized);
}
for (let i = 0; i < count; i++) {
newAttribute.setX(i, attribute.getX(i));
if (itemSize >= 2) {
newAttribute.setY(i, attribute.getY(i));
}
if (itemSize >= 3) {
newAttribute.setZ(i, attribute.getZ(i));
}
if (itemSize >= 4) {
newAttribute.setW(i, attribute.getW(i));
}
}
return newAttribute;
}
/**
* Deinterleaves all attributes on the given geometry.
*
* @param {BufferGeometry} geometry - The geometry to deinterleave.
*/
function deinterleaveGeometry(geometry) {
const attributes = geometry.attributes;
const morphTargets = geometry.morphTargets;
const attrMap = new Map();
for (const key in attributes) {
const attr = attributes[key];
if (attr.isInterleavedBufferAttribute) {
if (!attrMap.has(attr)) {
attrMap.set(attr, deinterleaveAttribute(attr));
}
attributes[key] = attrMap.get(attr);
}
}
for (const key in morphTargets) {
const attr = morphTargets[key];
if (attr.isInterleavedBufferAttribute) {
if (!attrMap.has(attr)) {
attrMap.set(attr, deinterleaveAttribute(attr));
}
morphTargets[key] = attrMap.get(attr);
}
}
}
/**
* Returns the amount of bytes used by all attributes to represent the geometry.
*
* @param {BufferGeometry} geometry - The geometry.
* @return {number} The estimate bytes used.
*/
function estimateBytesUsed(geometry) {
// Return the estimated memory used by this geometry in bytes
// Calculate using itemSize, count, and BYTES_PER_ELEMENT to account
// for InterleavedBufferAttributes.
let mem = 0;
for (const name in geometry.attributes) {
const attr = geometry.getAttribute(name);
mem += attr.count * attr.itemSize * attr.array.BYTES_PER_ELEMENT;
}
const indices = geometry.getIndex();
mem += indices ? indices.count * indices.itemSize * indices.array.BYTES_PER_ELEMENT : 0;
return mem;
}
/**
* Returns a new geometry with vertices for which all similar vertex attributes (within tolerance) are merged.
*
* @param {BufferGeometry} geometry - The geometry to merge vertices for.
* @param {number} [tolerance=1e-4] - The tolerance value.
* @return {BufferGeometry} - The new geometry with merged vertices.
*/
function mergeVertices(geometry, tolerance = 1e-4) {
tolerance = Math.max(tolerance, Number.EPSILON);
// Generate an index buffer if the geometry doesn't have one, or optimize it
// if it's already available.
const hashToIndex = {};
const indices = geometry.getIndex();
const positions = geometry.getAttribute("position");
const vertexCount = indices ? indices.count : positions.count;
// next value for triangle indices
let nextIndex = 0;
// attributes and new attribute arrays
const attributeNames = Object.keys(geometry.attributes);
const tmpAttributes = {};
const tmpMorphAttributes = {};
const newIndices = [];
const getters = ["getX", "getY", "getZ", "getW"];
const setters = ["setX", "setY", "setZ", "setW"];
// Initialize the arrays, allocating space conservatively. Extra
// space will be trimmed in the last step.
for (let i = 0, l = attributeNames.length; i < l; i++) {
const name = attributeNames[i];
const attr = geometry.attributes[name];
tmpAttributes[name] = new attr.constructor(
new attr.array.constructor(attr.count * attr.itemSize),
attr.itemSize,
attr.normalized,
);
const morphAttributes = geometry.morphAttributes[name];
if (morphAttributes) {
if (!tmpMorphAttributes[name]) tmpMorphAttributes[name] = [];
morphAttributes.forEach((morphAttr, i) => {
const array = new morphAttr.array.constructor(morphAttr.count * morphAttr.itemSize);
tmpMorphAttributes[name][i] = new morphAttr.constructor(
array,
morphAttr.itemSize,
morphAttr.normalized,
);
});
}
}
// convert the error tolerance to an amount of decimal places to truncate to
const halfTolerance = tolerance * 0.5;
const exponent = Math.log10(1 / tolerance);
const hashMultiplier = Math.pow(10, exponent);
const hashAdditive = halfTolerance * hashMultiplier;
for (let i = 0; i < vertexCount; i++) {
const index = indices ? indices.getX(i) : i;
// Generate a hash for the vertex attributes at the current index 'i'
let hash = "";
for (let j = 0, l = attributeNames.length; j < l; j++) {
const name = attributeNames[j];
const attribute = geometry.getAttribute(name);
const itemSize = attribute.itemSize;
for (let k = 0; k < itemSize; k++) {
// double tilde truncates the decimal value
hash += `${~~(attribute[getters[k]](index) * hashMultiplier + hashAdditive)},`;
}
}
// Add another reference to the vertex if it's already
// used by another index
if (hash in hashToIndex) {
newIndices.push(hashToIndex[hash]);
} else {
// copy data to the new index in the temporary attributes
for (let j = 0, l = attributeNames.length; j < l; j++) {
const name = attributeNames[j];
c… [17022 more characters not shown]