auto-git:
[add] src/rendering/terrain-layer-material.ts [add] tests/domain/terrains.test.ts [change] src/app/App.tsx [change] src/core/terrain-brush.ts [change] src/document/migrate-scene-document.ts [change] src/document/scene-document-validation.ts [change] src/document/scene-document.ts [change] src/document/terrains.ts [change] src/geometry/terrain-brush.ts [change] src/geometry/terrain-mesh.ts [change] src/runtime-three/rapier-collision-world.ts [change] src/runtime-three/runtime-host.ts [change] src/runtime-three/runtime-scene-build.ts [change] src/viewport-three/ViewportCanvas.tsx [change] src/viewport-three/ViewportPanel.tsx [change] src/viewport-three/viewport-host.ts [change] tests/domain/build-runtime-scene.test.ts [change] tests/domain/rapier-collision-world.test.ts [change] tests/domain/terrain.command.test.ts [change] tests/domain/water-material.test.ts [change] tests/geometry/terrain-brush.test.ts [change] tests/geometry/terrain-mesh.test.ts [change] tests/serialization/scene-document-json.test.ts [change] tests/unit/terrain-foundation.integration.test.tsx [change] tests/unit/viewport-canvas.test.tsx
This commit is contained in:
@@ -19,6 +19,7 @@ import { getFirstPersonPlayerShapeSignature } from "./player-collision";
|
||||
import type {
|
||||
RuntimeBrushTriMeshCollider,
|
||||
RuntimeNpcCollider,
|
||||
RuntimeTerrainHeightfieldCollider,
|
||||
RuntimeSceneCollider
|
||||
} from "./runtime-scene-build";
|
||||
|
||||
@@ -86,7 +87,11 @@ function scaleBoundsCenter(bounds: { min: Vec3; max: Vec3 }, scale: Vec3): Vec3
|
||||
};
|
||||
}
|
||||
|
||||
function createRapierHeightfieldHeights(collider: GeneratedModelHeightfieldCollider): Float32Array {
|
||||
function createRapierHeightfieldHeights(collider: {
|
||||
rows: number;
|
||||
cols: number;
|
||||
heights: ArrayLike<number>;
|
||||
}): Float32Array {
|
||||
const heights = new Float32Array(collider.heights.length);
|
||||
|
||||
// Rapier's heightfield samples are column-major, with the Z axis varying
|
||||
@@ -182,6 +187,45 @@ function attachTerrainModelCollider(world: RAPIER.World, collider: GeneratedMode
|
||||
);
|
||||
}
|
||||
|
||||
function attachTerrainCollider(
|
||||
world: RAPIER.World,
|
||||
collider: RuntimeTerrainHeightfieldCollider
|
||||
) {
|
||||
if (collider.rows < 2 || collider.cols < 2) {
|
||||
throw new Error(
|
||||
`Terrain collider ${collider.terrainId} must have at least a 2x2 height sample grid.`
|
||||
);
|
||||
}
|
||||
|
||||
const body = world.createRigidBody(
|
||||
RAPIER.RigidBodyDesc.fixed().setTranslation(
|
||||
collider.position.x,
|
||||
collider.position.y,
|
||||
collider.position.z
|
||||
)
|
||||
);
|
||||
const rowSubdivisions = collider.rows - 1;
|
||||
const colSubdivisions = collider.cols - 1;
|
||||
|
||||
world.createCollider(
|
||||
RAPIER.ColliderDesc.heightfield(
|
||||
rowSubdivisions,
|
||||
colSubdivisions,
|
||||
createRapierHeightfieldHeights(collider),
|
||||
{
|
||||
x: collider.maxX - collider.minX,
|
||||
y: 1,
|
||||
z: collider.maxZ - collider.minZ
|
||||
}
|
||||
).setTranslation(
|
||||
(collider.minX + collider.maxX) * 0.5,
|
||||
0,
|
||||
(collider.minZ + collider.maxZ) * 0.5
|
||||
),
|
||||
body
|
||||
);
|
||||
}
|
||||
|
||||
function attachDynamicModelCollider(world: RAPIER.World, collider: GeneratedModelCompoundCollider) {
|
||||
const body = createFixedBodyForModelCollider(world, collider);
|
||||
|
||||
@@ -385,6 +429,11 @@ export class RapierCollisionWorld {
|
||||
});
|
||||
|
||||
for (const collider of colliders) {
|
||||
if (collider.source === "terrain") {
|
||||
attachTerrainCollider(world, collider);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (collider.source === "brush") {
|
||||
attachBrushCollider(world, collider);
|
||||
continue;
|
||||
|
||||
@@ -27,6 +27,7 @@ import {
|
||||
Vector3,
|
||||
SpotLight,
|
||||
TextureLoader,
|
||||
Texture,
|
||||
WebGLRenderTarget,
|
||||
WebGLRenderer
|
||||
} from "three";
|
||||
@@ -88,6 +89,10 @@ import {
|
||||
} from "../rendering/water-material";
|
||||
import { createFogQualityMaterial } from "../rendering/fog-material";
|
||||
import { updatePlanarReflectionCamera } from "../rendering/planar-reflection";
|
||||
import {
|
||||
createTerrainLayerBlendMaterial,
|
||||
getTerrainLayerTexture
|
||||
} from "../rendering/terrain-layer-material";
|
||||
import {
|
||||
applyWhiteboxBevelToMaterial,
|
||||
shouldApplyWhiteboxBevel
|
||||
@@ -1924,14 +1929,7 @@ export class RuntimeHost {
|
||||
kind: "terrain",
|
||||
enabled: true
|
||||
}).geometry;
|
||||
const mesh = new Mesh(
|
||||
geometry,
|
||||
new MeshStandardMaterial({
|
||||
color: 0x708b57,
|
||||
roughness: 0.98,
|
||||
metalness: 0
|
||||
})
|
||||
);
|
||||
const mesh = new Mesh(geometry, this.createRuntimeTerrainMaterial(terrain));
|
||||
|
||||
mesh.position.set(
|
||||
terrain.position.x,
|
||||
@@ -1948,6 +1946,18 @@ export class RuntimeHost {
|
||||
this.applyShadowState();
|
||||
}
|
||||
|
||||
private createRuntimeTerrainMaterial(terrain: RuntimeTerrain): Material {
|
||||
const layerTextures = terrain.layers.map((layer) =>
|
||||
getTerrainLayerTexture(layer.material, (material) =>
|
||||
this.getOrCreateTextureSet(material).baseColor
|
||||
)
|
||||
) as [Texture, Texture, Texture, Texture];
|
||||
|
||||
return createTerrainLayerBlendMaterial({
|
||||
layerTextures
|
||||
});
|
||||
}
|
||||
|
||||
private createFogMaterialSet(
|
||||
brush: RuntimeBoxBrushInstance,
|
||||
volumeRenderPaths: {
|
||||
@@ -2771,7 +2781,43 @@ export class RuntimeHost {
|
||||
return [];
|
||||
}
|
||||
|
||||
for (const terrain of this.runtimeScene.terrains) {
|
||||
if (!terrain.visible) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const derivedMesh = buildTerrainDerivedMeshData({
|
||||
...terrain,
|
||||
kind: "terrain",
|
||||
enabled: true
|
||||
});
|
||||
|
||||
contactBounds.push({
|
||||
kind: "triangleMesh",
|
||||
vertices: derivedMesh.positions,
|
||||
indices: derivedMesh.indices,
|
||||
mergeProfile: "aggressive",
|
||||
transform: {
|
||||
position: terrain.position,
|
||||
rotationDegrees: {
|
||||
x: 0,
|
||||
y: 0,
|
||||
z: 0
|
||||
},
|
||||
scale: {
|
||||
x: 1,
|
||||
y: 1,
|
||||
z: 1
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
for (const collider of this.runtimeScene.colliders) {
|
||||
if (collider.source === "terrain") {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (collider.kind === "trimesh" && collider.source === "brush") {
|
||||
if (collider.brushId === brush.id) {
|
||||
continue;
|
||||
|
||||
@@ -56,7 +56,13 @@ import {
|
||||
type ScenePath,
|
||||
type ScenePathPoint
|
||||
} from "../document/paths";
|
||||
import { getTerrainBounds, getTerrains, type Terrain } from "../document/terrains";
|
||||
import {
|
||||
getTerrainBounds,
|
||||
getTerrainFootprintDepth,
|
||||
getTerrainFootprintWidth,
|
||||
getTerrains,
|
||||
type Terrain
|
||||
} from "../document/terrains";
|
||||
import {
|
||||
cloneWorldSettings,
|
||||
type WorldSettings
|
||||
@@ -169,11 +175,35 @@ export interface RuntimeTerrain {
|
||||
id: string;
|
||||
name?: string;
|
||||
visible: boolean;
|
||||
collisionEnabled: boolean;
|
||||
position: Vec3;
|
||||
sampleCountX: number;
|
||||
sampleCountZ: number;
|
||||
cellSize: number;
|
||||
heights: number[];
|
||||
layers: Array<{
|
||||
materialId: string | null;
|
||||
material: MaterialDef | null;
|
||||
}>;
|
||||
paintWeights: number[];
|
||||
}
|
||||
|
||||
export interface RuntimeTerrainHeightfieldCollider {
|
||||
kind: "heightfield";
|
||||
source: "terrain";
|
||||
terrainId: string;
|
||||
position: Vec3;
|
||||
rows: number;
|
||||
cols: number;
|
||||
heights: Float32Array;
|
||||
minX: number;
|
||||
maxX: number;
|
||||
minZ: number;
|
||||
maxZ: number;
|
||||
worldBounds: {
|
||||
min: Vec3;
|
||||
max: Vec3;
|
||||
};
|
||||
}
|
||||
|
||||
export interface RuntimeBrushTriMeshCollider {
|
||||
@@ -205,6 +235,7 @@ export interface RuntimeNpcCollider {
|
||||
|
||||
export type RuntimeSceneCollider =
|
||||
| RuntimeBrushTriMeshCollider
|
||||
| RuntimeTerrainHeightfieldCollider
|
||||
| GeneratedModelCollider
|
||||
| RuntimeNpcCollider;
|
||||
|
||||
@@ -724,16 +755,53 @@ function buildRuntimeCollider(brush: Brush): RuntimeBrushTriMeshCollider {
|
||||
};
|
||||
}
|
||||
|
||||
function buildRuntimeTerrain(terrain: Terrain): RuntimeTerrain {
|
||||
function buildRuntimeTerrain(
|
||||
terrain: Terrain,
|
||||
document: SceneDocument
|
||||
): RuntimeTerrain {
|
||||
return {
|
||||
id: terrain.id,
|
||||
name: terrain.name,
|
||||
visible: terrain.visible,
|
||||
collisionEnabled: terrain.collisionEnabled,
|
||||
position: cloneVec3(terrain.position),
|
||||
sampleCountX: terrain.sampleCountX,
|
||||
sampleCountZ: terrain.sampleCountZ,
|
||||
cellSize: terrain.cellSize,
|
||||
heights: [...terrain.heights]
|
||||
heights: [...terrain.heights],
|
||||
layers: terrain.layers.map((layer) => ({
|
||||
materialId: layer.materialId,
|
||||
material: resolveRuntimeMaterial(document, layer.materialId)
|
||||
})),
|
||||
paintWeights: [...terrain.paintWeights]
|
||||
};
|
||||
}
|
||||
|
||||
function buildRuntimeTerrainCollider(
|
||||
terrain: Terrain
|
||||
): RuntimeTerrainHeightfieldCollider | null {
|
||||
if (!terrain.collisionEnabled) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const bounds = getTerrainBounds(terrain);
|
||||
|
||||
return {
|
||||
kind: "heightfield",
|
||||
source: "terrain",
|
||||
terrainId: terrain.id,
|
||||
position: cloneVec3(terrain.position),
|
||||
rows: terrain.sampleCountX,
|
||||
cols: terrain.sampleCountZ,
|
||||
heights: new Float32Array(terrain.heights),
|
||||
minX: 0,
|
||||
maxX: getTerrainFootprintWidth(terrain),
|
||||
minZ: 0,
|
||||
maxZ: getTerrainFootprintDepth(terrain),
|
||||
worldBounds: {
|
||||
min: cloneVec3(bounds.min),
|
||||
max: cloneVec3(bounds.max)
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1610,7 +1678,9 @@ export function buildRuntimeSceneFromDocument(
|
||||
const enabledTerrains = getTerrains(document.terrains).filter(
|
||||
(terrain) => terrain.enabled
|
||||
);
|
||||
const terrains = enabledTerrains.map(buildRuntimeTerrain);
|
||||
const terrains = enabledTerrains.map((terrain) =>
|
||||
buildRuntimeTerrain(terrain, document)
|
||||
);
|
||||
const staticColliders: RuntimeSceneCollider[] = [];
|
||||
const volumes: RuntimeBoxVolumeCollection = {
|
||||
fog: [],
|
||||
@@ -1635,6 +1705,15 @@ export function buildRuntimeSceneFromDocument(
|
||||
|
||||
volumes.water.push(buildRuntimeWaterVolume(brush));
|
||||
}
|
||||
|
||||
for (const terrain of enabledTerrains) {
|
||||
const terrainCollider = buildRuntimeTerrainCollider(terrain);
|
||||
|
||||
if (terrainCollider !== null) {
|
||||
staticColliders.push(terrainCollider);
|
||||
}
|
||||
}
|
||||
|
||||
const enabledModelInstances = getModelInstances(
|
||||
document.modelInstances
|
||||
).filter((modelInstance) => modelInstance.enabled);
|
||||
|
||||
Reference in New Issue
Block a user