webgpu-grass 0.0.0-stage → 0.1.1
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 +21 -0
- package/README.md +645 -2
- package/lib/distance.d.ts +63 -0
- package/lib/distance.d.ts.map +1 -0
- package/lib/distance.js +148 -0
- package/lib/distance.js.map +1 -0
- package/lib/engine.d.ts +402 -0
- package/lib/engine.d.ts.map +1 -0
- package/lib/engine.js +3253 -0
- package/lib/engine.js.map +1 -0
- package/lib/field.d.ts +133 -0
- package/lib/field.d.ts.map +1 -0
- package/lib/field.js +244 -0
- package/lib/field.js.map +1 -0
- package/lib/flowers.d.ts +138 -0
- package/lib/flowers.d.ts.map +1 -0
- package/lib/flowers.js +117 -0
- package/lib/flowers.js.map +1 -0
- package/lib/index.d.ts +11 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +14 -0
- package/lib/index.js.map +1 -0
- package/lib/mix.d.ts +17 -0
- package/lib/mix.d.ts.map +1 -0
- package/lib/mix.js +95 -0
- package/lib/mix.js.map +1 -0
- package/lib/mixshare.d.ts +42 -0
- package/lib/mixshare.d.ts.map +1 -0
- package/lib/mixshare.js +144 -0
- package/lib/mixshare.js.map +1 -0
- package/lib/noise.d.ts +4 -0
- package/lib/noise.d.ts.map +1 -0
- package/lib/noise.js +49 -0
- package/lib/noise.js.map +1 -0
- package/lib/placement.d.ts +39 -0
- package/lib/placement.d.ts.map +1 -0
- package/lib/placement.js +67 -0
- package/lib/placement.js.map +1 -0
- package/lib/rank.d.ts +36 -0
- package/lib/rank.d.ts.map +1 -0
- package/lib/rank.js +158 -0
- package/lib/rank.js.map +1 -0
- package/lib/species.d.ts +408 -0
- package/lib/species.d.ts.map +1 -0
- package/lib/species.js +672 -0
- package/lib/species.js.map +1 -0
- package/lib/transitions.d.ts +72 -0
- package/lib/transitions.d.ts.map +1 -0
- package/lib/transitions.js +123 -0
- package/lib/transitions.js.map +1 -0
- package/package.json +60 -3
package/lib/noise.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// The noise the grass is varied by: one small texture that repeats without a
|
|
2
|
+
// seam, four fields of different coarseness in its four channels.
|
|
3
|
+
import * as THREE from "three/webgpu";
|
|
4
|
+
function hash(x, y, seed) {
|
|
5
|
+
let h = (Math.imul(x, 0x9e3779b1) ^ Math.imul(y, 0x85ebca77) ^ Math.imul(seed, 0xc2b2ae3d)) >>> 0;
|
|
6
|
+
h = Math.imul(h ^ (h >>> 15), 0x2c1b3c6d) >>> 0;
|
|
7
|
+
h = Math.imul(h ^ (h >>> 12), 0x297a2d39) >>> 0;
|
|
8
|
+
return ((h ^ (h >>> 15)) >>> 8) / 16777216;
|
|
9
|
+
}
|
|
10
|
+
/** Smooth value noise, `cells` lattice cells to the texture's side, so it wraps. */
|
|
11
|
+
function tileNoise(size, cells, seed) {
|
|
12
|
+
const out = new Float32Array(size * size);
|
|
13
|
+
for (let y = 0; y < size; y++) {
|
|
14
|
+
for (let x = 0; x < size; x++) {
|
|
15
|
+
const fx = (x / size) * cells;
|
|
16
|
+
const fy = (y / size) * cells;
|
|
17
|
+
const x0 = Math.floor(fx);
|
|
18
|
+
const y0 = Math.floor(fy);
|
|
19
|
+
const tx = fx - x0;
|
|
20
|
+
const ty = fy - y0;
|
|
21
|
+
const ux = tx * tx * (3 - 2 * tx);
|
|
22
|
+
const uy = ty * ty * (3 - 2 * ty);
|
|
23
|
+
const a = hash(x0 % cells, y0 % cells, seed);
|
|
24
|
+
const b = hash((x0 + 1) % cells, y0 % cells, seed);
|
|
25
|
+
const c = hash(x0 % cells, (y0 + 1) % cells, seed);
|
|
26
|
+
const d = hash((x0 + 1) % cells, (y0 + 1) % cells, seed);
|
|
27
|
+
out[y * size + x] = a + (b - a) * ux + (c - a) * uy + (a - b - c + d) * ux * uy;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return out;
|
|
31
|
+
}
|
|
32
|
+
/** 256 x 256, repeating: 4, 16, 64 and 32 cells to a side in r, g, b and a. */
|
|
33
|
+
export function makeNoiseTexture() {
|
|
34
|
+
const size = 256;
|
|
35
|
+
const channels = [tileNoise(size, 4, 11), tileNoise(size, 16, 23), tileNoise(size, 64, 37), tileNoise(size, 32, 53)];
|
|
36
|
+
const data = new Uint8Array(size * size * 4);
|
|
37
|
+
for (let i = 0; i < size * size; i++)
|
|
38
|
+
for (let c = 0; c < 4; c++)
|
|
39
|
+
data[i * 4 + c] = Math.round(channels[c][i] * 255);
|
|
40
|
+
const tex = new THREE.DataTexture(data, size, size, THREE.RGBAFormat, THREE.UnsignedByteType);
|
|
41
|
+
tex.wrapS = tex.wrapT = THREE.RepeatWrapping;
|
|
42
|
+
tex.magFilter = THREE.LinearFilter;
|
|
43
|
+
tex.minFilter = THREE.LinearMipmapLinearFilter;
|
|
44
|
+
tex.generateMipmaps = true;
|
|
45
|
+
tex.colorSpace = THREE.NoColorSpace;
|
|
46
|
+
tex.needsUpdate = true;
|
|
47
|
+
return tex;
|
|
48
|
+
}
|
|
49
|
+
//# sourceMappingURL=noise.js.map
|
package/lib/noise.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"noise.js","sourceRoot":"","sources":["../src/noise.ts"],"names":[],"mappings":"AAAA,6EAA6E;AAC7E,kEAAkE;AAElE,OAAO,KAAK,KAAK,MAAM,cAAc,CAAC;AAEtC,SAAS,IAAI,CAAC,CAAS,EAAE,CAAS,EAAE,IAAY;IAC9C,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,EAAE,UAAU,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,EAAE,UAAU,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE,UAAU,CAAC,CAAC,KAAK,CAAC,CAAC;IAClG,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,CAAC,EAAE,UAAU,CAAC,KAAK,CAA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EAAE;QAAE,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE;YAAE,IAAI,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC;IACrH,MAAM,GAAG,GAAG,IAAI,KAAK,CAAC,WAAW,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,CAAC,UAAU,EAAE,KAAK,CAAC,gBAAgB,CAAC,CAAC;IAC9F,GAAG,CAAC,KAAK,GAAG,GAAG,CAAC,KAAK,GAAG,KAAK,CAAC,cAAc,CAAC;IAC7C,GAAG,CAAC,SAAS,GAAG,KAAK,CAAC,YAAY,CAAC;IACnC,GAAG,CAAC,SAAS,GAAG,KAAK,CAAC,wBAAwB,CAAC;IAC/C,GAAG,CAAC,eAAe,GAAG,IAAI,CAAC;IAC3B,GAAG,CAAC,UAAU,GAAG,KAAK,CAAC,YAAY,CAAC;IACpC,GAAG,CAAC,WAAW,GAAG,IAAI,CAAC;IACvB,OAAO,GAAG,CAAC;AACb,CAAC","sourcesContent":["// The noise the grass is varied by: one small texture that repeats without a\n// seam, four fields of different coarseness in its four channels.\n\nimport * as THREE from \"three/webgpu\";\n\nfunction hash(x: number, y: number, seed: number): number {\n let h = (Math.imul(x, 0x9e3779b1) ^ Math.imul(y, 0x85ebca77) ^ Math.imul(seed, 0xc2b2ae3d)) >>> 0;\n h = Math.imul(h ^ (h >>> 15), 0x2c1b3c6d) >>> 0;\n h = Math.imul(h ^ (h >>> 12), 0x297a2d39) >>> 0;\n return ((h ^ (h >>> 15)) >>> 8) / 16777216;\n}\n\n/** Smooth value noise, `cells` lattice cells to the texture's side, so it wraps. */\nfunction tileNoise(size: number, cells: number, seed: number): Float32Array {\n const out = new Float32Array(size * size);\n for (let y = 0; y < size; y++) {\n for (let x = 0; x < size; x++) {\n const fx = (x / size) * cells;\n const fy = (y / size) * cells;\n const x0 = Math.floor(fx);\n const y0 = Math.floor(fy);\n const tx = fx - x0;\n const ty = fy - y0;\n const ux = tx * tx * (3 - 2 * tx);\n const uy = ty * ty * (3 - 2 * ty);\n const a = hash(x0 % cells, y0 % cells, seed);\n const b = hash((x0 + 1) % cells, y0 % cells, seed);\n const c = hash(x0 % cells, (y0 + 1) % cells, seed);\n const d = hash((x0 + 1) % cells, (y0 + 1) % cells, seed);\n out[y * size + x] = a + (b - a) * ux + (c - a) * uy + (a - b - c + d) * ux * uy;\n }\n }\n return out;\n}\n\n/** 256 x 256, repeating: 4, 16, 64 and 32 cells to a side in r, g, b and a. */\nexport function makeNoiseTexture(): THREE.DataTexture {\n const size = 256;\n const channels = [tileNoise(size, 4, 11), tileNoise(size, 16, 23), tileNoise(size, 64, 37), tileNoise(size, 32, 53)];\n const data = new Uint8Array(size * size * 4);\n for (let i = 0; i < size * size; i++) for (let c = 0; c < 4; c++) data[i * 4 + c] = Math.round(channels[c][i] * 255);\n const tex = new THREE.DataTexture(data, size, size, THREE.RGBAFormat, THREE.UnsignedByteType);\n tex.wrapS = tex.wrapT = THREE.RepeatWrapping;\n tex.magFilter = THREE.LinearFilter;\n tex.minFilter = THREE.LinearMipmapLinearFilter;\n tex.generateMipmaps = true;\n tex.colorSpace = THREE.NoColorSpace;\n tex.needsUpdate = true;\n return tex;\n}\n"]}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import * as THREE from "three/webgpu";
|
|
2
|
+
type Vec2 = THREE.Node<"vec2">;
|
|
3
|
+
type Float = THREE.Node<"float">;
|
|
4
|
+
/** After Dave Hoskins' hash12: even on whole-numbered cells, where a sine hash stands plants in rows. (Its twin: `hashCell` in rank.ts.) */
|
|
5
|
+
export declare const hashCell: THREE.TSL.FnNode<[THREE.Vector2 | THREE.Node<"vec2">], THREE.Node<"float">>;
|
|
6
|
+
/**
|
|
7
|
+
* The rank matrix (rank.ts `grassRanks`) where the shaders read it, four ranks to an entry of a uniform
|
|
8
|
+
* array (16 KB): not a storage buffer, of which a shader stage is sure of only eight. One to a grass.
|
|
9
|
+
*/
|
|
10
|
+
export declare function rankStore(): {
|
|
11
|
+
/** A cell's rank, 0..1 (cells counted from anywhere, below nothing too). (Its twin: `rankOf`.) */
|
|
12
|
+
rankOf(cell: Vec2): Float;
|
|
13
|
+
dispose(): void;
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Where a square's plant `idx` grows: its cell, counted from the square's corner about `focus`, and
|
|
17
|
+
* scattered within it by its cell's hash. A fine square hashes its cell within a repeat of 1024 cells: the
|
|
18
|
+
* hash loses its evenness on large cell numbers, and the leaves fall into rows. (Its twin: `finePlace`.)
|
|
19
|
+
*/
|
|
20
|
+
export declare function placeInSquare(idx: THREE.Node<"uint"> | Float, count: number, spacing: number, focus: Vec2, fine: boolean): {
|
|
21
|
+
cell: THREE.Node<"vec2">;
|
|
22
|
+
hcell: THREE.Node<"vec2">;
|
|
23
|
+
r0: THREE.Node<"float">;
|
|
24
|
+
r1: THREE.Node<"float">;
|
|
25
|
+
xz: THREE.Node<"vec2">;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* For checking: the first `plants` plants of a fine square of `count` cells a side, `spacing` m apart, about
|
|
29
|
+
* `focus` (m), as the card places them: for each, its x and z (m), its cell's rank, and its cell's hash.
|
|
30
|
+
*/
|
|
31
|
+
export declare function probePlacement(renderer: THREE.WebGPURenderer, o: {
|
|
32
|
+
count: number;
|
|
33
|
+
spacing: number;
|
|
34
|
+
focusX: number;
|
|
35
|
+
focusZ: number;
|
|
36
|
+
plants: number;
|
|
37
|
+
}): Promise<Float32Array>;
|
|
38
|
+
export {};
|
|
39
|
+
//# sourceMappingURL=placement.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"placement.d.ts","sourceRoot":"","sources":["../src/placement.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,KAAK,MAAM,cAAc,CAAC;AAItC,KAAK,IAAI,GAAG,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;AAC/B,KAAK,KAAK,GAAG,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;AAEjC,4IAA4I;AAC5I,eAAO,MAAM,QAAQ,6EAInB,CAAC;AAEH;;;GAGG;AACH,wBAAgB,SAAS;IAKrB,kGAAkG;IAClG,MAAM,OAAO,IAAI,GAAG,KAAK;IAOzB,OAAO;EAEV;AAED;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,GAAG,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,IAAI,EAAE,OAAO;;;;;;EASxH;AAED;;;GAGG;AACH,wBAAsB,cAAc,CAAC,QAAQ,EAAE,KAAK,CAAC,cAAc,EAAE,CAAC,EAAE;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,GAAG,OAAO,CAAC,YAAY,CAAC,CAajL"}
|
package/lib/placement.js
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
// Where the shaders stand each plant, and which they keep when a square is thinned: the graphics card's
|
|
2
|
+
// half of what rank.ts does in plain numbers. Both halves are kept in step: `probePlacement` runs this
|
|
3
|
+
// half on the card for a set of plants and hands back what it worked out, for the lab's check against
|
|
4
|
+
// rank.ts (`checkPlacement`).
|
|
5
|
+
import * as THREE from "three/webgpu";
|
|
6
|
+
import { Fn, dot, float, floor, fract, instanceIndex, int, mod, storage, uniform, uniformArray, vec2, vec3, vec4 } from "three/tsl";
|
|
7
|
+
import { RANK_SIDE, grassRanks } from "./rank.js";
|
|
8
|
+
/** After Dave Hoskins' hash12: even on whole-numbered cells, where a sine hash stands plants in rows. (Its twin: `hashCell` in rank.ts.) */
|
|
9
|
+
export const hashCell = Fn(([pIn]) => {
|
|
10
|
+
const p3 = fract(vec3(pIn.x, pIn.y, pIn.x).mul(0.1031)).toVar();
|
|
11
|
+
p3.addAssign(dot(p3, vec3(p3.y, p3.z, p3.x).add(33.33)));
|
|
12
|
+
return fract(p3.x.add(p3.y).mul(p3.z));
|
|
13
|
+
});
|
|
14
|
+
/**
|
|
15
|
+
* The rank matrix (rank.ts `grassRanks`) where the shaders read it, four ranks to an entry of a uniform
|
|
16
|
+
* array (16 KB): not a storage buffer, of which a shader stage is sure of only eight. One to a grass.
|
|
17
|
+
*/
|
|
18
|
+
export function rankStore() {
|
|
19
|
+
const r = grassRanks();
|
|
20
|
+
const packed = Array.from({ length: r.length / 4 }, (_, i) => new THREE.Vector4(r[i * 4], r[i * 4 + 1], r[i * 4 + 2], r[i * 4 + 3]));
|
|
21
|
+
const node = uniformArray(packed, "vec4");
|
|
22
|
+
return {
|
|
23
|
+
/** A cell's rank, 0..1 (cells counted from anywhere, below nothing too). (Its twin: `rankOf`.) */
|
|
24
|
+
rankOf(cell) {
|
|
25
|
+
const at = cell.sub(floor(cell.div(RANK_SIDE)).mul(RANK_SIDE));
|
|
26
|
+
const i = at.y.mul(RANK_SIDE).add(at.x);
|
|
27
|
+
const four = node.element(int(floor(i.div(4))));
|
|
28
|
+
const which = i.sub(floor(i.div(4)).mul(4));
|
|
29
|
+
return which.lessThan(0.5).select(four.x, which.lessThan(1.5).select(four.y, which.lessThan(2.5).select(four.z, four.w)));
|
|
30
|
+
},
|
|
31
|
+
dispose() { },
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Where a square's plant `idx` grows: its cell, counted from the square's corner about `focus`, and
|
|
36
|
+
* scattered within it by its cell's hash. A fine square hashes its cell within a repeat of 1024 cells: the
|
|
37
|
+
* hash loses its evenness on large cell numbers, and the leaves fall into rows. (Its twin: `finePlace`.)
|
|
38
|
+
*/
|
|
39
|
+
export function placeInSquare(idx, count, spacing, focus, fine) {
|
|
40
|
+
const gx = float(mod(float(idx), float(count)));
|
|
41
|
+
const gz = floor(float(idx).div(float(count)));
|
|
42
|
+
const cell = floor(focus.div(spacing)).add(vec2(gx, gz)).sub(count * 0.5);
|
|
43
|
+
const hcell = fine ? mod(cell, float(1024)) : cell;
|
|
44
|
+
const r0 = hashCell(hcell);
|
|
45
|
+
const r1 = hashCell(hcell.add(31.7));
|
|
46
|
+
const xz = cell.add(vec2(r0, r1)).mul(spacing);
|
|
47
|
+
return { cell, hcell, r0, r1, xz };
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* For checking: the first `plants` plants of a fine square of `count` cells a side, `spacing` m apart, about
|
|
51
|
+
* `focus` (m), as the card places them: for each, its x and z (m), its cell's rank, and its cell's hash.
|
|
52
|
+
*/
|
|
53
|
+
export async function probePlacement(renderer, o) {
|
|
54
|
+
const out = new THREE.StorageBufferAttribute(new Float32Array(o.plants * 4), 4);
|
|
55
|
+
const outNode = storage(out, "vec4", o.plants);
|
|
56
|
+
const ranks = rankStore();
|
|
57
|
+
const uFocus = uniform(new THREE.Vector2(o.focusX, o.focusZ));
|
|
58
|
+
const pass = Fn(() => {
|
|
59
|
+
const p = placeInSquare(instanceIndex, o.count, o.spacing, uFocus, true);
|
|
60
|
+
outNode.element(instanceIndex).assign(vec4(p.xz.x, p.xz.y, ranks.rankOf(p.cell), p.r0));
|
|
61
|
+
})().compute(o.plants);
|
|
62
|
+
await renderer.computeAsync(pass);
|
|
63
|
+
const data = new Float32Array(await renderer.getArrayBufferAsync(out));
|
|
64
|
+
pass.dispose?.();
|
|
65
|
+
return data;
|
|
66
|
+
}
|
|
67
|
+
//# sourceMappingURL=placement.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"placement.js","sourceRoot":"","sources":["../src/placement.ts"],"names":[],"mappings":"AAAA,wGAAwG;AACxG,uGAAuG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;IAChF,MAAM,OAAO,GAAG,OAAO,CAAC,GAAG,EAAE,MAAM,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC;IAC/C,MAAM,KAAK,GAAG,SAAS,EAAE,CAAC;IAC1B,MAAM,MAAM,GAAG,OAAO,CAAC,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC;IAC9D,MAAM,IAAI,GAAG,EAAE,CAAC,GAAG,EAAE;QACnB,MAAM,CAAC,GAAG,aAAa,CAAC,aAAa,EAAE,CAAC,CAAC,KAAK,EAAE,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC,CAAC;QACzE,OAAO,CAAC,OAAO,CAAC,aAAa,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;IAC1F,CAAC,CAAC,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC;IACvB,MAAM,QAAQ,CAAC,YAAY,CAAC,IAAI,CAAC,CAAC;IAClC,MAAM,IAAI,GAAG,IAAI,YAAY,CAAC,MAAM,QAAQ,CAAC,mBAAmB,CAAC,GAAG,CAAC,CAAC,CAAC;IACvE,IAAI,CAAC,OAAO,EAAE,EAAE,CAAC;IACjB,OAAO,IAAI,CAAC;AACd,CAAC","sourcesContent":["// Where the shaders stand each plant, and which they keep when a square is thinned: the graphics card's\n// half of what rank.ts does in plain numbers. Both halves are kept in step: `probePlacement` runs this\n// half on the card for a set of plants and hands back what it worked out, for the lab's check against\n// rank.ts (`checkPlacement`).\n\nimport * as THREE from \"three/webgpu\";\nimport { Fn, dot, float, floor, fract, instanceIndex, int, mod, storage, uniform, uniformArray, vec2, vec3, vec4 } from \"three/tsl\";\nimport { RANK_SIDE, grassRanks } from \"./rank.ts\";\n\ntype Vec2 = THREE.Node<\"vec2\">;\ntype Float = THREE.Node<\"float\">;\n\n/** After Dave Hoskins' hash12: even on whole-numbered cells, where a sine hash stands plants in rows. (Its twin: `hashCell` in rank.ts.) */\nexport const hashCell = Fn(([pIn]: [Vec2]) => {\n const p3 = fract(vec3(pIn.x, pIn.y, pIn.x).mul(0.1031)).toVar();\n p3.addAssign(dot(p3, vec3(p3.y, p3.z, p3.x).add(33.33)));\n return fract(p3.x.add(p3.y).mul(p3.z));\n});\n\n/**\n * The rank matrix (rank.ts `grassRanks`) where the shaders read it, four ranks to an entry of a uniform\n * array (16 KB): not a storage buffer, of which a shader stage is sure of only eight. One to a grass.\n */\nexport function rankStore() {\n const r = grassRanks();\n const packed = Array.from({ length: r.length / 4 }, (_, i) => new THREE.Vector4(r[i * 4], r[i * 4 + 1], r[i * 4 + 2], r[i * 4 + 3]));\n const node = uniformArray(packed, \"vec4\");\n return {\n /** A cell's rank, 0..1 (cells counted from anywhere, below nothing too). (Its twin: `rankOf`.) */\n rankOf(cell: Vec2): Float {\n const at = cell.sub(floor(cell.div(RANK_SIDE)).mul(RANK_SIDE));\n const i = at.y.mul(RANK_SIDE).add(at.x);\n const four = node.element(int(floor(i.div(4)))) as unknown as THREE.Node<\"vec4\">;\n const which = i.sub(floor(i.div(4)).mul(4));\n return which.lessThan(0.5).select(four.x, which.lessThan(1.5).select(four.y, which.lessThan(2.5).select(four.z, four.w))) as unknown as Float;\n },\n dispose() {},\n };\n}\n\n/**\n * Where a square's plant `idx` grows: its cell, counted from the square's corner about `focus`, and\n * scattered within it by its cell's hash. A fine square hashes its cell within a repeat of 1024 cells: the\n * hash loses its evenness on large cell numbers, and the leaves fall into rows. (Its twin: `finePlace`.)\n */\nexport function placeInSquare(idx: THREE.Node<\"uint\"> | Float, count: number, spacing: number, focus: Vec2, fine: boolean) {\n const gx = float(mod(float(idx), float(count)));\n const gz = floor(float(idx).div(float(count)));\n const cell = floor(focus.div(spacing)).add(vec2(gx, gz)).sub(count * 0.5);\n const hcell = fine ? mod(cell, float(1024)) : cell;\n const r0 = hashCell(hcell);\n const r1 = hashCell(hcell.add(31.7));\n const xz = cell.add(vec2(r0, r1)).mul(spacing);\n return { cell, hcell, r0, r1, xz };\n}\n\n/**\n * For checking: the first `plants` plants of a fine square of `count` cells a side, `spacing` m apart, about\n * `focus` (m), as the card places them: for each, its x and z (m), its cell's rank, and its cell's hash.\n */\nexport async function probePlacement(renderer: THREE.WebGPURenderer, o: { count: number; spacing: number; focusX: number; focusZ: number; plants: number }): Promise<Float32Array> {\n const out = new THREE.StorageBufferAttribute(new Float32Array(o.plants * 4), 4);\n const outNode = storage(out, \"vec4\", o.plants);\n const ranks = rankStore();\n const uFocus = uniform(new THREE.Vector2(o.focusX, o.focusZ));\n const pass = Fn(() => {\n const p = placeInSquare(instanceIndex, o.count, o.spacing, uFocus, true);\n outNode.element(instanceIndex).assign(vec4(p.xz.x, p.xz.y, ranks.rankOf(p.cell), p.r0));\n })().compute(o.plants);\n await renderer.computeAsync(pass);\n const data = new Float32Array(await renderer.getArrayBufferAsync(out));\n pass.dispose?.();\n return data;\n}\n"]}
|
package/lib/rank.d.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/** The rank matrix's side, in cells: it repeats every this many cells either way. */
|
|
2
|
+
export declare const RANK_SIDE = 64;
|
|
3
|
+
/**
|
|
4
|
+
* A blue-noise rank for each cell of an `n` by `n` square that repeats (rows along x), each 0..1: the
|
|
5
|
+
* cells with ranks below any share are spread as evenly as can be. Void-and-cluster with a Gaussian of
|
|
6
|
+
* `sigma` cells, wrapping round the square's edges.
|
|
7
|
+
*/
|
|
8
|
+
export declare function rankMatrix(n?: number, sigma?: number, seed?: number): Float32Array;
|
|
9
|
+
/** The rank matrix the grass uses, made once. */
|
|
10
|
+
export declare function grassRanks(): Float32Array;
|
|
11
|
+
/** A cell's rank, 0..1, from the matrix (cells counted from anywhere, below nothing too). */
|
|
12
|
+
export declare function rankOf(ranks: Float32Array, cx: number, cz: number): number;
|
|
13
|
+
/**
|
|
14
|
+
* The shaders' hash of a place (`hashCell` in placement.ts: Hoskins' hash without sine), in 32-bit floats as
|
|
15
|
+
* the graphics card works it. Its dot product is worked as the card's compiler does, as a chain of fused
|
|
16
|
+
* multiply-adds, each rounding once (an f32 times an f32 is exact in a double, so a double sum rounded is a
|
|
17
|
+
* fused one): so it was found to be on Apple's Metal, where the twin and the card agree to the last bit on
|
|
18
|
+
* every plant. A card that fuses otherwise differs in the last bits, which the hash's last step, the
|
|
19
|
+
* fraction of a number in the hundreds, makes a thousandth of a cell, and now and then a whole one
|
|
20
|
+
* (`checkPlacement` in the lab says how many).
|
|
21
|
+
*/
|
|
22
|
+
export declare function hashCell(x: number, y: number): number;
|
|
23
|
+
/**
|
|
24
|
+
* Where a fine square's plant `index` stands (m), as the shaders place it: its cell, counted from the
|
|
25
|
+
* square's corner about `focus` (m), and scattered within it by the hash of its cell (within a repeat of
|
|
26
|
+
* 1024 cells, as the shaders keep it). `count` cells a side, `spacing` m apart.
|
|
27
|
+
*/
|
|
28
|
+
export declare function finePlace(index: number, count: number, spacing: number, focusX: number, focusZ: number): {
|
|
29
|
+
cx: number;
|
|
30
|
+
cz: number;
|
|
31
|
+
x: number;
|
|
32
|
+
z: number;
|
|
33
|
+
r0: number;
|
|
34
|
+
r1: number;
|
|
35
|
+
};
|
|
36
|
+
//# sourceMappingURL=rank.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rank.d.ts","sourceRoot":"","sources":["../src/rank.ts"],"names":[],"mappings":"AAWA,qFAAqF;AACrF,eAAO,MAAM,SAAS,KAAK,CAAC;AAc5B;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,CAAC,SAAY,EAAE,KAAK,SAAM,EAAE,IAAI,SAAI,GAAG,YAAY,CAkE7E;AAGD,iDAAiD;AACjD,wBAAgB,UAAU,IAAI,YAAY,CAGzC;AAED,6FAA6F;AAC7F,wBAAgB,MAAM,CAAC,KAAK,EAAE,YAAY,EAAE,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,GAAG,MAAM,CAI1E;AAKD;;;;;;;;GAQG;AACH,wBAAgB,QAAQ,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAYrD;AAED;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG;IAAE,EAAE,EAAE,MAAM,CAAC;IAAC,EAAE,EAAE,MAAM,CAAC;IAAC,CAAC,EAAE,MAAM,CAAC;IAAC,CAAC,EAAE,MAAM,CAAC;IAAC,EAAE,EAAE,MAAM,CAAC;IAAC,EAAE,EAAE,MAAM,CAAA;CAAE,CAUjL"}
|
package/lib/rank.js
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
// Which plants are kept when a square is thinned, and where each stands: the plain arithmetic, with no
|
|
2
|
+
// three.js in it, so that the shaders' placement has a twin here that a test can run and set against what
|
|
3
|
+
// the graphics card works out (`checkPlacement` in the lab).
|
|
4
|
+
//
|
|
5
|
+
// A square of plants is a grid of cells, one plant to a cell, scattered within it. Thinning keeps a
|
|
6
|
+
// share of them. Keeping each by a coin of its own (white noise) leaves clumps and holes wherever the
|
|
7
|
+
// share is less than all; so each cell has a rank instead, from a blue-noise matrix, and a cell is kept
|
|
8
|
+
// while its rank is below the share. Whatever the share, the kept cells are spread evenly: the first
|
|
9
|
+
// tenth are a tenth of the cells as far from each other as they can be, and the next tenth fill the
|
|
10
|
+
// gaps between them, and so on. (Ulichney's void-and-cluster, 1993; the way bevy-grass ranks its blades.)
|
|
11
|
+
/** The rank matrix's side, in cells: it repeats every this many cells either way. */
|
|
12
|
+
export const RANK_SIDE = 64;
|
|
13
|
+
/** A seeded generator of numbers 0..1 (mulberry32): the same matrix every time. */
|
|
14
|
+
function seeded(seed) {
|
|
15
|
+
let a = seed >>> 0;
|
|
16
|
+
return () => {
|
|
17
|
+
a = (a + 0x6d2b79f5) >>> 0;
|
|
18
|
+
let t = a;
|
|
19
|
+
t = Math.imul(t ^ (t >>> 15), t | 1);
|
|
20
|
+
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
|
|
21
|
+
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* A blue-noise rank for each cell of an `n` by `n` square that repeats (rows along x), each 0..1: the
|
|
26
|
+
* cells with ranks below any share are spread as evenly as can be. Void-and-cluster with a Gaussian of
|
|
27
|
+
* `sigma` cells, wrapping round the square's edges.
|
|
28
|
+
*/
|
|
29
|
+
export function rankMatrix(n = RANK_SIDE, sigma = 1.5, seed = 7) {
|
|
30
|
+
const count = n * n;
|
|
31
|
+
const reach = Math.ceil(sigma * 4);
|
|
32
|
+
const kernel = [];
|
|
33
|
+
for (let dy = -reach; dy <= reach; dy++)
|
|
34
|
+
for (let dx = -reach; dx <= reach; dx++)
|
|
35
|
+
kernel.push([dx, dy, Math.exp(-(dx * dx + dy * dy) / (2 * sigma * sigma))]);
|
|
36
|
+
const energy = new Float64Array(count);
|
|
37
|
+
const on = new Uint8Array(count);
|
|
38
|
+
const touch = (i, sign) => {
|
|
39
|
+
const x = i % n;
|
|
40
|
+
const y = (i / n) | 0;
|
|
41
|
+
for (const [dx, dy, w] of kernel)
|
|
42
|
+
energy[((y + dy + n) % n) * n + ((x + dx + n) % n)] += sign * w;
|
|
43
|
+
};
|
|
44
|
+
/** The cell that is on (or off) where the others crowd it most (least). */
|
|
45
|
+
const most = (want) => {
|
|
46
|
+
let best = -1;
|
|
47
|
+
let at = 0;
|
|
48
|
+
for (let i = 0; i < count; i++)
|
|
49
|
+
if (on[i] === want && (best < 0 || energy[i] > energy[best]))
|
|
50
|
+
(best = i), (at = i);
|
|
51
|
+
return at;
|
|
52
|
+
};
|
|
53
|
+
const least = (want) => {
|
|
54
|
+
let best = -1;
|
|
55
|
+
let at = 0;
|
|
56
|
+
for (let i = 0; i < count; i++)
|
|
57
|
+
if (on[i] === want && (best < 0 || energy[i] < energy[best]))
|
|
58
|
+
(best = i), (at = i);
|
|
59
|
+
return at;
|
|
60
|
+
};
|
|
61
|
+
// A first scatter, a tenth of the cells, settled: the most crowded moved to the emptiest until it stays.
|
|
62
|
+
const random = seeded(seed);
|
|
63
|
+
const first = Math.floor(count / 10);
|
|
64
|
+
for (let placed = 0; placed < first;) {
|
|
65
|
+
const i = Math.floor(random() * count);
|
|
66
|
+
if (on[i])
|
|
67
|
+
continue;
|
|
68
|
+
on[i] = 1;
|
|
69
|
+
touch(i, 1);
|
|
70
|
+
placed++;
|
|
71
|
+
}
|
|
72
|
+
for (let guard = 0; guard < count * 4; guard++) {
|
|
73
|
+
const crowded = most(1);
|
|
74
|
+
on[crowded] = 0;
|
|
75
|
+
touch(crowded, -1);
|
|
76
|
+
const empty = least(0);
|
|
77
|
+
on[empty] = 1;
|
|
78
|
+
touch(empty, 1);
|
|
79
|
+
if (empty === crowded)
|
|
80
|
+
break;
|
|
81
|
+
}
|
|
82
|
+
const settled = on.slice();
|
|
83
|
+
const settledEnergy = energy.slice();
|
|
84
|
+
const rank = new Int32Array(count).fill(-1);
|
|
85
|
+
// Its cells ranked from the last: the most crowded taken off first.
|
|
86
|
+
for (let r = first - 1; r >= 0; r--) {
|
|
87
|
+
const crowded = most(1);
|
|
88
|
+
on[crowded] = 0;
|
|
89
|
+
touch(crowded, -1);
|
|
90
|
+
rank[crowded] = r;
|
|
91
|
+
}
|
|
92
|
+
// The rest ranked from the first scatter on: each into the emptiest place left.
|
|
93
|
+
on.set(settled);
|
|
94
|
+
energy.set(settledEnergy);
|
|
95
|
+
for (let r = first; r < count; r++) {
|
|
96
|
+
const empty = least(0);
|
|
97
|
+
on[empty] = 1;
|
|
98
|
+
touch(empty, 1);
|
|
99
|
+
rank[empty] = r;
|
|
100
|
+
}
|
|
101
|
+
const out = new Float32Array(count);
|
|
102
|
+
for (let i = 0; i < count; i++)
|
|
103
|
+
out[i] = (rank[i] + 0.5) / count;
|
|
104
|
+
return out;
|
|
105
|
+
}
|
|
106
|
+
let shared = null;
|
|
107
|
+
/** The rank matrix the grass uses, made once. */
|
|
108
|
+
export function grassRanks() {
|
|
109
|
+
shared ??= rankMatrix();
|
|
110
|
+
return shared;
|
|
111
|
+
}
|
|
112
|
+
/** A cell's rank, 0..1, from the matrix (cells counted from anywhere, below nothing too). */
|
|
113
|
+
export function rankOf(ranks, cx, cz) {
|
|
114
|
+
const ix = cx - RANK_SIDE * Math.floor(cx / RANK_SIDE);
|
|
115
|
+
const iz = cz - RANK_SIDE * Math.floor(cz / RANK_SIDE);
|
|
116
|
+
return ranks[iz * RANK_SIDE + ix];
|
|
117
|
+
}
|
|
118
|
+
const f = Math.fround;
|
|
119
|
+
const fract = (v) => f(v - Math.floor(v));
|
|
120
|
+
/**
|
|
121
|
+
* The shaders' hash of a place (`hashCell` in placement.ts: Hoskins' hash without sine), in 32-bit floats as
|
|
122
|
+
* the graphics card works it. Its dot product is worked as the card's compiler does, as a chain of fused
|
|
123
|
+
* multiply-adds, each rounding once (an f32 times an f32 is exact in a double, so a double sum rounded is a
|
|
124
|
+
* fused one): so it was found to be on Apple's Metal, where the twin and the card agree to the last bit on
|
|
125
|
+
* every plant. A card that fuses otherwise differs in the last bits, which the hash's last step, the
|
|
126
|
+
* fraction of a number in the hundreds, makes a thousandth of a cell, and now and then a whole one
|
|
127
|
+
* (`checkPlacement` in the lab says how many).
|
|
128
|
+
*/
|
|
129
|
+
export function hashCell(x, y) {
|
|
130
|
+
let px = fract(f(x * f(0.1031)));
|
|
131
|
+
let py = fract(f(y * f(0.1031)));
|
|
132
|
+
let pz = fract(f(x * f(0.1031)));
|
|
133
|
+
const k = f(33.33);
|
|
134
|
+
let d = f(px * f(py + k));
|
|
135
|
+
d = f(py * f(pz + k) + d);
|
|
136
|
+
d = f(pz * f(px + k) + d);
|
|
137
|
+
px = f(px + d);
|
|
138
|
+
py = f(py + d);
|
|
139
|
+
pz = f(pz + d);
|
|
140
|
+
return fract(f(f(px + py) * pz));
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Where a fine square's plant `index` stands (m), as the shaders place it: its cell, counted from the
|
|
144
|
+
* square's corner about `focus` (m), and scattered within it by the hash of its cell (within a repeat of
|
|
145
|
+
* 1024 cells, as the shaders keep it). `count` cells a side, `spacing` m apart.
|
|
146
|
+
*/
|
|
147
|
+
export function finePlace(index, count, spacing, focusX, focusZ) {
|
|
148
|
+
const gx = index % count;
|
|
149
|
+
const gz = Math.floor(index / count);
|
|
150
|
+
const cx = Math.floor(f(f(focusX) / f(spacing))) + gx - count * 0.5;
|
|
151
|
+
const cz = Math.floor(f(f(focusZ) / f(spacing))) + gz - count * 0.5;
|
|
152
|
+
const hx = cx - 1024 * Math.floor(cx / 1024);
|
|
153
|
+
const hz = cz - 1024 * Math.floor(cz / 1024);
|
|
154
|
+
const r0 = hashCell(hx, hz);
|
|
155
|
+
const r1 = hashCell(f(hx + f(31.7)), f(hz + f(31.7)));
|
|
156
|
+
return { cx, cz, x: f(f(cx + r0) * f(spacing)), z: f(f(cz + r1) * f(spacing)), r0, r1 };
|
|
157
|
+
}
|
|
158
|
+
//# sourceMappingURL=rank.js.map
|
package/lib/rank.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rank.js","sourceRoot":"","sources":["../src/rank.ts"],"names":[],"mappings":"AAAA,uGAAuG;AACvG,0GAA0G;AAC1G,6DAA6D;AAC7D,EAAE;AACF,oGAAoG;AACpG,sGAAsG;AACtG,wGAAwG;AACxG,qGAAqG;AACrG,oGAAoG;AACpG,0GAA0G;AAE1G,qFAAqF;AACrF,MAAM,CAAC,MAAM,SAAS,GAAG,EAAE,CAAC;AAE5B,mFA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AAG,IAAI,CAAC,KAAK,CAAC,EAAE,GAAG,IAAI,CAAC,CAAC;IAC7C,MAAM,EAAE,GAAG,QAAQ,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC;IAC5B,MAAM,EAAE,GAAG,QAAQ,CAAC,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IACtD,OAAO,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC,CAAC,OAAO,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC,CAAC,OAAO,CAAC,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC;AAC1F,CAAC","sourcesContent":["// Which plants are kept when a square is thinned, and where each stands: the plain arithmetic, with no\n// three.js in it, so that the shaders' placement has a twin here that a test can run and set against what\n// the graphics card works out (`checkPlacement` in the lab).\n//\n// A square of plants is a grid of cells, one plant to a cell, scattered within it. Thinning keeps a\n// share of them. Keeping each by a coin of its own (white noise) leaves clumps and holes wherever the\n// share is less than all; so each cell has a rank instead, from a blue-noise matrix, and a cell is kept\n// while its rank is below the share. Whatever the share, the kept cells are spread evenly: the first\n// tenth are a tenth of the cells as far from each other as they can be, and the next tenth fill the\n// gaps between them, and so on. (Ulichney's void-and-cluster, 1993; the way bevy-grass ranks its blades.)\n\n/** The rank matrix's side, in cells: it repeats every this many cells either way. */\nexport const RANK_SIDE = 64;\n\n/** A seeded generator of numbers 0..1 (mulberry32): the same matrix every time. */\nfunction seeded(seed: number): () => number {\n let a = seed >>> 0;\n return () => {\n a = (a + 0x6d2b79f5) >>> 0;\n let t = a;\n t = Math.imul(t ^ (t >>> 15), t | 1);\n t ^= t + Math.imul(t ^ (t >>> 7), t | 61);\n return ((t ^ (t >>> 14)) >>> 0) / 4294967296;\n };\n}\n\n/**\n * A blue-noise rank for each cell of an `n` by `n` square that repeats (rows along x), each 0..1: the\n * cells with ranks below any share are spread as evenly as can be. Void-and-cluster with a Gaussian of\n * `sigma` cells, wrapping round the square's edges.\n */\nexport function rankMatrix(n = RANK_SIDE, sigma = 1.5, seed = 7): Float32Array {\n const count = n * n;\n const reach = Math.ceil(sigma * 4);\n const kernel: [number, number, number][] = [];\n for (let dy = -reach; dy <= reach; dy++) for (let dx = -reach; dx <= reach; dx++) kernel.push([dx, dy, Math.exp(-(dx * dx + dy * dy) / (2 * sigma * sigma))]);\n const energy = new Float64Array(count);\n const on = new Uint8Array(count);\n const touch = (i: number, sign: number) => {\n const x = i % n;\n const y = (i / n) | 0;\n for (const [dx, dy, w] of kernel) energy[((y + dy + n) % n) * n + ((x + dx + n) % n)] += sign * w;\n };\n /** The cell that is on (or off) where the others crowd it most (least). */\n const most = (want: number) => {\n let best = -1;\n let at = 0;\n for (let i = 0; i < count; i++) if (on[i] === want && (best < 0 || energy[i] > energy[best])) (best = i), (at = i);\n return at;\n };\n const least = (want: number) => {\n let best = -1;\n let at = 0;\n for (let i = 0; i < count; i++) if (on[i] === want && (best < 0 || energy[i] < energy[best])) (best = i), (at = i);\n return at;\n };\n // A first scatter, a tenth of the cells, settled: the most crowded moved to the emptiest until it stays.\n const random = seeded(seed);\n const first = Math.floor(count / 10);\n for (let placed = 0; placed < first; ) {\n const i = Math.floor(random() * count);\n if (on[i]) continue;\n on[i] = 1;\n touch(i, 1);\n placed++;\n }\n for (let guard = 0; guard < count * 4; guard++) {\n const crowded = most(1);\n on[crowded] = 0;\n touch(crowded, -1);\n const empty = least(0);\n on[empty] = 1;\n touch(empty, 1);\n if (empty === crowded) break;\n }\n const settled = on.slice();\n const settledEnergy = energy.slice();\n const rank = new Int32Array(count).fill(-1);\n // Its cells ranked from the last: the most crowded taken off first.\n for (let r = first - 1; r >= 0; r--) {\n const crowded = most(1);\n on[crowded] = 0;\n touch(crowded, -1);\n rank[crowded] = r;\n }\n // The rest ranked from the first scatter on: each into the emptiest place left.\n on.set(settled);\n energy.set(settledEnergy);\n for (let r = first; r < count; r++) {\n const empty = least(0);\n on[empty] = 1;\n touch(empty, 1);\n rank[empty] = r;\n }\n const out = new Float32Array(count);\n for (let i = 0; i < count; i++) out[i] = (rank[i] + 0.5) / count;\n return out;\n}\n\nlet shared: Float32Array | null = null;\n/** The rank matrix the grass uses, made once. */\nexport function grassRanks(): Float32Array {\n shared ??= rankMatrix();\n return shared;\n}\n\n/** A cell's rank, 0..1, from the matrix (cells counted from anywhere, below nothing too). */\nexport function rankOf(ranks: Float32Array, cx: number, cz: number): number {\n const ix = cx - RANK_SIDE * Math.floor(cx / RANK_SIDE);\n const iz = cz - RANK_SIDE * Math.floor(cz / RANK_SIDE);\n return ranks[iz * RANK_SIDE + ix];\n}\n\nconst f = Math.fround;\nconst fract = (v: number) => f(v - Math.floor(v));\n\n/**\n * The shaders' hash of a place (`hashCell` in placement.ts: Hoskins' hash without sine), in 32-bit floats as\n * the graphics card works it. Its dot product is worked as the card's compiler does, as a chain of fused\n * multiply-adds, each rounding once (an f32 times an f32 is exact in a double, so a double sum rounded is a\n * fused one): so it was found to be on Apple's Metal, where the twin and the card agree to the last bit on\n * every plant. A card that fuses otherwise differs in the last bits, which the hash's last step, the\n * fraction of a number in the hundreds, makes a thousandth of a cell, and now and then a whole one\n * (`checkPlacement` in the lab says how many).\n */\nexport function hashCell(x: number, y: number): number {\n let px = fract(f(x * f(0.1031)));\n let py = fract(f(y * f(0.1031)));\n let pz = fract(f(x * f(0.1031)));\n const k = f(33.33);\n let d = f(px * f(py + k));\n d = f(py * f(pz + k) + d);\n d = f(pz * f(px + k) + d);\n px = f(px + d);\n py = f(py + d);\n pz = f(pz + d);\n return fract(f(f(px + py) * pz));\n}\n\n/**\n * Where a fine square's plant `index` stands (m), as the shaders place it: its cell, counted from the\n * square's corner about `focus` (m), and scattered within it by the hash of its cell (within a repeat of\n * 1024 cells, as the shaders keep it). `count` cells a side, `spacing` m apart.\n */\nexport function finePlace(index: number, count: number, spacing: number, focusX: number, focusZ: number): { cx: number; cz: number; x: number; z: number; r0: number; r1: number } {\n const gx = index % count;\n const gz = Math.floor(index / count);\n const cx = Math.floor(f(f(focusX) / f(spacing))) + gx - count * 0.5;\n const cz = Math.floor(f(f(focusZ) / f(spacing))) + gz - count * 0.5;\n const hx = cx - 1024 * Math.floor(cx / 1024);\n const hz = cz - 1024 * Math.floor(cz / 1024);\n const r0 = hashCell(hx, hz);\n const r1 = hashCell(f(hx + f(31.7)), f(hz + f(31.7)));\n return { cx, cz, x: f(f(cx + r0) * f(spacing)), z: f(f(cz + r1) * f(spacing)), r0, r1 };\n}\n"]}
|