[add] playwright.config.js [add] src/app/App.js [add] src/app/editor-store.js [add] src/app/use-editor-store.js [add] src/assets/audio-assets.js [add] src/assets/gltf-model-import.js [add] src/assets/image-assets.js [add] src/assets/model-instance-labels.js [add] src/assets/model-instance-rendering.js [add] src/assets/model-instances.js [add] src/assets/project-asset-storage.js [add] src/assets/project-assets.js [add] src/commands/brush-command-helpers.js [add] src/commands/command-history.js [add] src/commands/command.js [add] src/commands/commit-transform-session-command.js [add] src/commands/create-box-brush-command.js [add] src/commands/delete-box-brush-command.js [add] src/commands/delete-entity-command.js [add] src/commands/delete-interaction-link-command.js [add] src/commands/delete-model-instance-command.js [add] src/commands/import-audio-asset-command.js [add] src/commands/import-background-image-asset-command.js [add] src/commands/import-model-asset-command.js [add] src/commands/move-box-brush-command.js [add] src/commands/resize-box-brush-command.js [add] src/commands/rotate-box-brush-command.js [add] src/commands/set-box-brush-face-material-command.js [add] src/commands/set-box-brush-face-uv-state-command.js [add] src/commands/set-box-brush-name-command.js [add] src/commands/set-box-brush-transform-command.js [add] src/commands/set-entity-name-command.js [add] src/commands/set-model-instance-name-command.js [add] src/commands/set-player-start-command.js [add] src/commands/set-scene-name-command.js [add] src/commands/set-world-settings-command.js [add] src/commands/upsert-entity-command.js [add] src/commands/upsert-interaction-link-command.js [add] src/commands/upsert-model-instance-command.js [add] src/core/ids.js [add] src/core/selection.js [add] src/core/tool-mode.js [add] src/core/transform-session.js [add] src/core/vector.js [add] src/core/whitebox-selection-feedback.js [add] src/core/whitebox-selection-mode.js [add] src/document/brushes.js [add] src/document/migrate-scene-document.js [add] src/document/scene-document-validation.js [add] src/document/scene-document.js [add] src/document/world-settings.js [add] src/entities/entity-instances.js [add] src/entities/entity-labels.js [add] src/geometry/box-brush-components.js [add] src/geometry/box-brush-mesh.js [add] src/geometry/box-brush.js [add] src/geometry/box-face-uvs.js [add] src/geometry/grid-snapping.js [add] src/geometry/model-instance-collider-debug-mesh.js [add] src/geometry/model-instance-collider-generation.js [add] src/interactions/interaction-links.js [add] src/main.js [add] src/materials/starter-material-library.js [add] src/materials/starter-material-textures.js [add] src/rendering/advanced-rendering.js [add] src/runner-web/RunnerCanvas.js [add] src/runtime-three/first-person-navigation-controller.js [add] src/runtime-three/navigation-controller.js [add] src/runtime-three/orbit-visitor-navigation-controller.js [add] src/runtime-three/player-collision.js [add] src/runtime-three/rapier-collision-world.js [add] src/runtime-three/runtime-audio-system.js [add] src/runtime-three/runtime-host.js [add] src/runtime-three/runtime-interaction-system.js [add] src/runtime-three/runtime-scene-build.js [add] src/runtime-three/runtime-scene-validation.js [add] src/serialization/local-draft-storage.js [add] src/serialization/scene-document-json.js [add] src/shared-ui/HierarchicalMenu.js [add] src/shared-ui/Panel.js [add] src/shared-ui/world-background-style.js [add] src/viewport-three/ViewportCanvas.js [add] src/viewport-three/ViewportPanel.js [add] src/viewport-three/viewport-entity-markers.js [add] src/viewport-three/viewport-focus.js [add] src/viewport-three/viewport-host.js [add] src/viewport-three/viewport-layout.js [add] src/viewport-three/viewport-transient-state.js [add] src/viewport-three/viewport-view-modes.js [add] tests/domain/box-brush-face-editing.command.test.js [add] tests/domain/build-runtime-scene.test.js [add] tests/domain/create-box-brush.command.test.js [add] tests/domain/create-empty-scene-document.test.js [add] tests/domain/editor-store.test.js [add] tests/domain/entity.command.test.js [add] tests/domain/interaction-links.validation.test.js [add] tests/domain/model-import.test.js [add] tests/domain/model-instance.command.test.js [add] tests/domain/player-start.command.test.js [add] tests/domain/rapier-collision-world.test.js [add] tests/domain/runtime-audio-system.test.js [add] tests/domain/runtime-interaction-system.test.js [add] tests/domain/runtime-scene-validation.test.js [add] tests/domain/scene-document-validation.test.js [add] tests/domain/transform-session.command.test.js [add] tests/domain/world-settings.command.test.js [add] tests/domain/world-settings.test.js [add] tests/e2e/app-smoke.e2e.js [add] tests/e2e/box-brush-authoring.e2e.js [add] tests/e2e/entities-foundation.e2e.js [add] tests/e2e/face-material-authoring.e2e.js [add] tests/e2e/first-room-workflow.e2e.js [add] tests/e2e/import-draco-model-asset.e2e.js [add] tests/e2e/import-external-model-asset.e2e.js [add] tests/e2e/import-model-asset.e2e.js [add] tests/e2e/local-lights-and-background.e2e.js [add] tests/e2e/orthographic-views.e2e.js [add] tests/e2e/runner-v1.e2e.js [add] tests/e2e/runtime-click-interaction.e2e.js [add] tests/e2e/runtime-trigger-teleport.e2e.js [add] tests/e2e/viewport-quad-layout.e2e.js [add] tests/e2e/viewport-test-helpers.js [add] tests/e2e/whitebox-component-selection.e2e.js [add] tests/e2e/world-environment.e2e.js [add] tests/geometry/box-brush-geometry.test.js [add] tests/geometry/box-face-uvs.test.js [add] tests/geometry/model-instance-collider-generation.test.js [add] tests/helpers/model-collider-fixtures.js [add] tests/serialization/local-draft-storage.test.js [add] tests/serialization/project-asset-storage.test.js [add] tests/serialization/scene-document-json.test.js [add] tests/setup/vitest.setup.js [add] tests/unit/audio-assets.test.js [add] tests/unit/entity-instances.test.js [add] tests/unit/package-scripts.test.js [add] tests/unit/transform-foundation.integration.test.js [add] tests/unit/viewport-canvas.test.js [add] tests/unit/viewport-entity-markers.test.js [add] tests/unit/viewport-focus.test.js [add] tests/unit/viewport-layout.test.js [add] tests/unit/viewport-view-modes.test.js [add] vite.config.js [add] vitest.config.js
420 lines
18 KiB
JavaScript
420 lines
18 KiB
JavaScript
import { Euler, Group, MathUtils, Matrix4, Mesh, Quaternion, Vector3 } from "three";
|
|
const TERRAIN_GRID_EPSILON = 1e-4;
|
|
const DYNAMIC_TRIANGLE_TARGET = 48;
|
|
const DYNAMIC_SPLIT_DEPTH_LIMIT = 3;
|
|
export class ModelColliderGenerationError extends Error {
|
|
code;
|
|
constructor(code, message) {
|
|
super(message);
|
|
this.name = "ModelColliderGenerationError";
|
|
this.code = code;
|
|
}
|
|
}
|
|
function cloneVec3(vector) {
|
|
return {
|
|
x: vector.x,
|
|
y: vector.y,
|
|
z: vector.z
|
|
};
|
|
}
|
|
function vector3ToVec3(vector) {
|
|
return {
|
|
x: vector.x,
|
|
y: vector.y,
|
|
z: vector.z
|
|
};
|
|
}
|
|
function createBounds(min, max) {
|
|
return {
|
|
min: vector3ToVec3(min),
|
|
max: vector3ToVec3(max)
|
|
};
|
|
}
|
|
function createModelTransform(modelInstance) {
|
|
return {
|
|
position: cloneVec3(modelInstance.position),
|
|
rotationDegrees: cloneVec3(modelInstance.rotationDegrees),
|
|
scale: cloneVec3(modelInstance.scale)
|
|
};
|
|
}
|
|
function createModelTransformMatrix(modelInstance) {
|
|
const rotation = new Euler(MathUtils.degToRad(modelInstance.rotationDegrees.x), MathUtils.degToRad(modelInstance.rotationDegrees.y), MathUtils.degToRad(modelInstance.rotationDegrees.z), "XYZ");
|
|
const quaternion = new Quaternion().setFromEuler(rotation);
|
|
return new Matrix4().compose(new Vector3(modelInstance.position.x, modelInstance.position.y, modelInstance.position.z), quaternion, new Vector3(modelInstance.scale.x, modelInstance.scale.y, modelInstance.scale.z));
|
|
}
|
|
function computeBoundsFromPoints(points) {
|
|
const min = new Vector3(Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY);
|
|
const max = new Vector3(Number.NEGATIVE_INFINITY, Number.NEGATIVE_INFINITY, Number.NEGATIVE_INFINITY);
|
|
let hasPoint = false;
|
|
for (const point of points) {
|
|
hasPoint = true;
|
|
min.min(point);
|
|
max.max(point);
|
|
}
|
|
if (!hasPoint) {
|
|
throw new ModelColliderGenerationError("missing-model-collider-geometry", "The selected model does not contain any collision-capable geometry.");
|
|
}
|
|
return createBounds(min, max);
|
|
}
|
|
function computeBoundsFromFloat32Points(points) {
|
|
if (points.length < 3) {
|
|
throw new ModelColliderGenerationError("missing-model-collider-geometry", "The selected model does not contain any collision-capable geometry.");
|
|
}
|
|
const min = new Vector3(Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY);
|
|
const max = new Vector3(Number.NEGATIVE_INFINITY, Number.NEGATIVE_INFINITY, Number.NEGATIVE_INFINITY);
|
|
for (let index = 0; index < points.length; index += 3) {
|
|
min.x = Math.min(min.x, points[index]);
|
|
min.y = Math.min(min.y, points[index + 1]);
|
|
min.z = Math.min(min.z, points[index + 2]);
|
|
max.x = Math.max(max.x, points[index]);
|
|
max.y = Math.max(max.y, points[index + 1]);
|
|
max.z = Math.max(max.z, points[index + 2]);
|
|
}
|
|
return createBounds(min, max);
|
|
}
|
|
function computeWorldBoundsFromLocalBox(localBounds, modelMatrix) {
|
|
const min = localBounds.min;
|
|
const max = localBounds.max;
|
|
const corners = [
|
|
new Vector3(min.x, min.y, min.z),
|
|
new Vector3(min.x, min.y, max.z),
|
|
new Vector3(min.x, max.y, min.z),
|
|
new Vector3(min.x, max.y, max.z),
|
|
new Vector3(max.x, min.y, min.z),
|
|
new Vector3(max.x, min.y, max.z),
|
|
new Vector3(max.x, max.y, min.z),
|
|
new Vector3(max.x, max.y, max.z)
|
|
];
|
|
return computeBoundsFromPoints(corners.map((corner) => corner.applyMatrix4(modelMatrix)));
|
|
}
|
|
function readIndexedVertex(position, index, matrix) {
|
|
return new Vector3(position.getX(index), position.getY(index), position.getZ(index)).applyMatrix4(matrix);
|
|
}
|
|
function getMeshGeometry(object) {
|
|
const maybeMesh = object;
|
|
if (maybeMesh.isMesh !== true) {
|
|
return null;
|
|
}
|
|
return maybeMesh.geometry;
|
|
}
|
|
function collectMeshTriangleClusters(template) {
|
|
template.updateMatrixWorld(true);
|
|
const clusters = [];
|
|
template.traverse((object) => {
|
|
const geometry = getMeshGeometry(object);
|
|
if (geometry === null) {
|
|
return;
|
|
}
|
|
const position = geometry.getAttribute("position");
|
|
if (position === undefined || position.itemSize < 3 || position.count < 3) {
|
|
return;
|
|
}
|
|
const matrix = object.matrixWorld;
|
|
const index = geometry.getIndex();
|
|
const triangles = [];
|
|
if (index === null) {
|
|
for (let vertexIndex = 0; vertexIndex <= position.count - 3; vertexIndex += 3) {
|
|
triangles.push({
|
|
a: readIndexedVertex(position, vertexIndex, matrix),
|
|
b: readIndexedVertex(position, vertexIndex + 1, matrix),
|
|
c: readIndexedVertex(position, vertexIndex + 2, matrix)
|
|
});
|
|
}
|
|
}
|
|
else {
|
|
for (let triangleIndex = 0; triangleIndex <= index.count - 3; triangleIndex += 3) {
|
|
triangles.push({
|
|
a: readIndexedVertex(position, index.getX(triangleIndex), matrix),
|
|
b: readIndexedVertex(position, index.getX(triangleIndex + 1), matrix),
|
|
c: readIndexedVertex(position, index.getX(triangleIndex + 2), matrix)
|
|
});
|
|
}
|
|
}
|
|
if (triangles.length > 0) {
|
|
clusters.push({
|
|
triangles
|
|
});
|
|
}
|
|
});
|
|
return clusters;
|
|
}
|
|
function flattenTriangleClusters(clusters) {
|
|
return clusters.flatMap((cluster) => cluster.triangles);
|
|
}
|
|
function buildTriMeshBuffers(triangles) {
|
|
const vertices = new Float32Array(triangles.length * 9);
|
|
const indices = new Uint32Array(triangles.length * 3);
|
|
let vertexOffset = 0;
|
|
for (let triangleIndex = 0; triangleIndex < triangles.length; triangleIndex += 1) {
|
|
const triangle = triangles[triangleIndex];
|
|
vertices[vertexOffset] = triangle.a.x;
|
|
vertices[vertexOffset + 1] = triangle.a.y;
|
|
vertices[vertexOffset + 2] = triangle.a.z;
|
|
vertices[vertexOffset + 3] = triangle.b.x;
|
|
vertices[vertexOffset + 4] = triangle.b.y;
|
|
vertices[vertexOffset + 5] = triangle.b.z;
|
|
vertices[vertexOffset + 6] = triangle.c.x;
|
|
vertices[vertexOffset + 7] = triangle.c.y;
|
|
vertices[vertexOffset + 8] = triangle.c.z;
|
|
indices[triangleIndex * 3] = triangleIndex * 3;
|
|
indices[triangleIndex * 3 + 1] = triangleIndex * 3 + 1;
|
|
indices[triangleIndex * 3 + 2] = triangleIndex * 3 + 2;
|
|
vertexOffset += 9;
|
|
}
|
|
return {
|
|
vertices,
|
|
indices
|
|
};
|
|
}
|
|
function computeClusterCentroid(triangles) {
|
|
const centroid = {
|
|
x: 0,
|
|
y: 0,
|
|
z: 0
|
|
};
|
|
let pointCount = 0;
|
|
for (const triangle of triangles) {
|
|
centroid.x += triangle.a.x + triangle.b.x + triangle.c.x;
|
|
centroid.y += triangle.a.y + triangle.b.y + triangle.c.y;
|
|
centroid.z += triangle.a.z + triangle.b.z + triangle.c.z;
|
|
pointCount += 3;
|
|
}
|
|
return {
|
|
x: centroid.x / pointCount,
|
|
y: centroid.y / pointCount,
|
|
z: centroid.z / pointCount
|
|
};
|
|
}
|
|
function getTriangleBounds(triangles) {
|
|
return computeBoundsFromPoints(triangles.flatMap((triangle) => [triangle.a, triangle.b, triangle.c]));
|
|
}
|
|
function splitTriangleCluster(triangles, depth) {
|
|
if (triangles.length <= DYNAMIC_TRIANGLE_TARGET || depth >= DYNAMIC_SPLIT_DEPTH_LIMIT) {
|
|
return {
|
|
kind: "leaf",
|
|
triangles
|
|
};
|
|
}
|
|
const bounds = getTriangleBounds(triangles);
|
|
const size = {
|
|
x: bounds.max.x - bounds.min.x,
|
|
y: bounds.max.y - bounds.min.y,
|
|
z: bounds.max.z - bounds.min.z
|
|
};
|
|
const splitAxis = size.x >= size.y && size.x >= size.z ? "x" : size.y >= size.z ? "y" : "z";
|
|
const sortedTriangles = [...triangles].sort((left, right) => computeClusterCentroid([left])[splitAxis] - computeClusterCentroid([right])[splitAxis]);
|
|
const splitIndex = Math.floor(sortedTriangles.length * 0.5);
|
|
if (splitIndex <= 0 || splitIndex >= sortedTriangles.length) {
|
|
return {
|
|
kind: "leaf",
|
|
triangles
|
|
};
|
|
}
|
|
return {
|
|
kind: "split",
|
|
left: sortedTriangles.slice(0, splitIndex),
|
|
right: sortedTriangles.slice(splitIndex)
|
|
};
|
|
}
|
|
function collectConvexHullPointClouds(cluster, depth = 0) {
|
|
const split = splitTriangleCluster(cluster, depth);
|
|
if (split.kind === "leaf") {
|
|
return [dedupeTriangleClusterPoints(split.triangles)];
|
|
}
|
|
return [...collectConvexHullPointClouds(split.left, depth + 1), ...collectConvexHullPointClouds(split.right, depth + 1)];
|
|
}
|
|
function quantizeCoordinate(value) {
|
|
return (Math.round(value / TERRAIN_GRID_EPSILON) * TERRAIN_GRID_EPSILON).toFixed(4);
|
|
}
|
|
function dedupeTriangleClusterPoints(triangles) {
|
|
const pointLookup = new Map();
|
|
for (const triangle of triangles) {
|
|
for (const point of [triangle.a, triangle.b, triangle.c]) {
|
|
const key = `${quantizeCoordinate(point.x)}:${quantizeCoordinate(point.y)}:${quantizeCoordinate(point.z)}`;
|
|
if (!pointLookup.has(key)) {
|
|
pointLookup.set(key, {
|
|
x: point.x,
|
|
y: point.y,
|
|
z: point.z
|
|
});
|
|
}
|
|
}
|
|
}
|
|
if (pointLookup.size < 4) {
|
|
throw new ModelColliderGenerationError("unsupported-dynamic-model-collider", "Dynamic collision requires volumetric geometry that can form at least one convex hull.");
|
|
}
|
|
return new Float32Array(Array.from(pointLookup.values()).flatMap((point) => [point.x, point.y, point.z]));
|
|
}
|
|
function buildSimpleBoxCollider(modelInstance, asset) {
|
|
const boundingBox = asset.metadata.boundingBox;
|
|
if (boundingBox === null) {
|
|
throw new ModelColliderGenerationError("missing-model-collider-bounds", `Model instance ${modelInstance.id} cannot use simple collision because the asset does not have a measurable bounding box.`);
|
|
}
|
|
const localBounds = createBounds(new Vector3(boundingBox.min.x, boundingBox.min.y, boundingBox.min.z), new Vector3(boundingBox.max.x, boundingBox.max.y, boundingBox.max.z));
|
|
return {
|
|
source: "modelInstance",
|
|
instanceId: modelInstance.id,
|
|
assetId: modelInstance.assetId,
|
|
mode: "simple",
|
|
kind: "box",
|
|
visible: modelInstance.collision.visible,
|
|
transform: createModelTransform(modelInstance),
|
|
center: {
|
|
x: (boundingBox.min.x + boundingBox.max.x) * 0.5,
|
|
y: (boundingBox.min.y + boundingBox.max.y) * 0.5,
|
|
z: (boundingBox.min.z + boundingBox.max.z) * 0.5
|
|
},
|
|
size: cloneVec3(boundingBox.size),
|
|
localBounds,
|
|
worldBounds: computeWorldBoundsFromLocalBox(localBounds, createModelTransformMatrix(modelInstance))
|
|
};
|
|
}
|
|
function buildTriMeshCollider(modelInstance, asset, loadedAsset) {
|
|
if (loadedAsset === undefined) {
|
|
throw new ModelColliderGenerationError("missing-model-collider-geometry", `Model instance ${modelInstance.id} cannot build ${modelInstance.collision.mode} collision until asset geometry has loaded.`);
|
|
}
|
|
const triangles = flattenTriangleClusters(collectMeshTriangleClusters(loadedAsset.template));
|
|
if (triangles.length === 0) {
|
|
throw new ModelColliderGenerationError("missing-model-collider-geometry", `Model instance ${modelInstance.id} cannot use ${modelInstance.collision.mode} collision because the asset has no mesh triangles.`);
|
|
}
|
|
const buffers = buildTriMeshBuffers(triangles);
|
|
const localBounds = computeBoundsFromFloat32Points(buffers.vertices);
|
|
return {
|
|
source: "modelInstance",
|
|
instanceId: modelInstance.id,
|
|
assetId: asset.id,
|
|
mode: "static",
|
|
kind: "trimesh",
|
|
visible: modelInstance.collision.visible,
|
|
transform: createModelTransform(modelInstance),
|
|
vertices: buffers.vertices,
|
|
indices: buffers.indices,
|
|
triangleCount: triangles.length,
|
|
localBounds,
|
|
worldBounds: computeWorldBoundsFromLocalBox(localBounds, createModelTransformMatrix(modelInstance))
|
|
};
|
|
}
|
|
function buildTerrainCollider(modelInstance, asset, loadedAsset) {
|
|
if (loadedAsset === undefined) {
|
|
throw new ModelColliderGenerationError("missing-model-collider-geometry", `Model instance ${modelInstance.id} cannot build terrain collision until asset geometry has loaded.`);
|
|
}
|
|
const triangles = flattenTriangleClusters(collectMeshTriangleClusters(loadedAsset.template));
|
|
if (triangles.length === 0) {
|
|
throw new ModelColliderGenerationError("missing-model-collider-geometry", `Model instance ${modelInstance.id} cannot use terrain collision because the asset has no mesh triangles.`);
|
|
}
|
|
const heightLookup = new Map();
|
|
const xValues = new Map();
|
|
const zValues = new Map();
|
|
for (const triangle of triangles) {
|
|
for (const point of [triangle.a, triangle.b, triangle.c]) {
|
|
const xKey = quantizeCoordinate(point.x);
|
|
const zKey = quantizeCoordinate(point.z);
|
|
const key = `${xKey}:${zKey}`;
|
|
const previousPoint = heightLookup.get(key);
|
|
if (previousPoint !== undefined && Math.abs(previousPoint.y - point.y) > TERRAIN_GRID_EPSILON) {
|
|
throw new ModelColliderGenerationError("unsupported-terrain-model-collider", `Model instance ${modelInstance.id} cannot use terrain collision because the source mesh is not a single-valued heightfield over X/Z.`);
|
|
}
|
|
heightLookup.set(key, {
|
|
x: point.x,
|
|
y: point.y,
|
|
z: point.z
|
|
});
|
|
xValues.set(xKey, point.x);
|
|
zValues.set(zKey, point.z);
|
|
}
|
|
}
|
|
const sortedX = Array.from(xValues.values()).sort((left, right) => left - right);
|
|
const sortedZ = Array.from(zValues.values()).sort((left, right) => left - right);
|
|
if (sortedX.length < 2 || sortedZ.length < 2) {
|
|
throw new ModelColliderGenerationError("unsupported-terrain-model-collider", `Model instance ${modelInstance.id} cannot use terrain collision because the source mesh does not form a regular X/Z grid.`);
|
|
}
|
|
const expectedTriangleCount = (sortedX.length - 1) * (sortedZ.length - 1) * 2;
|
|
if (triangles.length !== expectedTriangleCount) {
|
|
throw new ModelColliderGenerationError("unsupported-terrain-model-collider", `Model instance ${modelInstance.id} cannot use terrain collision because the source mesh is not a clean regular-grid terrain surface.`);
|
|
}
|
|
const heights = new Float32Array(sortedX.length * sortedZ.length);
|
|
for (let zIndex = 0; zIndex < sortedZ.length; zIndex += 1) {
|
|
for (let xIndex = 0; xIndex < sortedX.length; xIndex += 1) {
|
|
const key = `${quantizeCoordinate(sortedX[xIndex])}:${quantizeCoordinate(sortedZ[zIndex])}`;
|
|
const point = heightLookup.get(key);
|
|
if (point === undefined) {
|
|
throw new ModelColliderGenerationError("unsupported-terrain-model-collider", `Model instance ${modelInstance.id} cannot use terrain collision because the source mesh is missing one or more regular-grid height samples.`);
|
|
}
|
|
heights[xIndex + zIndex * sortedX.length] = point.y;
|
|
}
|
|
}
|
|
const localBounds = computeBoundsFromPoints(Array.from(heightLookup.values(), (point) => new Vector3(point.x, point.y, point.z)));
|
|
return {
|
|
source: "modelInstance",
|
|
instanceId: modelInstance.id,
|
|
assetId: asset.id,
|
|
mode: "terrain",
|
|
kind: "heightfield",
|
|
visible: modelInstance.collision.visible,
|
|
transform: createModelTransform(modelInstance),
|
|
rows: sortedX.length,
|
|
cols: sortedZ.length,
|
|
heights,
|
|
minX: sortedX[0],
|
|
maxX: sortedX.at(-1) ?? sortedX[0],
|
|
minZ: sortedZ[0],
|
|
maxZ: sortedZ.at(-1) ?? sortedZ[0],
|
|
localBounds,
|
|
worldBounds: computeWorldBoundsFromLocalBox(localBounds, createModelTransformMatrix(modelInstance))
|
|
};
|
|
}
|
|
function buildDynamicCollider(modelInstance, asset, loadedAsset) {
|
|
if (loadedAsset === undefined) {
|
|
throw new ModelColliderGenerationError("missing-model-collider-geometry", `Model instance ${modelInstance.id} cannot build dynamic collision until asset geometry has loaded.`);
|
|
}
|
|
const triangleClusters = collectMeshTriangleClusters(loadedAsset.template);
|
|
if (triangleClusters.length === 0) {
|
|
throw new ModelColliderGenerationError("missing-model-collider-geometry", `Model instance ${modelInstance.id} cannot use dynamic collision because the asset has no mesh triangles.`);
|
|
}
|
|
const pieces = triangleClusters
|
|
.flatMap((cluster) => collectConvexHullPointClouds(cluster.triangles))
|
|
.map((points, index) => ({
|
|
id: `${modelInstance.id}-piece-${index + 1}`,
|
|
points,
|
|
localBounds: computeBoundsFromFloat32Points(points)
|
|
}));
|
|
if (pieces.length === 0) {
|
|
throw new ModelColliderGenerationError("unsupported-dynamic-model-collider", `Model instance ${modelInstance.id} could not derive any convex pieces for dynamic collision.`);
|
|
}
|
|
const localBounds = computeBoundsFromPoints(pieces.flatMap((piece) => {
|
|
const points = [];
|
|
for (let pointIndex = 0; pointIndex < piece.points.length; pointIndex += 3) {
|
|
points.push(new Vector3(piece.points[pointIndex], piece.points[pointIndex + 1], piece.points[pointIndex + 2]));
|
|
}
|
|
return points;
|
|
}));
|
|
return {
|
|
source: "modelInstance",
|
|
instanceId: modelInstance.id,
|
|
assetId: asset.id,
|
|
mode: "dynamic",
|
|
kind: "compound",
|
|
visible: modelInstance.collision.visible,
|
|
transform: createModelTransform(modelInstance),
|
|
pieces,
|
|
decomposition: "spatial-bisect",
|
|
runtimeBehavior: "fixedQueryOnly",
|
|
localBounds,
|
|
worldBounds: computeWorldBoundsFromLocalBox(localBounds, createModelTransformMatrix(modelInstance))
|
|
};
|
|
}
|
|
export function buildGeneratedModelCollider(modelInstance, asset, loadedAsset) {
|
|
switch (modelInstance.collision.mode) {
|
|
case "none":
|
|
return null;
|
|
case "simple":
|
|
return buildSimpleBoxCollider(modelInstance, asset);
|
|
case "static":
|
|
return buildTriMeshCollider(modelInstance, asset, loadedAsset);
|
|
case "terrain":
|
|
return buildTerrainCollider(modelInstance, asset, loadedAsset);
|
|
case "dynamic":
|
|
return buildDynamicCollider(modelInstance, asset, loadedAsset);
|
|
}
|
|
}
|