@threedog/terrain-noise 0.0.0-stage → 0.1.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 penstic
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/index.d.ts ADDED
@@ -0,0 +1,55 @@
1
+ // Types of @threedog/terrain-noise for code that imports it from the published package
2
+ // (TypeScript does not read JS inside node_modules, so without this the import would be untyped).
3
+ import type { TerrainNoise } from 'threedog';
4
+
5
+ /** Biome condition: all optional; any: [cond, cond] matches if either holds */
6
+ export interface BiomeCondition {
7
+ minHeight?: number; maxHeight?: number;
8
+ /** Max height above the water surface */
9
+ maxAboveWater?: number;
10
+ minSlope?: number;
11
+ /** Temperature 0–1 */
12
+ minTemp?: number; maxTemp?: number;
13
+ /** Moisture 0–1 */
14
+ minMoist?: number; maxMoist?: number;
15
+ /** Closeness to a river 0–1 */
16
+ minRiver?: number;
17
+ }
18
+ /** Biome rule: checked top to bottom, the first match names the ground type (leave the last without conditions as the default) */
19
+ export interface BiomeRule extends BiomeCondition { name: string; any?: BiomeCondition[] }
20
+ /** Settings of terrainPreset() (n.params inside the generator) */
21
+ export interface TerrainPresetOptions {
22
+ /** Landform, default 'hills' */
23
+ shape?: 'hills' | 'island' | 'canyon' | 'flat';
24
+ /** Overall relief multiplier */
25
+ heightScale?: number;
26
+ /** Mountain strength */
27
+ mountains?: number;
28
+ /** Carve rivers, default true */
29
+ rivers?: boolean;
30
+ /** River width (0.02 narrow, 0.05 wide) */
31
+ riverWidth?: number;
32
+ /** Biome rule table (default BIOMES) */
33
+ biomes?: BiomeRule[];
34
+ tempScale?: number; moistScale?: number;
35
+ /** Temperature drop per meter of altitude */
36
+ tempLapse?: number;
37
+ noise?: { baseScale?: number; baseAmp?: number; hillScale?: number; hillAmp?: number; mountainScale?: number; mountainAmp?: number; mountainThreshold?: number; seaBias?: number };
38
+ island?: { center?: number[]; radius?: number; falloff?: number; depth?: number };
39
+ canyon?: { scale?: number; depth?: number; plateau?: number };
40
+ }
41
+ /** The default biome rule table: beach, snow, rock, riverbank, wetland, savanna, forest, highland, meadow */
42
+ export const BIOMES: BiomeRule[];
43
+ /**
44
+ * Noise terrain generator for terrain() / openWorld({ terrain }) / createTerrain(): noise hills (or island, canyon, plain), rivers, biomes.
45
+ * @example engine.use(openWorld({ seed: 7, camera: 'follow', terrain: terrainPreset({ shape: 'island' }) }))
46
+ */
47
+ export function terrainPreset(opts?: TerrainPresetOptions): {
48
+ height: (x: number, z: number, n: TerrainNoise) => number;
49
+ groundType: (x: number, z: number, h: number, slope: number, n: TerrainNoise) => string;
50
+ params: Record<string, any>;
51
+ };
52
+ /** Display names of the preset's ground types ('ground.<type>' keys in zh-CN and en): engine.i18n.add(terrainStrings) */
53
+ export const terrainStrings: Record<string, Record<string, string>>;
54
+ /** Closeness to a river (0 away, up to 1 in the middle), e.g. riverAt(x, z, createTerrain({ seed, ...terrainPreset() }).noise) */
55
+ export function riverAt(x: number, z: number, n: { fbm: TerrainNoise['fbm']; params: Record<string, any> }): number;
package/index.js ADDED
@@ -0,0 +1,119 @@
1
+ // @threedog/terrain-noise: an optional terrain generator. Noise hills (or island / canyon / gentle plain), rivers, and a biome rule table
2
+ // (beach, snow, rock, riverbank, wetland, savanna, forest, highland, meadow). The engine has no built-in generator: terrain() alone is flat
3
+ // ground; a game passes this preset (or its own height / groundType functions) explicitly:
4
+ // import { terrainPreset } from '@threedog/terrain-noise';
5
+ // engine.use(openWorld({ seed: 7, camera: 'follow', terrain: terrainPreset({ shape: 'island' }) }));
6
+ // Why its own package and not part of a style pack: the shape of the world (where hills, rivers and forests are) is gameplay, and a style
7
+ // pack holds looks only (packages/engine/docs/recipes/09-style-pack.md). Swapping the look must never move a river. The colors of these ground types belong
8
+ // to whichever style pack the game uses (lowpoly: style.terrain.biomeColors, keyed by the names below).
9
+ // height and groundType run in terrain workers, so they are pure: they read only their parameters (n.params is the options below) and Math.
10
+ // The ground type names are plain strings that vegetation rules, style colors and a game's footstep map match by name.
11
+
12
+ // Biome rules: checked top to bottom, the first match is the biome there (the last has no condition and is the default).
13
+ // Conditions (all optional): minHeight/maxHeight altitude, maxAboveWater height above water, minSlope slope, minTemp/maxTemp temperature 0-1,
14
+ // minMoist/maxMoist moisture 0-1, minRiver closeness to a river (0-1); any: [cond, cond] matches if either holds.
15
+ export const BIOMES = [
16
+ { name: 'beach', maxAboveWater: 1.3 },
17
+ { name: 'snow', any: [{ minHeight: 46 }, { maxTemp: 0.2 }] },
18
+ { name: 'rock', any: [{ minSlope: 0.8 }, { minHeight: 34 }] },
19
+ { name: 'riverbank', minRiver: 0.05 },
20
+ { name: 'wetland', minMoist: 0.62, maxHeight: 6 },
21
+ { name: 'savanna', maxMoist: 0.4, minTemp: 0.5 },
22
+ { name: 'forest', minMoist: 0.5 },
23
+ { name: 'highland', minHeight: 16 },
24
+ { name: 'meadow' },
25
+ ];
26
+
27
+ /** Display names of the preset's ground types (describe() groundName): createEngine({ strings: terrainStrings }) or engine.i18n.add(terrainStrings) */
28
+ export const terrainStrings = {
29
+ 'zh-CN': { 'ground.meadow': '草地', 'ground.forest': '森林', 'ground.beach': '沙滩', 'ground.rock': '岩石', 'ground.snow': '雪地',
30
+ 'ground.wetland': '湿地', 'ground.riverbank': '河岸', 'ground.savanna': '草原', 'ground.highland': '高地' },
31
+ en: { 'ground.meadow': 'meadow', 'ground.forest': 'forest', 'ground.beach': 'beach', 'ground.rock': 'rock', 'ground.snow': 'snow',
32
+ 'ground.wetland': 'wetland', 'ground.riverbank': 'riverbank', 'ground.savanna': 'savanna', 'ground.highland': 'highland' },
33
+ };
34
+
35
+ const DEFAULTS = {
36
+ shape: 'hills', // 'hills' | 'island' | 'canyon' (mesas) | 'flat' (gentle plain)
37
+ heightScale: 1, // overall relief multiplier
38
+ mountains: 1, // mountain strength
39
+ rivers: true, // carve rivers
40
+ riverWidth: 0.03, // river width (noise threshold, 0.02 narrow, 0.05 wide)
41
+ biomes: BIOMES,
42
+ tempScale: 0.0011, moistScale: 0.0014, tempLapse: 0.006, // temperature / moisture noise scale; temperature drop per meter of altitude
43
+ noise: { baseScale: 0.0035, baseAmp: 46, hillScale: 0.016, hillAmp: 10, mountainScale: 0.0018, mountainAmp: 900, mountainThreshold: 0.52, seaBias: 0.42 },
44
+ island: { center: [0, 0], radius: 700, falloff: 350, depth: 30 },
45
+ canyon: { scale: 0.004, depth: 40, plateau: 14 },
46
+ };
47
+
48
+ /** Height in meters (pure: runs in workers) */
49
+ function height(x, z, n) {
50
+ const P = n.params, N = P.noise, fbm = n.fbm, smooth = t => t * t * (3 - 2 * t);
51
+ const hills = () => {
52
+ const base = fbm(x * N.baseScale, z * N.baseScale, 4);
53
+ const hl = fbm(x * N.hillScale + 71, z * N.hillScale - 33, 3);
54
+ const m = Math.max(0, fbm(x * N.mountainScale + 500, z * N.mountainScale + 200, 4) - N.mountainThreshold);
55
+ return ((base - N.seaBias) * N.baseAmp + (hl - 0.5) * N.hillAmp) * P.heightScale + m * m * N.mountainAmp * P.mountains;
56
+ };
57
+ let h;
58
+ if (P.shape === 'flat') h = (fbm(x * 0.01, z * 0.01, 2) - 0.5) * 2 * P.heightScale + 4;
59
+ else if (P.shape === 'island') {
60
+ const I = P.island, d = Math.hypot(x - I.center[0], z - I.center[1]), t = Math.min(1, Math.max(0, (d - I.radius) / I.falloff));
61
+ h = hills() * (1 - t) + 6 * (1 - t) - I.depth * smooth(t);
62
+ } else if (P.shape === 'canyon') {
63
+ const C = P.canyon, r = Math.abs(fbm(x * C.scale + 13, z * C.scale - 7, 4) - 0.5) * 2;
64
+ const terrace = Math.floor(fbm(x * 0.01, z * 0.01, 2) * 4) / 4;
65
+ h = C.plateau + terrace * 8 - C.depth * Math.pow(1 - Math.min(1, r * 3), 2);
66
+ } else h = hills();
67
+ // rivers: thin bands where noise is near 0.5 are dug below the water surface
68
+ if (P.rivers) {
69
+ const r = Math.abs(fbm(x * 0.0022 + 40, z * 0.0022 - 70, 3) - 0.5), w = P.riverWidth, bank = w * 2.2;
70
+ if (r < bank) h = h + (n.waterLevel - 1.6 - h) * Math.min(1, smooth(1 - r / bank) * (r < w ? 1 : 0.85) * 1.25);
71
+ }
72
+ return h;
73
+ }
74
+
75
+ /** Ground type from the biome rule table (pure: runs in workers) */
76
+ function groundType(x, z, h, sl, n) {
77
+ const P = n.params, fbm = n.fbm, W = n.waterLevel;
78
+ let tp, ms, rv; // computed on demand; skipped if no rule uses them
79
+ const temp = () => tp ??= fbm(x * P.tempScale + 300, z * P.tempScale - 90, 3) - h * P.tempLapse;
80
+ const moist = () => ms ??= fbm(x * P.moistScale - 200, z * P.moistScale + 100, 3);
81
+ const river = () => rv ??= (() => {
82
+ if (!P.rivers) return 0;
83
+ const r = Math.abs(fbm(x * 0.0022 + 40, z * 0.0022 - 70, 3) - 0.5), w = P.riverWidth, bank = w * 2.2, t = 1 - r / bank;
84
+ return r >= bank ? 0 : t * t * (3 - 2 * t) * (r < w ? 1 : 0.85);
85
+ })();
86
+ const test = c => c.any ? c.any.some(test) : !((c.minHeight != null && h < c.minHeight) || (c.maxHeight != null && h > c.maxHeight) ||
87
+ (c.maxAboveWater != null && h >= W + c.maxAboveWater) || (c.minSlope != null && sl < c.minSlope) ||
88
+ (c.minTemp != null && temp() < c.minTemp) || (c.maxTemp != null && temp() > c.maxTemp) ||
89
+ (c.minMoist != null && moist() < c.minMoist) || (c.maxMoist != null && moist() > c.maxMoist) ||
90
+ (c.minRiver != null && river() < c.minRiver));
91
+ for (const rule of P.biomes) if (test(rule)) return rule.name;
92
+ return P.biomes[P.biomes.length - 1].name;
93
+ }
94
+
95
+ /**
96
+ * Closeness to a river at (x, z): 0 away from rivers, up to 1 in the middle of one. Pure; pass the noise helpers of a terrain built with
97
+ * the preset, e.g. riverAt(x, z, createTerrain({ seed, ...terrainPreset() }).noise), to pick a dry spot before the engine starts.
98
+ * @param {number} x
99
+ * @param {number} z
100
+ * @param {{ fbm: (x: number, z: number, octaves?: number) => number, params: any }} n
101
+ */
102
+ export function riverAt(x, z, n) {
103
+ const P = n.params;
104
+ if (!P?.rivers) return 0;
105
+ const r = Math.abs(n.fbm(x * 0.0022 + 40, z * 0.0022 - 70, 3) - 0.5), w = P.riverWidth, bank = w * 2.2, t = 1 - r / bank;
106
+ return r >= bank ? 0 : t * t * (3 - 2 * t) * (r < w ? 1 : 0.85);
107
+ }
108
+
109
+ /**
110
+ * Noise terrain preset: spread it into terrain() (or openWorld({ terrain })). Options override the defaults above; noise, island and
111
+ * canyon merge field by field.
112
+ * @example engine.use(terrain({ seed: 7, ...terrainPreset({ shape: 'island', rivers: false }) }))
113
+ * @param {Partial<typeof DEFAULTS>} [opts]
114
+ */
115
+ export function terrainPreset(opts = {}) {
116
+ const params = { ...DEFAULTS, ...opts };
117
+ for (const k of ['noise', 'island', 'canyon']) params[k] = { ...DEFAULTS[k], ...(opts[k] || {}) };
118
+ return { waterLevel: 0, height, groundType, params }; // this generator's rivers and coasts are carved toward sea level 0
119
+ }
package/package.json CHANGED
@@ -1,6 +1,29 @@
1
1
  {
2
2
  "name": "@threedog/terrain-noise",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "Optional terrain generator for threedog: noise hills, island, canyon or plain shapes, rivers and a biome rule table (world shape only, no colors)",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/penstic123-code/ThreeDog.js-Game-Engine.git",
9
+ "directory": "packages/terrain-noise"
10
+ },
11
+ "homepage": "https://github.com/penstic123-code/ThreeDog.js-Game-Engine#readme",
12
+ "type": "module",
13
+ "main": "./index.js",
14
+ "types": "./index.d.ts",
15
+ "exports": {
16
+ ".": {
17
+ "types": "./index.d.ts",
18
+ "default": "./index.js"
19
+ },
20
+ "./package.json": "./package.json"
21
+ },
22
+ "files": [
23
+ "*.js",
24
+ "*.d.ts"
25
+ ],
26
+ "peerDependencies": {
27
+ "threedog": "^0.1.0"
28
+ }
29
+ }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.