Skip to content

Commit 266638d

Browse files
snow-jallenclaude
andcommitted
3D view: time of day, worn ground and grass, readable armies, collapsing buildings, Sanpete landmarks
- Each map gets a time of day from its seed (morning, noon, golden hour, overcast): sun, sky, haze and a split-tone color grade together (atmosphere.js). - The ground shows the match: paths worn where units walk, scorch marks where things burned (groundmarks.js), and grass that sways and is trodden flat on the paths (grass.js). - Armies read from afar: units drawn larger and rimmed in team color as the camera pulls back, and map-key icons over them when zoomed far out (icons.js). - Smoke and dust billow instead of being discs; destroyed buildings collapse into dust. - Landmarks in the hills around the map, laid out roughly as the valley is: the Fairview mammoth, Spring City, Moroni's turkey farms, the Ephraim Co-op and Relief Society granary, the Manti Temple, Palisade Reservoir and the Snow "S" (landmarks.js; Snow College theme only). - Director mode shoots like a film: letterbox, depth of field, a slow push-in, and slow motion in replays when a headquarters falls. - Labels hidden behind the land or under a more important label fade out (labels.js). - view.js gives up its labels, team colors and materials to labels.js, team.js and material.js. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01V8fdNFMnkyDNTFoKqQyD6L
1 parent 93c172c commit 266638d

17 files changed

Lines changed: 1519 additions & 272 deletions

File tree

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
// 3D match view: the light a match is played in. Each map gets a time of day from its seed (a clear
2+
// morning, high noon, golden hour, or a gray day), which sets the sun, the sky, the haze and the color
3+
// grade together, so the four looks are each consistent and the same map always looks the same.
4+
import * as THREE from 'three';
5+
import { Sky } from 'three/addons/objects/Sky.js';
6+
7+
const sunFrom = (elevation, azimuth) => new THREE.Vector3().setFromSphericalCoords(1,
8+
THREE.MathUtils.degToRad(90 - elevation), THREE.MathUtils.degToRad(azimuth));
9+
10+
/**
11+
* The looks. sky: three.js Sky settings; grade: warm (highlight tint), cool (shadow tint), split (how much
12+
* of each), contrast, saturation, vignette. haze tints the far ground toward the horizon.
13+
*/
14+
export const MOODS = {
15+
morning: {
16+
sun: sunFrom(24, 125), sunColor: '#ffd9b0', sunI: 2.5, shadow: 0.9,
17+
sky: { turbidity: 4, rayleigh: 1.9, mieCoefficient: 0.004, mieDirectionalG: 0.8 },
18+
hemi: ['#bfd6ff', '#5d5442', 0.6], env: 0.6, exposure: 1.0, fog: '#c8d5e2', fogFar: 4.2,
19+
grade: { warm: [1.06, 1.0, 0.9], cool: [0.9, 0.97, 1.1], split: 0.55, contrast: 1.06, saturation: 1.0, vignette: 0.5 },
20+
},
21+
noon: {
22+
sun: sunFrom(52, 205), sunColor: '#fff0da', sunI: 2.9, shadow: 1,
23+
sky: { turbidity: 3.2, rayleigh: 1.1, mieCoefficient: 0.003, mieDirectionalG: 0.82 },
24+
hemi: ['#b9d3ff', '#6a5a40', 0.55], env: 0.55, exposure: 0.95, fog: '#bccbd8', fogFar: 4.6,
25+
grade: { warm: [1.04, 1.0, 0.94], cool: [0.95, 0.99, 1.05], split: 0.35, contrast: 1.06, saturation: 1.02, vignette: 0.45 },
26+
},
27+
golden: {
28+
sun: sunFrom(13, 250), sunColor: '#ffb27a', sunI: 2.7, shadow: 0.95,
29+
sky: { turbidity: 7.5, rayleigh: 2.6, mieCoefficient: 0.006, mieDirectionalG: 0.86 },
30+
hemi: ['#c4bdf0', '#6b4a30', 0.5], env: 0.5, exposure: 1.05, fog: '#e0c6a8', fogFar: 3.8,
31+
grade: { warm: [1.12, 1.0, 0.82], cool: [0.86, 0.96, 1.12], split: 0.8, contrast: 1.08, saturation: 1.04, vignette: 0.65 },
32+
},
33+
overcast: {
34+
sun: sunFrom(42, 190), sunColor: '#e9eef4', sunI: 1.25, shadow: 0.45,
35+
sky: { turbidity: 12, rayleigh: 0.5, mieCoefficient: 0.02, mieDirectionalG: 0.6 },
36+
hemi: ['#d2dbe4', '#5e5a50', 1.0], env: 0.95, exposure: 1.05, fog: '#b9c1c9', fogFar: 3.4,
37+
grade: { warm: [1.0, 1.0, 0.98], cool: [0.94, 0.98, 1.04], split: 0.3, contrast: 1.04, saturation: 0.9, vignette: 0.45 },
38+
},
39+
};
40+
41+
/** Which look a map gets: mostly fair weather, now and then a gray day. */
42+
export function moodFor(seed) {
43+
const order = ['noon', 'morning', 'golden', 'noon', 'morning', 'golden', 'overcast'];
44+
return order[((seed % order.length) + order.length) % order.length];
45+
}
46+
47+
/**
48+
* Light the view's scene for `name`: the sky dome and the image-based light taken from it, the sun and
49+
* its shadows, the fill light, the haze and the exposure. The grade pass picks the mood up from
50+
* view.mood (setGrade).
51+
*/
52+
export function applyMood(view, name) {
53+
const mood = MOODS[name] ?? MOODS.noon;
54+
view.mood = mood;
55+
view.moodName = MOODS[name] ? name : 'noon';
56+
view.sunDir = mood.sun.clone();
57+
const u = view.sky.material.uniforms;
58+
for (const [k, v] of Object.entries(mood.sky)) u[k].value = v;
59+
u.sunPosition.value.copy(mood.sun);
60+
61+
// Image-based lighting from the same sky, so every surface picks up its color.
62+
const pmrem = new THREE.PMREMGenerator(view.renderer);
63+
const envScene = new THREE.Scene();
64+
const envSky = new Sky();
65+
envSky.scale.setScalar(100);
66+
Object.entries(u).forEach(([k, v]) => { envSky.material.uniforms[k].value = v.value?.clone ? v.value.clone() : v.value; });
67+
envScene.add(envSky);
68+
view.envRT?.dispose();
69+
view.envRT = pmrem.fromScene(envScene, 0, 0.1, 200);
70+
view.scene.environment = view.envRT.texture;
71+
view.scene.environmentIntensity = mood.env;
72+
pmrem.dispose();
73+
envSky.material.dispose();
74+
75+
view.sun.color.set(mood.sunColor);
76+
view.sun.intensity = mood.sunI;
77+
view.sun.shadow.intensity = mood.shadow;
78+
view.hemi.color.set(mood.hemi[0]);
79+
view.hemi.groundColor.set(mood.hemi[1]);
80+
view.hemi.intensity = mood.hemi[2];
81+
view.scene.fog.color.set(mood.fog);
82+
view.renderer.toneMappingExposure = mood.exposure;
83+
if (view.gradePass) setGrade(view.gradePass, mood);
84+
}
85+
86+
/** Copy a mood's grade into the grade pass's uniforms. */
87+
export function setGrade(pass, mood) {
88+
const g = mood.grade, u = pass.uniforms;
89+
u.uWarm.value.set(...g.warm);
90+
u.uCool.value.set(...g.cool);
91+
u.uSplit.value = g.split;
92+
u.uContrast.value = g.contrast;
93+
u.uSaturation.value = g.saturation;
94+
u.uVignette.value = g.vignette;
95+
}
96+
97+
/**
98+
* The color grade, after bloom and before tone mapping's output pass: split toning (cool shadows, warm
99+
* highlights), a gentle S-curve for contrast, saturation, and a soft vignette.
100+
*/
101+
export function gradeShader() {
102+
return {
103+
uniforms: {
104+
tDiffuse: { value: null },
105+
uWarm: { value: new THREE.Vector3(1.04, 1, 0.94) }, uCool: { value: new THREE.Vector3(0.95, 0.99, 1.05) },
106+
uSplit: { value: 0.35 }, uContrast: { value: 1.08 }, uSaturation: { value: 1.08 }, uVignette: { value: 0.55 },
107+
},
108+
vertexShader: 'varying vec2 vUv; void main() { vUv = uv; gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0); }',
109+
fragmentShader: `
110+
uniform sampler2D tDiffuse; uniform vec3 uWarm; uniform vec3 uCool;
111+
uniform float uSplit; uniform float uContrast; uniform float uSaturation; uniform float uVignette;
112+
varying vec2 vUv;
113+
void main() {
114+
vec4 c = texture2D(tDiffuse, vUv);
115+
float l = dot(c.rgb, vec3(0.2126, 0.7152, 0.0722));
116+
// Split toning: shadows lean cool, highlights warm.
117+
vec3 tint = mix(uCool, uWarm, smoothstep(0.05, 0.9, l));
118+
c.rgb = mix(c.rgb, c.rgb * tint, uSplit);
119+
// Contrast about mid-gray in a log-ish space, so HDR highlights aren't crushed.
120+
vec3 lc = log2(max(c.rgb, vec3(1e-4)) / 0.18);
121+
c.rgb = 0.18 * exp2(lc * uContrast);
122+
l = dot(c.rgb, vec3(0.2126, 0.7152, 0.0722));
123+
c.rgb = max(mix(vec3(l), c.rgb, uSaturation), 0.0);
124+
vec2 d = vUv - 0.5;
125+
c.rgb *= 1.0 - dot(d, d) * uVignette;
126+
gl_FragColor = c;
127+
}`,
128+
};
129+
}

‎src/NetRts.Server/wwwroot/3d/camera.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,9 @@ export class CameraRig {
370370
d.spot = { x: spot.x, z: spot.z };
371371
}
372372
// A spot can say how far back to sit (a page framing a whole area); otherwise the fight's size decides.
373-
this.goal.dist = spot.dist ?? clamp(d.near - 1 + Math.sqrt(spot.size || 1) * d.spread, d.near, d.far);
373+
// ...and the camera creeps in a little through each shot, the way a film camera dollies.
374+
const push = 1 - 0.14 * clamp((now - d.lastCut) / (d.cutEvery * 1.6), 0, 1);
375+
this.goal.dist = spot.dist ?? clamp(d.near - 1 + Math.sqrt(spot.size || 1) * d.spread, d.near, d.far) * push;
374376
} else if (!d.spot) {
375377
d.spot = { x: this.terrain.W / 2, z: this.terrain.H / 2 };
376378
this.goal.dist = Math.max(this.terrain.W, this.terrain.H) * 0.6;

‎src/NetRts.Server/wwwroot/3d/effects.js‎

Lines changed: 35 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -23,34 +23,56 @@ class ParticlePool {
2323
this.alpha0 = new Float32Array(MAX_PARTICLES);
2424
this.rgb = new Float32Array(MAX_PARTICLES * 3);
2525
this.cool = new Float32Array(MAX_PARTICLES); // fire cools from yellow to red to smoke
26+
this.seed = new Float32Array(MAX_PARTICLES); // each puff's own shape and turn
2627
const geo = new THREE.BufferGeometry();
2728
this.aPos = new THREE.BufferAttribute(this.pos, 3).setUsage(THREE.DynamicDrawUsage);
2829
this.aCol = new THREE.BufferAttribute(this.col, 4).setUsage(THREE.DynamicDrawUsage);
2930
this.aSize = new THREE.BufferAttribute(this.size, 1).setUsage(THREE.DynamicDrawUsage);
3031
geo.setAttribute('position', this.aPos);
3132
geo.setAttribute('pcolor', this.aCol);
3233
geo.setAttribute('psize', this.aSize);
34+
this.aSeed = new THREE.BufferAttribute(this.seed, 1).setUsage(THREE.DynamicDrawUsage);
35+
geo.setAttribute('pseed', this.aSeed);
3336
geo.setDrawRange(0, 0);
3437
this.material = new THREE.ShaderMaterial({
3538
uniforms: { uScale: { value: 600 } },
3639
vertexShader: `
37-
attribute vec4 pcolor; attribute float psize;
40+
attribute vec4 pcolor; attribute float psize; attribute float pseed;
3841
uniform float uScale;
39-
varying vec4 vCol;
42+
varying vec4 vCol; varying float vSeed;
4043
void main() {
41-
vCol = pcolor;
44+
vCol = pcolor; vSeed = pseed;
4245
vec4 mv = modelViewMatrix * vec4(position, 1.0);
4346
gl_Position = projectionMatrix * mv;
4447
gl_PointSize = clamp(psize * uScale / max(0.1, -mv.z), 0.0, 400.0);
4548
}`,
46-
fragmentShader: `
47-
varying vec4 vCol;
49+
fragmentShader: additive ? `
50+
varying vec4 vCol; varying float vSeed;
4851
void main() {
4952
vec2 p = gl_PointCoord * 2.0 - 1.0;
5053
float r = dot(p, p);
5154
if (r > 1.0) discard;
52-
float a = ${additive ? 'pow(1.0 - r, 1.6)' : 'smoothstep(1.0, 0.15, r)'};
53-
gl_FragColor = vec4(vCol.rgb${additive ? ' * a * vCol.a' : ''}, ${additive ? '1.0' : 'vCol.a * a'});
55+
float a = pow(1.0 - r, 1.6);
56+
gl_FragColor = vec4(vCol.rgb * a * vCol.a, 1.0);
57+
}` : `
58+
varying vec4 vCol; varying float vSeed;
59+
float h(vec2 p) { return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453); }
60+
float vn(vec2 p) {
61+
vec2 i = floor(p), f = fract(p);
62+
f = f * f * (3.0 - 2.0 * f);
63+
return mix(mix(h(i), h(i + vec2(1.0, 0.0)), f.x), mix(h(i + vec2(0.0, 1.0)), h(i + vec2(1.0, 1.0)), f.x), f.y);
64+
}
65+
void main() {
66+
// A billowing puff rather than a disc: a lumpy outline, turned its own way, lit from above.
67+
vec2 p = gl_PointCoord * 2.0 - 1.0;
68+
float c = cos(vSeed), s = sin(vSeed);
69+
vec2 q = mat2(c, -s, s, c) * p;
70+
float n = vn(q * 2.3 + vSeed) * 0.6 + vn(q * 5.1 - vSeed * 1.7) * 0.4;
71+
float r = dot(p, p) + (n - 0.5) * 0.6;
72+
if (r > 1.0) discard;
73+
float a = smoothstep(1.0, 0.2, r);
74+
float light = mix(1.18, 0.7, gl_PointCoord.y) * (0.82 + n * 0.36);
75+
gl_FragColor = vec4(vCol.rgb * light, vCol.a * a);
5476
}`,
5577
transparent: true,
5678
depthWrite: false,
@@ -72,6 +94,7 @@ class ParticlePool {
7294
this.size0[i] = size; this.grow[i] = grow;
7395
this.grav[i] = gravity; this.drag[i] = drag;
7496
this.alpha0[i] = alpha; this.cool[i] = cool;
97+
this.seed[i] = Math.random() * 60;
7598
}
7699

77100
update(dt) {
@@ -107,13 +130,14 @@ class ParticlePool {
107130
this.aPos.needsUpdate = true;
108131
this.aCol.needsUpdate = true;
109132
this.aSize.needsUpdate = true;
133+
this.aSeed.needsUpdate = true;
110134
}
111135

112136
move(from, to) {
113137
for (const [arr, w] of [[this.pos, 3], [this.vel, 3], [this.rgb, 3], [this.col, 4]]) {
114138
for (let c = 0; c < w; c++) arr[to * w + c] = arr[from * w + c];
115139
}
116-
for (const arr of [this.size, this.life, this.max, this.grow, this.size0, this.grav, this.drag, this.alpha0, this.cool]) arr[to] = arr[from];
140+
for (const arr of [this.size, this.life, this.max, this.grow, this.size0, this.grav, this.drag, this.alpha0, this.cool, this.seed]) arr[to] = arr[from];
117141
}
118142

119143
clear() { this.n = 0; this.geo.setDrawRange(0, 0); }
@@ -162,10 +186,6 @@ export class Effects {
162186
this.rings = [];
163187
this.ringGeo = new THREE.RingGeometry(0.93, 1, 64).rotateX(-Math.PI / 2);
164188

165-
// Scorch marks left by destroyed buildings.
166-
this.scorchTex = radialTexture([[0, 'rgba(10,8,6,0.85)'], [0.55, 'rgba(25,18,12,0.55)'], [1, 'rgba(0,0,0,0)']]);
167-
this.scorches = [];
168-
169189
// One light for explosions, always present so materials never recompile when it fires.
170190
this.flash = new THREE.PointLight('#ffb070', 0, 9, 1.6);
171191
scene.add(this.flash);
@@ -281,20 +301,8 @@ export class Effects {
281301
this.rings.push({ mesh, radius, life, max: life, alpha });
282302
}
283303

284-
scorch(p, r) {
285-
const mesh = new THREE.Mesh(new THREE.PlaneGeometry(2 * r, 2 * r).rotateX(-Math.PI / 2),
286-
new THREE.MeshBasicMaterial({ map: this.scorchTex, transparent: true, depthWrite: false, polygonOffset: true, polygonOffsetFactor: -2 }));
287-
mesh.position.set(p.x, p.y + 0.02, p.z);
288-
mesh.rotation.y = Math.random() * Math.PI;
289-
mesh.renderOrder = 1;
290-
this.scene.add(mesh);
291-
this.scorches.push(mesh);
292-
// Siege shells leave a lot of these: past a limit the oldest fade from the record.
293-
if (this.scorches.length > 90) {
294-
const old = this.scorches.shift();
295-
this.scene.remove(old); old.geometry.dispose(); old.material.dispose();
296-
}
297-
}
304+
/** Burned ground, kept for the rest of the match (groundmarks.js). */
305+
scorch(p, r) { this.terrain.marks.scorch(p.x, p.z, r * 0.85); }
298306

299307
// ---------------------------------------------------------------- projectiles
300308

@@ -558,8 +566,7 @@ export class Effects {
558566
this.emitters.length = 0;
559567
this.rings.forEach((r) => { this.scene.remove(r.mesh); r.mesh.material.dispose(); });
560568
this.rings.length = 0;
561-
this.scorches.forEach((s) => { this.scene.remove(s); s.geometry.dispose(); s.material.dispose(); });
562-
this.scorches.length = 0;
569+
this.terrain.marks.clear();
563570
this.flash.intensity = 0;
564571
this.shake = 0;
565572
}
@@ -572,19 +579,5 @@ export class Effects {
572579
o.material?.dispose();
573580
}
574581
this.ringGeo.dispose();
575-
this.scorchTex.dispose();
576582
}
577583
}
578-
579-
function radialTexture(stops) {
580-
const c = document.createElement('canvas');
581-
c.width = c.height = 128;
582-
const g = c.getContext('2d');
583-
const gr = g.createRadialGradient(64, 64, 0, 64, 64, 64);
584-
for (const [o, s] of stops) gr.addColorStop(o, s);
585-
g.fillStyle = gr;
586-
g.fillRect(0, 0, 128, 128);
587-
const t = new THREE.CanvasTexture(c);
588-
t.colorSpace = THREE.SRGBColorSpace;
589-
return t;
590-
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// 3D match view: grass on the valley floor. Tufts of a few blades, thick in the meadows and thin on dry
2+
// ground, swaying in the wind. They read the ground marks (groundmarks.js): where units have worn a path
3+
// the grass is trodden flat, and where fire has been it's gone.
4+
import * as THREE from 'three';
5+
import { mergeGeometries } from 'three/addons/utils/BufferGeometryUtils.js';
6+
import { fbm, rng, smoothstep } from './noise.js';
7+
8+
const MAX_TUFTS = 70000;
9+
10+
/** One tuft: five blades leaning out from the middle, dark at the root and light at the tip. */
11+
function tuftGeometry() {
12+
const blades = [];
13+
for (let i = 0; i < 5; i++) {
14+
const a = (i / 5) * Math.PI * 2 + i * 0.7;
15+
const lean = 0.04 + (i % 3) * 0.02, h = 0.16 + (i % 2) * 0.06, w = 0.026;
16+
const ox = Math.cos(a) * 0.035, oz = Math.sin(a) * 0.035;
17+
const px = Math.cos(a + Math.PI / 2) * w, pz = Math.sin(a + Math.PI / 2) * w;
18+
const g = new THREE.BufferGeometry();
19+
g.setAttribute('position', new THREE.BufferAttribute(new Float32Array([
20+
ox - px, 0, oz - pz, ox + px, 0, oz + pz, ox + Math.cos(a) * lean, h, oz + Math.sin(a) * lean]), 3));
21+
g.setAttribute('color', new THREE.BufferAttribute(new Float32Array([0.62, 0.62, 0.6, 0.62, 0.62, 0.6, 1.2, 1.2, 1.05]), 3));
22+
// Every blade faces the sky, so a tuft takes the same light as the ground it grows from.
23+
g.setAttribute('normal', new THREE.BufferAttribute(new Float32Array([0, 1, 0, 0, 1, 0, 0, 1, 0]), 3));
24+
blades.push(g);
25+
}
26+
return mergeGeometries(blades);
27+
}
28+
29+
/** The grass for a terrain: one instanced mesh (hidden at low quality by the view). */
30+
export function buildGrass(terrain) {
31+
const { W, H, seed } = terrain;
32+
const R = rng(seed * 104729 + 7);
33+
const starts = terrain.map.startPositions || [];
34+
const items = [];
35+
for (let z = -4; z < H + 4 && items.length < MAX_TUFTS; z += 0.34) {
36+
for (let x = -4; x < W + 4 && items.length < MAX_TUFTS; x += 0.34) {
37+
const jx = x + (R() - 0.5) * 0.3, jz = z + (R() - 0.5) * 0.3;
38+
if (terrain.outside(jx, jz) > 3) continue;
39+
const h = terrain.heightAt(jx, jz);
40+
if (terrain.lastCliff > 0.04) continue;
41+
// Meadows and bare patches; a little thinner where bases start, so the yards read clearly.
42+
const meadow = fbm(jx * 0.11, jz * 0.11, seed + 77);
43+
const dry = smoothstep(0.62, 0.85, fbm(jx * 0.06 + 40, jz * 0.06, seed + 41));
44+
let dens = smoothstep(0.3, 0.6, meadow) * 0.85 + 0.15 - dry * 0.5;
45+
if (starts.some((p) => Math.hypot(p.x + 0.5 - jx, p.y + 0.5 - jz) < 2.5)) dens *= 0.35;
46+
if (R() > dens) continue;
47+
items.push({ x: jx, y: h, z: jz, s: 0.7 + R() * 0.7, r: R() * Math.PI * 2, meadow, dry });
48+
}
49+
}
50+
51+
const geo = tuftGeometry();
52+
const mat = new THREE.MeshStandardMaterial({ vertexColors: true, roughness: 1, metalness: 0, side: THREE.DoubleSide });
53+
const u = terrain.uniforms;
54+
mat.onBeforeCompile = (sh) => {
55+
Object.assign(sh.uniforms, { uMarks: u.uMarks, uMapSize: u.uMapSize, uTime: u.uTime });
56+
sh.vertexShader = sh.vertexShader
57+
.replace('#include <common>', '#include <common>\nuniform sampler2D uMarks; uniform vec2 uMapSize; uniform float uTime;')
58+
.replace('#include <begin_vertex>', `#include <begin_vertex>
59+
{
60+
vec4 root = instanceMatrix * vec4(0.0, 0.0, 0.0, 1.0);
61+
vec2 m = root.xz / uMapSize;
62+
float inMap = step(0.0, m.x) * step(0.0, m.y) * step(m.x, 1.0) * step(m.y, 1.0);
63+
vec2 mk = texture2D(uMarks, m).rg * inMap;
64+
// Trodden flat on a path; burned away.
65+
float keep = (1.0 - smoothstep(0.12, 0.55, mk.r)) * (1.0 - smoothstep(0.05, 0.3, mk.g));
66+
transformed.y *= keep;
67+
float sway = sin(uTime * 1.5 + root.x * 0.7 + root.z * 0.45) + 0.45 * sin(uTime * 2.6 + root.x * 1.9 - root.z * 0.8);
68+
transformed.xz += vec2(0.7, 0.35) * sway * 0.12 * position.y * keep;
69+
}`);
70+
};
71+
mat.customProgramCacheKey = () => 'netrts-grass';
72+
73+
const mesh = new THREE.InstancedMesh(geo, mat, Math.max(1, items.length));
74+
mesh.count = items.length;
75+
const m = new THREE.Matrix4(), q = new THREE.Quaternion(), v = new THREE.Vector3(), s = new THREE.Vector3(), up = new THREE.Vector3(0, 1, 0);
76+
const c = new THREE.Color(), lush = new THREE.Color('#4f7a2c'), fresh = new THREE.Color('#7da343'), straw = new THREE.Color('#a39a58');
77+
items.forEach((it, i) => {
78+
m.compose(v.set(it.x, it.y - 0.01, it.z), q.setFromAxisAngle(up, it.r), s.set(it.s, it.s * (0.8 + it.meadow * 0.6), it.s));
79+
mesh.setMatrixAt(i, m);
80+
c.copy(lush).lerp(fresh, smoothstep(0.35, 0.7, it.meadow)).lerp(straw, it.dry * 0.8);
81+
mesh.setColorAt(i, c);
82+
});
83+
mesh.receiveShadow = true;
84+
mesh.castShadow = false;
85+
mesh.frustumCulled = false;
86+
mesh.name = 'grass';
87+
return mesh;
88+
}

0 commit comments

Comments
 (0)