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.

fix(terrain): a planet away from the origin is drawn where it stands and culled by the box it is drawn in

yhhd27059f919c5
diff --git a/sdk/src/terrain/planetShader.ts b/sdk/src/terrain/planetShader.ts
index 546ef8e78..ebb390dca 100644
--- a/sdk/src/terrain/planetShader.ts
+++ b/sdk/src/terrain/planetShader.ts
@@ -106,7 +106,8 @@ void esMaterialVertex(inout vec3 local, inout vec3 localNormal, in vec2 uv) {
float h = pnPageHeight(texel);
vec3 dir = pnDirection(f, u0 + q.x * span, v0 + q.y * span);
vec3 P = dir * (u_planetRadius + h);
- local = (P - boxMin) / boxSize;
+ // The model is the node's box in the world, which includes where the planet stands: the point is the planet's, so its centre is added.
+ local = (u_planetCenter.xyz + P - boxMin) / boxSize;
vec2 nxz = textureLod(u_heightPages, texel / u_atlasTexels, 0.0).ba * 2.0 - 1.0;
vec3 nl = vec3(nxz.x, sqrt(max(1.0 - dot(nxz, nxz), 0.0)), nxz.y);
vec3 n = nl.x * pnEu(f, dir) + nl.y * dir + nl.z * pnEv(f, dir);
@@ -180,7 +181,7 @@ fn esMaterialVertex(local : ptr<function, vec3f>, localNormal : ptr<function, ve
let h = pnPageHeight(texel);
let dir = pnDirection(f, u0 + q.x * span, v0 + q.y * span);
let P = dir * (mc.u_planetRadius + h);
- *local = (P - boxMin) / boxSize;
+ *local = (mc.u_planetCenter.xyz + P - boxMin) / boxSize;
let nxz = textureSampleLevel(u_heightPages, u_heightPages_s, texel / mc.u_atlasTexels, 0.0).ba * 2.0 - vec2f(1.0);
let nl = vec3f(nxz.x, sqrt(max(1.0 - dot(nxz, nxz), 0.0)), nxz.y);
let n = nl.x * pnEu(f, dir) + nl.y * dir + nl.z * pnEv(f, dir);
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index 0608ac15f..6af19bb09 100644
--- a/tools/releaseNotes.mjs
+++ b/tools/releaseNotes.mjs
@@ -2918,6 +2918,8 @@ export const NOTED = {
{"internal":"The first layer of spherical terrain: pure maths and node selection, not yet drawn."},
"feat(terrain): a Planet component draws ground on a sphere from a provider that answers by direction":
{"note":"A `Planet`: ground on a sphere."},
+ "fix(terrain): a planet away from the origin is drawn where it stands and culled by the box it is drawn in":
+ {"internal":"A fix to the Planet shader of this release."},
"feat(assets): a project names folders its asset database leaves alone":
{"note":"A project can keep folders out of its asset database."},
"feat(worldgen): a plant part may cast no shadow, and far cells are capped at two cells wide":