Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Load the world material shader in the nature world

Nguyen Xuan An635d49c54dac
diff --git a/changes/449.md b/changes/449.md
new file mode 100644
index 00000000..6dcdb414
--- /dev/null
+++ b/changes/449.md
@@ -0,0 +1 @@
+- The nature world draws again: `src/nature.js` loads the world material shader that water, foliage and terrain need (#449).
diff --git a/changes/449.size.md b/changes/449.size.md
new file mode 100644
index 00000000..9f03dbef
--- /dev/null
+++ b/changes/449.size.md
@@ -0,0 +1 @@
+- entry:nature: the nature world uses water, foliage and terrain shading, which moved to the world-material-shader addon in #281; without it the page and the addon fail to draw, so the addon must carry it like vegetation, grass and clutter do.
diff --git a/src/nature.js b/src/nature.js
index 14e76850..bc9269f5 100644
--- a/src/nature.js
+++ b/src/nature.js
@@ -1,5 +1,6 @@
import { mat4, quat } from 'gl-matrix';
import { box, cylinder, sphere } from './geometry.js';
+import './world-material-shader.js';
const FULL_TURN = Math.PI * 2;
const DETAIL_PRESETS = {
diff --git a/tests/world-addon-shader-imports.test.js b/tests/world-addon-shader-imports.test.js
new file mode 100644
index 00000000..86625987
--- /dev/null
+++ b/tests/world-addon-shader-imports.test.js
@@ -0,0 +1,49 @@
+import { test } from 'node:test';
+import assert from 'node:assert/strict';
+import { execFileSync } from 'node:child_process';
+
+// Since the world materials moved out of the core bundle (#281), each addon that builds tagged world parts must load
+// their shader code itself. Each case runs in a fresh process that imports only that addon, so an import another test
+// happened to load cannot hide a missing one (#449).
+const WORLD_ADDONS = [
+ { module: 'nature.js', build: 'createNatureWorld()' },
+ { module: 'terrain.js', build: 'createTerrain({ size: 16, resolution: 4, chunks: 1, levels: 1, material: { tag: "terrain", color: [1, 1, 1], metallic: 0, roughness: 1, emission: 0 } })' },
+ { module: 'grass.js', build: 'createGrass({ area: { minX: -2, maxX: 2, minZ: -2, maxZ: 2 }, density: 4 })' },
+ { module: 'vegetation.js', build: 'createTree("broadleaf", 1)' },
+];
+
+function childScript({ module, build }) {
+ const name = build.slice(0, build.indexOf('('));
+ return `
+ import { ${name} } from './src/${module}';
+ import { createFragmentShader, createVertexShader } from './src/shaders.js';
+ const built = ${build};
+ const tags = new Set();
+ const seen = new Set();
+ function collect(value) {
+ if (!value || typeof value !== 'object' || seen.has(value) || ArrayBuffer.isView(value)) return;
+ seen.add(value);
+ if (typeof value.tag === 'string') tags.add(value.tag);
+ for (const child of Object.values(value)) collect(child);
+ }
+ collect(built);
+ // The tags renderer.js (TAG_SHADER_FEATURES) turns into shader features; other tags such as rock draw with stock code.
+ const tagFeatures = new Set(['floor', 'water', 'foliage', 'terrain', 'grass', 'fire', 'smoke', 'unlit']);
+ const features = [...tags].filter(tag => tagFeatures.has(tag));
+ if (features.length === 0) throw new Error('The world has no tagged parts, so this case checks nothing.');
+ createVertexShader(features);
+ createFragmentShader(features);
+ console.log(JSON.stringify(features));
+ `;
+}
+
+for (const addon of WORLD_ADDONS) {
+ test(`${addon.module} alone loads the shader code its world parts need`, () => {
+ const output = execFileSync(process.execPath, ['--input-type=module', '--eval', childScript(addon)], {
+ cwd: new URL('..', import.meta.url),
+ encoding: 'utf8',
+ stdio: ['ignore', 'pipe', 'pipe'],
+ });
+ assert.ok(Array.isArray(JSON.parse(output.trim().split('\n').at(-1))));
+ });
+}