Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

feat(worldgen): a plant part may cast no shadow, and far cells are capped at two cells wide

yhh57b931943546
diff --git a/docs/astro/src/content/docs/world/generation.mdx b/docs/astro/src/content/docs/world/generation.mdx
index 945616653..ba1905426 100644
--- a/docs/astro/src/content/docs/world/generation.mdx
+++ b/docs/astro/src/content/docs/world/generation.mdx
@@ -94,7 +94,7 @@ group stands.
### Plants
-A member names a mesh and a material, or a **plant**: an entry in the rules' `plants`, for what a mesh and a material cannot say. A plant is its `parts` (a trunk and a crown are two, each with its `mesh`, a cheaper `lod1Mesh`, `material`, `texture`, `tint`, whether it is `opaque` and, for a card that stands for a crown, how `soft` its normals are: turned toward up by that share, so it is lit as a rounded crown and not as the flat board it is), the `lodDistance` past which the cheaper meshes stand in and the `drawDistance` past which it is not drawn, a `far` version (a tree's crossed cards, baked to an atlas of their own) drawn from `drawDistance` out to `farDistance`, and optionally:
+A member names a mesh and a material, or a **plant**: an entry in the rules' `plants`, for what a mesh and a material cannot say. A plant is its `parts` (a trunk and a crown are two, each with its `mesh`, a cheaper `lod1Mesh`, `material`, `texture`, `tint`, whether it is `opaque` and, for a card that stands for a crown, how `soft` its normals are: turned toward up by that share, so it is lit as a rounded crown and not as the flat board it is, and whether it `castShadows`: `false` for the ground cover too small to be worth a shadow-map draw), the `lodDistance` past which the cheaper meshes stand in and the `drawDistance` past which it is not drawn, a `far` version (a tree's crossed cards, baked to an atlas of their own) drawn from `drawDistance` out to `farDistance`, and optionally:
| Field | What it is |
|---|---|
diff --git a/docs/astro/src/content/docs/zh-cn/world/generation.mdx b/docs/astro/src/content/docs/zh-cn/world/generation.mdx
index 0f995cb68..bdcd124c4 100644
--- a/docs/astro/src/content/docs/zh-cn/world/generation.mdx
+++ b/docs/astro/src/content/docs/zh-cn/world/generation.mdx
@@ -71,7 +71,7 @@ registerWorldRules({
### 植物
-成员可以是网格加材质,也可以是**植物**:规则 `plants` 里的一项,用来表达网格和材质表达不了的东西。植物由 `parts` 部件组成(树干和树冠是两个部件,各有 `mesh`、更省的 `lod1Mesh`、`material`、`texture`、`tint`、是否 `opaque`,以及对代表树冠的卡片用 `soft` 说明它的法线朝上偏转多少:按这个比例转向上方,让它像圆润的树冠那样受光,而不是像一块平板),超过 `lodDistance` 换成更省的网格,超过 `drawDistance` 不再绘制,`far` 远景版本(树的交叉卡片,烘到自己的图集上)从 `drawDistance` 一直画到 `farDistance`,另外可选:
+成员可以是网格加材质,也可以是**植物**:规则 `plants` 里的一项,用来表达网格和材质表达不了的东西。植物由 `parts` 部件组成(树干和树冠是两个部件,各有 `mesh`、更省的 `lod1Mesh`、`material`、`texture`、`tint`、是否 `opaque`,以及对代表树冠的卡片用 `soft` 说明它的法线朝上偏转多少:按这个比例转向上方,让它像圆润的树冠那样受光,而不是像一块平板;`castShadows` 为 `false` 表示不投影,给小到不值得进阴影图的地表覆盖物用),超过 `lodDistance` 换成更省的网格,超过 `drawDistance` 不再绘制,`far` 远景版本(树的交叉卡片,烘到自己的图集上)从 `drawDistance` 一直画到 `farDistance`,另外可选:
| 字段 | 含义 |
|---|---|
diff --git a/sdk/src/foliage/plantGrowth.ts b/sdk/src/foliage/plantGrowth.ts
index 3d43d1a3e..0a5406c73 100644
--- a/sdk/src/foliage/plantGrowth.ts
+++ b/sdk/src/foliage/plantGrowth.ts
@@ -122,7 +122,7 @@ export class PlantGrowth {
world.insert(e, RuntimeOnly);
world.insert(e, Transform, {});
world.insert(e, MeshRenderer, {
- mesh: made(near), material: r.material, texture: r.texture, lit: true, opaque: r.part.opaque !== false, cullBackfaces: false,
+ mesh: made(near), material: r.material, texture: r.texture, lit: true, opaque: r.part.opaque !== false, cullBackfaces: false, castShadows: r.part.castShadows !== false,
color: { r: r.part.tint[0], g: r.part.tint[1], b: r.part.tint[2], a: r.part.tint[3] },
} as Partial<MeshRendererData>);
if (lodDef) {
@@ -170,7 +170,7 @@ export class PlantGrowth {
world.insert(e, RuntimeOnly);
world.insert(e, Transform, {});
world.insert(e, MeshRenderer, {
- mesh: handle, material: r.material, texture: r.texture, lit: true, opaque: r.part.opaque !== false, cullBackfaces: false, enabled: false,
+ mesh: handle, material: r.material, texture: r.texture, lit: true, opaque: r.part.opaque !== false, cullBackfaces: false, castShadows: r.part.castShadows !== false, enabled: false,
color: { r: r.part.tint[0], g: r.part.tint[1], b: r.part.tint[2], a: r.part.tint[3] },
} as Partial<MeshRendererData>);
world.setParent(e, entity);
diff --git a/sdk/src/foliage/plantKind.ts b/sdk/src/foliage/plantKind.ts
index cbbc519de..9dd8a685b 100644
--- a/sdk/src/foliage/plantKind.ts
+++ b/sdk/src/foliage/plantKind.ts
@@ -22,6 +22,8 @@ export interface PlacedPart {
* on the side the sun is on and a dark slab on the other. A crown is lit as a rounded thing, and facing up is how a card is.
*/
soft?: number;
+ /** False for a part that casts no shadow (default true): the ground cover that is a hand's width tall, which is drawn into the shadow map for nothing. */
+ castShadows?: boolean;
}
/** A kind of plant: its parts, and how far it is drawn. */
diff --git a/sdk/src/worldgen/rulesAsset.ts b/sdk/src/worldgen/rulesAsset.ts
index d540372a1..71b93d0ae 100644
--- a/sdk/src/worldgen/rulesAsset.ts
+++ b/sdk/src/worldgen/rulesAsset.ts
@@ -67,6 +67,7 @@ function part(v: unknown, where: string): PlantKind['parts'][number] {
texture: typeof p.texture === 'string' ? p.texture : '', tint: (tint as unknown[]).map((c, i) => num(c, `${where}.tint[${i}]`)) as [number, number, number, number],
opaque: p.opaque !== false,
...(p.soft === undefined ? {} : { soft: Math.min(Math.max(num(p.soft, `${where}.soft`), 0), 1) }),
+ ...(p.castShadows === false ? { castShadows: false } : {}),
};
}
diff --git a/sdk/src/worldgen/worldgenPlugin.ts b/sdk/src/worldgen/worldgenPlugin.ts
index 0389bbaa7..500e0b714 100644
--- a/sdk/src/worldgen/worldgenPlugin.ts
+++ b/sdk/src/worldgen/worldgenPlugin.ts
@@ -521,7 +521,7 @@ export class WorldGenPlugin implements Plugin {
if (!rendering || !built.plants || built.plants.reach <= layer.drawDistance) { chunks.remove(farId); continue; }
// A far cell is as many chunks wide as the shortest span a far version is shown over allows: the far versions are one draw a kind a cell, and a ring
// of ground cover a few chunks wide would otherwise be thousands of them.
- const farSide = Math.min(Math.max(Math.floor(built.plants.span / size), 1), 8);
+ const farSide = Math.min(Math.max(Math.floor(built.plants.span / size), 1), 2);
const fsize = size * farSide;
const fextent: [number, number, number, number] | null = bounds
? [Math.floor(bounds[0] / fsize), Math.floor(bounds[1] / fsize), Math.ceil(bounds[2] / fsize), Math.ceil(bounds[3] / fsize)] : null;
diff --git a/sdk/tests/worldgen-rules-asset.test.ts b/sdk/tests/worldgen-rules-asset.test.ts
index 1778857c7..08da71221 100644
--- a/sdk/tests/worldgen-rules-asset.test.ts
+++ b/sdk/tests/worldgen-rules-asset.test.ts
@@ -44,7 +44,7 @@ describe('plants in a world rules file', () => {
const r = JSON.parse(JSON.stringify(good));
r.plants = {
oak: { parts: [{ mesh: 'assets/oak_trunk.esmesh', material: 'assets/bark.esmaterial' }, { mesh: 'assets/oak_crown.esmesh', lod1Mesh: 'assets/oak_crown1.esmesh', opaque: true }],
- lodDistance: 6000, drawDistance: 16000, far: [{ mesh: 'assets/oak_cards.esmesh', soft: 0.85 }], farDistance: 200000, farKeep: 0.25, tints: [[0.9, 1, 0.9], [1, 1, 1]], sink: 8 },
+ lodDistance: 6000, drawDistance: 16000, far: [{ mesh: 'assets/oak_cards.esmesh', soft: 0.85, castShadows: false }], farDistance: 200000, farKeep: 0.25, tints: [[0.9, 1, 0.9], [1, 1, 1]], sink: 8 },
};
r.recipes[0].anchor = { id: 'oak', plant: 'oak', footprint: 8 };
delete r.recipes[0].anchor.mesh;
@@ -60,6 +60,8 @@ describe('plants in a world rules file', () => {
expect(oak.parts[1]!.lod1Mesh).toBe('assets/oak_crown1.esmesh');
expect(oak.far[0]!.mesh).toBe('assets/oak_cards.esmesh');
expect(oak.far[0]!.soft).toBe(0.85);
+ expect(oak.far[0]!.castShadows).toBe(false);
+ expect(oak.parts[0]!.castShadows).toBeUndefined();
expect(oak.parts[0]!.soft).toBeUndefined();
expect(oak.tints).toHaveLength(2);
expect(oak.sink).toBe(8);