@threenative/terrain 0.1.9 → 0.1.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +40 -59
- package/THIRD_PARTY_NOTICES.md +2 -1
- package/dist/bake-DJ5cxg0C.d.ts +82 -0
- package/dist/editor/index.d.ts +4 -4
- package/dist/editor/index.js +130 -128
- package/dist/editor/server.d.ts +2 -2
- package/dist/editor/server.js +38 -35
- package/dist/editor/worker.d.ts +1 -1
- package/dist/editor/worker.js +101 -101
- package/dist/export.d.ts +1 -1
- package/dist/index.d.ts +156 -273
- package/dist/index.js +562 -378
- package/dist/kit-world.d.ts +107 -0
- package/dist/kit-world.js +5134 -0
- package/dist/kit.d.ts +21 -4
- package/dist/kit.js +337 -223
- package/dist/mcp/cook-child.js +1963 -1543
- package/dist/mcp/server.js +716 -429
- package/dist/prop-package-Dwz0EWoB.d.ts +141 -0
- package/dist/{server-B5RdvEAC.d.ts → server-LnFwY8sr.d.ts} +1 -1
- package/dist/starter.d.ts +1142 -7
- package/dist/starter.js +2102 -120
- package/dist/{terrain-CAEaW0mQ.d.ts → terrain-DYCyAOCJ.d.ts} +1 -1
- package/dist/three.d.ts +1 -1
- package/dist/three.js +1 -1
- package/dist/{types-BpH00qsx.d.ts → types-kpI0C4hW.d.ts} +1 -1
- package/dist/{world-package-B8QgHjAQ.d.ts → world-package-DqJC3-Nn.d.ts} +2 -2
- package/docs/authoring/editor.md +73 -0
- package/docs/authoring/new-environment.md +97 -0
- package/docs/authoring/recipes.md +97 -0
- package/docs/authoring/world-export.md +66 -0
- package/package.json +24 -8
- package/starter/alpine/README.md +22 -12
- package/starter/alpine/bake.mjs +14 -1
- package/starter/alpine/biome.data.json +187 -25
- package/starter/alpine/heightmap.r16 +0 -0
- package/starter/alpine/playerRig.ts +89 -0
- package/starter/alpine/recipe.json +169 -15
- package/starter/alpine/render/bakedFile.ts +17 -0
- package/starter/alpine/render/benchmark.ts +77 -0
- package/starter/alpine/render/biomeData.ts +14 -0
- package/starter/alpine/render/biomes.ts +687 -0
- package/starter/alpine/render/ground/cover.ts +565 -0
- package/starter/alpine/render/ground/horizon.ts +219 -0
- package/starter/alpine/render/ground/palette.d.ts +1 -0
- package/starter/alpine/render/ground/palette.js +11 -0
- package/starter/alpine/render/ground/scatter.ts +104 -0
- package/starter/alpine/render/ground/terrain.ts +1860 -0
- package/starter/alpine/render/kitAssets.ts +28 -0
- package/starter/alpine/render/loading.ts +354 -0
- package/starter/alpine/render/lookKnobs.ts +37 -0
- package/starter/alpine/render/orbitCamera.ts +155 -0
- package/starter/alpine/render/props/farForest.ts +94 -0
- package/starter/alpine/render/props/pack.ts +1114 -0
- package/starter/alpine/render/props/prepared.ts +225 -0
- package/starter/alpine/render/props/propMaterials.ts +1239 -0
- package/starter/alpine/render/props/propProgress.ts +2 -0
- package/starter/alpine/render/props/propWorld.ts +105 -0
- package/starter/alpine/render/props/props.ts +633 -0
- package/starter/alpine/render/props/spruce.ts +440 -0
- package/starter/alpine/render/sky/atmosphere.ts +329 -0
- package/starter/alpine/render/sky/sky.ts +382 -0
- package/starter/alpine/render/texturePreparation.ts +135 -0
- package/starter/alpine/render/water/river.ts +1132 -0
- package/starter/alpine/render/worldNames.ts +6 -0
- package/starter/alpine/scene.ts +715 -0
- package/starter/alpine/sceneProps.ts +561 -0
- package/starter/alpine/sceneState.ts +33 -0
- package/starter/alpine/world/terrainAssets.ts +145 -0
- package/starter/alpine/world/walkBand.ts +47 -0
- package/starter/alpine/world/water.ts +74 -0
- package/starter/alpine/worlds.ts +13 -0
- package/starter/arbor/wind.ts +84 -0
- package/starter/coastal/README.md +26 -25
- package/starter/coastal/assets.json +10 -0
- package/starter/coastal/bake.mjs +14 -1
- package/starter/coastal/biome.data.json +18 -3
- package/starter/coastal/playerRig.ts +89 -0
- package/starter/coastal/recipe.json +220 -18
- package/starter/coastal/render/bakedFile.ts +17 -0
- package/starter/coastal/render/benchmark.ts +81 -0
- package/starter/coastal/render/biomeData.ts +14 -0
- package/starter/coastal/render/biomes.ts +678 -0
- package/starter/coastal/render/ground/cover.ts +565 -0
- package/starter/coastal/render/ground/horizon.ts +219 -0
- package/starter/coastal/render/ground/palette.d.ts +1 -0
- package/starter/coastal/render/ground/palette.js +11 -0
- package/starter/coastal/render/ground/scatter.ts +104 -0
- package/starter/coastal/render/ground/terrain.ts +1829 -0
- package/starter/coastal/render/kitAssets.ts +28 -0
- package/starter/coastal/render/loading.ts +354 -0
- package/starter/coastal/render/lookKnobs.ts +37 -0
- package/starter/coastal/render/orbitCamera.ts +155 -0
- package/starter/coastal/render/props/farForest.ts +94 -0
- package/starter/coastal/render/props/pack.ts +1109 -0
- package/starter/coastal/render/props/prepared.ts +225 -0
- package/starter/coastal/render/props/propMaterials.ts +1239 -0
- package/starter/coastal/render/props/propProgress.ts +2 -0
- package/starter/coastal/render/props/propWorld.ts +105 -0
- package/starter/coastal/render/props/props.ts +633 -0
- package/starter/coastal/render/props/spruce.ts +440 -0
- package/starter/coastal/render/sky/atmosphere.ts +329 -0
- package/starter/coastal/render/sky/sky.ts +382 -0
- package/starter/coastal/render/texturePreparation.ts +135 -0
- package/starter/coastal/render/water/ocean.ts +284 -0
- package/starter/coastal/render/water/river.ts +1132 -0
- package/starter/coastal/render/worldNames.ts +6 -0
- package/starter/coastal/scene.ts +740 -0
- package/starter/coastal/sceneProps.ts +559 -0
- package/starter/coastal/sceneState.ts +33 -0
- package/starter/coastal/world/terrainAssets.ts +151 -0
- package/starter/coastal/world/walkBand.ts +47 -0
- package/starter/coastal/world/water.ts +74 -0
- package/starter/coastal/worlds.ts +13 -0
- package/starter/desert/README.md +51 -23
- package/starter/desert/bake-flora.mjs +142 -0
- package/starter/desert/bake-rocks.mjs +291 -0
- package/starter/desert/bake.mjs +14 -1
- package/starter/desert/biome.data.json +24 -12
- package/starter/desert/playerRig.ts +89 -0
- package/starter/desert/recipe.json +63 -16
- package/starter/desert/render/bakedFile.ts +17 -0
- package/starter/desert/render/benchmark.ts +79 -0
- package/starter/desert/render/biomeData.ts +14 -0
- package/starter/desert/render/biomes.ts +660 -0
- package/starter/desert/render/ground/cover.ts +565 -0
- package/starter/desert/render/ground/debris.ts +136 -0
- package/starter/desert/render/ground/horizon.ts +219 -0
- package/starter/desert/render/ground/palette.d.ts +1 -0
- package/starter/desert/render/ground/palette.js +11 -0
- package/starter/desert/render/ground/scatter.ts +104 -0
- package/starter/desert/render/ground/terrain.ts +1854 -0
- package/starter/desert/render/kitAssets.ts +28 -0
- package/starter/desert/render/loading.ts +354 -0
- package/starter/desert/render/lookKnobs.ts +37 -0
- package/starter/desert/render/orbitCamera.ts +155 -0
- package/starter/desert/render/props/farForest.ts +96 -0
- package/starter/desert/render/props/formations.ts +630 -0
- package/starter/desert/render/props/pack.ts +1195 -0
- package/starter/desert/render/props/prepared.ts +225 -0
- package/starter/desert/render/props/propMaterials.ts +1255 -0
- package/starter/desert/render/props/propProgress.ts +2 -0
- package/starter/desert/render/props/propWorld.ts +105 -0
- package/starter/desert/render/props/props.ts +624 -0
- package/starter/desert/render/props/spruce.ts +440 -0
- package/starter/desert/render/sand.ts +581 -0
- package/starter/desert/render/sky/atmosphere.ts +329 -0
- package/starter/desert/render/sky/sky.ts +382 -0
- package/starter/desert/render/texturePreparation.ts +135 -0
- package/starter/desert/render/water/river.ts +1132 -0
- package/starter/desert/render/worldNames.ts +6 -0
- package/starter/desert/scene.ts +748 -0
- package/starter/desert/sceneProps.ts +562 -0
- package/starter/desert/sceneState.ts +33 -0
- package/starter/desert/world/terrainAssets.ts +162 -0
- package/starter/desert/world/walkBand.ts +47 -0
- package/starter/desert/world/water.ts +74 -0
- package/starter/desert/worlds.ts +13 -0
- package/starter/forest/README.md +59 -116
- package/starter/forest/assets.json +14 -14
- package/starter/forest/bake.mjs +26 -4
- package/starter/forest/playerRig.ts +89 -0
- package/starter/forest/recipe.json +233 -43
- package/starter/forest/render/bakedFile.ts +17 -0
- package/starter/forest/render/benchmark.ts +77 -0
- package/starter/forest/render/biomeData.ts +14 -0
- package/starter/forest/render/biomes.ts +637 -0
- package/starter/forest/render/ground/cover.ts +565 -0
- package/starter/forest/render/ground/horizon.ts +219 -0
- package/starter/forest/render/ground/palette.d.ts +1 -0
- package/starter/forest/render/ground/palette.js +11 -0
- package/starter/forest/render/ground/scatter.ts +104 -0
- package/starter/forest/render/ground/terrain.ts +1796 -0
- package/starter/forest/render/kitAssets.ts +28 -0
- package/starter/forest/render/loading.ts +354 -0
- package/starter/forest/render/lookKnobs.ts +37 -0
- package/starter/forest/render/orbitCamera.ts +155 -0
- package/starter/forest/render/props/farForest.ts +94 -0
- package/starter/forest/render/props/pack.ts +1078 -0
- package/starter/forest/render/props/prepared.ts +226 -0
- package/starter/forest/render/props/propMaterials.ts +1247 -0
- package/starter/forest/render/props/propProgress.ts +2 -0
- package/starter/forest/render/props/propWorld.ts +105 -0
- package/starter/forest/render/props/props.ts +624 -0
- package/starter/forest/render/props/spruce.ts +440 -0
- package/starter/forest/render/sky/atmosphere.ts +329 -0
- package/starter/forest/render/sky/sky.ts +382 -0
- package/starter/forest/render/texturePreparation.ts +135 -0
- package/starter/forest/render/water/river.ts +1132 -0
- package/starter/forest/render/worldNames.ts +6 -0
- package/starter/forest/scene.ts +715 -0
- package/starter/forest/sceneProps.ts +559 -0
- package/starter/forest/sceneState.ts +33 -0
- package/starter/forest/surface.json +2 -2
- package/starter/forest/world/terrainAssets.ts +158 -0
- package/starter/forest/world/walkBand.ts +47 -0
- package/starter/forest/world/water.ts +74 -0
- package/starter/forest/worlds.ts +13 -0
- package/starter/tundra/README.md +35 -14
- package/starter/tundra/bake.mjs +14 -1
- package/starter/tundra/biome.data.json +6 -6
- package/starter/tundra/playerRig.ts +89 -0
- package/starter/tundra/recipe.json +111 -110
- package/starter/tundra/render/bakedFile.ts +17 -0
- package/starter/tundra/render/benchmark.ts +79 -0
- package/starter/tundra/render/biomeData.ts +14 -0
- package/starter/tundra/render/biomes.ts +714 -0
- package/starter/tundra/render/ground/cover.ts +565 -0
- package/starter/tundra/render/ground/horizon.ts +219 -0
- package/starter/tundra/render/ground/palette.d.ts +1 -0
- package/starter/tundra/render/ground/palette.js +11 -0
- package/starter/tundra/render/ground/scatter.ts +104 -0
- package/starter/tundra/render/ground/terrain.ts +1874 -0
- package/starter/tundra/render/kitAssets.ts +28 -0
- package/starter/tundra/render/loading.ts +354 -0
- package/starter/tundra/render/lookKnobs.ts +37 -0
- package/starter/tundra/render/orbitCamera.ts +155 -0
- package/starter/tundra/render/props/farForest.ts +96 -0
- package/starter/tundra/render/props/pack.ts +1088 -0
- package/starter/tundra/render/props/prepared.ts +225 -0
- package/starter/tundra/render/props/propMaterials.ts +1247 -0
- package/starter/tundra/render/props/propProgress.ts +2 -0
- package/starter/tundra/render/props/propWorld.ts +105 -0
- package/starter/tundra/render/props/props.ts +624 -0
- package/starter/tundra/render/props/spruce.ts +440 -0
- package/starter/tundra/render/sky/atmosphere.ts +329 -0
- package/starter/tundra/render/sky/sky.ts +382 -0
- package/starter/tundra/render/texturePreparation.ts +135 -0
- package/starter/tundra/render/water/river.ts +1132 -0
- package/starter/tundra/render/worldNames.ts +6 -0
- package/starter/tundra/scene.ts +715 -0
- package/starter/tundra/sceneProps.ts +559 -0
- package/starter/tundra/sceneState.ts +33 -0
- package/starter/tundra/world/terrainAssets.ts +159 -0
- package/starter/tundra/world/walkBand.ts +47 -0
- package/starter/tundra/world/water.ts +74 -0
- package/starter/tundra/worlds.ts +13 -0
- package/starter-assets.json +4 -4
- package/starter-credits.json +901 -15
- package/AGENT_GUIDE.md +0 -209
- package/starter/forest/sky.ts +0 -38
- package/starter/forest/water.ts +0 -257
- package/starter/forest/world.ts +0 -182
package/starter/forest/world.ts
DELETED
|
@@ -1,182 +0,0 @@
|
|
|
1
|
-
import type { ICtx } from "@threenative/core";
|
|
2
|
-
import {
|
|
3
|
-
type ILoadTerrainSplatOptions,
|
|
4
|
-
type ITerrainSplatMaterialContext,
|
|
5
|
-
type IWorldPackage,
|
|
6
|
-
loadTerrainSplat,
|
|
7
|
-
} from "@threenative/core/world";
|
|
8
|
-
import type { IPhysicsContext } from "@threenative/physics";
|
|
9
|
-
import {
|
|
10
|
-
type IStarterWorld,
|
|
11
|
-
type IStarterWorldSpec,
|
|
12
|
-
PropColliders as StarterPropColliders,
|
|
13
|
-
addStarterWorld,
|
|
14
|
-
} from "@threenative/terrain/starter";
|
|
15
|
-
import {
|
|
16
|
-
type Box3,
|
|
17
|
-
type BufferGeometry,
|
|
18
|
-
Color,
|
|
19
|
-
EquirectangularReflectionMapping,
|
|
20
|
-
type Mesh,
|
|
21
|
-
type MeshStandardMaterial,
|
|
22
|
-
type Object3D,
|
|
23
|
-
type Texture,
|
|
24
|
-
Vector3,
|
|
25
|
-
} from "three";
|
|
26
|
-
import { cameraViewMatrix, normalWorld, vec3, vec4 } from "three/tsl";
|
|
27
|
-
import { MeshPhysicalNodeMaterial } from "three/webgpu";
|
|
28
|
-
import surface from "./surface.json" with { type: "json" };
|
|
29
|
-
import { type IForestWater, type IForestWaterFile, addForestWater } from "./water.js";
|
|
30
|
-
/** Where the game's asset source holds the baked `world/` folder. */
|
|
31
|
-
export const FOREST_URL = "terrain/forest/world.json";
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* What stops the player, per placed asset, in metres at the placement's scale 1. `null` walks
|
|
35
|
-
* through. A capsule stands on the placement's origin. Change a row to change every copy.
|
|
36
|
-
*/
|
|
37
|
-
export const COLLIDERS: Readonly<
|
|
38
|
-
Record<
|
|
39
|
-
string,
|
|
40
|
-
{ capsule: readonly [halfHeight: number, radius: number] } | { sphere: number } | null
|
|
41
|
-
>
|
|
42
|
-
> = {
|
|
43
|
-
fir: { capsule: [3, 0.35] },
|
|
44
|
-
"fir-c": { capsule: [3, 0.35] },
|
|
45
|
-
boulder: { sphere: 0.9 },
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
/** How strongly the sky lights the props; the sun stays the dominant light on a crown. */
|
|
49
|
-
export const PROP_SKY_LIGHT = 0.65;
|
|
50
|
-
/** Multiplies every boulder's albedo: the CC0 scan photographed pink-orange, the forest's stone is cool grey. */
|
|
51
|
-
export const BOULDER_TINT = new Color(0.6, 0.74, 0.86);
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* The ground's dielectric specular, 0 to 1 (three's `specularIntensity`). The default 1 lays a pale
|
|
55
|
-
* sheen over soil and grass wherever the sun is low or ahead of the camera, so sunlit ground reads
|
|
56
|
-
* lighter than its albedo and the shaded floor loses its contrast. 0 leaves the photographed albedo.
|
|
57
|
-
*/
|
|
58
|
-
export const GROUND_SPECULAR = 0;
|
|
59
|
-
|
|
60
|
-
/** The splat's own shading in a physical material, which is what carries `specularIntensity`. */
|
|
61
|
-
function groundMaterial({
|
|
62
|
-
color,
|
|
63
|
-
normal,
|
|
64
|
-
orm,
|
|
65
|
-
}: ITerrainSplatMaterialContext): MeshPhysicalNodeMaterial {
|
|
66
|
-
const material = new MeshPhysicalNodeMaterial();
|
|
67
|
-
material.name = "terrain-splat";
|
|
68
|
-
material.colorNode = color;
|
|
69
|
-
// A top-down tangent frame, as the engine's own splat material builds it.
|
|
70
|
-
const n = normalWorld;
|
|
71
|
-
const tangent = vec3(1, 0, 0).sub(n.mul(n.x)).normalize();
|
|
72
|
-
const bitangent = n.cross(tangent).normalize();
|
|
73
|
-
const t = normal.mul(2).sub(1);
|
|
74
|
-
const perturbed = tangent.mul(t.x).add(bitangent.mul(t.y)).add(n.mul(t.z)).normalize();
|
|
75
|
-
material.normalNode = cameraViewMatrix.mul(vec4(perturbed, 0)).xyz.normalize();
|
|
76
|
-
material.aoNode = orm.r;
|
|
77
|
-
material.roughnessNode = orm.g;
|
|
78
|
-
material.metalnessNode = orm.b;
|
|
79
|
-
material.specularIntensity = GROUND_SPECULAR;
|
|
80
|
-
return material;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/** Metres around `follow` within which props collide; beyond it they only draw. */
|
|
84
|
-
export const PROP_COLLIDER_REACH = 60;
|
|
85
|
-
|
|
86
|
-
export interface IForestWorld extends IStarterWorld {
|
|
87
|
-
readonly water: IForestWater;
|
|
88
|
-
}
|
|
89
|
-
const spec: IStarterWorldSpec<IForestWater> = {
|
|
90
|
-
name: "Forest",
|
|
91
|
-
url: FOREST_URL,
|
|
92
|
-
colliders: COLLIDERS,
|
|
93
|
-
colliderReach: PROP_COLLIDER_REACH,
|
|
94
|
-
loadSurface: (ctx, url) =>
|
|
95
|
-
loadTerrainSplat({
|
|
96
|
-
assets: ctx.assets,
|
|
97
|
-
url,
|
|
98
|
-
table: surface as unknown as ILoadTerrainSplatOptions["table"],
|
|
99
|
-
materialFactory: groundMaterial,
|
|
100
|
-
}),
|
|
101
|
-
lighting: { sky: "sky.hdr", lods: true, dress: dressProp },
|
|
102
|
-
streaming: {
|
|
103
|
-
ring: 5,
|
|
104
|
-
impostors: true,
|
|
105
|
-
gpuScene: false,
|
|
106
|
-
// Every LOD casts: with only LOD0 (the default), each tree past 45 m dropped its shadow on the first refilter.
|
|
107
|
-
shadows: { cast: true, castLevels: 4, receive: true },
|
|
108
|
-
budgets: { residentCells: 64, instances: 40_000, bytes: 64_000_000 },
|
|
109
|
-
terrain: { streamRadius: 6 },
|
|
110
|
-
},
|
|
111
|
-
water: (ctx, file, field) => addForestWater(ctx, file as IForestWaterFile, field),
|
|
112
|
-
};
|
|
113
|
-
export class PropColliders extends StarterPropColliders {
|
|
114
|
-
constructor(
|
|
115
|
-
ctx: ICtx<Record<string, unknown>, IPhysicsContext>,
|
|
116
|
-
follow: Object3D,
|
|
117
|
-
manifest: IWorldPackage,
|
|
118
|
-
records: Float32Array,
|
|
119
|
-
) {
|
|
120
|
-
super(ctx, follow, manifest, records, COLLIDERS, PROP_COLLIDER_REACH, "Forest");
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
export async function addForest(
|
|
124
|
-
ctx: ICtx<Record<string, unknown>, IPhysicsContext>,
|
|
125
|
-
follow: Object3D,
|
|
126
|
-
url = FOREST_URL,
|
|
127
|
-
whileCurrent: () => boolean = () => true,
|
|
128
|
-
): Promise<IForestWorld> {
|
|
129
|
-
const result = await addStarterWorld(ctx, follow, spec, url, whileCurrent);
|
|
130
|
-
if (!result.extra) throw new Error("Forest world: missing water.");
|
|
131
|
-
return { ...result, water: result.extra };
|
|
132
|
-
}
|
|
133
|
-
/** One prop mesh: the sky as its envMap, and matte, crown-shaded foliage. */
|
|
134
|
-
function dressProp(
|
|
135
|
-
mesh: Mesh,
|
|
136
|
-
sky: Texture,
|
|
137
|
-
id: string,
|
|
138
|
-
originals: ReadonlyMap<MeshStandardMaterial, { color: Color }>,
|
|
139
|
-
): void {
|
|
140
|
-
sky.mapping = EquirectangularReflectionMapping;
|
|
141
|
-
const tint = id.startsWith("boulder") ? BOULDER_TINT : undefined;
|
|
142
|
-
const surfaces = [mesh.material ?? []].flat() as MeshStandardMaterial[];
|
|
143
|
-
const foliage = surfaces.some((material) => material.alphaTest > 0);
|
|
144
|
-
for (const material of surfaces) {
|
|
145
|
-
material.envMap = sky;
|
|
146
|
-
material.envMapIntensity = foliage ? FOLIAGE_SKY_LIGHT : PROP_SKY_LIGHT;
|
|
147
|
-
const original = originals.get(material);
|
|
148
|
-
if (tint !== undefined && original !== undefined)
|
|
149
|
-
material.color.copy(original.color).multiply(tint);
|
|
150
|
-
if (material.alphaTest <= 0) continue;
|
|
151
|
-
material.roughness = 0.85;
|
|
152
|
-
(material as MeshStandardMaterial & { specularIntensity?: number }).specularIntensity = 0.15;
|
|
153
|
-
}
|
|
154
|
-
if (foliage && mesh.isMesh) bendCrownNormals(mesh.geometry, CROWN_NORMAL_BEND);
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
/** How strongly the sky lights foliage, which the sun alone leaves near-black from most sides. */
|
|
158
|
-
export const FOLIAGE_SKY_LIGHT = 1;
|
|
159
|
-
/** 0 keeps each card's own normal, 1 points every normal straight out from the crown's axis. */
|
|
160
|
-
export const CROWN_NORMAL_BEND = 0.7;
|
|
161
|
-
|
|
162
|
-
export function bendCrownNormals(geometry: BufferGeometry, bend: number): void {
|
|
163
|
-
const position = geometry.getAttribute("position");
|
|
164
|
-
const normal = geometry.getAttribute("normal");
|
|
165
|
-
if (position === undefined || normal === undefined) return;
|
|
166
|
-
// One cached model hands the same geometry to every visit; a second bend would lerp the bent
|
|
167
|
-
// normals again and the shading would drift. The original normals are gone after the first pass,
|
|
168
|
-
// so the geometry records the bend it already took.
|
|
169
|
-
if (geometry.userData.crownBend !== undefined) return;
|
|
170
|
-
geometry.userData.crownBend = bend;
|
|
171
|
-
geometry.computeBoundingBox();
|
|
172
|
-
const box = geometry.boundingBox as Box3;
|
|
173
|
-
const midY = (box.min.y + box.max.y) / 2;
|
|
174
|
-
const out = new Vector3();
|
|
175
|
-
const own = new Vector3();
|
|
176
|
-
for (let i = 0; i < position.count; i++) {
|
|
177
|
-
out.set(position.getX(i), (position.getY(i) - midY) * 0.5, position.getZ(i)).normalize();
|
|
178
|
-
own.fromBufferAttribute(normal, i).lerp(out, bend).normalize();
|
|
179
|
-
normal.setXYZ(i, own.x, own.y, own.z);
|
|
180
|
-
}
|
|
181
|
-
normal.needsUpdate = true;
|
|
182
|
-
}
|