@margoengine/terrain 0.1.0 → 1.0.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 +201 -201
- package/NOTICE +4 -4
- package/README.md +2367 -1808
- package/dist/bed-mesh.d.ts +40 -0
- package/dist/bed-mesh.js +95 -0
- package/dist/blend.js +4 -4
- package/dist/colors.d.ts +6 -0
- package/dist/deform.d.ts +41 -10
- package/dist/deform.js +169 -87
- package/dist/displace.js +4 -4
- package/dist/elevation.d.ts +182 -0
- package/dist/elevation.js +155 -0
- package/dist/far.d.ts +66 -0
- package/dist/far.js +191 -0
- package/dist/furrows.d.ts +74 -0
- package/dist/furrows.js +157 -0
- package/dist/grid.d.ts +6 -3
- package/dist/ground-change.d.ts +1 -1
- package/dist/ground-change.js +232 -111
- package/dist/ground-contact-settings.d.ts +27 -0
- package/dist/ground-contact-settings.js +22 -0
- package/dist/ground-contact.d.ts +91 -0
- package/dist/ground-contact.js +241 -0
- package/dist/ground-model.d.ts +83 -1
- package/dist/imagery.js +4 -4
- package/dist/index.d.ts +120 -9
- package/dist/index.js +1 -1
- package/dist/layers-pbr.js +9 -9
- package/dist/overlays.js +4 -4
- package/dist/planet-complex.d.ts +31 -0
- package/dist/planet-craters.d.ts +28 -3
- package/dist/planet-rays.d.ts +37 -0
- package/dist/planet-recipes.d.ts +10 -1
- package/dist/planet-recipes.js +341 -115
- package/dist/planet.d.ts +5 -0
- package/dist/planet.js +112 -104
- package/dist/relief-lighting.d.ts +7 -0
- package/dist/relief-lighting.js +36 -23
- package/dist/rock-surface.d.ts +3 -0
- package/dist/rock-surface.js +28 -16
- package/dist/rvt.js +5 -5
- package/dist/scour.d.ts +90 -0
- package/dist/scour.js +153 -0
- package/dist/sculpt.js +4 -4
- package/dist/shader.d.ts +120 -5
- package/dist/shared/settings.d.ts +89 -89
- package/dist/{src-C6dtSaAb.js → src-Cr7Gm4n0.js} +359 -148
- package/dist/stream-window.d.ts +123 -0
- package/dist/stream.d.ts +95 -26
- package/dist/stream.js +440 -248
- package/dist/wet-ground.js +4 -4
- package/package.json +175 -148
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
// @margo/core-built
|
|
1
|
+
// @margo/core-built 1.0.0
|
|
2
2
|
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
-
const l=(engine,version="
|
|
3
|
+
const l=(engine,version="1.0.0",source=import.meta.url)=>__margoRuntime(engine,version,source);
|
|
4
4
|
import { t as e } from "./lighting-layout-CaDtDa5G.js";
|
|
5
5
|
import { t } from "./errors-fc4i1pSL.js";
|
|
6
6
|
import { n, o as r, r as i, t as a } from "./hdr-format-DRhzGS7G.js";
|
|
@@ -14,21 +14,111 @@ import { computeVertexNormals as m, toGeometry as h } from "@margoengine/geometr
|
|
|
14
14
|
//#region src/grid.ts
|
|
15
15
|
function g(e) {
|
|
16
16
|
if (!Number.isInteger(e) || e < 8 || e > 256 || e % 4) throw RangeError("Clipmap resolution must be a multiple of four in 8..256");
|
|
17
|
-
let t = e + 1, n = new Float32Array(
|
|
18
|
-
for (let t = 0,
|
|
19
|
-
for (let
|
|
20
|
-
|
|
21
|
-
|
|
17
|
+
let t = e + 1, n = t ** 2, r = new Float32Array((n + e ** 2) * 2), i = new Uint32Array(e ** 2 * 12), a = new Uint32Array(e ** 2 * 9);
|
|
18
|
+
for (let t = 0, n = 0; t <= e; t++) for (let i = 0; i <= e; i++) r[n++] = i - e / 2, r[n++] = t - e / 2;
|
|
19
|
+
for (let t = 0, i = n * 2; t < e; t++) for (let n = 0; n < e; n++) r[i++] = n + .5 - e / 2, r[i++] = t + .5 - e / 2;
|
|
20
|
+
for (let r = 0, o = 0, s = 0; r < e; r++) for (let c = 0; c < e; c++) {
|
|
21
|
+
let l = r * t + c, u = l + 1, d = l + t, f = d + 1, p = n + r * e + c, m = [
|
|
22
|
+
l,
|
|
23
|
+
d,
|
|
24
|
+
p,
|
|
25
|
+
d,
|
|
26
|
+
f,
|
|
27
|
+
p,
|
|
28
|
+
f,
|
|
29
|
+
u,
|
|
30
|
+
p,
|
|
31
|
+
u,
|
|
32
|
+
l,
|
|
33
|
+
p
|
|
34
|
+
];
|
|
35
|
+
i.set(m, o), o += 12, (c < e / 4 || c >= e * 3 / 4 || r < e / 4 || r >= e * 3 / 4) && (a.set(m, s), s += 12);
|
|
22
36
|
}
|
|
23
37
|
return {
|
|
24
|
-
vertices:
|
|
25
|
-
center:
|
|
26
|
-
ring:
|
|
38
|
+
vertices: r,
|
|
39
|
+
center: i,
|
|
40
|
+
ring: a
|
|
27
41
|
};
|
|
28
42
|
}
|
|
29
43
|
//#endregion
|
|
30
44
|
//#region ../shared/light-terms.js
|
|
31
|
-
var _ = "a2/(3.14159265*pow(max(dot(cross(n,h),cross(n,h)),step(nh,0.0))+a2*nh*nh,2.0))", v = "2*nl*nv/max(nl*sqrt(a2+(1-a2)*nv*nv)+nv*sqrt(a2+(1-a2)*nl*nl),1e-30)", y = "\nstruct Frame {camera:mat4x4f,eye:vec4f,light:vec4f,radiance:vec4f,grid:vec4f,offset:vec4f,map:vec4f,heights:vec4f}\nstruct Layer {heightSlope:vec4f,params:vec4f}\n@group(0) @binding(0) var<uniform> frame:Frame;\n@group(0) @binding(1) var heightMap:texture_2d<f32>;\n@group(0) @binding(2) var normalMap:texture_2d<f32>;\n@group(0) @binding(3) var terrainLayers:texture_2d_array<f32>;\n@group(0) @binding(4) var terrainSampler:sampler;\n@group(0) @binding(5) var<storage,read> layerRules:array<Layer>;\nfn height(position:vec2f)->f32 {\n let dimensions=vec2i(textureDimensions(heightMap));let uv=clamp((position-frame.map.xy)/frame.map.zw,vec2f(0),vec2f(1));let pixel=uv*vec2f(dimensions-vec2i(1));let lo=vec2i(floor(pixel));let hi=min(lo+vec2i(1),dimensions-vec2i(1));let t=fract(pixel);\n let a=mix(textureLoad(heightMap,lo,0).r,textureLoad(heightMap,vec2i(hi.x,lo.y),0).r,t.x);let b=mix(textureLoad(heightMap,vec2i(lo.x,hi.y),0).r,textureLoad(heightMap,hi,0).r,t.x);\n return mix(a,b,t.y)*frame.heights.x+frame.heights.y;\n}\nstruct Output {@builtin(position) @invariant clip:vec4f,@location(0) position:vec3f,@location(1) normal:vec3f,@location(2) color:vec3f,@location(3) material:vec4f}\n@vertex fn vertex(@location(0) grid:vec2f)->Output {\n let edge=max(abs(grid.x),abs(grid.y));let morph=clamp((edge-frame.grid.w*0.5+2)/2,0.0,1.0);\n let aligned=mix(grid,round(grid*0.5)*2,morph);let inner=clamp((frame.grid.w*0.5-edge)/(frame.grid.w*0.25),0.0,1.0);\n let xz=frame.grid.xy+aligned*frame.grid.z+frame.offset.xy*inner;let position=vec3f(xz.x,height(xz),xz.y);\n let step=frame.map.zw/max(vec2f(textureDimensions(heightMap))-1,vec2f(1));\n var normal=normalize(vec3f((height(xz-vec2f(step.x,0))-height(xz+vec2f(step.x,0)))/(2*step.x),1,(height(xz-vec2f(0,step.y))-height(xz+vec2f(0,step.y)))/(2*step.y)));\n if(frame.offset.w>0.5){let dimensions=vec2i(textureDimensions(normalMap));let uv=clamp((xz-frame.map.xy)/frame.map.zw,vec2f(0),vec2f(1));let sampled=textureLoad(normalMap,min(vec2i(uv*vec2f(dimensions)),dimensions-vec2i(1)),0).xyz*2-1;if(dot(sampled,sampled)>0.00001){normal=normalize(sampled);}}\n var o:Output;o.position=position;o.clip=frame.camera*vec4f(position,1);o.normal=normal;o.color=vec3f(1);o.material=vec4f(0.9,0,1,1);return o;\n}\n", b = "{let hd=vec2i(textureDimensions(terrainHoles));if(textureLoad(terrainHoles,clamp(vec2i(floor((i.position.xz-frame.map.xy)/frame.map.zw*vec2f(hd))),vec2i(0),hd-vec2i(1)),0).r<0.5){discard;}}", x = "if(any(i.position.xz<frame.map.xy)||any(i.position.xz>frame.map.xy+frame.map.zw)){discard;}", S = (e) => e.replace("struct Layer {heightSlope:vec4f,params:vec4f}", "struct Layer {heightSlope:vec4f,params:vec4f,surface:vec4f}"), C = (e) => e.includes("anchor:vec4f}") ? e : e.replace("heights:vec4f}", "heights:vec4f,anchor:vec4f}").replace(/struct Layer \{([^}]*)\}/, "struct Layer {$1,wrap:vec4f,tone:vec4f}").split("let p=position*scale;").join("let p=(position+layerRules[layer].wrap.xyz)*scale;").replace("let p=position*m;let q=position*m*0.302;", "let p=(position+layerRules[layer].wrap.xyz)*layerRules[layer].params.x/24.0;let q=p*0.302+layerRules[layer].tone.xyz;"), w = "\nvar<private> terrainRelief:f32;var<private> terrainRepeat:f32;\nfn terrainMacro(p:vec2f,q:vec2f,dx:vec2f,dy:vec2f,m:f32,layer:u32,top:f32,norm:f32)->f32 {\n let luma=vec3f(0.2126,0.7152,0.0722);let w=dot(textureSampleLevel(terrainLayers,terrainSampler,q,i32(layer),max(top-3.0,0.0)).rgb,luma)*norm;\n let k=w*16.0;let i=floor(k);let t=smoothstep(0.2,0.8,k-i);\n let a=textureSampleGrad(terrainLayers,terrainSampler,p+sin(vec2f(3,7)*i),i32(layer),dx*m,dy*m).rgb;\n let b=textureSampleGrad(terrainLayers,terrainSampler,p+sin(vec2f(3,7)*(i+1.0)),i32(layer),dx*m,dy*m).rgb;\n return dot(mix(a,b,t),luma)*norm*w;\n}\nfn terrainSurface(sample:vec3f,position:vec3f,projection:vec3f,dx:vec3f,dy:vec3f,layer:u32)->vec4f {\n let s=layerRules[layer].surface;let luma=vec3f(0.2126,0.7152,0.0722);var color=sample;\n if(s.y>0.0){\n let top=f32(textureNumLevels(terrainLayers)-1u);let norm=1.0/max(dot(textureSampleLevel(terrainLayers,terrainSampler,vec2f(0.5),i32(layer),top).rgb,luma),0.0001);\n let m=s.z;let p=position*m;let q=position*m*0.302;var tone=0.0;\n if(projection.x>0.01){tone+=projection.x*terrainMacro(p.zy,q.yz,dx.zy,dy.zy,m,layer,top,norm);}\n if(projection.y>0.01){tone+=projection.y*terrainMacro(p.xz,q.zx,dx.xz,dy.xz,m,layer,top,norm);}\n if(projection.z>0.01){tone+=projection.z*terrainMacro(p.xy,q.yx,dx.xy,dy.xy,m,layer,top,norm);}\n color*=max(1.0-s.y+s.y*tone,0.0);\n }\n return vec4f(color,dot(sample,luma)*s.x);\n}\nfn terrainBump(position:vec3f,normal:vec3f)->vec3f {\n let px=dpdxFine(position);let py=dpdyFine(position);let ax=1.0/max(length(px),1e-20);let ay=1.0/max(length(py),1e-20);\n let dx=px*ax;let dy=py*ay;let r1=cross(dy,normal);let r2=cross(normal,dx);let det=dot(dx,r1);\n let g=sign(det)*(dpdxFine(terrainRelief)*ax*r1+dpdyFine(terrainRelief)*ay*r2)*min(1.0,1.0/max(64.0*terrainRepeat*max(length(px),length(py)),1e-20));\n let bumped=abs(det)*normal-g*min(1.0,min(1.0,4.0*abs(det))*abs(det)/max(length(g),1e-20));\n return select(normal,normalize(bumped),dot(bumped,bumped)>0.0);\n}", T = (e) => e ? "let terrainN=normalize(i.normal);let base=terrainColor(i.position,terrainN);let n=terrainBump(i.position,terrainN);" : "let n=normalize(i.normal);let base=terrainColor(i.position,n);", E = (e = !1) => `${r}${e ? w : ""}
|
|
45
|
+
var _ = "a2/(3.14159265*pow(max(dot(cross(n,h),cross(n,h)),step(nh,0.0))+a2*nh*nh,2.0))", v = "2*nl*nv/max(nl*sqrt(a2+(1-a2)*nv*nv)+nv*sqrt(a2+(1-a2)*nl*nl),1e-30)", y = "\nstruct Frame {camera:mat4x4f,eye:vec4f,light:vec4f,radiance:vec4f,grid:vec4f,offset:vec4f,map:vec4f,heights:vec4f,ambient:vec4f}\nstruct Layer {heightSlope:vec4f,params:vec4f}\n@group(0) @binding(0) var<uniform> frame:Frame;\n@group(0) @binding(1) var heightMap:texture_2d<f32>;\n@group(0) @binding(2) var normalMap:texture_2d<f32>;\n@group(0) @binding(3) var terrainLayers:texture_2d_array<f32>;\n@group(0) @binding(4) var terrainSampler:sampler;\n@group(0) @binding(5) var<storage,read> layerRules:array<Layer>;\nfn height(position:vec2f)->f32 {\n let dimensions=vec2i(textureDimensions(heightMap));let uv=clamp((position-frame.map.xy)/frame.map.zw,vec2f(0),vec2f(1));let pixel=uv*vec2f(dimensions-vec2i(1));let lo=vec2i(floor(pixel));let hi=min(lo+vec2i(1),dimensions-vec2i(1));let t=fract(pixel);\n let a=mix(textureLoad(heightMap,lo,0).r,textureLoad(heightMap,vec2i(hi.x,lo.y),0).r,t.x);let b=mix(textureLoad(heightMap,vec2i(lo.x,hi.y),0).r,textureLoad(heightMap,hi,0).r,t.x);\n return mix(a,b,t.y)*frame.heights.x+frame.heights.y;\n}\nfn terrainCatmull(t:f32)->vec4f {let t2=t*t;let t3=t2*t;return vec4f(-0.5*t3+t2-0.5*t,1.5*t3-2.5*t2+1.0,-1.5*t3+2.0*t2+0.5*t,0.5*t3-0.5*t2);}\nfn terrainCubic(position:vec2f)->f32 {\n let dimensions=vec2i(textureDimensions(heightMap));let uv=clamp((position-frame.map.xy)/frame.map.zw,vec2f(0),vec2f(1));let pixel=uv*vec2f(dimensions-vec2i(1));let lo=vec2i(floor(pixel));\n let wx=terrainCatmull(fract(pixel.x));let wy=terrainCatmull(fract(pixel.y));var h=0.0;\n for(var j=0;j<4;j++){for(var k=0;k<4;k++){h+=wx[k]*wy[j]*textureLoad(heightMap,clamp(lo+vec2i(k-1,j-1),vec2i(0),dimensions-vec2i(1)),0).r;}}\n return h*frame.heights.x+frame.heights.y;\n}\nfn terrainMorph(g:vec2f)->vec3f {\n let edge=max(abs(g.x),abs(g.y));let morph=clamp((edge-frame.grid.w*0.5+2)/2,0.0,1.0);\n return vec3f(mix(g,round(g*0.5)*2,morph),select(0.0,1.0,edge<=frame.grid.w*0.25));\n}\nfn terrainMorphXZ(m:vec3f)->vec2f {return frame.grid.xy+m.xy*frame.grid.z+frame.offset.xy*m.z;}\nstruct Output {@builtin(position) @invariant clip:vec4f,@location(0) position:vec3f,@location(1) normal:vec3f,@location(2) color:vec3f,@location(3) material:vec4f}\n@vertex fn vertex(@location(0) grid:vec2f)->Output {\n var m=terrainMorph(grid);var lift=0.0;let centre=fract(grid.x)!=0.0;\n if(centre){\n let a=terrainMorph(grid-vec2f(0.5));let b=terrainMorph(grid+vec2f(0.5,-0.5));let c=terrainMorph(grid+vec2f(-0.5,0.5));let d=terrainMorph(grid+vec2f(0.5));\n let ad=(height(terrainMorphXZ(a))+height(terrainMorphXZ(d)))*0.5;let bc=(height(terrainMorphXZ(b))+height(terrainMorphXZ(c)))*0.5;\n let r=terrainCubic(terrainMorphXZ((a+b+c+d)*0.25));let along=abs(r-ad)<abs(r-bc);\n m=select((b+c)*0.5,(a+d)*0.5,along);lift=select(bc,ad,along);\n }\n let aligned=m.xy;let inner=m.z;\n let xz=frame.grid.xy+aligned*frame.grid.z+frame.offset.xy*inner;let position=vec3f(xz.x,select(height(xz),lift,centre),xz.y);\n let step=frame.map.zw/max(vec2f(textureDimensions(heightMap))-1,vec2f(1));\n var normal=normalize(vec3f((height(xz-vec2f(step.x,0))-height(xz+vec2f(step.x,0)))/(2*step.x),1,(height(xz-vec2f(0,step.y))-height(xz+vec2f(0,step.y)))/(2*step.y)));\n if(frame.offset.w>0.5){let dimensions=vec2i(textureDimensions(normalMap));let uv=clamp((xz-frame.map.xy)/frame.map.zw,vec2f(0),vec2f(1));let sampled=textureLoad(normalMap,min(vec2i(uv*vec2f(dimensions)),dimensions-vec2i(1)),0).xyz*2-1;if(dot(sampled,sampled)>0.00001){normal=normalize(sampled);}}\n var o:Output;o.position=position;o.clip=frame.camera*vec4f(position,1);o.normal=normal;o.color=vec3f(1);o.material=vec4f(0.9,0,1,1);return o;\n}\n", b = "{let hd=vec2i(textureDimensions(terrainHoles));if(textureLoad(terrainHoles,clamp(vec2i(floor((i.position.xz-frame.map.xy)/frame.map.zw*vec2f(hd))),vec2i(0),hd-vec2i(1)),0).r<0.5){discard;}}", x = "if(any(i.position.xz<frame.map.xy)||any(i.position.xz>frame.map.xy+frame.map.zw)){discard;}", S = (e) => e.replace("struct Layer {heightSlope:vec4f,params:vec4f}", "struct Layer {heightSlope:vec4f,params:vec4f,surface:vec4f}"), C = "o.clip=frame.camera*vec4f(position,1);", w = "let terrainE=frame.view.xy+aligned*frame.grid.z+frame.offset.xy*inner;o.eye=vec3f(terrainE.x,position.y-frame.view.z,terrainE.y);o.clip=frame.camera*vec4f(o.eye,0)+frame.column;", T = (e) => e.includes("anchor:vec4f,view:vec4f") ? e : e.replace("ambient:vec4f}", "ambient:vec4f,anchor:vec4f,view:vec4f,column:vec4f}").replace(/struct Layer \{([^}]*)\}/, "struct Layer {$1,wrap:vec4f,tone:vec4f,eyeWrap:vec4f,eyeTone:vec4f}").replace("@location(3) material:vec4f}", "@location(3) material:vec4f,@location(12) eye:vec3f}").replace(C, w).replace("fn terrainRockP(p:vec3f)->vec3f {return p;}", "fn terrainRockP(p:vec3f)->vec3f {return p+vec3f(frame.anchor.x,frame.anchor.z,frame.anchor.y);}").replace("fn terrainPatchXZ(p:vec3f)->vec2f {return p.xz;}", "fn terrainPatchXZ(p:vec3f)->vec2f {return p.xz+frame.anchor.xy;}").split("let p=position*scale;").join("let p=(position+layerRules[layer].wrap.xyz)*scale;").replace("let p=position*m;let q=position*m*0.302;", "let p=(position+layerRules[layer].wrap.xyz)*layerRules[layer].params.x/24.0;let q=p*0.302+layerRules[layer].tone.xyz;"), E = "@fragment fn fragment(i:Output)->@location(0) vec4f {", D = "terrainEyeOn=true;terrainEyeAt=i.eye;terrainEyeLocal=i.position;", O = (e) => e.includes("var<private> terrainEyeOn:bool;") || !e.includes("eye:vec3f}") ? e : "var<private> terrainEyeOn:bool;var<private> terrainEyeAt:vec3f;var<private> terrainEyeLocal:vec3f;\nfn terrainTexAt(p:vec3f)->vec3f {return select(p,terrainEyeAt+(p-terrainEyeLocal),terrainEyeOn);}\nfn terrainTexPoint(p:vec3f,l:u32)->vec3f {return terrainTexAt(p)+select(layerRules[l].wrap.xyz,layerRules[l].eyeWrap.xyz,terrainEyeOn);}\nfn terrainToneOf(l:u32)->vec3f {return select(layerRules[l].tone.xyz,layerRules[l].eyeTone.xyz,terrainEyeOn);}\n" + e.replace(E, E + D).replace(/\(position\+layerRules\[layer\]\.wrap\.xyz\)/g, "terrainTexPoint(position,layer)").replace(/layerRules\[layer\]\.tone\.xyz/g, "terrainToneOf(layer)").replace(/(dpd[xy](Fine)?)\(position\)/g, "$1(terrainTexAt(position))"), k = "\nvar<private> terrainRelief:f32;var<private> terrainRepeat:f32;\nfn terrainMacro(p:vec2f,q:vec2f,dx:vec2f,dy:vec2f,m:f32,layer:u32,top:f32,norm:f32)->f32 {\n let luma=vec3f(0.2126,0.7152,0.0722);let w=dot(textureSampleLevel(terrainLayers,terrainSampler,q,i32(layer),max(top-3.0,0.0)).rgb,luma)*norm;\n let k=w*16.0;let i=floor(k);let t=smoothstep(0.2,0.8,k-i);\n let a=textureSampleGrad(terrainLayers,terrainSampler,p+sin(vec2f(3,7)*i),i32(layer),dx*m,dy*m).rgb;\n let b=textureSampleGrad(terrainLayers,terrainSampler,p+sin(vec2f(3,7)*(i+1.0)),i32(layer),dx*m,dy*m).rgb;\n return dot(mix(a,b,t),luma)*norm*w;\n}\nfn terrainReliefTap(p:vec2f,layer:u32,level:u32,size:i32)->f32 {\n let st=p*f32(size)-0.5;let i=floor(st);let f=st-i;let r=vec2f(1.0)-f;\n let a=r*r*r/6.0;let b=(3.0*f*f*f-6.0*f*f+4.0)/6.0;let d=f*f*f/6.0;let c=vec2f(1.0)-a-b-d;\n let wx=vec4f(a.x,b.x,c.x,d.x);let wy=vec4f(a.y,b.y,c.y,d.y);let first=vec2i(i)-1;var h=0.0;\n for(var y=0;y<4;y++){for(var x=0;x<4;x++){\n let t=((first+vec2i(x,y))%size+size)%size;\n h+=wx[x]*wy[y]*dot(textureLoad(terrainLayers,t,i32(layer),level).rgb,vec3f(0.2126,0.7152,0.0722));\n }}\n return h;\n}\nfn terrainReliefLuma(position:vec3f,projection:vec3f,layer:u32)->f32 {\n let scale=layerRules[layer].params.x;let p=position*scale;let texels=textureDimensions(terrainLayers).x;\n let level=u32(clamp(floor(log2(f32(texels)/128.0)),0.0,f32(textureNumLevels(terrainLayers)-1u)));let size=i32(texels>>level);\n var h=0.0;var total=0.0;\n if(projection.x>0.01){h+=projection.x*terrainReliefTap(p.zy,layer,level,size);total+=projection.x;}\n if(projection.y>0.01){h+=projection.y*terrainReliefTap(p.xz,layer,level,size);total+=projection.y;}\n if(projection.z>0.01){h+=projection.z*terrainReliefTap(p.xy,layer,level,size);total+=projection.z;}\n return h/max(total,0.00001);\n}\nfn terrainReliefTint(h:f32,layer:u32)->f32 {return h;}\nfn terrainSurface(sample:vec3f,position:vec3f,projection:vec3f,dx:vec3f,dy:vec3f,layer:u32)->vec4f {\n let s=layerRules[layer].surface;let luma=vec3f(0.2126,0.7152,0.0722);var color=sample;\n if(s.y>0.0){\n let top=f32(textureNumLevels(terrainLayers)-1u);let norm=1.0/max(dot(textureSampleLevel(terrainLayers,terrainSampler,vec2f(0.5),i32(layer),top).rgb,luma),0.0001);\n let m=s.z;let p=position*m;let q=position*m*0.302;var tone=0.0;\n if(projection.x>0.01){tone+=projection.x*terrainMacro(p.zy,q.yz,dx.zy,dy.zy,m,layer,top,norm);}\n if(projection.y>0.01){tone+=projection.y*terrainMacro(p.xz,q.zx,dx.xz,dy.xz,m,layer,top,norm);}\n if(projection.z>0.01){tone+=projection.z*terrainMacro(p.xy,q.yx,dx.xy,dy.xy,m,layer,top,norm);}\n color*=max(1.0-s.y+s.y*tone,0.0);\n }\n var h=dot(sample,luma);\n if(s.x>0.0){\n let near=1.0-smoothstep(1.0/128.0,1.0/64.0,layerRules[layer].params.x*max(length(dx),length(dy)));\n if(near>0.0){h=mix(h,terrainReliefTint(terrainReliefLuma(position,projection,layer),layer),near);}\n }\n return vec4f(color,h*s.x);\n}\nfn terrainBump(position:vec3f,normal:vec3f)->vec3f {\n let px=dpdxFine(position);let py=dpdyFine(position);let ax=1.0/max(length(px),1e-20);let ay=1.0/max(length(py),1e-20);\n let dx=px*ax;let dy=py*ay;let r1=cross(dy,normal);let r2=cross(normal,dx);let det=dot(dx,r1);\n let g=sign(det)*(dpdxFine(terrainRelief)*ax*r1+dpdyFine(terrainRelief)*ay*r2)*min(1.0,1.0/max(64.0*terrainRepeat*max(length(px),length(py)),1e-20));\n let bumped=abs(det)*normal-g*min(1.0,min(1.0,4.0*abs(det))*abs(det)/max(length(g),1e-20));\n return select(normal,normalize(bumped),dot(bumped,bumped)>0.0);\n}", A = (e, t = !1) => t ? e ? "let terrainN=terrainRock(i.position,normalize(i.normal));let base=terrainColor(i.position,normalize(i.normal))*terrainRockShade;let n=terrainBump(i.position,terrainN);" : "let n=terrainRock(i.position,normalize(i.normal));let base=terrainColor(i.position,normalize(i.normal))*terrainRockShade;" : e ? "let terrainN=normalize(i.normal);let base=terrainColor(i.position,terrainN);let n=terrainBump(i.position,terrainN);" : "let n=normalize(i.normal);let base=terrainColor(i.position,n);", j = (e) => {
|
|
46
|
+
let t = Math.fround(e).toPrecision(9);
|
|
47
|
+
return t.includes(".") || t.includes("e") ? t : t + ".0";
|
|
48
|
+
}, ee = (e) => {
|
|
49
|
+
let t = e.map >= 0;
|
|
50
|
+
return `
|
|
51
|
+
${t ? `@group(0) @binding(${e.map}) var terrainStructure:texture_2d<f32>;` : ""}
|
|
52
|
+
var<private> terrainRockShade:f32=1.0;var<private> terrainRockLoose:f32=0.0;
|
|
53
|
+
fn terrainRockP(p:vec3f)->vec3f {return p;}
|
|
54
|
+
fn terrainRockHash(c:vec3i)->vec3f {
|
|
55
|
+
var v=bitcast<vec3u>(c)*1664525u+1013904223u;
|
|
56
|
+
v.x+=v.y*v.z;v.y+=v.z*v.x;v.z+=v.x*v.y;v^=v>>vec3u(16u);v.x+=v.y*v.z;v.y+=v.z*v.x;v.z+=v.x*v.y;
|
|
57
|
+
return vec3f(v>>vec3u(8u))/16777216.0;
|
|
58
|
+
}
|
|
59
|
+
fn terrainRockNoise(p:vec2f,seed:i32)->f32 {
|
|
60
|
+
let i=floor(p);let f=p-i;let u=f*f*f*(f*(f*6.0-15.0)+10.0);let c=vec2i(i);
|
|
61
|
+
return mix(mix(terrainRockHash(vec3i(c,seed)).x,terrainRockHash(vec3i(c+vec2i(1,0),seed)).x,u.x),mix(terrainRockHash(vec3i(c+vec2i(0,1),seed)).x,terrainRockHash(vec3i(c+vec2i(1,1),seed)).x,u.x),u.y);
|
|
62
|
+
}
|
|
63
|
+
fn terrainRockFacets(p:vec3f,normal:vec3f,size:f32,fp:f32,steep:f32,weight:f32,seed:vec3i)->vec3f {
|
|
64
|
+
let q=p*vec3f(1.0,0.42,1.0)/size;let base=vec3i(floor(q));var d1=1e9;var d2=1e9;var id=vec3i(0);var id2=vec3i(0);
|
|
65
|
+
for(var j=0;j<27;j++){
|
|
66
|
+
let cell=base+vec3i(j%3-1,(j/3)%3-1,j/9-1);let d=distance(q,vec3f(cell)+terrainRockHash(cell+seed));
|
|
67
|
+
if(d<d1){d2=d1;id2=id;d1=d;id=cell+seed;}else if(d<d2){d2=d;id2=cell+seed;}
|
|
68
|
+
}
|
|
69
|
+
let r=fp/size;let near=steep*weight*mix(1.0,0.3,smoothstep(0.006,0.04,r))*(1.0-smoothstep(0.12,0.4,r));
|
|
70
|
+
let own=0.5+0.5*smoothstep(0.0,0.14+2.0*r,d2-d1);
|
|
71
|
+
let tilt=mix(terrainRockHash(id2+vec3i(17,-31,7)),terrainRockHash(id+vec3i(17,-31,7)),own)*2.0-1.0;
|
|
72
|
+
let joint=mix(1.0,0.82+0.18*smoothstep(0.0,max(0.05,2.0*r),d2-d1),1.0-smoothstep(0.006,0.04,r));
|
|
73
|
+
let tone=mix(terrainRockHash(id2).y,terrainRockHash(id).y,own);
|
|
74
|
+
terrainRockShade*=mix(1.0,joint*(1.0+${j(e.tone)}*(tone-0.5)),near);
|
|
75
|
+
return normalize(normal+${j(e.relief)}*near*(tilt-normal*dot(tilt,normal)));
|
|
76
|
+
}
|
|
77
|
+
fn terrainRock(position:vec3f,normal:vec3f)->vec3f {
|
|
78
|
+
terrainRockShade=1.0;terrainRockLoose=0.0;
|
|
79
|
+
let p=terrainRockP(position);let fp=max(max(length(dpdx(position)),length(dpdy(position))),0.0001);
|
|
80
|
+
let steep=smoothstep(${j(e.slope[0])},${j(e.slope[1])},degrees(acos(clamp(normal.y,0.0,1.0))));
|
|
81
|
+
var m=normal/max(normal.y,0.05);
|
|
82
|
+
${t ? ` let sd=vec2f(textureDimensions(terrainStructure));let e=frame.map.zw/sd;
|
|
83
|
+
let uv=clamp((position.xz-frame.map.xy)/frame.map.zw,vec2f(0.5)/sd,vec2f(1)-vec2f(0.5)/sd);
|
|
84
|
+
let c=textureSampleLevel(terrainStructure,terrainSampler,uv,0);let ex=vec2f(1.0/sd.x,0);let ez=vec2f(0,1.0/sd.y);
|
|
85
|
+
let gx=(textureSampleLevel(terrainStructure,terrainSampler,uv+ex,0).r-textureSampleLevel(terrainStructure,terrainSampler,uv-ex,0).r)/(2.0*e.x);
|
|
86
|
+
let gz=(textureSampleLevel(terrainStructure,terrainSampler,uv+ez,0).r-textureSampleLevel(terrainStructure,terrainSampler,uv-ez,0).r)/(2.0*e.y);
|
|
87
|
+
let depth=${j(e.gullies)}*mix(0.3,1.0,steep)*(1.0-smoothstep(0.25,0.6,fp/max(e.x,e.y)));
|
|
88
|
+
m.x+=depth*gx;m.z+=depth*gz;
|
|
89
|
+
terrainRockShade*=1.0-0.7*${j(e.tone)}*c.r*mix(0.4,1.0,steep);
|
|
90
|
+
terrainRockLoose=max(0.85*c.g,0.5*smoothstep(0.7,1.0,c.r)*(1.0-steep));
|
|
91
|
+
` : ""}${e.strata > 0 ? ` let s=(p.y-dot(vec2f(${j(e.dip[0])},${j(e.dip[1])}),p.xz))/${j(e.strata)}+1.6*(terrainRockNoise(p.xz/${j(e.strata * 9)},7)-0.5)+0.35*(terrainRockNoise(p.xz/${j(e.strata * 2.3)},8)-0.5);
|
|
92
|
+
let ws=fwidth(s);let fade=1.0-smoothstep(0.1,0.3,ws);let bed=floor(s);let f=s-bed;let w=clamp(ws,0.03,0.12);
|
|
93
|
+
let keep=smoothstep(0.32,0.62,terrainRockNoise(p.xz/${j(e.strata * 4.5)}+vec2f(bed*17.13,bed*-9.71),9));
|
|
94
|
+
let bench=smoothstep(${j(.7)}-w,${j(.7)}+w,f)*(1.0-smoothstep(1.0-w,1.0,f));
|
|
95
|
+
let k=${j(e.ledges)}*steep*fade*keep;let factor=mix(1.0,mix(${j(1.3214285714285716)},${j(.25)},bench),k);
|
|
96
|
+
m.x*=factor;m.z*=factor;
|
|
97
|
+
terrainRockLoose=max(terrainRockLoose,bench*k*0.7);
|
|
98
|
+
terrainRockShade*=1.0+${j(e.tone)}*0.6*(terrainRockHash(vec3i(i32(bed),3,5)).x-0.5)*steep*(1.0-smoothstep(0.3,1.0,ws));
|
|
99
|
+
` : ""}${e.undulation > 0 ? ` let flat=1.0-steep;var lift=0.0;
|
|
100
|
+
for(var oc=0;oc<3;oc++){
|
|
101
|
+
let l=select(select(8.0,20.0,oc==1),48.0,oc==0);let a=${j(e.undulation)}*select(select(0.2,0.45,oc==1),1.0,oc==0);
|
|
102
|
+
let k=flat*(1.0-smoothstep(0.05,0.25,fp/l));let q=p.xz/l;let i=floor(q);let f=q-i;let c=vec2i(i);
|
|
103
|
+
let u=f*f*f*(f*(f*6.0-15.0)+10.0);let du=30.0*f*f*(f*(f-2.0)+1.0);
|
|
104
|
+
let h00=terrainRockHash(vec3i(c,61+oc)).x;let h10=terrainRockHash(vec3i(c+vec2i(1,0),61+oc)).x;
|
|
105
|
+
let h01=terrainRockHash(vec3i(c+vec2i(0,1),61+oc)).x;let h11=terrainRockHash(vec3i(c+vec2i(1,1),61+oc)).x;
|
|
106
|
+
let h=mix(mix(h00,h10,u.x),mix(h01,h11,u.x),u.y);
|
|
107
|
+
let g=vec2f(du.x*mix(h10-h00,h11-h01,u.y),du.y*mix(h01-h00,h11-h10,u.x))*a/l;
|
|
108
|
+
m.x-=k*g.x;m.z-=k*g.y;lift+=k*(h-0.5)*a/${j(e.undulation)};
|
|
109
|
+
}
|
|
110
|
+
terrainRockShade*=1.0+0.12*lift;
|
|
111
|
+
` : ""} var n=normalize(m);
|
|
112
|
+
${e.facets > 0 ? ` n=terrainRockFacets(p,n,${j(e.facets * 4.1)},fp,steep,0.7,vec3i(41,7,-13));
|
|
113
|
+
n=terrainRockFacets(p,n,${j(e.facets)},fp,steep,1.0,vec3i(0));
|
|
114
|
+
` : ""} return n;
|
|
115
|
+
}${e.debris >= 0 ? `
|
|
116
|
+
fn terrainRockDebris(packed:ptr<function,array<vec4f,4>>) {
|
|
117
|
+
let k=clamp(terrainRockLoose,0.0,1.0);var sum=0.0;
|
|
118
|
+
for(var layer=0u;layer<u32(frame.heights.z);layer++){sum+=(*packed)[layer/4u][layer%4u];(*packed)[layer/4u][layer%4u]*=1.0-k;}
|
|
119
|
+
(*packed)[${e.debris >> 2}u][${e.debris & 3}u]+=k*sum;
|
|
120
|
+
}` : ""}`;
|
|
121
|
+
}, M = (e = !1, t) => `${r}${e ? k : ""}${t ? ee(t) : ""}
|
|
32
122
|
fn terrainColor(position:vec3f,normal:vec3f)->vec3f {
|
|
33
123
|
let slope=1-clamp(normal.y,0.0,1.0);let axes=pow(abs(normal),vec3f(8));let projection=axes/max(axes.x+axes.y+axes.z,0.00001);let dx=dpdx(position);let dy=dpdy(position);var color=vec3f(0);var total=0.0;var fallback=vec3f(0);${e ? "var relief=0.0;var fallbackRelief=0.0;var repeat=0.0;" : ""}
|
|
34
124
|
for(var layer=0u;layer<u32(frame.heights.z);layer++){
|
|
@@ -45,17 +135,53 @@ fn terrainColor(position:vec3f,normal:vec3f)->vec3f {
|
|
|
45
135
|
}
|
|
46
136
|
@fragment fn fragment(i:Output)->@location(0) vec4f {
|
|
47
137
|
${x}
|
|
48
|
-
${
|
|
138
|
+
${A(e, !!t)}let v=normalize(frame.eye.xyz-i.position);let l=frame.light.xyz;let h=(v+l)/max(length(v+l),0.0001);
|
|
49
139
|
let nl=clamp(dot(n,l),0.0,1.0);let nv=clamp(dot(n,v),0.0001,1.0);let nh=clamp(dot(n,h),0.0,1.0);let vh=clamp(dot(v,h),0.0,1.0);let a=i.material.x*i.material.x;let a2=a*a;
|
|
50
140
|
let d=${_};let g=${v};let fresnel=vec3f(0.04)+vec3f(0.96)*pow(1-vh,5.0);
|
|
51
141
|
let direct=((1-fresnel)*base/3.14159265+d*g*fresnel/max(4*nl*nv,0.0001))*nl;
|
|
52
|
-
let linear=direct*frame.radiance.xyz*frame.light.w+base*frame.radiance.w;
|
|
142
|
+
let linear=direct*frame.radiance.xyz*frame.light.w+base*frame.radiance.w*frame.ambient.rgb;
|
|
53
143
|
if(frame.heights.w>0.5){return vec4f(linear,1);}
|
|
54
144
|
let mapped=pbrNeutral(max(linear,vec3f(0)));return vec4f(select(mapped*12.92,1.055*pow(mapped,vec3f(1.0/2.4))-0.055,mapped>=vec3f(0.0031308)),1);
|
|
55
|
-
}`,
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
145
|
+
}`, N = "\nfn terrainLayerSample(position:vec3f,projection:vec3f,dx:vec3f,dy:vec3f,layer:u32)->vec3f {\n let scale=layerRules[layer].params.x;let p=position*scale;\n let x=textureSampleGrad(terrainLayers,terrainSampler,p.zy,i32(layer),dx.zy*scale,dy.zy*scale).rgb;\n let y=textureSampleGrad(terrainLayers,terrainSampler,p.xz,i32(layer),dx.xz*scale,dy.xz*scale).rgb;\n let z=textureSampleGrad(terrainLayers,terrainSampler,p.xy,i32(layer),dx.xy*scale,dy.xy*scale).rgb;\n return x*projection.x+y*projection.y+z*projection.z;\n}", P = ({ size: e, contrast: t, warp: n, terraces: r }) => `
|
|
146
|
+
var<private> terrainPatchWeights:array<f32,16>;
|
|
147
|
+
fn terrainPatchXZ(p:vec3f)->vec2f {return p.xz;}
|
|
148
|
+
fn terrainPatchHash(c:vec2f,layer:u32)->f32 {
|
|
149
|
+
var v=vec3u(bitcast<u32>(i32(c.x)),bitcast<u32>(i32(c.y)),layer)*1664525u+1013904223u;
|
|
150
|
+
v.x+=v.y*v.z;v.y+=v.z*v.x;v.z+=v.x*v.y;v^=v>>vec3u(16u);v.x+=v.y*v.z;v.y+=v.z*v.x;v.z+=v.x*v.y;
|
|
151
|
+
return f32(v.x>>8u)/16777216.0;
|
|
152
|
+
}
|
|
153
|
+
fn terrainPatchNoise(p:vec2f,layer:u32)->f32 {
|
|
154
|
+
let i=floor(p);let f=p-i;let u=f*f*f*(f*(f*6.0-15.0)+10.0);
|
|
155
|
+
return mix(mix(terrainPatchHash(i,layer),terrainPatchHash(i+vec2f(1,0),layer),u.x),mix(terrainPatchHash(i+vec2f(0,1),layer),terrainPatchHash(i+vec2f(1,1),layer),u.x),u.y);
|
|
156
|
+
}
|
|
157
|
+
fn terrainPatch(xz:vec2f,layer:u32,footprint:f32)->f32 {
|
|
158
|
+
var p=xz*${(1 / e).toPrecision(9)};var cell=${e.toPrecision(9)};var amp=0.5;var sum=0.0;var norm=0.0;
|
|
159
|
+
for(var k=0u;k<3u;k++){let fade=1.0-smoothstep(0.25,0.5,footprint/cell);sum+=amp*mix(0.5,terrainPatchNoise(p,layer*3u+k),fade);norm+=amp;p=p*2.03+vec2f(19.1,7.3);cell/=2.03;amp*=0.5;}${n > 0 ? `
|
|
160
|
+
let broad=1.0-smoothstep(0.25,0.5,footprint/${(e * 3.3).toPrecision(9)});sum+=0.75*mix(0.5,terrainPatchNoise(xz*${(1 / (e * 3.3)).toPrecision(9)}+vec2f(3.7,8.1),layer*3u+48u),broad);norm+=0.75;` : ""}
|
|
161
|
+
return sum/norm;
|
|
162
|
+
}
|
|
163
|
+
fn terrainPatchBlend(packed:ptr<function,array<vec4f,4>>,position:vec2f,height:f32,up:f32,footprint:f32) {
|
|
164
|
+
var xz=position;${n > 0 ? `
|
|
165
|
+
let wp=position*${(1 / (e * 2.6)).toPrecision(9)};
|
|
166
|
+
xz+=${(n * e * 2.2).toPrecision(9)}*(vec2f(terrainPatchNoise(wp,61u),terrainPatchNoise(wp+vec2f(5.2,1.3),62u))-0.5);` : ""}${r > 0 ? `
|
|
167
|
+
let phase=height*${(1 / r).toPrecision(9)}+1.3*terrainPatchNoise(position*${(1 / (r * 7)).toPrecision(9)},63u);
|
|
168
|
+
let tilt=degrees(acos(clamp(up,0.0,1.0)));
|
|
169
|
+
let bands=${(.22 * t).toPrecision(9)}*(1.0-smoothstep(0.12,0.35,fwidth(phase)))*smoothstep(8.0,14.0,tilt)*(1.0-smoothstep(30.0,38.0,tilt))*smoothstep(0.42,0.68,terrainPatchNoise(position*${(1 / (r * 14)).toPrecision(9)},64u));` : ""}
|
|
170
|
+
var top=-1.0;var plain=0.0;
|
|
171
|
+
for(var layer=0u;layer<u32(frame.heights.z);layer++){
|
|
172
|
+
let w=(*packed)[layer/4u][layer%4u];terrainPatchWeights[layer]=-1.0;if(w<=0.0){continue;}
|
|
173
|
+
let t=w+${t.toPrecision(9)}*terrainPatch(xz,layer,footprint)${r > 0 ? "+bands*sin(6.2831853*phase+f32(layer)*2.1)" : ""};terrainPatchWeights[layer]=t;top=max(top,t);plain+=w;
|
|
174
|
+
}
|
|
175
|
+
let line=top-${(.05 * t).toPrecision(9)};var sum=0.0;
|
|
176
|
+
for(var layer=0u;layer<u32(frame.heights.z);layer++){let b=max(terrainPatchWeights[layer]-line,0.0);terrainPatchWeights[layer]=b;sum+=b;}
|
|
177
|
+
let near=1.0-smoothstep(0.3,0.8,footprint*${(1 / e).toPrecision(9)});
|
|
178
|
+
for(var layer=0u;layer<u32(frame.heights.z);layer++){
|
|
179
|
+
terrainPatchWeights[layer]=mix((*packed)[layer/4u][layer%4u]/max(plain,0.00001),terrainPatchWeights[layer]/max(sum,0.00001),near);
|
|
180
|
+
}
|
|
181
|
+
}`, F = (e, t = !1, n = !1, r, i) => {
|
|
182
|
+
let a = M(t, i), o = a.slice(a.indexOf("fn terrainColor("), a.indexOf("@fragment fn fragment(")), s = Math.min(4, Math.max(1, Math.floor(e))), c = `
|
|
183
|
+
${Array.from({ length: s }, (e, t) => t).map((e) => `@group(0) @binding(${6 + e}) var terrainWeights${e}:texture_2d<f32>;`).join("\n")}
|
|
184
|
+
${N}${r ? P(r) : ""}
|
|
59
185
|
fn terrainColor(position:vec3f,normal:vec3f)->vec3f {
|
|
60
186
|
let axes=pow(abs(normal),vec3f(8));let projection=axes/max(axes.x+axes.y+axes.z,0.00001);let dx=dpdx(position);let dy=dpdy(position);
|
|
61
187
|
let d=vec2f(textureDimensions(terrainWeights0));let uv=clamp((position.xz-frame.map.xy)/frame.map.zw,vec2f(0.5)/d,vec2f(1)-vec2f(0.5)/d);
|
|
@@ -64,25 +190,25 @@ fn terrainColor(position:vec3f,normal:vec3f)->vec3f {
|
|
|
64
190
|
1,
|
|
65
191
|
2,
|
|
66
192
|
3
|
|
67
|
-
].map((e) => e <
|
|
68
|
-
var color=vec3f(0);var total=0.0;${t ? "var relief=0.0;var repeat=0.0;" : ""}
|
|
193
|
+
].map((e) => e < s ? `textureSampleLevel(terrainWeights${e},terrainSampler,uv,0)` : "vec4f(0)").join(",")});
|
|
194
|
+
${i && i.debris >= 0 ? "terrainRockDebris(&packed);" : ""}${r ? "terrainPatchBlend(&packed,terrainPatchXZ(position),position.y,normal.y,max(length(dx),length(dy)));" : ""}var color=vec3f(0);var total=0.0;${t ? "var relief=0.0;var repeat=0.0;" : ""}
|
|
69
195
|
for(var layer=0u;layer<u32(frame.heights.z);layer++){
|
|
70
|
-
let weight=packed[layer/4u][layer%4u];if(weight<=0.0){continue;}
|
|
196
|
+
${r ? "let weight=terrainPatchWeights[layer];" : "let weight=packed[layer/4u][layer%4u];"}if(weight<=0.0){continue;}
|
|
71
197
|
${t ? "let surface=terrainSurface(terrainLayerSample(position,projection,dx,dy,layer),position,projection,dx,dy,layer);color+=surface.rgb*weight;relief+=surface.w*weight;repeat+=layerRules[layer].params.x*weight;total+=weight;" : "color+=terrainLayerSample(position,projection,dx,dy,layer)*weight;total+=weight;"}
|
|
72
198
|
}
|
|
73
199
|
${t ? "if(total>0.00001){terrainRelief=relief/total;terrainRepeat=repeat/total;return color/total;}" : "if(total>0.00001){return color/total;}"}
|
|
74
200
|
${t ? "let surface=terrainSurface(terrainLayerSample(position,projection,dx,dy,0u),position,projection,dx,dy,0u);terrainRelief=surface.w;terrainRepeat=layerRules[0].params.x;return surface.rgb;" : "return terrainLayerSample(position,projection,dx,dy,0u);"}
|
|
75
201
|
}
|
|
76
|
-
${
|
|
77
|
-
return n ? `@group(0) @binding(${6 +
|
|
202
|
+
${a.replace(o, "")}`;
|
|
203
|
+
return n ? `@group(0) @binding(${6 + s}) var terrainTint:texture_2d<f32>;
|
|
78
204
|
var<private> terrainTintTexel:vec4f;
|
|
79
205
|
fn terrainTinted(c:vec3f,layer:u32)->vec3f {return mix(c,terrainTintTexel.rgb,terrainTintTexel.a*layerRules[layer].params.w);}
|
|
80
|
-
${
|
|
81
|
-
},
|
|
82
|
-
function
|
|
206
|
+
${c.replace("var packed=", "let td=vec2f(textureDimensions(terrainTint));terrainTintTexel=textureSampleLevel(terrainTint,terrainSampler,clamp((position.xz-frame.map.xy)/frame.map.zw,vec2f(0.5)/td,vec2f(1)-vec2f(0.5)/td),0);var packed=").replace(/terrainLayerSample\(position,projection,dx,dy,(layer|0u)\)/g, "terrainTinted($&,$1)").replace("fn terrainReliefTint(h:f32,layer:u32)->f32 {return h;}", "fn terrainReliefTint(h:f32,layer:u32)->f32 {return mix(h,dot(terrainTintTexel.rgb,vec3f(0.2126,0.7152,0.0722)),terrainTintTexel.a*layerRules[layer].params.w);}")}` : c;
|
|
207
|
+
}, I = "let n=vec2f(textureDimensions(heightMap));let uv=((position.xz-frame.map.xy)/frame.map.zw*(n-1.0)+0.5)/n;o.clip=vec4f(uv.x*2.0-1.0,1.0-2.0*uv.y,0.0,1.0);";
|
|
208
|
+
function te(e, t, n, r, i, a) {
|
|
83
209
|
let o = e.adapter?.capabilities?.tier;
|
|
84
210
|
if (o === "ldr") return;
|
|
85
|
-
let s = o === "half-float" ? "rgba16float" : "r32float", c = Math.min(2049, t.width), l = Math.min(2049, t.height), u = [c, l], d = a ? "+frame.anchor.z" : "", f = s === "rgba16float" ? "vec4f" : "f32", p = (e) => f === "vec4f" ? `vec4f(${e},0.0,0.0,1.0)` : e, m = e.createShaderModule({ code: i(r.replace(
|
|
211
|
+
let s = o === "half-float" ? "rgba16float" : "r32float", c = Math.min(2049, t.width), l = Math.min(2049, t.height), u = [c, l], d = a ? "+frame.anchor.z" : "", f = s === "rgba16float" ? "vec4f" : "f32", p = (e) => f === "vec4f" ? `vec4f(${e},0.0,0.0,1.0)` : e, m = e.createShaderModule({ code: i(r.replace(a ? w : C, I) + `
|
|
86
212
|
@vertex fn seedVertex(@builtin(vertex_index) i:u32)->@builtin(position) vec4f {
|
|
87
213
|
return vec4f(array<vec2f,3>(vec2f(-1,-1),vec2f(3,-1),vec2f(-1,3))[i],0,1);
|
|
88
214
|
}
|
|
@@ -146,11 +272,11 @@ function A(e, t, n, r, i, a) {
|
|
|
146
272
|
}
|
|
147
273
|
//#endregion
|
|
148
274
|
//#region src/colors.ts
|
|
149
|
-
var
|
|
275
|
+
var ne = 0, L = [
|
|
150
276
|
.5,
|
|
151
277
|
.42,
|
|
152
278
|
.29
|
|
153
|
-
],
|
|
279
|
+
], re = [[
|
|
154
280
|
.133,
|
|
155
281
|
.263,
|
|
156
282
|
.033
|
|
@@ -158,12 +284,16 @@ var j = 0, M = [
|
|
|
158
284
|
.274,
|
|
159
285
|
.342,
|
|
160
286
|
.064
|
|
161
|
-
]],
|
|
287
|
+
]], ie = [
|
|
162
288
|
.253,
|
|
163
289
|
.223,
|
|
164
290
|
.187
|
|
165
|
-
]
|
|
166
|
-
|
|
291
|
+
], ae = Object.freeze([
|
|
292
|
+
L[0],
|
|
293
|
+
L[1],
|
|
294
|
+
L[2]
|
|
295
|
+
]);
|
|
296
|
+
function R(e, t) {
|
|
167
297
|
let n = 0, r = 50, i = e.geometry?.vertices;
|
|
168
298
|
if (i?.length) {
|
|
169
299
|
n = Infinity, r = -Infinity;
|
|
@@ -172,9 +302,9 @@ function F(e, t) {
|
|
|
172
302
|
[n, r] = [a + n * t, a + r * t].sort((e, t) => e - t), r > n || (r = n + 1);
|
|
173
303
|
}
|
|
174
304
|
return {
|
|
175
|
-
sand:
|
|
176
|
-
meadow:
|
|
177
|
-
stone:
|
|
305
|
+
sand: L,
|
|
306
|
+
meadow: re,
|
|
307
|
+
stone: ie,
|
|
178
308
|
shore: [.3, 1.5],
|
|
179
309
|
meadowHeight: [n, r],
|
|
180
310
|
stoneHeight: [r + 1, r + 2],
|
|
@@ -186,9 +316,9 @@ function F(e, t) {
|
|
|
186
316
|
...Object.fromEntries(Object.entries(t).filter(([, e]) => e !== void 0))
|
|
187
317
|
};
|
|
188
318
|
}
|
|
189
|
-
function
|
|
319
|
+
function z(e, t) {
|
|
190
320
|
if (typeof t != "object" || !t) throw TypeError("Terrain color options must be an object; pass {} or leave them out for the defaults");
|
|
191
|
-
let n =
|
|
321
|
+
let n = R(e, t);
|
|
192
322
|
if (n.meadow.length !== 2) throw RangeError("Expected two meadow colors");
|
|
193
323
|
for (let e of [
|
|
194
324
|
n.sand,
|
|
@@ -227,9 +357,9 @@ function I(e, t) {
|
|
|
227
357
|
sand: o
|
|
228
358
|
};
|
|
229
359
|
}
|
|
230
|
-
var
|
|
231
|
-
function
|
|
232
|
-
let r = /* @__PURE__ */ new Float32Array(44), i = e.wetDarkening, a = (e) =>
|
|
360
|
+
var B = 11;
|
|
361
|
+
function V(e, t, n) {
|
|
362
|
+
let r = /* @__PURE__ */ new Float32Array(44), i = e.wetDarkening, a = (e) => q(.6 * t * (e[1] - e[0])), o = n ?? {
|
|
233
363
|
wind: [1, 0],
|
|
234
364
|
ripple: .1,
|
|
235
365
|
ripples: 1,
|
|
@@ -237,29 +367,29 @@ function R(e, t, n) {
|
|
|
237
367
|
sorting: 1,
|
|
238
368
|
broad: 0
|
|
239
369
|
}, s = Math.hypot(o.wind[0], o.wind[1]);
|
|
240
|
-
return r.set([...e.sand, i], 0), r.set([...e.meadow[0], 1 - i], 4), r.set([...e.meadow[1], i < 1 ?
|
|
370
|
+
return r.set([...e.sand, i], 0), r.set([...e.meadow[0], 1 - i], 4), r.set([...e.meadow[1], i < 1 ? q(.4 / (1 - i)) : 0], 8), r.set(e.stone, 12), r.set([...e.shore, ...e.wetHeight], 16), r.set([...e.meadowHeight, ...e.stoneHeight], 20), r.set(e.stoneNormal, 24), r.set([
|
|
241
371
|
a(e.meadowHeight),
|
|
242
372
|
a(e.shore),
|
|
243
373
|
a(e.stoneHeight),
|
|
244
374
|
a(e.stoneNormal)
|
|
245
375
|
], 28), r.set([
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
376
|
+
q(o.wind[0] / s),
|
|
377
|
+
q(o.wind[1] / s),
|
|
378
|
+
q(o.ripple),
|
|
379
|
+
q(o.ripples * t)
|
|
250
380
|
], 32), r.set([
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
381
|
+
q(.1 * o.sorting * t),
|
|
382
|
+
q(.07 * o.sorting * t),
|
|
383
|
+
q(.16 * o.streaks * t),
|
|
384
|
+
q(.3 * o.broad * t)
|
|
255
385
|
], 36), r.set([
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
386
|
+
q(.14 * o.sorting * t),
|
|
387
|
+
q(o.ripple / 4),
|
|
388
|
+
q(o.ripple / 2)
|
|
259
389
|
], 40), r;
|
|
260
390
|
}
|
|
261
|
-
var
|
|
262
|
-
function
|
|
391
|
+
var H = "let terrainColorFootprint=max(length(dpdx(i.position)),length(dpdy(i.position)));", U = "struct TerrainSurface {color:vec3f,normal:vec3f,rough:f32}\n", W = "\nfn terrainBlend(t:f32,ha:f32,hb:f32)->f32 {\n let a=ha+1.0-t;let b=hb+t;let m=max(a,b)-0.2;let x=max(a-m,0.0);let y=max(b-m,0.0);return y/(x+y);\n}\n", G = "\n@group(0) @binding(0) var depth:texture_2d<f32>;\n@group(0) @binding(1) var before:texture_2d<f32>;\n@group(0) @binding(2) var after:texture_storage_2d<rgba32float,write>;\n@group(0) @binding(3) var<uniform> drying:vec4f;\n@compute @workgroup_size(8,8) fn main(@builtin(global_invocation_id) id:vec3u){\n let size=vec2i(textureDimensions(after));let p=vec2i(id.xy);if(any(p>=size)){return;}\n let now=smoothstep(0.001,0.025,textureLoad(depth,p,0).r);let w=textureLoad(before,p,0);\n let m=max(now,w.x*exp(-drying.x/drying.y));let f=max(now,w.y*exp(-drying.x/drying.z));\n textureStore(after,p,vec4f(m,f,(1.0-exp(-3.0*m))/(1.0-exp(-3.0)),0.0));\n}";
|
|
392
|
+
function K(e, t = 900, n = 20) {
|
|
263
393
|
let r = Math.exp(-e / t);
|
|
264
394
|
return {
|
|
265
395
|
moisture: r,
|
|
@@ -267,14 +397,14 @@ function U(e, t = 900, n = 20) {
|
|
|
267
397
|
darkening: (1 - Math.exp(-3 * r)) / (1 - Math.exp(-3))
|
|
268
398
|
};
|
|
269
399
|
}
|
|
270
|
-
var
|
|
400
|
+
var q = (e) => Math.round(e * 1e6) / 1e6, J = (e) => String(q(e)), oe = class e {coreRequirement=__margoCoreRequirement("1.0.0",import.meta.url);
|
|
271
401
|
constructor(e, t = {}) {
|
|
272
|
-
this.mesh = e, this.name = "terrain:colors:" + ++
|
|
402
|
+
this.mesh = e, this.name = "terrain:colors:" + ++ne, this.source = "", this.shader = "", this.disposed = !1, this.wetBinding = 40, this.lookBinding = 41, this.lookDirty = !0, this.wetSource = { texture: void 0 }, this.wetTarget = { texture: void 0 }, this.wetSize = {
|
|
273
403
|
width: 1,
|
|
274
404
|
height: 1
|
|
275
405
|
}, this.drying = /* @__PURE__ */ new Float32Array(4), this.recordSource = { texture: void 0 }, this.recordTarget = { texture: void 0 }, this.elapsed = 0, this.dryTime = 900, this.glossTime = 20;
|
|
276
|
-
let { options: n, detail: r, wetness: i, sand: a } =
|
|
277
|
-
this.wetness = i, this.given = t, this.dryTime = n.wetDryTime, this.glossTime = n.wetGlossTime, this.look =
|
|
406
|
+
let { options: n, detail: r, wetness: i, sand: a } = z(e, t);
|
|
407
|
+
this.wetness = i, this.given = t, this.dryTime = n.wetDryTime, this.glossTime = n.wetGlossTime, this.look = V(n, r, a);
|
|
278
408
|
let o = (e, t) => `terrainLook[${e}].${t}`, s = n.variation ?? 0, c = (e, t, n, i) => `smoothstep(${e},${t},${i}${r ? `+large*${n}` : ""})`, l = (e, t) => r === 1 ? `terrainBlend(${e},${t})` : `mix(${e},terrainBlend(${e},${t}),${r})`, u = !!a && r > 0;
|
|
279
409
|
this.sandOn = u;
|
|
280
410
|
let d = u ? `
|
|
@@ -318,8 +448,8 @@ fn terrainSurface(p:vec3f,n:vec3f,footprint:f32)->TerrainSurface {
|
|
|
318
448
|
let grass=0.275+0.35*fine;let stoneT=${l("steep", "grass,0.25+0.5*rock")};
|
|
319
449
|
let sandT=${l("shore", "0.15-0.3*fine,mix(grass,0.25+0.5*rock,stoneT)")};${d}` : "\n let stoneT=steep;let sandT=shore;"}
|
|
320
450
|
${i ? "let wr=terrainWet(p);let wet=wr.x;let glint=wr.y;" : `let wet=mix(${o(0, "w")},1.0,smoothstep(${o(4, "z")},${o(4, "w")},h));let glint=wet;`}
|
|
321
|
-
s.color=mix(${o(0, "xyz")}${r ? `*(1.0+${
|
|
322
|
-
s.normal=${r ? `normalize(n-(grad-n*dot(n,grad))*(${
|
|
451
|
+
s.color=mix(${o(0, "xyz")}${r ? `*(1.0+${J(.2 * r)}*fine)` : ""}${u ? "*sandShade" : ""},mix(meadow,${o(3, "xyz")}${r ? `*(1.0+${J(.6 * r)}*rock)` : ""},stoneT),sandT)*wet${s ? `*(${J(1 - s)}+${J(2 * s)}*(0.65*large+0.35*fine))` : ""};
|
|
452
|
+
s.normal=${r ? `normalize(n-(grad-n*dot(n,grad))*(${J(.15 * r)}*stoneT*sandT)${u ? "-(sandGrad-n*dot(n,sandGrad))" : ""})` : "n"};
|
|
323
453
|
s.rough=select(1.0,0.6+${o(2, "w")}*(glint-${o(0, "w")}),${o(0, "w")}<1.0);return s;
|
|
324
454
|
}
|
|
325
455
|
`, this.wetCode = i ? `
|
|
@@ -344,7 +474,7 @@ fn terrainWet(position:vec3f)->vec2f {
|
|
|
344
474
|
}
|
|
345
475
|
` : "";
|
|
346
476
|
}
|
|
347
|
-
static attach(t, n, r = {}) {__margoAssertCore(t,__margoCoreRequirement("
|
|
477
|
+
static attach(t, n, r = {}) {__margoAssertCore(t,__margoCoreRequirement("1.0.0",import.meta.url));
|
|
348
478
|
let i = new e(n, r);
|
|
349
479
|
return l(t).use(i), i;
|
|
350
480
|
}
|
|
@@ -375,15 +505,15 @@ fn terrainWet(position:vec3f)->vec2f {
|
|
|
375
505
|
...e.sandSurface
|
|
376
506
|
};
|
|
377
507
|
}
|
|
378
|
-
let r =
|
|
379
|
-
this.look =
|
|
508
|
+
let r = z(this.mesh, n);
|
|
509
|
+
this.look = V(r.options, r.detail, r.sand), this.dryTime = r.options.wetDryTime, this.glossTime = r.options.wetGlossTime, this.given = n, this.lookDirty = !0;
|
|
380
510
|
}
|
|
381
511
|
patch(e, t) {
|
|
382
512
|
let n = s(e);
|
|
383
513
|
if (!n) return e;
|
|
384
|
-
let { code: r } = n, i = r.indexOf(
|
|
514
|
+
let { code: r } = n, i = r.indexOf(H), a = i < 0 ? n.at : i + 81, o = "_" + t, l = (`@group(0) @binding(${this.lookBinding}) var<uniform> terrainLook:array<vec4f,${B}>;
|
|
385
515
|
` + this.wetCode.replace("@binding(9)", `@binding(${this.wetBinding})`) + this.surfaceCode).replace(/terrainSurface|terrainWater|terrainWet|terrainMoisture|terrainLook/g, (e) => e + o);
|
|
386
|
-
return (r.includes("struct TerrainSurface ") ? "" :
|
|
516
|
+
return (r.includes("struct TerrainSurface ") ? "" : U) + (this.surfaceCode.includes("terrainNoise(") && !r.includes("fn terrainNoise(") ? c : "") + (this.surfaceCode.includes("terrainBlend(") && !r.includes("fn terrainBlend(") ? W : "") + l + r.slice(0, a) + (i < 0 ? H : "") + `if(i.terrainId==${t + 1}.0){let s=terrainSurface${o}(i.position,n,terrainColorFootprint);i.color=s.color;i.material.x*=s.rough;n=s.normal;}` + r.slice(a);
|
|
387
517
|
}
|
|
388
518
|
registerPasses(e) {
|
|
389
519
|
e.add({
|
|
@@ -423,7 +553,7 @@ fn terrainWet(position:vec3f)->vec2f {
|
|
|
423
553
|
}), this.recordPipeline ??= e.device.createComputePipeline({
|
|
424
554
|
layout: "auto",
|
|
425
555
|
compute: {
|
|
426
|
-
module: e.device.createShaderModule({ code:
|
|
556
|
+
module: e.device.createShaderModule({ code: G }),
|
|
427
557
|
entryPoint: "main"
|
|
428
558
|
}
|
|
429
559
|
}), this.recordGroup = e.device.createBindGroup({
|
|
@@ -493,38 +623,38 @@ fn terrainWet(position:vec3f)->vec2f {
|
|
|
493
623
|
};
|
|
494
624
|
//#endregion
|
|
495
625
|
//#region src/heightfield.ts
|
|
496
|
-
function
|
|
626
|
+
function Y(e, t, n, r) {
|
|
497
627
|
if (typeof e != "function" || t.length !== 2 || !Number.isFinite(t[0]) || !Number.isFinite(t[1]) || t[0] <= 0 || t[1] <= 0 || t[0] > 1e5 || t[1] > 1e5 || r.length !== 2 || !Number.isFinite(Math.fround(r[0])) || !Number.isFinite(Math.fround(r[1])) || !Number.isInteger(n) || n < 1 || n > 1024) throw RangeError("Invalid terrain heightfield; pass a height function, a size of 1..100000 per axis, a finite origin and an integer resolution from 1 to 1024.");
|
|
498
628
|
}
|
|
499
|
-
function
|
|
629
|
+
function X(e, t, n) {
|
|
500
630
|
let r = e(t, n);
|
|
501
631
|
if (!Number.isFinite(Math.fround(r))) throw RangeError("Terrain height must be finite");
|
|
502
632
|
return r;
|
|
503
633
|
}
|
|
504
|
-
function
|
|
634
|
+
function Z(e, t) {
|
|
505
635
|
let { size: n, resolution: r = 192, origin: i = [-n[0] / 2, -n[1] / 2] } = t;
|
|
506
|
-
|
|
636
|
+
Y(e, n, r, i);
|
|
507
637
|
let a = p(n[0], n[1], r, r);
|
|
508
638
|
for (let t = 0; t < a.positions.length; t += 3) {
|
|
509
639
|
let r = a.positions[t] + i[0] + n[0] / 2, o = -a.positions[t + 1] + i[1] + n[1] / 2;
|
|
510
|
-
a.positions[t] = r, a.positions[t + 1] =
|
|
640
|
+
a.positions[t] = r, a.positions[t + 1] = X(e, r, o), a.positions[t + 2] = o;
|
|
511
641
|
}
|
|
512
642
|
return m(a);
|
|
513
643
|
}
|
|
514
|
-
function
|
|
515
|
-
return h(
|
|
644
|
+
function se(e, t) {
|
|
645
|
+
return h(Z(e, t));
|
|
516
646
|
}
|
|
517
|
-
var
|
|
647
|
+
var ce = 0, le = class e {coreRequirement=__margoCoreRequirement("1.0.0",import.meta.url);
|
|
518
648
|
constructor(e, t) {
|
|
519
|
-
this.height = e, this.options = t, this.name = "terrain-heightmap:" + ++
|
|
649
|
+
this.height = e, this.options = t, this.name = "terrain-heightmap:" + ++ce, this.disposed = !1;
|
|
520
650
|
let { size: n, resolution: r = 256, origin: i = [-n[0] / 2, -n[1] / 2] } = t;
|
|
521
|
-
|
|
651
|
+
Y(e, n, r, i), this.options = {
|
|
522
652
|
size: [n[0], n[1]],
|
|
523
653
|
resolution: r,
|
|
524
654
|
origin: [i[0], i[1]]
|
|
525
655
|
};
|
|
526
656
|
}
|
|
527
|
-
static attach(t, n, r) {__margoAssertCore(t,__margoCoreRequirement("
|
|
657
|
+
static attach(t, n, r) {__margoAssertCore(t,__margoCoreRequirement("1.0.0",import.meta.url));
|
|
528
658
|
let i = new e(n, r);
|
|
529
659
|
return l(t).use(i), i;
|
|
530
660
|
}
|
|
@@ -533,7 +663,7 @@ var Z = 0, Q = class e {coreRequirement=__margoCoreRequirement("0.1.0",import.me
|
|
|
533
663
|
let t = e.gpu, { size: n, resolution: r = 256, origin: i = [-n[0] / 2, -n[1] / 2] } = this.options;
|
|
534
664
|
if (r > t.limits.maxTextureDimension2D) throw RangeError("Heightmap resolution exceeds the GPU texture limit; reduce resolution to the device maxTextureDimension2D or less.");
|
|
535
665
|
let a = new Float32Array(r * r);
|
|
536
|
-
for (let e = 0; e < r; e++) for (let t = 0; t < r; t++) a[e * r + t] =
|
|
666
|
+
for (let e = 0; e < r; e++) for (let t = 0; t < r; t++) a[e * r + t] = X(this.height, i[0] + (t + .5) / r * n[0], i[1] + (e + .5) / r * n[1]);
|
|
537
667
|
this.engine = e, this.texture = t.createTexture({
|
|
538
668
|
size: [r, r],
|
|
539
669
|
format: "r32float",
|
|
@@ -555,16 +685,28 @@ var Z = 0, Q = class e {coreRequirement=__margoCoreRequirement("0.1.0",import.me
|
|
|
555
685
|
};
|
|
556
686
|
//#endregion
|
|
557
687
|
//#region src/index.ts
|
|
558
|
-
function
|
|
688
|
+
function ue(e, t) {
|
|
689
|
+
if (typeof e != "object" || !e) return !1;
|
|
690
|
+
let n = (e, t, n) => e === void 0 || e >= t && e <= n, r = (e) => e === void 0 || e === 0 || e >= .5 && e <= 500, i = e.slope ?? [28, 44], a = e.dip ?? [0, 0];
|
|
691
|
+
return Array.isArray(i) && i.length === 2 && i[0] >= 0 && i[0] < i[1] && i[1] <= 90 && Array.isArray(a) && a.length === 2 && a.every((e) => e >= -4 && e <= 4) && r(e.strata) && r(e.facets) && n(e.ledges, 0, 1) && n(e.relief, 0, 1) && n(e.tone, 0, 1) && n(e.gullies, 0, 100) && n(e.undulation, 0, 10) && (e.debris === void 0 || Number.isInteger(e.debris) && e.debris >= 0 && e.debris < t);
|
|
692
|
+
}
|
|
693
|
+
function Q(e, t, n, r, i = new Float64Array(r * 2)) {
|
|
559
694
|
for (let a = 0; a < r; a++) {
|
|
560
695
|
let r = n * 2 ** a;
|
|
561
696
|
i[a * 2] = Math.floor(e[0] / r) * r - t[0], i[a * 2 + 1] = Math.floor(e[2] / r) * r - t[2];
|
|
562
697
|
}
|
|
563
698
|
return i;
|
|
564
699
|
}
|
|
565
|
-
|
|
700
|
+
function $(e, t, n) {
|
|
701
|
+
for (let r = 0; r < 4; r++) e[56 + r] = t[12 + r] + t[r] * n[0] + t[4 + r] * n[1] + t[8 + r] * n[2];
|
|
702
|
+
}
|
|
703
|
+
var de = 0, fe = class r {coreRequirement=__margoCoreRequirement("1.0.0",import.meta.url);
|
|
566
704
|
constructor(e) {
|
|
567
|
-
this.options = e, this.name = "terrain:" +
|
|
705
|
+
this.options = e, this.name = "terrain:" + de++, this.shadowVersion = 0, this.disposed = !1, this.localShadowPipelines = [], this.rings = [], this.sent = /* @__PURE__ */ new WeakMap(), this.bitViews = /* @__PURE__ */ new WeakMap(), this.pipelines = /* @__PURE__ */ new Map(), this.lightingGroups = /* @__PURE__ */ new Map(), this.lighting = /* @__PURE__ */ new Set(), this.lightingEntries = [], this.centerCount = 0, this.ringCount = 0, this.captureCode = /* @__PURE__ */ new Map(), this.disposers = /* @__PURE__ */ new Set(), this.shiftValues = /* @__PURE__ */ new Float64Array(3), this.rulesShift = /* @__PURE__ */ new Float64Array(3), this.rulesEye = new Float64Array([
|
|
706
|
+
NaN,
|
|
707
|
+
NaN,
|
|
708
|
+
NaN
|
|
709
|
+
]), this.eyeLocal = /* @__PURE__ */ new Float64Array(3), this.eye = /* @__PURE__ */ new Float64Array(3), this.plan = /* @__PURE__ */ new Float64Array(), this.validate();
|
|
568
710
|
let t = e.clipmapAnchor;
|
|
569
711
|
t && t !== "camera" && (this.anchoredEye = [
|
|
570
712
|
t[0],
|
|
@@ -588,13 +730,13 @@ var ee = 0, te = class r {coreRequirement=__margoCoreRequirement("0.1.0",import.
|
|
|
588
730
|
get surfaceHeightTexture() {
|
|
589
731
|
return this.capturedSurface?.texture;
|
|
590
732
|
}
|
|
591
|
-
static attach(e, t) {__margoAssertCore(e,__margoCoreRequirement("
|
|
733
|
+
static attach(e, t) {__margoAssertCore(e,__margoCoreRequirement("1.0.0",import.meta.url));
|
|
592
734
|
let n = new r(t);
|
|
593
735
|
return l(e).use(n), n;
|
|
594
736
|
}
|
|
595
737
|
validate() {
|
|
596
738
|
let e = this.options;
|
|
597
|
-
if (typeof e.textures != "function" || !Number.isInteger(e.resolution ?? 64) || (e.resolution ?? 64) < 8 || (e.resolution ?? 64) > 256 || (e.resolution ?? 64) % 4 || !Number.isInteger(e.levels ?? 6) || (e.levels ?? 6) < 1 || (e.levels ?? 6) > 12 || e.roughness !== void 0 && !(e.roughness >= 0 && e.roughness <= 1) || !Number.isFinite(Math.fround(e.cellSize ?? 1)) || (e.cellSize ?? 1) <= 0 || !Number.isFinite(Math.fround(e.heightScale ?? 100)) || !Number.isFinite(Math.fround(e.heightOffset ?? 0)) || !Array.isArray(e.layers) || e.layers.length < 4 || e.layers.length > 16) throw RangeError("Invalid terrain options; check textures, resolution (multiple of 4, 8..256), levels (1..12), roughness (0..1), cellSize above 0, finite heightScale and heightOffset, and 4 to 16 layers.");
|
|
739
|
+
if (typeof e.textures != "function" || !Number.isInteger(e.resolution ?? 64) || (e.resolution ?? 64) < 8 || (e.resolution ?? 64) > 256 || (e.resolution ?? 64) % 4 || !Number.isInteger(e.levels ?? 6) || (e.levels ?? 6) < 1 || (e.levels ?? 6) > 12 || e.roughness !== void 0 && !(e.roughness >= 0 && e.roughness <= 1) || e.patches !== void 0 && (typeof e.patches != "object" || e.patches === null || !((e.patches.size ?? 14) >= .5 && (e.patches.size ?? 14) <= 1e4) || !((e.patches.contrast ?? .6) > 0 && (e.patches.contrast ?? .6) <= 1) || !((e.patches.warp ?? 0) >= 0 && (e.patches.warp ?? 0) <= 1) || !(e.patches.terraces === void 0 || e.patches.terraces === 0 || e.patches.terraces >= .5 && e.patches.terraces <= 1e3)) || e.rock !== void 0 && !ue(e.rock, e.layers?.length ?? 0) || !Number.isFinite(Math.fround(e.cellSize ?? 1)) || (e.cellSize ?? 1) <= 0 || !Number.isFinite(Math.fround(e.heightScale ?? 100)) || !Number.isFinite(Math.fround(e.heightOffset ?? 0)) || !Array.isArray(e.layers) || e.layers.length < 4 || e.layers.length > 16) throw RangeError("Invalid terrain options; check textures, resolution (multiple of 4, 8..256), levels (1..12), roughness (0..1), patches (size 0.5..10000 m, contrast above 0 to 1, warp 0..1, terraces 0 or 0.5..1000 m), rock (slope 0 <= start < full <= 90, strata and facets 0 or 0.5..500 m, ledges, relief and tone 0..1, dip -4..4, gullies 0..100 m, undulation 0..10 m, debris a layer index), cellSize above 0, finite heightScale and heightOffset, and 4 to 16 layers.");
|
|
598
740
|
d(e.origin ?? [0, 0], 2, "Terrain origin"), e.clipmapAnchor !== void 0 && e.clipmapAnchor !== "camera" && d(e.clipmapAnchor, 2, "Terrain clipmap anchor");
|
|
599
741
|
let t = e.floatingOrigin;
|
|
600
742
|
if (t !== void 0 && (typeof t?.onRebase != "function" || t.origin?.length !== 3)) throw TypeError("Invalid terrain floatingOrigin; pass a FloatingOrigin from @margoengine/cameras/origin.");
|
|
@@ -624,7 +766,7 @@ var ee = 0, te = class r {coreRequirement=__margoCoreRequirement("0.1.0",import.
|
|
|
624
766
|
setup(e) {
|
|
625
767
|
if (this.disposed || !e.gpu) throw new f(this.disposed ? "disposed" : "unsupported", "Terrain requires WebGPU and a live module; attach a new Terrain to a WebGPU engine; see https://margoengine.com/errors#webgpu");
|
|
626
768
|
this.engine = e, this.device = e.gpu, this.textures = this.options.textures(this.device);
|
|
627
|
-
let t = this.textures, n = t.weights, r = t.tint, i = t.holes;
|
|
769
|
+
let t = this.textures, n = t.weights, r = t.tint, i = t.holes, a = t.structure;
|
|
628
770
|
if (r !== void 0 && (!n || !["rgba8unorm", "rgba8unorm-srgb"].includes(r?.format) || r.depthOrArrayLayers !== 1)) throw TypeError("Terrain tint must be one rgba8unorm or rgba8unorm-srgb 2D texture, given with weights");
|
|
629
771
|
if (n !== void 0 && (!Array.isArray(n) || n.length < 2 || n.length > 4 || n.length * 4 < this.options.layers.length)) throw TypeError("Terrain weights must be 2 to 4 rgba8unorm 2D textures, one per 4 layers; pass the same texture twice for 4 layers");
|
|
630
772
|
for (let e of [
|
|
@@ -633,7 +775,8 @@ var ee = 0, te = class r {coreRequirement=__margoCoreRequirement("0.1.0",import.
|
|
|
633
775
|
...t.normalmap ? [t.normalmap] : [],
|
|
634
776
|
...n ?? [],
|
|
635
777
|
...r ? [r] : [],
|
|
636
|
-
...i ? [i] : []
|
|
778
|
+
...i ? [i] : [],
|
|
779
|
+
...a ? [a] : []
|
|
637
780
|
]) if (e.dimension !== "2d" || !(e.usage & GPUTextureUsage.TEXTURE_BINDING) || e.sampleCount !== 1) throw TypeError("Terrain textures must be sampled 2D textures");
|
|
638
781
|
if (t.heightmap.depthOrArrayLayers !== 1 || t.normalmap && t.normalmap.depthOrArrayLayers !== 1 || t.layers.depthOrArrayLayers !== this.options.layers.length || ![
|
|
639
782
|
"rgba8unorm",
|
|
@@ -643,8 +786,8 @@ var ee = 0, te = class r {coreRequirement=__margoCoreRequirement("0.1.0",import.
|
|
|
643
786
|
"r32float",
|
|
644
787
|
"rgba16float",
|
|
645
788
|
"rgba8unorm"
|
|
646
|
-
].includes(t.heightmap.format) || t.normalmap && !["rgba8unorm", "rgba16float"].includes(t.normalmap.format) || n && n.some((e) => e.format !== "rgba8unorm" || e.depthOrArrayLayers !== 1) || i && (!["r8unorm", "rgba8unorm"].includes(i.format) || i.depthOrArrayLayers !== 1)) throw TypeError("Invalid terrain texture format or layer count; use a layers array texture with one layer per terrain layer and a supported heightmap format.");
|
|
647
|
-
let
|
|
789
|
+
].includes(t.heightmap.format) || t.normalmap && !["rgba8unorm", "rgba16float"].includes(t.normalmap.format) || n && n.some((e) => e.format !== "rgba8unorm" || e.depthOrArrayLayers !== 1) || i && (!["r8unorm", "rgba8unorm"].includes(i.format) || i.depthOrArrayLayers !== 1) || a && (a.format !== "rgba8unorm" || a.depthOrArrayLayers !== 1)) throw TypeError("Invalid terrain texture format or layer count; use a layers array texture with one layer per terrain layer and a supported heightmap format.");
|
|
790
|
+
let o = g(this.options.resolution ?? 64), s = (e, t) => {
|
|
648
791
|
if (e.byteLength > this.device.limits.maxBufferSize) throw RangeError("Terrain grid exceeds the device maxBufferSize; reduce resolution.");
|
|
649
792
|
let n = this.device.createBuffer({
|
|
650
793
|
size: e.byteLength,
|
|
@@ -657,56 +800,59 @@ var ee = 0, te = class r {coreRequirement=__margoCoreRequirement("0.1.0",import.
|
|
|
657
800
|
}
|
|
658
801
|
return n;
|
|
659
802
|
};
|
|
660
|
-
this.vertex = o
|
|
803
|
+
this.vertex = s(o.vertices, GPUBufferUsage.VERTEX), this.center = s(o.center, GPUBufferUsage.INDEX), this.ring = s(o.ring, GPUBufferUsage.INDEX), this.centerCount = o.center.length, this.ringCount = o.ring.length, this.rules = s(this.ruleData(), GPUBufferUsage.STORAGE), this.rulesShift.set(this.shiftValues), this.sampler = this.device.createSampler({
|
|
661
804
|
addressModeU: "repeat",
|
|
662
805
|
addressModeV: "repeat",
|
|
663
806
|
magFilter: "linear",
|
|
664
807
|
minFilter: "linear",
|
|
665
|
-
mipmapFilter: "linear"
|
|
808
|
+
mipmapFilter: "linear",
|
|
809
|
+
maxAnisotropy: 16
|
|
666
810
|
});
|
|
667
|
-
let
|
|
811
|
+
let c = GPUShaderStage.VERTEX, l = GPUShaderStage.FRAGMENT;
|
|
668
812
|
this.layout = this.device.createBindGroupLayout({ entries: [
|
|
813
|
+
...t.source?.entries(this.sourceBinding()) ?? [],
|
|
669
814
|
{
|
|
670
815
|
binding: 0,
|
|
671
|
-
visibility:
|
|
816
|
+
visibility: c | l,
|
|
672
817
|
buffer: { type: "uniform" }
|
|
673
818
|
},
|
|
674
819
|
{
|
|
675
820
|
binding: 1,
|
|
676
|
-
visibility:
|
|
821
|
+
visibility: c | l,
|
|
677
822
|
texture: { sampleType: "unfilterable-float" }
|
|
678
823
|
},
|
|
679
824
|
{
|
|
680
825
|
binding: 2,
|
|
681
|
-
visibility:
|
|
826
|
+
visibility: c | l,
|
|
682
827
|
texture: { sampleType: "unfilterable-float" }
|
|
683
828
|
},
|
|
684
829
|
{
|
|
685
830
|
binding: 3,
|
|
686
|
-
visibility:
|
|
831
|
+
visibility: l,
|
|
687
832
|
texture: { viewDimension: "2d-array" }
|
|
688
833
|
},
|
|
689
834
|
{
|
|
690
835
|
binding: 4,
|
|
691
|
-
visibility:
|
|
836
|
+
visibility: l,
|
|
692
837
|
sampler: { type: "filtering" }
|
|
693
838
|
},
|
|
694
839
|
{
|
|
695
840
|
binding: 5,
|
|
696
|
-
visibility:
|
|
841
|
+
visibility: l,
|
|
697
842
|
buffer: { type: "read-only-storage" }
|
|
698
843
|
},
|
|
699
844
|
...[
|
|
700
845
|
...n ?? [],
|
|
701
846
|
...r ? [r] : [],
|
|
702
|
-
...t.holes ? [t.holes] : []
|
|
847
|
+
...t.holes ? [t.holes] : [],
|
|
848
|
+
...a ? [a] : []
|
|
703
849
|
].map((e, t) => ({
|
|
704
850
|
binding: 6 + t,
|
|
705
|
-
visibility:
|
|
851
|
+
visibility: l,
|
|
706
852
|
texture: {}
|
|
707
853
|
}))
|
|
708
854
|
] });
|
|
709
|
-
let
|
|
855
|
+
let u = [
|
|
710
856
|
t.heightmap.createView(),
|
|
711
857
|
(t.normalmap ?? t.heightmap).createView(),
|
|
712
858
|
t.layers.createView({ dimension: "2d-array" }),
|
|
@@ -714,47 +860,51 @@ var ee = 0, te = class r {coreRequirement=__margoCoreRequirement("0.1.0",import.
|
|
|
714
860
|
{ buffer: this.rules },
|
|
715
861
|
...n ? n.map((e) => e.createView()) : [],
|
|
716
862
|
...r ? [r.createView()] : [],
|
|
717
|
-
...t.holes ? [t.holes.createView()] : []
|
|
718
|
-
|
|
863
|
+
...t.holes ? [t.holes.createView()] : [],
|
|
864
|
+
...a ? [a.createView()] : [],
|
|
865
|
+
...t.source?.resources() ?? []
|
|
866
|
+
], d = (e) => this.device.createBindGroup({
|
|
719
867
|
layout: this.layout,
|
|
720
868
|
entries: [{
|
|
721
869
|
binding: 0,
|
|
722
870
|
resource: { buffer: e }
|
|
723
|
-
}, ...
|
|
871
|
+
}, ...u.map((e, t) => ({
|
|
724
872
|
binding: t + 1,
|
|
725
873
|
resource: e
|
|
726
874
|
}))]
|
|
727
875
|
});
|
|
728
|
-
this.ringGroup =
|
|
729
|
-
let
|
|
876
|
+
this.ringGroup = d;
|
|
877
|
+
let p = this.options.floatingOrigin ? 60 : 48;
|
|
730
878
|
for (let e = 0; e < (this.options.levels ?? 6); e++) {
|
|
731
879
|
let e = this.device.createBuffer({
|
|
732
|
-
size:
|
|
880
|
+
size: p * 4,
|
|
733
881
|
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
|
|
734
882
|
}), t = [], n = {
|
|
735
883
|
uniform: e,
|
|
736
884
|
group: void 0,
|
|
737
|
-
values: new Float32Array(
|
|
885
|
+
values: new Float32Array(p),
|
|
738
886
|
shadowUniforms: t,
|
|
739
887
|
shadowGroups: [],
|
|
740
|
-
shadowValues: new Float32Array(
|
|
888
|
+
shadowValues: new Float32Array(p),
|
|
741
889
|
localUniforms: [],
|
|
742
|
-
localGroups: []
|
|
890
|
+
localGroups: [],
|
|
891
|
+
captureUniforms: [],
|
|
892
|
+
captureGroups: []
|
|
743
893
|
};
|
|
744
|
-
this.rings.push(n), n.group =
|
|
894
|
+
this.rings.push(n), n.group = d(e);
|
|
745
895
|
for (let e = 0; e < 4; e++) {
|
|
746
896
|
let e = this.device.createBuffer({
|
|
747
|
-
size:
|
|
897
|
+
size: p * 4,
|
|
748
898
|
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
|
|
749
899
|
});
|
|
750
|
-
t.push(e), n.shadowGroups.push(
|
|
900
|
+
t.push(e), n.shadowGroups.push(d(e));
|
|
751
901
|
}
|
|
752
902
|
}
|
|
753
|
-
let
|
|
754
|
-
this.shadowModule =
|
|
903
|
+
let m = this.device.createShaderModule({ code: this.clip(y + `@fragment fn outside(i:Output){${x}}`) });
|
|
904
|
+
this.shadowModule = m, this.shadowPipeline = this.device.createRenderPipeline({
|
|
755
905
|
layout: this.device.createPipelineLayout({ bindGroupLayouts: [this.layout] }),
|
|
756
906
|
vertex: {
|
|
757
|
-
module:
|
|
907
|
+
module: m,
|
|
758
908
|
entryPoint: "vertex",
|
|
759
909
|
buffers: [{
|
|
760
910
|
arrayStride: 8,
|
|
@@ -766,7 +916,7 @@ var ee = 0, te = class r {coreRequirement=__margoCoreRequirement("0.1.0",import.
|
|
|
766
916
|
}]
|
|
767
917
|
},
|
|
768
918
|
fragment: {
|
|
769
|
-
module:
|
|
919
|
+
module: m,
|
|
770
920
|
entryPoint: "outside",
|
|
771
921
|
targets: []
|
|
772
922
|
},
|
|
@@ -778,7 +928,7 @@ var ee = 0, te = class r {coreRequirement=__margoCoreRequirement("0.1.0",import.
|
|
|
778
928
|
}), this.invalidate();
|
|
779
929
|
}
|
|
780
930
|
ruleData() {
|
|
781
|
-
let e = this.textures.tint, t = !!this.options.floatingOrigin, n = this.shiftValues, r = this.surface() ? 12 : 8, i = r + (t ?
|
|
931
|
+
let e = this.textures.tint, t = !!this.options.floatingOrigin, n = this.shiftValues, r = this.surface() ? 12 : 8, i = r + (t ? 16 : 0), a = new Float32Array(this.options.layers.length * i);
|
|
782
932
|
return this.options.layers.forEach((o, s) => {
|
|
783
933
|
if (a.set([
|
|
784
934
|
o.height[0] - n[1],
|
|
@@ -795,14 +945,18 @@ var ee = 0, te = class r {coreRequirement=__margoCoreRequirement("0.1.0",import.
|
|
|
795
945
|
0
|
|
796
946
|
] : []
|
|
797
947
|
], s * i), !t) return;
|
|
798
|
-
let c = Math.fround(o.scale), l = [
|
|
948
|
+
let c = Math.fround(o.scale), l = this.eye, u = [
|
|
799
949
|
n[0] - this.anchor[0],
|
|
800
950
|
n[1],
|
|
801
951
|
n[2] - this.anchor[1]
|
|
952
|
+
], d = [
|
|
953
|
+
l[0] - this.anchor[0],
|
|
954
|
+
l[1],
|
|
955
|
+
l[2] - this.anchor[1]
|
|
802
956
|
];
|
|
803
|
-
for (let e = 0;
|
|
804
|
-
let
|
|
805
|
-
a[s * i +
|
|
957
|
+
for (let [e, t] of [[u, r], [d, r + 8]]) for (let n = 0; n < 3; n++) {
|
|
958
|
+
let r = Math.floor(e[n] * c / 24), o = r * Math.fround(.302);
|
|
959
|
+
a[s * i + t + n] = e[n] - r * 24 / c, a[s * i + t + 4 + n] = o - Math.floor(o);
|
|
806
960
|
}
|
|
807
961
|
}), a;
|
|
808
962
|
}
|
|
@@ -824,10 +978,10 @@ var ee = 0, te = class r {coreRequirement=__margoCoreRequirement("0.1.0",import.
|
|
|
824
978
|
prepare(e) {
|
|
825
979
|
let t = this.options.origin, r = this.options.size, i = this.options.resolution ?? 64, o = this.options.heightScale ?? 100, s = this.options.heightOffset ?? 0, c = this.options.layers.length, l = this.options.cellSize ?? 1, u = +a(e.format), d = +!!this.textures.normalmap, f = this.options.floatingOrigin, p = this.shiftValues, m = e.camera.position;
|
|
826
980
|
if (f) {
|
|
827
|
-
for (let e = 0; e < 3; e++) this.eye[e] = m[e] + p[e];
|
|
981
|
+
for (let e = 0; e < 3; e++) this.eyeLocal[e] = m[e], this.eye[e] = m[e] + p[e];
|
|
828
982
|
m = this.eye;
|
|
829
|
-
let t = this.rulesShift;
|
|
830
|
-
(t[0] !== p[0] || t[1] !== p[1] || t[2] !== p[2]) && (e.device.queue.writeBuffer(this.rules, 0, this.ruleData()), t.set(p));
|
|
983
|
+
let t = this.rulesShift, n = this.rulesEye, r = this.eye;
|
|
984
|
+
(t[0] !== p[0] || t[1] !== p[1] || t[2] !== p[2] || n[0] !== r[0] || n[1] !== r[1] || n[2] !== r[2]) && (e.device.queue.writeBuffer(this.rules, 0, this.ruleData()), t.set(p), n.set(r));
|
|
831
985
|
}
|
|
832
986
|
if (this.plan.length !== this.rings.length * 2 && (this.plan = new Float64Array(this.rings.length * 2)), !this.anchoredEye && this.options.clipmapAnchor !== "camera") {
|
|
833
987
|
let t = this.engine?.camera?.position ?? e.camera.position;
|
|
@@ -837,12 +991,19 @@ var ee = 0, te = class r {coreRequirement=__margoCoreRequirement("0.1.0",import.
|
|
|
837
991
|
t[2] + p[2]
|
|
838
992
|
];
|
|
839
993
|
}
|
|
840
|
-
let h =
|
|
994
|
+
let h = Q(this.anchoredEye ?? m, p, l, this.rings.length, this.plan), g = 0, _ = 0, v = !1;
|
|
841
995
|
for (let a = 0; a < this.rings.length; a++) {
|
|
842
996
|
let m = this.rings[a].values, y = l * 2 ** a, b = h[a * 2], x = h[a * 2 + 1];
|
|
843
|
-
(m[28] !== Math.fround(b) || m[29] !== Math.fround(x)) && (v = !0), m.set(e.camera.matrix), m.set(e.camera.position, 16), n(m, 20, this.engine.lighting.direction), m.set(this.engine.lighting.radiance, 24), m[28] = b, m[29] = x, m[30] = y, m[31] = i, m[32] = a ? g - b : 0, m[33] = a ? _ - x : 0, m[34] = a, m[35] = d, m[36] = (t?.[0] ?? 0) - p[0], m[37] = (t?.[1] ?? 0) - p[2], m[38] = r[0], m[39] = r[1], m[40] = o, m[41] = s - p[1], m[42] = c, m[43] = u
|
|
997
|
+
(m[28] !== Math.fround(b) || m[29] !== Math.fround(x)) && (v = !0), m.set(e.camera.matrix), m.set(e.camera.position, 16), n(m, 20, this.engine.lighting.direction), m.set(this.engine.lighting.radiance, 24), m[28] = b, m[29] = x, m[30] = y, m[31] = i, m[32] = a ? g - b : 0, m[33] = a ? _ - x : 0, m[34] = a, m[35] = d, m[36] = (t?.[0] ?? 0) - p[0], m[37] = (t?.[1] ?? 0) - p[2], m[38] = r[0], m[39] = r[1], m[40] = o, m[41] = s - p[1], m[42] = c, m[43] = u;
|
|
998
|
+
let S = this.engine.lighting.ambient;
|
|
999
|
+
if (m[44] = S?.[0] ?? 1, m[45] = S?.[1] ?? 1, m[46] = S?.[2] ?? 1, m[47] = 0, f) {
|
|
1000
|
+
m[48] = p[0] - this.anchor[0], m[49] = p[2] - this.anchor[1], m[50] = p[1], m[51] = 0;
|
|
1001
|
+
let t = this.eyeLocal;
|
|
1002
|
+
m[52] = b - t[0], m[53] = x - t[2], m[54] = t[1], m[55] = 0, $(m, e.camera.matrix, t);
|
|
1003
|
+
}
|
|
1004
|
+
this.upload(e.device.queue, this.rings[a].uniform, m), g = b, _ = x;
|
|
844
1005
|
}
|
|
845
|
-
v && this.invalidate(), this.options.captureSurfaceHeight && (this.capturedSurface ??=
|
|
1006
|
+
v && this.invalidate(), this.options.captureSurfaceHeight && (this.capturedSurface ??= te(e.device, this.textures.heightmap, this.layout, f ? T(y) : y, (e) => this.clip(e), !!f), this.capturedSurface?.draw(e.encoder, this.shadowVersion, this.rings, {
|
|
846
1007
|
vertex: this.vertex,
|
|
847
1008
|
center: this.center,
|
|
848
1009
|
ring: this.ring,
|
|
@@ -854,7 +1015,7 @@ var ee = 0, te = class r {coreRequirement=__margoCoreRequirement("0.1.0",import.
|
|
|
854
1015
|
let e = this.base();
|
|
855
1016
|
for (let t of this.lighting) e = t.shader(e, 1);
|
|
856
1017
|
for (let t of this.lighting) t.finish && (e = t.finish(e, 1));
|
|
857
|
-
return e;
|
|
1018
|
+
return this.options.floatingOrigin ? O(e) : e;
|
|
858
1019
|
}
|
|
859
1020
|
colorSource() {
|
|
860
1021
|
if (!this.disposed && this.layout && this.rings[0]?.group) return {
|
|
@@ -864,13 +1025,42 @@ var ee = 0, te = class r {coreRequirement=__margoCoreRequirement("0.1.0",import.
|
|
|
864
1025
|
};
|
|
865
1026
|
}
|
|
866
1027
|
base() {
|
|
867
|
-
let e = this.options.roughness, t = this.surface(), n = e === void 0 ? y : y.replace("o.material=vec4f(0.9,0,1,1)", `o.material=vec4f(${e},0,1,1)`), r = (t ? S(n) : n) + (this.textures?.weights ?
|
|
868
|
-
|
|
1028
|
+
let e = this.options.roughness, t = this.surface(), n = e === void 0 ? y : y.replace("o.material=vec4f(0.9,0,1,1)", `o.material=vec4f(${e},0,1,1)`), r = (t ? S(n) : n) + (this.textures?.weights ? F(this.textures.weights.length, t, !!this.textures.tint, this.options.patches ? {
|
|
1029
|
+
size: this.options.patches.size ?? 14,
|
|
1030
|
+
contrast: this.options.patches.contrast ?? .6,
|
|
1031
|
+
warp: this.options.patches.warp ?? 0,
|
|
1032
|
+
terraces: this.options.patches.terraces ?? 0
|
|
1033
|
+
} : void 0, this.rockShader()) : M(t, this.rockShader()));
|
|
1034
|
+
return this.clip(this.options.floatingOrigin ? T(r) : r);
|
|
869
1035
|
}
|
|
870
1036
|
clip(e) {
|
|
871
1037
|
let t = this.textures;
|
|
872
|
-
return t?.holes
|
|
873
|
-
${e.split(x).join(x + b)}` : e;
|
|
1038
|
+
return t?.holes && (e = `@group(0) @binding(${this.holesBinding()}) var terrainHoles:texture_2d<f32>;
|
|
1039
|
+
${e.split(x).join(x + b)}`), t?.source ? t.source.shader(e, this.sourceBinding()) : e;
|
|
1040
|
+
}
|
|
1041
|
+
holesBinding() {
|
|
1042
|
+
let e = this.textures;
|
|
1043
|
+
return 6 + (e.weights?.length ?? 0) + +!!e.tint;
|
|
1044
|
+
}
|
|
1045
|
+
sourceBinding() {
|
|
1046
|
+
let e = this.textures;
|
|
1047
|
+
return this.holesBinding() + +!!e.holes + +!!e.structure;
|
|
1048
|
+
}
|
|
1049
|
+
rockShader() {
|
|
1050
|
+
let e = this.options.rock;
|
|
1051
|
+
if (e) return {
|
|
1052
|
+
slope: e.slope ?? [28, 44],
|
|
1053
|
+
strata: e.strata ?? 9,
|
|
1054
|
+
ledges: e.ledges ?? .6,
|
|
1055
|
+
dip: e.dip ?? [0, 0],
|
|
1056
|
+
facets: e.facets ?? 18,
|
|
1057
|
+
relief: e.relief ?? .45,
|
|
1058
|
+
gullies: e.gullies ?? 6,
|
|
1059
|
+
undulation: e.undulation ?? 0,
|
|
1060
|
+
tone: e.tone ?? .35,
|
|
1061
|
+
debris: this.textures?.weights && e.debris !== void 0 ? e.debris : -1,
|
|
1062
|
+
map: this.textures?.structure ? this.sourceBinding() - 1 : -1
|
|
1063
|
+
};
|
|
874
1064
|
}
|
|
875
1065
|
surface() {
|
|
876
1066
|
return this.options.layers.some((e) => (e.bump ?? 0) > 0 || (e.macro ?? 0) > 0);
|
|
@@ -948,7 +1138,8 @@ ${e.split(x).join(x + b)}` : e;
|
|
|
948
1138
|
if (u === void 0) {
|
|
949
1139
|
let e = "@fragment fn fragment(i:Output)->@location(0) vec4f {";
|
|
950
1140
|
if (s.split(e).length !== 2) throw t("unsupported", "Terrain capture needs the terrain fragment entry point once; a lighting effect changed it");
|
|
951
|
-
|
|
1141
|
+
let n = x + (this.textures?.holes ? b : ""), r = this.textures?.source;
|
|
1142
|
+
u = c + s.replace(e, "fn margoShade(i:Output)->vec4f {") + `\n@fragment fn capture(i:Output)->MargoSSR {${this.options.floatingOrigin ? D : ""}${r ? r.shader(n, this.sourceBinding()) : n}${A(this.surface(), !!this.options.rock)}let v=normalize(frame.eye.xyz-i.position);${c.includes("var<private> margoPosition:") ? "margoPosition=i.position;" : ""}return margoSSR(n,v,base,i.material.y,i.material.x);}\n`, this.captureCode.size >= 16 && this.captureCode.clear(), this.captureCode.set(l, u);
|
|
952
1143
|
}
|
|
953
1144
|
let d = this.lightingEntries;
|
|
954
1145
|
d.length = 0;
|
|
@@ -988,7 +1179,24 @@ ${e.split(x).join(x + b)}` : e;
|
|
|
988
1179
|
}
|
|
989
1180
|
let m = o.begin();
|
|
990
1181
|
m.setPipeline(p), d.length && m.setBindGroup(1, this.lightingGroup(n.device, p, d)), m.setVertexBuffer(0, this.vertex);
|
|
991
|
-
|
|
1182
|
+
let h = this.otherCamera(n.camera), g = n.view?.index ?? 0;
|
|
1183
|
+
for (let e = 0; e < this.rings.length; e++) {
|
|
1184
|
+
let t = this.rings[e], r = t.group;
|
|
1185
|
+
if (h) {
|
|
1186
|
+
let e = t.captureUniforms[g];
|
|
1187
|
+
e || (e = t.captureUniforms[g] = n.device.createBuffer({
|
|
1188
|
+
size: t.values.length * 4,
|
|
1189
|
+
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
|
|
1190
|
+
}), t.captureGroups[g] = this.ringGroup(e)), t.shadowValues.set(t.values), t.shadowValues.set(n.camera.matrix), t.shadowValues.set(n.camera.position, 16), this.options.floatingOrigin && $(t.shadowValues, n.camera.matrix, this.eyeLocal), this.upload(n.device.queue, e, t.shadowValues), r = t.captureGroups[g];
|
|
1191
|
+
}
|
|
1192
|
+
m.setBindGroup(0, r), m.setIndexBuffer(e ? this.ring : this.center, "uint32"), m.drawIndexed(e ? this.ringCount : this.centerCount);
|
|
1193
|
+
}
|
|
1194
|
+
}
|
|
1195
|
+
otherCamera(e) {
|
|
1196
|
+
let t = this.rings[0].values, n = e.matrix, r = e.position;
|
|
1197
|
+
for (let e = 0; e < 16; e++) if (t[e] !== Math.fround(n[e])) return !0;
|
|
1198
|
+
for (let e = 0; e < 3; e++) if (t[16 + e] !== Math.fround(r[e])) return !0;
|
|
1199
|
+
return !1;
|
|
992
1200
|
}
|
|
993
1201
|
lightingGroup(e, t, n) {
|
|
994
1202
|
let r = this.lightingGroups.get(t);
|
|
@@ -1060,14 +1268,14 @@ ${e.split(x).join(x + b)}` : e;
|
|
|
1060
1268
|
}
|
|
1061
1269
|
}
|
|
1062
1270
|
}
|
|
1063
|
-
drawLocalShadow({ context: e, pass: t, matrix: n, layer: r }) {
|
|
1271
|
+
drawLocalShadow({ context: e, pass: t, matrix: n, layer: r, depthCompare: i = "greater" }) {
|
|
1064
1272
|
if (this.disposed || !this.shadowModule || !this.ringGroup) return;
|
|
1065
1273
|
if (!Number.isInteger(r) || r < 0) throw RangeError("Invalid local shadow layer; pass the integer layer index LocalShadows gives");
|
|
1066
|
-
|
|
1067
|
-
this.
|
|
1068
|
-
layout:
|
|
1274
|
+
if (i !== "greater" && i !== "less-equal") throw RangeError("Invalid local shadow depthCompare; pass 'greater' or 'less-equal'");
|
|
1275
|
+
let a = e.device, o = this.shadowModule, s = i === "less-equal", c = r * 2 + +!!s, l = this.localShadowPipelines[+!!s] ??= a.createRenderPipeline({
|
|
1276
|
+
layout: a.createPipelineLayout({ bindGroupLayouts: [this.layout] }),
|
|
1069
1277
|
vertex: {
|
|
1070
|
-
module:
|
|
1278
|
+
module: o,
|
|
1071
1279
|
entryPoint: "vertex",
|
|
1072
1280
|
buffers: [{
|
|
1073
1281
|
arrayStride: 8,
|
|
@@ -1079,22 +1287,23 @@ ${e.split(x).join(x + b)}` : e;
|
|
|
1079
1287
|
}]
|
|
1080
1288
|
},
|
|
1081
1289
|
fragment: {
|
|
1082
|
-
module:
|
|
1290
|
+
module: o,
|
|
1083
1291
|
entryPoint: "outside",
|
|
1084
1292
|
targets: []
|
|
1085
1293
|
},
|
|
1086
1294
|
depthStencil: {
|
|
1087
1295
|
format: "depth32float",
|
|
1088
1296
|
depthWriteEnabled: !0,
|
|
1089
|
-
depthCompare:
|
|
1297
|
+
depthCompare: i
|
|
1090
1298
|
}
|
|
1091
|
-
})
|
|
1299
|
+
});
|
|
1300
|
+
t.setPipeline(l), t.setVertexBuffer(0, this.vertex);
|
|
1092
1301
|
for (let e = 0; e < this.rings.length; e++) {
|
|
1093
|
-
let
|
|
1094
|
-
|
|
1095
|
-
size:
|
|
1302
|
+
let r = this.rings[e], i = r.localUniforms[c];
|
|
1303
|
+
i || (i = r.localUniforms[c] = a.createBuffer({
|
|
1304
|
+
size: r.values.length * 4,
|
|
1096
1305
|
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
|
|
1097
|
-
}),
|
|
1306
|
+
}), r.localGroups[c] = this.ringGroup(i)), r.shadowValues.set(r.values), r.shadowValues.set(n), this.upload(a.queue, i, r.shadowValues), t.setBindGroup(0, r.localGroups[c]), t.setIndexBuffer(e ? this.ring : this.center, "uint32"), t.drawIndexed(e ? this.ringCount : this.centerCount);
|
|
1098
1307
|
}
|
|
1099
1308
|
}
|
|
1100
1309
|
upload(e, t, n) {
|
|
@@ -1119,7 +1328,8 @@ ${e.split(x).join(x + b)}` : e;
|
|
|
1119
1328
|
this.textures.layers,
|
|
1120
1329
|
...this.textures.weights ?? [],
|
|
1121
1330
|
this.textures.tint,
|
|
1122
|
-
this.textures.holes
|
|
1331
|
+
this.textures.holes,
|
|
1332
|
+
this.textures.structure
|
|
1123
1333
|
] : [])) e?.destroy();
|
|
1124
1334
|
for (let e of [
|
|
1125
1335
|
this.vertex,
|
|
@@ -1130,9 +1340,10 @@ ${e.split(x).join(x + b)}` : e;
|
|
|
1130
1340
|
for (let e of this.rings) for (let t of [
|
|
1131
1341
|
e.uniform,
|
|
1132
1342
|
...e.shadowUniforms,
|
|
1133
|
-
...e.localUniforms
|
|
1343
|
+
...e.localUniforms,
|
|
1344
|
+
...e.captureUniforms
|
|
1134
1345
|
]) t?.destroy();
|
|
1135
|
-
this.textures = void 0, this.vertex = this.center = this.ring = this.rules = void 0, this.rings = [], this.pipelines.clear(), this.occluder = void 0, this.lightingGroups.clear(), this.
|
|
1346
|
+
this.textures = void 0, this.vertex = this.center = this.ring = this.rules = void 0, this.rings = [], this.pipelines.clear(), this.occluder = void 0, this.lightingGroups.clear(), this.localShadowPipelines = [], this.shadowPipeline = this.shadowModule = this.ringGroup = void 0, this.layout = void 0, this.sampler = void 0;
|
|
1136
1347
|
}
|
|
1137
1348
|
onDeviceRestored(e) {
|
|
1138
1349
|
this.release(), this.setup(e);
|
|
@@ -1152,4 +1363,4 @@ ${e.split(x).join(x + b)}` : e;
|
|
|
1152
1363
|
}
|
|
1153
1364
|
};
|
|
1154
1365
|
//#endregion
|
|
1155
|
-
export {
|
|
1366
|
+
export { Z as a, K as c, se as i, g as l, Q as n, ae as o, le as r, oe as s, fe as t };
|