Spaces:
Running
Running
v1.33.27 — planet terrain themes, full-map fog, outer-range normals, crater flooding, balance
Browse files- src/engine/terrain.js +81 -3
src/engine/terrain.js
CHANGED
|
@@ -35,6 +35,14 @@ const SEABED=-26; // floor the ocean bottom so water has dept
|
|
| 35 |
|
| 36 |
let terrMesh=null, terrVerts=null, terrVAO=null, terrVBO=null, terrIBO=null, terrIdxCount=0;
|
| 37 |
let waterMesh=null, waterVAO=null, waterVBO=null, waterIBO=null, waterIdxCount=0, waterVerts=null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
/* The playable heightfield still ends at MAP, but the camera is deliberately
|
| 39 |
allowed to overhang it so a corner base can remain centred while the player
|
| 40 |
rotates. A cheap low-density skirt gives that overhang real depth instead
|
|
@@ -140,7 +148,7 @@ function terrColor(h,slope,TH,wx,wy,out){
|
|
| 140 |
const gx=clamp(wx/MAP*PGS|0,0,PGS-1), gy=clamp(wy/MAP*PGS|0,0,PGS-1);
|
| 141 |
if(ROADG[gy*PGS+gx]){
|
| 142 |
const rc=curTheme==='ashland'?[65,56,52]:curTheme==='arctic'?[91,104,113]:
|
| 143 |
-
curTheme==='vespera'?[
|
| 144 |
c=LP(c,rc,0.72);
|
| 145 |
}
|
| 146 |
}
|
|
@@ -234,8 +242,28 @@ function buildTerrainEdgeMesh(TH){
|
|
| 234 |
const axis=(a,b,n)=>Array.from({length:n+1},(_,i)=>a+(b-a)*i/n);
|
| 235 |
const along=axis(-TERR_EDGE_EXT,MAP+TERR_EDGE_EXT,72);
|
| 236 |
const middle=axis(0,MAP,48);
|
| 237 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 238 |
const smooth=q=>q*q*(3-2*q);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 239 |
const addRect=(xs,zs)=>{
|
| 240 |
const base=verts.length/12, nx=xs.length;
|
| 241 |
for(const z of zs) for(const x of xs){
|
|
@@ -257,7 +285,10 @@ function buildTerrainEdgeMesh(TH){
|
|
| 257 |
const cr=cc[0]+(peakCol[0]-cc[0])*pk+(capCol[0]-peakCol[0])*cap*pk;
|
| 258 |
const cg=cc[1]+(peakCol[1]-cc[1])*pk+(capCol[1]-peakCol[1])*cap*pk;
|
| 259 |
const cb=cc[2]+(peakCol[2]-cc[2])*pk+(capCol[2]-peakCol[2])*cap*pk;
|
| 260 |
-
|
|
|
|
|
|
|
|
|
|
| 261 |
x*.035,z*.035,MAT.EARTH);
|
| 262 |
}
|
| 263 |
for(let z=0;z<zs.length-1;z++) for(let x=0;x<xs.length-1;x++){
|
|
@@ -373,6 +404,27 @@ function terrainDirty(wx,wy,rad){
|
|
| 373 |
crater and foundation cut with a small sub-upload, never a full rebuild. */
|
| 374 |
const k=TS/MAP, hp=6;
|
| 375 |
uploadHeightTex((wx-rad)*k-hp,(wy-rad)*k-hp,(wx+rad)*k+hp,(wy+rad)*k+hp);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 376 |
}
|
| 377 |
|
| 378 |
/* ---------- WATER -----------------------------------------------------------
|
|
@@ -395,6 +447,7 @@ function buildWaterMesh(TH){
|
|
| 395 |
if(!wet) continue;
|
| 396 |
for(const [dx,dz] of [[0,0],[1,0],[0,1],[1,1]]) need[(z+dz)*TVERT+(x+dx)]=1;
|
| 397 |
}
|
|
|
|
| 398 |
let vn=0;
|
| 399 |
const wc=TH.wShal, wd=TH.wDeep;
|
| 400 |
for(let z=0;z<TVERT;z++) for(let x=0;x<TVERT;x++){
|
|
@@ -448,8 +501,33 @@ function animateWater(t){
|
|
| 448 |
if(!waterVerts||!waterIdxCount) return;
|
| 449 |
wavePhase=t;
|
| 450 |
const n=waterVerts.length/12;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 451 |
for(let k=0;k<n;k++){
|
| 452 |
const o=k*12, x=waterVerts[o], z=waterVerts[o+2];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 453 |
const w1=Math.sin(x*0.021+t*1.15)*1.5, w2=Math.sin(z*0.017-t*0.86)*1.2;
|
| 454 |
const w3=Math.sin((x+z)*0.033+t*1.9)*0.7;
|
| 455 |
waterVerts[o+1]=WATER_Y+w1+w2+w3;
|
|
|
|
| 35 |
|
| 36 |
let terrMesh=null, terrVerts=null, terrVAO=null, terrVBO=null, terrIBO=null, terrIdxCount=0;
|
| 37 |
let waterMesh=null, waterVAO=null, waterVBO=null, waterIBO=null, waterIdxCount=0, waterVerts=null;
|
| 38 |
+
/* WATER HAS TO FOLLOW THE GROUND IT SITS IN. The water sheet is built once
|
| 39 |
+
from the heightfield, but deformTerrain rewrites that heightfield all match
|
| 40 |
+
long — craters, superweapon pits, singularity collapses. Nothing rebuilt the
|
| 41 |
+
sheet, so a crater blasted below sea level stayed a dry hole in the middle of
|
| 42 |
+
a lake. Keep the coverage map and the theme that built it, flag a mismatch
|
| 43 |
+
from terrainDirty (the single choke point every deform already passes
|
| 44 |
+
through), and rebuild on the maintenance tick rather than mid-explosion. */
|
| 45 |
+
let waterNeed=null, waterTH=null, waterDirty=false, waterRebuildT=0, waterBaseCol=null;
|
| 46 |
/* The playable heightfield still ends at MAP, but the camera is deliberately
|
| 47 |
allowed to overhang it so a corner base can remain centred while the player
|
| 48 |
rotates. A cheap low-density skirt gives that overhang real depth instead
|
|
|
|
| 148 |
const gx=clamp(wx/MAP*PGS|0,0,PGS-1), gy=clamp(wy/MAP*PGS|0,0,PGS-1);
|
| 149 |
if(ROADG[gy*PGS+gx]){
|
| 150 |
const rc=curTheme==='ashland'?[65,56,52]:curTheme==='arctic'?[91,104,113]:
|
| 151 |
+
curTheme==='vespera'?[78,62,72]:[73,76,66];
|
| 152 |
c=LP(c,rc,0.72);
|
| 153 |
}
|
| 154 |
}
|
|
|
|
| 242 |
const axis=(a,b,n)=>Array.from({length:n+1},(_,i)=>a+(b-a)*i/n);
|
| 243 |
const along=axis(-TERR_EDGE_EXT,MAP+TERR_EDGE_EXT,72);
|
| 244 |
const middle=axis(0,MAP,48);
|
| 245 |
+
/* THE SMEAR AXIS. Six rows carried the whole 960-unit skirt, so every
|
| 246 |
+
distance-driven band in the terrain shader (outer haze, exclusion zone,
|
| 247 |
+
foam/storm patterns) was reconstructed from 5 linear spans across a
|
| 248 |
+
strongly curved superellipse field — which is exactly what read as long
|
| 249 |
+
stretched stripes in the outer ranges. Eleven rows costs +2.9k triangles
|
| 250 |
+
against a 205k-triangle terrain (+1.4%, build-time only, zero fill cost)
|
| 251 |
+
and makes those contours curve properly. */
|
| 252 |
+
const bands=[0,60,130,230,330,470,610,700,800,880,TERR_EDGE_EXT];
|
| 253 |
const smooth=q=>q*q*(3-2*q);
|
| 254 |
+
/* One height function, used for the vertex AND for its normal. Every skirt
|
| 255 |
+
vertex previously shipped (0,1,0): a 255-unit ridged mountain range lit as
|
| 256 |
+
a flat plane, with no relief anywhere in the outer ranges. The per-pixel
|
| 257 |
+
normal path cannot rescue it either — outside 0..1 the height texture
|
| 258 |
+
clamps, so its central difference is identically zero along the outward
|
| 259 |
+
axis, which generates precisely the long streaks reported. */
|
| 260 |
+
const edgeH=(x,z)=>{
|
| 261 |
+
const outside=Math.max(0,-x,x-MAP,-z,z-MAP), q=clamp(outside/TERR_EDGE_EXT,0,1);
|
| 262 |
+
const bx=clamp(x,0,MAP), bz=clamp(z,0,MAP), join=clamp(outside/150,0,1);
|
| 263 |
+
const relief=(Math.sin(x*.0067+z*.0031)+Math.sin(z*.0083-x*.0027))*(1-q)*reliefAmp*join;
|
| 264 |
+
const build=smooth(clamp((outside-120)/(TERR_EDGE_EXT*0.82-120),0,1));
|
| 265 |
+
return terrainH(bx,bz)-smooth(q)*sink*(1-build)+relief+ridged(x,z)*peakAmp*build;
|
| 266 |
+
};
|
| 267 |
const addRect=(xs,zs)=>{
|
| 268 |
const base=verts.length/12, nx=xs.length;
|
| 269 |
for(const z of zs) for(const x of xs){
|
|
|
|
| 285 |
const cr=cc[0]+(peakCol[0]-cc[0])*pk+(capCol[0]-peakCol[0])*cap*pk;
|
| 286 |
const cg=cc[1]+(peakCol[1]-cc[1])*pk+(capCol[1]-peakCol[1])*cap*pk;
|
| 287 |
const cb=cc[2]+(peakCol[2]-cc[2])*pk+(capCol[2]-peakCol[2])*cap*pk;
|
| 288 |
+
const eps=32;
|
| 289 |
+
let nvx=edgeH(x-eps,z)-edgeH(x+eps,z), nvy=2*eps, nvz=edgeH(x,z-eps)-edgeH(x,z+eps);
|
| 290 |
+
const nl=Math.hypot(nvx,nvy,nvz)||1;
|
| 291 |
+
verts.push(x,h,z, nvx/nl,nvy/nl,nvz/nl, cr/255,cg/255,cb/255,
|
| 292 |
x*.035,z*.035,MAT.EARTH);
|
| 293 |
}
|
| 294 |
for(let z=0;z<zs.length-1;z++) for(let x=0;x<xs.length-1;x++){
|
|
|
|
| 404 |
crater and foundation cut with a small sub-upload, never a full rebuild. */
|
| 405 |
const k=TS/MAP, hp=6;
|
| 406 |
uploadHeightTex((wx-rad)*k-hp,(wy-rad)*k-hp,(wx+rad)*k+hp,(wy+rad)*k+hp);
|
| 407 |
+
/* Did this deform change what is underwater? Only the touched window is
|
| 408 |
+
scanned, and only against the coverage the current sheet was built from,
|
| 409 |
+
so ordinary shell craters on dry high ground cost four comparisons and
|
| 410 |
+
never trigger a rebuild. */
|
| 411 |
+
if(waterNeed&&!waterDirty){
|
| 412 |
+
const cx0=clamp(gx0,0,TGRID),cx1=clamp(gx1,0,TGRID),cz0=clamp(gz0,0,TGRID),cz1=clamp(gz1,0,TGRID);
|
| 413 |
+
for(let z=cz0;z<=cz1&&!waterDirty;z++) for(let x=cx0;x<=cx1;x++){
|
| 414 |
+
const wxv=x*cell, wyv=z*cell;
|
| 415 |
+
const hx=clamp(Math.round(wxv/MAP*(TS-1)),0,TS-1), hy=clamp(Math.round(wyv/MAP*(TS-1)),0,TS-1);
|
| 416 |
+
const wet=heightF[hy*TS+hx]<WATER_H+0.012?1:0;
|
| 417 |
+
if(wet!==waterNeed[z*TVERT+x]){ waterDirty=true; break; }
|
| 418 |
+
}
|
| 419 |
+
}
|
| 420 |
+
}
|
| 421 |
+
/* Rebuild the flooded sheet at most a few times a second: a bombardment that
|
| 422 |
+
opens twenty craters in one second pays for one rebuild, not twenty. */
|
| 423 |
+
function waterMaintain(dt){
|
| 424 |
+
if(waterRebuildT>0) waterRebuildT-=dt;
|
| 425 |
+
if(!waterDirty||waterRebuildT>0||!waterTH||typeof gl==='undefined'||!gl) return;
|
| 426 |
+
waterRebuildT=0.45;
|
| 427 |
+
buildWaterMesh(waterTH);
|
| 428 |
}
|
| 429 |
|
| 430 |
/* ---------- WATER -----------------------------------------------------------
|
|
|
|
| 447 |
if(!wet) continue;
|
| 448 |
for(const [dx,dz] of [[0,0],[1,0],[0,1],[1,1]]) need[(z+dz)*TVERT+(x+dx)]=1;
|
| 449 |
}
|
| 450 |
+
waterNeed=need; waterTH=TH; waterDirty=false; waterBaseCol=null;
|
| 451 |
let vn=0;
|
| 452 |
const wc=TH.wShal, wd=TH.wDeep;
|
| 453 |
for(let z=0;z<TVERT;z++) for(let x=0;x<TVERT;x++){
|
|
|
|
| 501 |
if(!waterVerts||!waterIdxCount) return;
|
| 502 |
wavePhase=t;
|
| 503 |
const n=waterVerts.length/12;
|
| 504 |
+
/* WATER OBEYS FOG OF WAR TOO. The sheet is drawn by the MODEL program, which
|
| 505 |
+
has no fog sampler, so an unscouted lake stayed fully legible inside the
|
| 506 |
+
black — you could read the whole coastline of a map you had never visited.
|
| 507 |
+
Colour is already rewritten here every few ticks, so the darkening rides
|
| 508 |
+
along for free instead of costing a new uniform, sampler or draw call.
|
| 509 |
+
Base colours are kept in waterBaseCol so the dimming can never compound. */
|
| 510 |
+
const fogging=(typeof fogGameplayActive==='function')&&fogGameplayActive()&&(typeof covAt==='function');
|
| 511 |
+
if(fogging&&(!waterBaseCol||waterBaseCol.length!==waterVerts.length)){
|
| 512 |
+
waterBaseCol=new Float32Array(waterVerts.length);
|
| 513 |
+
waterBaseCol.set(waterVerts);
|
| 514 |
+
}
|
| 515 |
+
/* Fog turning OFF has to give the colour back. Without this the last dimmed
|
| 516 |
+
values simply stayed, so revealing the map (or a replay/observer view) left
|
| 517 |
+
every previously-unscouted lake rendered as a black hole in the ground. */
|
| 518 |
+
if(!fogging&&waterBaseCol){
|
| 519 |
+
for(let k=0;k<n;k++){ const o=k*12;
|
| 520 |
+
waterVerts[o+6]=waterBaseCol[o+6]; waterVerts[o+7]=waterBaseCol[o+7]; waterVerts[o+8]=waterBaseCol[o+8]; }
|
| 521 |
+
waterBaseCol=null;
|
| 522 |
+
}
|
| 523 |
for(let k=0;k<n;k++){
|
| 524 |
const o=k*12, x=waterVerts[o], z=waterVerts[o+2];
|
| 525 |
+
if(fogging){
|
| 526 |
+
const seen=covAt(x,z)?1:(fogExploredAt(x,z)?0.34:0.045);
|
| 527 |
+
waterVerts[o+6]=waterBaseCol[o+6]*seen;
|
| 528 |
+
waterVerts[o+7]=waterBaseCol[o+7]*seen;
|
| 529 |
+
waterVerts[o+8]=waterBaseCol[o+8]*seen;
|
| 530 |
+
}
|
| 531 |
const w1=Math.sin(x*0.021+t*1.15)*1.5, w2=Math.sin(z*0.017-t*0.86)*1.2;
|
| 532 |
const w3=Math.sin((x+z)*0.033+t*1.9)*0.7;
|
| 533 |
waterVerts[o+1]=WATER_Y+w1+w2+w3;
|