@margoengine/vfx 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +1642 -3
- package/dist/base-C9upJytz.js +658 -0
- package/dist/base.d.ts +767 -0
- package/dist/base.js +1 -0
- package/dist/blown-sand.d.ts +218 -0
- package/dist/blown-sand.js +700 -0
- package/dist/clouds.d.ts +229 -0
- package/dist/clouds.js +566 -0
- package/dist/compat-CVSEj1Bp.js +217 -0
- package/dist/compat.d.ts +59 -0
- package/dist/curves.d.ts +11 -0
- package/dist/curves.js +59 -0
- package/dist/debris.d.ts +111 -0
- package/dist/debris.js +258 -0
- package/dist/depth-collide.d.ts +7 -0
- package/dist/depth-collide.js +96 -0
- package/dist/emitter.d.ts +10 -0
- package/dist/emitter.js +125 -0
- package/dist/events.d.ts +9 -0
- package/dist/events.js +131 -0
- package/dist/extra-CgGjbojB.js +4 -0
- package/dist/extra.d.ts +2 -0
- package/dist/fields.d.ts +11 -0
- package/dist/fields.js +95 -0
- package/dist/fire-front.d.ts +539 -0
- package/dist/fire-front.js +840 -0
- package/dist/fire.d.ts +119 -0
- package/dist/fire.js +173 -0
- package/dist/flame-ramp-VoBKpYnM.js +75 -0
- package/dist/flame-ramp.d.ts +61 -0
- package/dist/flame.d.ts +260 -0
- package/dist/flame.js +605 -0
- package/dist/flipbook.d.ts +7 -0
- package/dist/flipbook.js +23 -0
- package/dist/fog-B-kBv7g0.js +32 -0
- package/dist/frame-BuvMFBPp.js +4 -0
- package/dist/frame.d.ts +7 -0
- package/dist/gunfire.d.ts +696 -0
- package/dist/gunfire.js +1204 -0
- package/dist/hash-Btsp6fGK.js +7 -0
- package/dist/hash.d.ts +2 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +60 -0
- package/dist/json.d.ts +53 -0
- package/dist/json.js +237 -0
- package/dist/lit.d.ts +11 -0
- package/dist/lit.js +105 -0
- package/dist/mesh.d.ts +14 -0
- package/dist/mesh.js +225 -0
- package/dist/node-system.d.ts +315 -0
- package/dist/node-system.js +384 -0
- package/dist/nodes.d.ts +14 -0
- package/dist/nodes.js +293 -0
- package/dist/obstacles.d.ts +3 -0
- package/dist/obstacles.js +25 -0
- package/dist/over-life.d.ts +14 -0
- package/dist/over-life.js +93 -0
- package/dist/require-webgpu--tUXPsmA.js +7 -0
- package/dist/ribbons.d.ts +20 -0
- package/dist/ribbons.js +151 -0
- package/dist/schedule.d.ts +29 -0
- package/dist/schedule.js +47 -0
- package/dist/settings.d.ts +362 -0
- package/dist/settings.js +707 -0
- package/dist/shader.d.ts +44 -0
- package/dist/shadows.d.ts +26 -0
- package/dist/shadows.js +182 -0
- package/dist/shapes.d.ts +34 -0
- package/dist/shapes.js +81 -0
- package/dist/shared/fog.d.ts +33 -0
- package/dist/shared/pass.d.ts +53 -0
- package/dist/shared/settings.d.ts +89 -0
- package/dist/skinned.d.ts +11 -0
- package/dist/skinned.js +104 -0
- package/dist/smoke.d.ts +215 -0
- package/dist/smoke.js +280 -0
- package/dist/soft.d.ts +17 -0
- package/dist/soft.js +55 -0
- package/dist/solid.d.ts +168 -0
- package/dist/solid.js +403 -0
- package/dist/sort.d.ts +3 -0
- package/dist/sort.js +58 -0
- package/dist/space.d.ts +14 -0
- package/dist/space.js +88 -0
- package/dist/stretch.d.ts +7 -0
- package/dist/stretch.js +40 -0
- package/dist/texture.d.ts +8 -0
- package/dist/texture.js +45 -0
- package/dist/view-RKprOPAy.js +30 -0
- package/dist/view-rect-DkeiiUtw.js +13 -0
- package/dist/wrap.d.ts +3 -0
- package/dist/wrap.js +5 -0
- package/package.json +192 -6
package/dist/flame.js
ADDED
|
@@ -0,0 +1,605 @@
|
|
|
1
|
+
// @margo/core-built 0.1.0
|
|
2
|
+
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
+
|
|
4
|
+
import { t as e } from "./view-RKprOPAy.js";
|
|
5
|
+
import { n as t, r as n, t as r } from "./fog-B-kBv7g0.js";
|
|
6
|
+
import { t as i } from "./view-rect-DkeiiUtw.js";
|
|
7
|
+
import { soft as a, softDepthWgsl as o } from "./soft.js";
|
|
8
|
+
import { a as s, c, i as l, n as u, o as d, r as f, s as p, t as m } from "./flame-ramp-VoBKpYnM.js";
|
|
9
|
+
import { t as h } from "./require-webgpu--tUXPsmA.js";
|
|
10
|
+
import { finiteVector as g } from "@margoengine/math/validate";
|
|
11
|
+
//#region ../shared/noise-wgsl.js
|
|
12
|
+
var _ = "fn hash1(n:u32)->f32 {var x=n*747796405u+2891336453u;x=((x>>((x>>28u)+4u))^x)*277803737u;x=(x>>22u)^x;return f32(x)/4294967295.0;}\nfn hash3(p:vec3i)->f32 {let q=bitcast<vec3u>(p);return hash1((q.x*73856093u)^(q.y*19349663u)^(q.z*83492791u));}\nfn vnoise3(p:vec3f)->f32 {\n let i=vec3i(floor(p));let f=p-floor(p);let s=f*f*(3.0-2.0*f);\n let a=mix(mix(hash3(i),hash3(i+vec3i(1,0,0)),s.x),mix(hash3(i+vec3i(0,1,0)),hash3(i+vec3i(1,1,0)),s.x),s.y);\n let b=mix(mix(hash3(i+vec3i(0,0,1)),hash3(i+vec3i(1,0,1)),s.x),mix(hash3(i+vec3i(0,1,1)),hash3(i+vec3i(1,1,1)),s.x),s.y);\n return mix(a,b,s.z);\n}", v = 9.80665, y = .5, b = .25;
|
|
13
|
+
function x(e) {
|
|
14
|
+
if (!(Number.isFinite(e) && e >= 0)) throw RangeError("Fireline intensity must be a finite kW/m of 0 or more");
|
|
15
|
+
return .0775 * e ** .46;
|
|
16
|
+
}
|
|
17
|
+
function S(e) {
|
|
18
|
+
if (!(Number.isFinite(e) && e >= 0)) throw RangeError("Flame length must be a finite number of metres, 0 or more");
|
|
19
|
+
return (e / .0775) ** (1 / .46);
|
|
20
|
+
}
|
|
21
|
+
function C(e, t) {
|
|
22
|
+
if (!(Number.isFinite(e) && e >= 0)) throw RangeError("Heat release must be a finite number of watts, 0 or more");
|
|
23
|
+
if (!(Number.isFinite(t) && t >= 0)) throw RangeError("Fire diameter must be a finite number of metres, 0 or more");
|
|
24
|
+
return Math.max(.235 * (e / 1e3) ** .4 - 1.02 * t, 0);
|
|
25
|
+
}
|
|
26
|
+
var w = 1.5;
|
|
27
|
+
function T(e) {
|
|
28
|
+
if (!(Number.isFinite(e) && e > 0)) throw RangeError("Fire diameter must be a finite number of metres above 0");
|
|
29
|
+
return w / Math.sqrt(e);
|
|
30
|
+
}
|
|
31
|
+
function E(e) {
|
|
32
|
+
let [t, n] = e;
|
|
33
|
+
return Math.max(Math.min(Math.sqrt(4 * t * n / Math.PI), 2 * Math.min(t, n)), .05);
|
|
34
|
+
}
|
|
35
|
+
var D = 5.95, O = 5.96, ee = .5, k = 1.35, A = 2, j = 4, M = 18, N = 64, P = 256, F = .03, te = 5.588238, I = 5e-4, L = .15, R = 3, z = 4, B = 16, V = .5, H = .3;
|
|
36
|
+
function U(e, t, n = H) {
|
|
37
|
+
if (!(Number.isFinite(e) && e >= 0)) throw RangeError("Heat release must be a finite number of watts, 0 or more");
|
|
38
|
+
if (!(Number.isFinite(n) && n >= 0 && n <= 1)) throw RangeError("Radiant fraction must be a number from 0 to 1");
|
|
39
|
+
return n * e * s(t);
|
|
40
|
+
}
|
|
41
|
+
function W(e, t) {
|
|
42
|
+
if (!(Number.isFinite(e) && e >= 0)) throw RangeError("Flame length must be a finite number of metres, 0 or more");
|
|
43
|
+
if (!(Number.isFinite(t) && t >= 0)) throw RangeError("Fire diameter must be a finite number of metres, 0 or more");
|
|
44
|
+
return 1e3 * ((e + 1.02 * t) / .235) ** 2.5;
|
|
45
|
+
}
|
|
46
|
+
var G = (e) => {
|
|
47
|
+
let t = Math.imul(e >>> 0, 747796405) + 2891336453 >>> 0;
|
|
48
|
+
return t = Math.imul((t >>> (t >>> 28) + 4 ^ t) >>> 0, 277803737) >>> 0, t = (t >>> 22 ^ t) >>> 0, t / 4294967295;
|
|
49
|
+
}, K = (e) => {
|
|
50
|
+
let t = Math.floor(e), n = e - t, r = n * n * (3 - 2 * n);
|
|
51
|
+
return G(t | 0) + (G(t + 1 | 0) - G(t | 0)) * r;
|
|
52
|
+
}, q = (e) => Math.min(Math.max(e, 0), 1), J = 128, Y = 112, X = 116, Z = 120, ne = 124, Q = /* @__PURE__ */ new WeakMap(), re = 0, $ = /* @__PURE__ */ new WeakMap(), ie = (e, n, r, i) => `
|
|
53
|
+
struct F {camera:mat4x4f,eye:vec4f,base:vec4f,shape:vec4f,turn:vec4f,motion:vec4f,look:vec4f,box:vec4f,forward:vec4f,ramp:array<vec4f,${B}>,extra:vec4f,ground:vec4f,glow:vec4f,outer:vec4f}
|
|
54
|
+
@group(0) @binding(0) var<uniform> u:F;
|
|
55
|
+
${n ? t(0, 5) : ""}
|
|
56
|
+
${i ? o(e) : ""}
|
|
57
|
+
struct Out {@builtin(position) clip:vec4f,@location(0) world:vec3f}
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
fn toWorld(p:vec3f)->vec3f {
|
|
61
|
+
var s=vec3f(p.x+u.shape.z*p.y,p.y,p.z+u.shape.w*p.y);
|
|
62
|
+
s.y+=u.ground.x*s.x+u.ground.y*s.z;
|
|
63
|
+
return u.base.xyz+vec3f(u.turn.x*s.x+u.turn.y*s.z,s.y,-u.turn.y*s.x+u.turn.x*s.z);
|
|
64
|
+
}
|
|
65
|
+
fn toLocal(v:vec3f)->vec3f {
|
|
66
|
+
var r=vec3f(u.turn.x*v.x-u.turn.y*v.z,v.y,u.turn.y*v.x+u.turn.x*v.z);
|
|
67
|
+
r.y-=u.ground.x*r.x+u.ground.y*r.z;
|
|
68
|
+
return vec3f(r.x-u.shape.z*r.y,r.y,r.z-u.shape.w*r.y);
|
|
69
|
+
}
|
|
70
|
+
fn corner(v:u32,margin:f32,top:f32)->Out {
|
|
71
|
+
var corners=array<u32,36>(0u,2u,1u,1u,2u,3u,4u,5u,6u,5u,7u,6u,0u,1u,4u,1u,5u,4u,2u,6u,3u,3u,6u,7u,0u,4u,2u,2u,4u,6u,1u,3u,5u,3u,7u,5u);
|
|
72
|
+
let k=corners[v];
|
|
73
|
+
let c=vec3f(f32(k&1u),f32((k>>1u)&1u),f32((k>>2u)&1u));
|
|
74
|
+
let w=toWorld(vec3f((c.x*2.0-1.0)*(u.shape.x+margin),mix(u.box.z,top,c.y),(c.z*2.0-1.0)*(u.shape.y+margin)));
|
|
75
|
+
var o:Out;o.clip=u.camera*vec4f(w,1.0);o.world=w;return o;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
@vertex fn vertex(@builtin(vertex_index) v:u32)->Out {return corner(v,u.box.x,u.box.y);}
|
|
79
|
+
@vertex fn glowVertex(@builtin(vertex_index) v:u32)->Out {return corner(v,u.outer.x,u.outer.y);}
|
|
80
|
+
${_}
|
|
81
|
+
fn vnoise1(v:f32)->f32 {let i=floor(v);let f=v-i;let s=f*f*(3.0-2.0*f);let a=bitcast<u32>(i32(i));return mix(hash1(a),hash1(a+1u),s);}
|
|
82
|
+
fn fbm(p:vec3f)->f32 {return 0.5161*vnoise3(p)+0.2581*vnoise3(p*2.03+vec3f(17.1,3.7,9.2))+0.129*vnoise3(p*4.07+vec3f(5.3,11.9,2.1))+0.0645*vnoise3(p*8.11+vec3f(9.7,1.3,13.4))+0.0323*vnoise3(p*16.3+vec3f(3.1,7.7,5.9));}
|
|
83
|
+
fn table(x:f32)->vec3f {let t=clamp(x,0.0,1.0)*15.0;let i=u32(floor(t));let j=min(i+1u,15u);return mix(u.ramp[i].rgb,u.ramp[j].rgb,t-floor(t));}
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
fn trees(q:vec3f,h0:f32)->vec2f {
|
|
89
|
+
let S=u.extra.y;let c=floor(q.xz/S);var best=vec2f(-9.0,h0);
|
|
90
|
+
for(var j=-1;j<=1;j++){for(var i=-1;i<=1;i++){
|
|
91
|
+
let id=vec2i(c)+vec2i(i,j);let at=(vec2f(id)+0.2+0.6*vec2f(hash3(vec3i(id,7)),hash3(vec3i(id,13))))*S;
|
|
92
|
+
let h=(h0+0.3*hash3(vec3i(id,29)))/(0.6+0.8*hash3(vec3i(id,41)));
|
|
93
|
+
let r=0.34*pow(clamp(1.0-h/${k.toFixed(2)},0.0,1.0),0.7)*smoothstep(-0.08,0.12,h);
|
|
94
|
+
let v=2.6*(r-length(q.xz-at)/S);
|
|
95
|
+
if(v>best.x){best=vec2f(v,h);}
|
|
96
|
+
}}
|
|
97
|
+
return best;
|
|
98
|
+
}
|
|
99
|
+
fn zone(q:vec3f)->vec2f {
|
|
100
|
+
let L=u.base.w;var h=q.y/L;
|
|
101
|
+
if(h<u.box.z/L||h>${A.toFixed(2)}){return vec2f(0.0);}
|
|
102
|
+
let along=select(q.z,q.x,u.shape.x>=u.shape.y);
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
h/=1.0+select(0.6,0.0,u.extra.y>0.0)*smoothstep(0.5*L,2.0*L,max(u.shape.x,u.shape.y))*(vnoise1(along/(0.8*L)+3.7*u.look.w+0.15*u.turn.z*u.eye.w)-0.5);
|
|
107
|
+
|
|
108
|
+
var column=0.0;
|
|
109
|
+
if(u.extra.y>0.0){
|
|
110
|
+
let t=trees(q,h);column=t.x;h=max(t.y,0.0);
|
|
111
|
+
if(column<-1.2){return vec2f(0.0);}
|
|
112
|
+
}
|
|
113
|
+
let puff=sin(6.2831853*(u.turn.z*u.eye.w-q.y/u.turn.w+vnoise1(along/u.look.z+u.look.w)));
|
|
114
|
+
let taper=pow(clamp(1.0-h/${k.toFixed(2)},0.0,1.0),0.6);
|
|
115
|
+
let narrow=min(u.shape.x,u.shape.y)+0.12*L;
|
|
116
|
+
let ext=max(vec2f(u.shape.x,u.shape.y)+0.12*L-narrow*(1.0-taper)-0.2*L*smoothstep(0.0,1.3,h),vec2f(0.0));
|
|
117
|
+
let d=abs(q.xz)-ext;
|
|
118
|
+
var inner=-(length(max(d,vec2f(0.0)))+min(max(d.x,d.y),0.0))/(0.35*L+0.5*narrow);
|
|
119
|
+
if(u.extra.y>0.0){inner=min(inner+0.6,column);}
|
|
120
|
+
inner+=0.3*u.motion.z*puff*smoothstep(0.1,0.9,h);
|
|
121
|
+
if(inner<-1.2){return vec2f(0.0);}
|
|
122
|
+
let s=u.motion.y;let rise=u.motion.x*u.eye.w;
|
|
123
|
+
let warp=vec3f(vnoise3(vec3f(q.x/s,(q.y-rise)/(2.2*s),q.z/s)+vec3f(31.7,0.0,7.3)),0.0,vnoise3(vec3f(q.x/s,(q.y-rise)/(2.2*s),q.z/s)+vec3f(2.9,0.0,43.1)))-vec3f(0.5,0.0,0.5);
|
|
124
|
+
let p=vec3f(q.x/s+1.4*warp.x,(q.y-rise)/(2.6*s),q.z/s+1.4*warp.z);
|
|
125
|
+
let n=fbm(p);
|
|
126
|
+
let amp=mix(0.35,1.25,smoothstep(0.0,1.1,h));
|
|
127
|
+
var v=inner+amp*(n-0.5)*2.0-0.55*smoothstep(0.45,1.55,h)+0.25;
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
if(v>-0.35&&v<0.35){
|
|
131
|
+
let w=vec3f(p.x*4.0+2.0*warp.x,p.y*1.4,p.z*4.0+2.0*warp.z);
|
|
132
|
+
v+=(0.24*(vnoise3(w)-0.5)+0.14*(vnoise3(w*2.0+vec3f(7.3,1.1,3.9))-0.5))*2.0*smoothstep(0.0,0.6,h+0.2);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
let density=smoothstep(0.0,0.03,v)*smoothstep(0.0,0.08,h+0.3*(n-0.5));
|
|
137
|
+
|
|
138
|
+
let heat=clamp(2.0*v+0.3-0.7*h+0.25*(1.0-smoothstep(0.0,0.15,h)),0.0,1.0);
|
|
139
|
+
return vec2f(density,heat);
|
|
140
|
+
}
|
|
141
|
+
fn encodeSrgb(c:vec3f)->vec3f {return select(1.055*pow(c,vec3f(1.0/2.4))-0.055,c*12.92,c<=vec3f(0.0031308));}
|
|
142
|
+
|
|
143
|
+
fn boxHit(o:vec3f,d:vec3f,lo:vec3f,hi:vec3f)->vec2f {
|
|
144
|
+
let inv=1.0/select(d,vec3f(1e-6),abs(d)<vec3f(1e-6));
|
|
145
|
+
let ta=(lo-o)*inv;let tb=(hi-o)*inv;
|
|
146
|
+
let tn=min(ta,tb);let tf=max(ta,tb);
|
|
147
|
+
return vec2f(max(max(tn.x,tn.y),tn.z),min(min(tf.x,tf.y),tf.z));
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
struct Ray {o:vec3f,d:vec3f,rd:vec3f,span:vec4f}
|
|
152
|
+
fn rayOf(i:Out,margin:f32,top:f32,glow:bool)->Ray {
|
|
153
|
+
var r:Ray;
|
|
154
|
+
let ray=i.world-u.eye.xyz;let along=length(ray);r.rd=ray/along;
|
|
155
|
+
r.o=toLocal(u.eye.xyz-u.base.xyz);r.d=toLocal(r.rd);
|
|
156
|
+
let m=vec2f(u.shape.x,u.shape.y)+margin;
|
|
157
|
+
let box=boxHit(r.o,r.d,vec3f(-m.x,u.box.z,-m.y),vec3f(m.x,top,m.y));
|
|
158
|
+
let enter=box.x;let leave=box.y;
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
${i ? "let mine=!(abs(along-leave)>abs(along-max(enter,0.0))&&enter>0.0);" : "let mine=select(!(abs(along-enter)>abs(along-leave)||enter<0.0),abs(along-leave)<=abs(along-enter),glow&&enter<0.0);"}
|
|
162
|
+
var end=leave;
|
|
163
|
+
${i ? "if(textureLoad(sceneDepth,vec2i(i.clip.xy),0)>0.0){end=min(end,softGap(i.clip,0.0)/max(dot(r.rd,u.forward.xyz),1e-4));}" : ""}
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
var ground=-1.0;
|
|
167
|
+
let floor=select(0.0,u.box.z+0.16*u.base.w,u.extra.y>0.0);
|
|
168
|
+
if(r.d.y<0.0){let tg=(floor-r.o.y)/r.d.y;if(tg>max(enter,0.0)&&tg<end){end=tg;ground=tg;}}
|
|
169
|
+
r.span=vec4f(max(enter,0.0),end,ground,select(0.0,1.0,mine&&end>max(enter,0.0)));
|
|
170
|
+
return r;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
struct March {glow:vec3f,trans:f32,middle:f32,surface:f32}
|
|
175
|
+
fn march(o:vec3f,d:vec3f,t0:f32,t1:f32,pixel:vec2f,stopAt:f32)->March {
|
|
176
|
+
var m:March;m.glow=vec3f(0.0);m.trans=1.0;m.middle=t0;m.surface=-1.0;
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
let dt=min((t1-t0)/${N}.0,${F}*u.base.w);
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
let px=pixel+${te}*u.extra.x;let dither=fract(52.9829189*fract(0.06711056*px.x+0.00583715*px.y));
|
|
183
|
+
var t=t0+dt*dither;
|
|
184
|
+
let k=u.box.w/u.base.w;
|
|
185
|
+
var last=t;var entered=false;
|
|
186
|
+
for(var n=0;n<${P};n++){
|
|
187
|
+
if(t>t1){break;}
|
|
188
|
+
var z=zone(o+d*t);
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
if(z.x>0.0&&!entered){
|
|
192
|
+
entered=true;var a0=last;var b0=t;
|
|
193
|
+
for(var r=0;r<5;r++){let mid=0.5*(a0+b0);if(zone(o+d*mid).x>0.0){b0=mid;}else{a0=mid;}}
|
|
194
|
+
t=b0;z=zone(o+d*t);
|
|
195
|
+
}
|
|
196
|
+
last=t;
|
|
197
|
+
if(z.x>0.0){
|
|
198
|
+
|
|
199
|
+
let a=1.0-exp(-z.x*(0.2+0.8*z.y)*k*dt);
|
|
200
|
+
m.glow+=m.trans*a*table(z.y);
|
|
201
|
+
if(m.trans>0.5){m.middle=t;}
|
|
202
|
+
m.trans*=1.0-a;
|
|
203
|
+
if(stopAt>0.0&&m.trans<stopAt){m.surface=t;break;}
|
|
204
|
+
if(m.trans<0.004){break;}
|
|
205
|
+
}
|
|
206
|
+
t+=select(dt,2.0*dt,z.x<=0.0);
|
|
207
|
+
}
|
|
208
|
+
return m;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
fn glowAt(e:vec3f,rd:vec3f,ta:f32,tb:f32,tg:f32)->vec3f {
|
|
214
|
+
let n=i32(u.extra.w);
|
|
215
|
+
if(n<=0||tb<=ta){return vec3f(0.0);}
|
|
216
|
+
let L=u.base.w;let R=u.extra.z;let a2=0.0625*L*L;let kR=1.0/(R*R+a2);
|
|
217
|
+
let long=u.shape.x>=u.shape.y;let span=1.6*max(u.shape.x,u.shape.y);
|
|
218
|
+
let normal=normalize(vec3f(-u.outer.z,1.0,-u.outer.w));
|
|
219
|
+
var air=0.0;var lit=0.0;
|
|
220
|
+
for(var k=0;k<n;k++){
|
|
221
|
+
let a=select(0.0,(f32(k)/f32(max(n-1,1))-0.5)*span,n>1);
|
|
222
|
+
let s=toWorld(select(vec3f(0.0,u.glow.w,a),vec3f(a,u.glow.w,0.0),long));
|
|
223
|
+
let es=s-e;let tc=dot(es,rd);let h2=max(dot(es,es)-tc*tc,0.0);
|
|
224
|
+
let c2=R*R-h2;
|
|
225
|
+
if(c2>0.0){
|
|
226
|
+
let c=sqrt(c2);let x0=clamp(ta-tc,-c,c);let x1=clamp(tb-tc,-c,c);let b=sqrt(h2+a2);
|
|
227
|
+
air+=max((atan(x1/b)-atan(x0/b))/b-(x1-x0)*kR,0.0);
|
|
228
|
+
}
|
|
229
|
+
if(tg>0.0){
|
|
230
|
+
let sv=s-(e+rd*tg);let dd=dot(sv,sv);
|
|
231
|
+
if(dd<R*R){lit+=max(dot(normal,sv),0.0)*inverseSqrt(max(dd,1e-6))*(1.0/(dd+a2)-kR);}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
return u.glow.rgb*(u.ground.w*0.07957747*air+u.ground.z*0.31830989*lit);
|
|
235
|
+
}
|
|
236
|
+
@fragment fn fragment(i:Out)->@location(0) vec4f {
|
|
237
|
+
let r=rayOf(i,u.box.x,u.box.y,false);
|
|
238
|
+
if(r.span.w<=0.0){discard;}
|
|
239
|
+
let m=march(r.o,r.d,r.span.x,r.span.y,i.clip.xy,0.0);
|
|
240
|
+
|
|
241
|
+
let alpha=(1.0-m.trans*m.trans)*u.motion.w;
|
|
242
|
+
if(alpha<=0.0){discard;}
|
|
243
|
+
|
|
244
|
+
var c=u.look.x*(vec3f(1.0)-exp(-u.look.y*m.glow/(max(1.0-m.trans,1e-5)*u.look.x)));
|
|
245
|
+
${n ? "c=mix(c,margoFog.color.rgb,fogAmount(u.eye.xyz+r.rd*m.middle));" : ""}
|
|
246
|
+
${r ? "c=encodeSrgb(max(c,vec3f(0.0)));" : ""}
|
|
247
|
+
return vec4f(c*alpha,alpha);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
struct Glow {@location(0) color:vec4f,@builtin(frag_depth) depth:f32}
|
|
252
|
+
@fragment fn glowFragment(i:Out)->Glow {
|
|
253
|
+
let r=rayOf(i,u.outer.x,u.outer.y,true);
|
|
254
|
+
if(r.span.w<=0.0){discard;}
|
|
255
|
+
var add=glowAt(u.eye.xyz,r.rd,r.span.x,r.span.y,r.span.z);
|
|
256
|
+
${n ? "add*=1.0-fogAmount(u.eye.xyz+r.rd*r.span.y);" : ""}
|
|
257
|
+
if(max(add.x,max(add.y,add.z))<=0.0){discard;}
|
|
258
|
+
${r ? "add=encodeSrgb(max(add,vec3f(0.0)));" : ""}
|
|
259
|
+
var o:Glow;o.color=vec4f(add,0.0);o.depth=1.0;
|
|
260
|
+
if(r.span.x>0.0){let p=u.camera*vec4f(u.eye.xyz+r.rd*r.span.x,1.0);o.depth=clamp(p.z/p.w,0.0,1.0);}
|
|
261
|
+
return o;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
struct Depth {@builtin(frag_depth) depth:f32}
|
|
266
|
+
@fragment fn depthFragment(i:Out)->Depth {
|
|
267
|
+
let r=rayOf(i,u.box.x,u.box.y,false);
|
|
268
|
+
if(r.span.w<=0.0||u.motion.w<1.0){discard;}
|
|
269
|
+
let m=march(r.o,r.d,r.span.x,r.span.y,i.clip.xy,${ee});
|
|
270
|
+
if(m.surface<0.0){discard;}
|
|
271
|
+
let p=u.camera*vec4f(u.eye.xyz+r.rd*m.surface,1.0);
|
|
272
|
+
var o:Depth;o.depth=clamp(p.z/p.w,0.0,1.0);return o;
|
|
273
|
+
}
|
|
274
|
+
`, ae = class t {coreRequirement=__margoCoreRequirement("0.1.0",import.meta.url);
|
|
275
|
+
constructor(e, t) {
|
|
276
|
+
this.disposed = !1, this.clock = 0, this.current = 0, this.key = [], this.values = new Float32Array(J), this.view = /* @__PURE__ */ new Float32Array(16), this.heat = {
|
|
277
|
+
position: [
|
|
278
|
+
0,
|
|
279
|
+
0,
|
|
280
|
+
0
|
|
281
|
+
],
|
|
282
|
+
length: 0,
|
|
283
|
+
footprint: [0, 0],
|
|
284
|
+
yaw: 0,
|
|
285
|
+
lean: [0, 0],
|
|
286
|
+
time: 0
|
|
287
|
+
}, this.fogData = /* @__PURE__ */ new Float32Array(12), this.steady = [], this.coreFloor = 0, this.samples = 0, this.owned = [], this.groups = /* @__PURE__ */ new Map(), this.attachment = {
|
|
288
|
+
view: void 0,
|
|
289
|
+
loadOp: "load",
|
|
290
|
+
storeOp: "store"
|
|
291
|
+
}, this.depthAttachment = {
|
|
292
|
+
view: void 0,
|
|
293
|
+
depthReadOnly: !0
|
|
294
|
+
}, this.name = "vfx-flame:" + re++, this.seed = t * 7.31 % 97, this.options = e, this.check();
|
|
295
|
+
let n = e.lights, r = n && "names" in n && "clustered" in n ? n : void 0, i = r ? r.clustered : n && "maxLights" in n ? n : void 0;
|
|
296
|
+
if (n && !i) throw TypeError("Invalid Flame lights; pass a Clustered from @margoengine/clustered or its LightGroups");
|
|
297
|
+
let a = [];
|
|
298
|
+
if (i) {
|
|
299
|
+
for (let e = 0; e < 4; e++) a.push({
|
|
300
|
+
position: [
|
|
301
|
+
0,
|
|
302
|
+
-1e6,
|
|
303
|
+
0
|
|
304
|
+
],
|
|
305
|
+
range: 1,
|
|
306
|
+
intensity: 0,
|
|
307
|
+
color: [
|
|
308
|
+
1,
|
|
309
|
+
.5,
|
|
310
|
+
.2
|
|
311
|
+
],
|
|
312
|
+
radius: 0
|
|
313
|
+
});
|
|
314
|
+
if (r) {
|
|
315
|
+
let e = "flame";
|
|
316
|
+
for (let t = 2; r.names.includes(e); t++) e = `flame ${t}`;
|
|
317
|
+
this.target = this.group = r.group(e);
|
|
318
|
+
} else this.target = i;
|
|
319
|
+
}
|
|
320
|
+
this.lights = a, this.write(!0);
|
|
321
|
+
}
|
|
322
|
+
length() {
|
|
323
|
+
let e = this.options;
|
|
324
|
+
if (e.length !== void 0) return e.length;
|
|
325
|
+
if (e.intensity !== void 0) return x(e.intensity);
|
|
326
|
+
let [t, n] = e.footprint ?? [0, 0];
|
|
327
|
+
return C(e.heat ?? 0, Math.sqrt(4 * t * n / Math.PI));
|
|
328
|
+
}
|
|
329
|
+
check() {
|
|
330
|
+
let e = this.options;
|
|
331
|
+
if (!e || typeof e != "object") throw TypeError("Flame options must be an object with a position and a length, intensity or heat");
|
|
332
|
+
if (g(e.position, 3, "Flame position"), e.length === void 0 && e.intensity === void 0 && e.heat === void 0) throw TypeError("Flame needs a length (m), an intensity (kW/m) or a heat (W)");
|
|
333
|
+
c(this.length(), "length", 0, 1e3), e.footprint && (c(e.footprint[0], "footprint x", 0, 1e5), c(e.footprint[1], "footprint z", 0, 1e5)), c(e.opacity ?? 1, "opacity", 0, 1), c(e.flicker ?? 1, "flicker", 0, 3), c(e.tongues ?? 1, "tongues", .25, 4), c(e.crowns ?? 0, "crowns", 0, 1e3), e.slope !== void 0 && (g(e.slope, 2, "Flame slope"), c(e.slope[0], "slope x", -10, 10), c(e.slope[1], "slope z", -10, 10)), c(e.glow ?? 1, "glow", 0, 8), c(e.haze ?? I, "haze", 0, 1), c(e.groundAlbedo ?? 0, "groundAlbedo", 0, 1), c(e.glowRadius ?? R, "glowRadius", .5, 10), c(e.yaw ?? 0, "yaw", -1e3, 1e3), c(e.lightPower ?? 1, "lightPower", 0, 1e6);
|
|
334
|
+
}
|
|
335
|
+
lookOf() {
|
|
336
|
+
let e = this.options, t = this.look, n = (e, n) => t && typeof t[e] == "number" ? t[e] : n, r = t?.["flames.edgeColor"] ?? [
|
|
337
|
+
.55,
|
|
338
|
+
.12,
|
|
339
|
+
.04
|
|
340
|
+
];
|
|
341
|
+
return {
|
|
342
|
+
core: n("flames.core", e.core ?? 2200),
|
|
343
|
+
edge: n("flames.edge", e.edge ?? 1e3),
|
|
344
|
+
peak: n("flames.peak", e.peak ?? 1),
|
|
345
|
+
flicker: n("flames.flicker", 1) * (e.flicker ?? 1),
|
|
346
|
+
tongues: n("flames.tongues", 1) * (e.tongues ?? 1),
|
|
347
|
+
size: n("flames.size", 1),
|
|
348
|
+
radiance: n("flames.radiance", 1),
|
|
349
|
+
opacity: n("flames.opacity", 1) * (e.opacity ?? 1),
|
|
350
|
+
ramp: n("flames.ramp", 0),
|
|
351
|
+
glow: n("flames.glow", 1) * (e.glow ?? 1),
|
|
352
|
+
haze: n("flames.haze", 1) * (e.haze ?? I),
|
|
353
|
+
edgeColor: r
|
|
354
|
+
};
|
|
355
|
+
}
|
|
356
|
+
write(e = !1) {
|
|
357
|
+
let t = this.options, n = this.lookOf(), r = [
|
|
358
|
+
n.core,
|
|
359
|
+
n.edge,
|
|
360
|
+
t.cooling ?? 1.2,
|
|
361
|
+
t.exposure ?? .3,
|
|
362
|
+
n.ramp,
|
|
363
|
+
n.edgeColor[0],
|
|
364
|
+
n.edgeColor[1],
|
|
365
|
+
n.edgeColor[2]
|
|
366
|
+
];
|
|
367
|
+
if (!e && r.every((e, t) => e === this.key[t])) return;
|
|
368
|
+
for (let e = 0; e < r.length; e++) this.key[e] = r[e];
|
|
369
|
+
let i = c(n.core, "core", 800, 3e3), a = c(Math.min(n.edge, i), "edge", 600, 3e3), o = c(t.cooling ?? 1.2, "cooling", .2, 5), s = c(t.exposure ?? .3, "exposure", 0, 1), l = d(i)[1], u = this.values;
|
|
370
|
+
this.coreFloor = Math.min(...f(i));
|
|
371
|
+
for (let e = 0; e < B; e++) {
|
|
372
|
+
let t = e / 15, r = a + (i - a) * t ** o, c = Math.min(1, (d(r)[1] / l) ** s), p = f(r);
|
|
373
|
+
for (let r = 0; r < 3; r++) u[48 + e * 4 + r] = p[r] * c + (n.edgeColor[r] - p[r] * c) * n.ramp * (1 - t);
|
|
374
|
+
u[48 + e * 4 + 3] = 1;
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
setFlameLook(e) {
|
|
378
|
+
return this.look = { ...e }, this.write(), this;
|
|
379
|
+
}
|
|
380
|
+
get plume() {
|
|
381
|
+
let e = this.options, t = this.heat, n = this.values;
|
|
382
|
+
return t.position[0] = e.position[0], t.position[1] = e.position[1], t.position[2] = e.position[2], t.length = this.current, t.footprint[0] = 2 * n[24], t.footprint[1] = 2 * n[25], t.yaw = e.yaw ?? 0, t.time = this.clock, t;
|
|
383
|
+
}
|
|
384
|
+
get flameLength() {
|
|
385
|
+
return this.current;
|
|
386
|
+
}
|
|
387
|
+
get time() {
|
|
388
|
+
return this.clock;
|
|
389
|
+
}
|
|
390
|
+
get puffing() {
|
|
391
|
+
let e = this.length();
|
|
392
|
+
return T(E(this.options.footprint ?? [.5 * e, .5 * e]));
|
|
393
|
+
}
|
|
394
|
+
setup(e) {
|
|
395
|
+
this.engine = e;
|
|
396
|
+
}
|
|
397
|
+
registerPasses(e) {
|
|
398
|
+
e.add({
|
|
399
|
+
name: this.name + ":draw",
|
|
400
|
+
order: D,
|
|
401
|
+
execute: (e) => {
|
|
402
|
+
e.backend === "webgpu" && this.draw(e, !1);
|
|
403
|
+
}
|
|
404
|
+
}), e.add({
|
|
405
|
+
name: this.name + ":depth",
|
|
406
|
+
order: O,
|
|
407
|
+
execute: (e) => {
|
|
408
|
+
e.backend === "webgpu" && this.draw(e, !0);
|
|
409
|
+
}
|
|
410
|
+
});
|
|
411
|
+
}
|
|
412
|
+
lean(e, t, n) {
|
|
413
|
+
let r = this.options, i = r.wind, a = 0, o = 0;
|
|
414
|
+
if (i && typeof i == "object" && "sample" in i) {
|
|
415
|
+
let e = i.sample(r.position[0], r.position[1] + .5 * this.current, r.position[2], this.clock);
|
|
416
|
+
a = e[0] ?? 0, o = e[2] ?? 0;
|
|
417
|
+
} else i && (a = i[0], o = i[1]);
|
|
418
|
+
let s = Math.cos(r.yaw ?? 0), c = Math.sin(r.yaw ?? 0), l = Math.max(-3, Math.min(3, a / e)), u = Math.max(-3, Math.min(3, o / e));
|
|
419
|
+
this.heat.lean[0] = l, this.heat.lean[1] = u, t[n] = s * l - c * u, t[n + 1] = c * l + s * u;
|
|
420
|
+
}
|
|
421
|
+
advanceTime(e) {
|
|
422
|
+
this.disposed || (this.clock += e > 0 ? e : 0);
|
|
423
|
+
}
|
|
424
|
+
resetTime() {
|
|
425
|
+
this.clock = this.current = 0;
|
|
426
|
+
}
|
|
427
|
+
onPreUpdate(e) {
|
|
428
|
+
if (this.disposed) return;
|
|
429
|
+
this.advanceTime(e), this.check(), this.write();
|
|
430
|
+
let t = this.options, n = this.length(), r = this.lookOf(), i = t.footprint ?? [.5 * n, .5 * n], a = this.values;
|
|
431
|
+
this.current = n, c(r.peak, "peak", 1e-6, 1e3);
|
|
432
|
+
let o = Math.max(n, .001), s = y * Math.sqrt(v * o), l = E(i), u = T(l), d = t.yaw ?? 0;
|
|
433
|
+
a[19] = this.clock, a[20] = t.position[0], a[21] = n > 0 ? t.position[1] : -1e6, a[22] = t.position[2], a[23] = o, a[24] = i[0] / 2 * r.size, a[25] = i[1] / 2 * r.size, this.lean(s, a, 26), a[28] = Math.cos(d), a[29] = Math.sin(d), a[30] = u, a[31] = s / u, a[32] = s, a[33] = .22 * o * r.tongues, a[34] = r.flicker, a[35] = q(r.opacity), a[36] = r.peak, a[37] = Math.min(j * r.radiance, 2 / Math.max(this.coreFloor, .001)), a[38] = l, a[39] = this.seed, a[40] = .2 * o + .3 * Math.min(i[0], i[1]), a[41] = A * o, a[42] = (t.crowns ? -.46 : -.16) * o, a[43] = n > 0 ? M : 0, a[Y] = Math.floor(this.clock * 240) % 4096, a[113] = t.crowns ?? 0, this.glow(o, i, r, s, a), this.light(o, i, u, l, r.flicker, a[39]);
|
|
434
|
+
}
|
|
435
|
+
glow(e, t, n, r, i) {
|
|
436
|
+
let a = this.options, o = Math.cos(a.yaw ?? 0), s = Math.sin(a.yaw ?? 0), c = a.slope?.[0] ?? 0, l = a.slope?.[1] ?? 0, u = c * o - l * s, d = c * s + l * o, p = Math.min(z, Math.max(1, Math.round(Math.max(t[0], t[1]) / e))), m = this.current > 0 && n.glow > 0, h = m ? V * n.peak * n.radiance * n.glow * e * (.5 * (t[0] + t[1]) + .24 * e) * (a.lightPower ?? 1) : 0, g = f(Math.max(1e3, Math.min(n.core * .8, 3e3))), _ = (a.glowRadius ?? R) * e, v = Math.max(Math.abs(i[26]), Math.abs(i[27])), y = Math.max(Math.abs(u), Math.abs(d)), b = a.groundAlbedo ?? (a.lights ? 0 : L);
|
|
437
|
+
i[114] = _, i[115] = m ? p : 0, i[X] = u, i[117] = d, i[118] = b, i[119] = n.haze;
|
|
438
|
+
for (let e = 0; e < 3; e++) i[Z + e] = h / p * g[e];
|
|
439
|
+
i[123] = .35 * e, i[ne] = m ? Math.max(i[40], _ * (1 + y + v)) : i[40], i[125] = m ? Math.max(i[41], .35 * e + _ * (1 + y)) : i[41], i[126] = c, i[127] = l;
|
|
440
|
+
}
|
|
441
|
+
light(e, t, n, r, i, a) {
|
|
442
|
+
if (!this.target || this.target.isDisposed) return;
|
|
443
|
+
let o = this.options, s = this.lights, c = t[0] >= t[1], l = c ? t[0] : t[1], u = Math.min(4, Math.max(1, Math.round(l / Math.max(e, .001)))), d = this.current > 0, p = this.lookOf(), m = d ? V * p.peak * p.radiance * e * (.5 * (t[0] + t[1]) + .24 * e) * (o.lightPower ?? 1) : 0, h = Math.cos(o.yaw ?? 0), g = Math.sin(o.yaw ?? 0), _ = f(Math.max(1e3, Math.min(p.core * .8, 3e3)));
|
|
444
|
+
for (let d = 0; d < s.length; d++) {
|
|
445
|
+
let f = s[d], p = u > 1 ? (d / (u - 1) - .5) * l * .8 : 0, b = c ? p : 0, x = c ? 0 : p, S = 6.2831853 * (n * this.clock - .35 * e / (y * Math.sqrt(v * e) / n) + K(p / r + a)), C = Math.max(.15, 1 + i * (.3 * Math.sin(S) + .25 * (K(this.clock * n * 2.7 + d * 5.1 + a) - .5))), w = f.position;
|
|
446
|
+
w[0] = o.position[0] + h * b + g * x, w[1] = o.position[1] + .35 * e, w[2] = o.position[2] - g * b + h * x, f.range = 30 * e, f.radius = .25 * Math.min(e, Math.max(t[0], t[1]) / u + .2 * e), f.intensity = d < u ? m / u * C : 0, f.color = _, this.steady[d] = d < u ? m / u : 0;
|
|
447
|
+
}
|
|
448
|
+
this.target.setLights(s);
|
|
449
|
+
}
|
|
450
|
+
draw(t, o) {
|
|
451
|
+
if (this.disposed) return;
|
|
452
|
+
let s = t.device, c = this.options, l = this.values, u = e(this.view, t.camera);
|
|
453
|
+
if (!o) {
|
|
454
|
+
l.set(t.camera.matrix);
|
|
455
|
+
for (let e = 0; e < 3; e++) l[16 + e] = t.camera.position[e], l[44 + e] = -u[e * 4 + 2];
|
|
456
|
+
this.uniform ??= s.createBuffer({
|
|
457
|
+
size: 512,
|
|
458
|
+
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
|
|
459
|
+
}), s.queue.writeBuffer(this.uniform, 0, l);
|
|
460
|
+
}
|
|
461
|
+
if (!this.uniform) return;
|
|
462
|
+
let d = r(c.fog ?? null), f = !t.format.includes("float") && !t.format.endsWith("-srgb");
|
|
463
|
+
d && !o && (n(d, t.camera.position, this.fogData), this.fogBuffer ??= s.createBuffer({
|
|
464
|
+
size: 48,
|
|
465
|
+
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
|
|
466
|
+
}), s.queue.writeBuffer(this.fogBuffer, 0, this.fogData));
|
|
467
|
+
let p = 0, m = c.depth;
|
|
468
|
+
m && (this.softRuntime ??= a.create(s, this.owned), o ? p = this.samples : (p = this.samples = this.softRuntime.write(t, m(), u, 1), this.softRuntime.changed && this.groups.clear()));
|
|
469
|
+
let h = t.descriptor.multisample?.count ?? 1, g = t.descriptor.depthStencil.format, _ = [
|
|
470
|
+
t.format,
|
|
471
|
+
g,
|
|
472
|
+
h,
|
|
473
|
+
p,
|
|
474
|
+
!!d,
|
|
475
|
+
!!m
|
|
476
|
+
].join(":"), v = Q.get(s);
|
|
477
|
+
v || Q.set(s, v = /* @__PURE__ */ new Map());
|
|
478
|
+
let y = v.get(_);
|
|
479
|
+
if (!y) {
|
|
480
|
+
let e = s.createShaderModule({ code: ie(p, !!d, f, !!m) }), n = {
|
|
481
|
+
color: {
|
|
482
|
+
srcFactor: "one",
|
|
483
|
+
dstFactor: "one-minus-src-alpha"
|
|
484
|
+
},
|
|
485
|
+
alpha: {
|
|
486
|
+
srcFactor: "one",
|
|
487
|
+
dstFactor: "one-minus-src-alpha"
|
|
488
|
+
}
|
|
489
|
+
}, r = (n, r, i, a) => s.createRenderPipeline({
|
|
490
|
+
layout: "auto",
|
|
491
|
+
vertex: {
|
|
492
|
+
module: e,
|
|
493
|
+
entryPoint: n
|
|
494
|
+
},
|
|
495
|
+
fragment: {
|
|
496
|
+
module: e,
|
|
497
|
+
entryPoint: r,
|
|
498
|
+
targets: [i]
|
|
499
|
+
},
|
|
500
|
+
primitive: { topology: "triangle-list" },
|
|
501
|
+
multisample: t.descriptor.multisample,
|
|
502
|
+
depthStencil: {
|
|
503
|
+
format: g,
|
|
504
|
+
...a
|
|
505
|
+
}
|
|
506
|
+
});
|
|
507
|
+
y = [
|
|
508
|
+
r("glowVertex", "glowFragment", {
|
|
509
|
+
format: t.format,
|
|
510
|
+
blend: n
|
|
511
|
+
}, {
|
|
512
|
+
depthWriteEnabled: !1,
|
|
513
|
+
depthCompare: "greater-equal"
|
|
514
|
+
}),
|
|
515
|
+
r("vertex", "fragment", {
|
|
516
|
+
format: t.format,
|
|
517
|
+
blend: n
|
|
518
|
+
}, {
|
|
519
|
+
depthWriteEnabled: !1,
|
|
520
|
+
depthCompare: m ? "always" : "greater-equal"
|
|
521
|
+
}),
|
|
522
|
+
r("vertex", "depthFragment", {
|
|
523
|
+
format: t.format,
|
|
524
|
+
writeMask: 0
|
|
525
|
+
}, {
|
|
526
|
+
depthWriteEnabled: !0,
|
|
527
|
+
depthCompare: "greater-equal"
|
|
528
|
+
})
|
|
529
|
+
], v.set(_, y);
|
|
530
|
+
}
|
|
531
|
+
let b = o ? [y[2]] : l[115] > 0 ? [y[0], y[1]] : [y[1]];
|
|
532
|
+
for (let e of [
|
|
533
|
+
y[0],
|
|
534
|
+
y[1],
|
|
535
|
+
y[2]
|
|
536
|
+
]) this.groups.has(e) || this.groups.set(e, s.createBindGroup({
|
|
537
|
+
layout: e.getBindGroupLayout(0),
|
|
538
|
+
entries: [
|
|
539
|
+
{
|
|
540
|
+
binding: 0,
|
|
541
|
+
resource: { buffer: this.uniform }
|
|
542
|
+
},
|
|
543
|
+
...d ? [{
|
|
544
|
+
binding: 5,
|
|
545
|
+
resource: { buffer: this.fogBuffer }
|
|
546
|
+
}] : [],
|
|
547
|
+
...p ? this.softRuntime.entries : []
|
|
548
|
+
]
|
|
549
|
+
}));
|
|
550
|
+
if (!(this.current > 0) || o && !((this.options.opacity ?? 1) >= 1 && this.lookOf().opacity >= 1)) return;
|
|
551
|
+
this.attachment.view = t.color, this.depthAttachment.view = t.depth, this.depthAttachment.depthReadOnly = !o || void 0, o ? (this.depthAttachment.depthLoadOp = "load", this.depthAttachment.depthStoreOp = "store") : (delete this.depthAttachment.depthLoadOp, delete this.depthAttachment.depthStoreOp), this.depthAttachment.stencilReadOnly = g.includes("stencil") ? !0 : void 0;
|
|
552
|
+
let x = t.encoder.beginRenderPass({
|
|
553
|
+
colorAttachments: [this.attachment],
|
|
554
|
+
depthStencilAttachment: this.depthAttachment
|
|
555
|
+
});
|
|
556
|
+
i(x, t);
|
|
557
|
+
for (let e of b) x.setPipeline(e), x.setBindGroup(0, this.groups.get(e)), x.draw(36), this.engine && this.engine.stats.drawCalls++;
|
|
558
|
+
x.end();
|
|
559
|
+
}
|
|
560
|
+
setLength(e) {
|
|
561
|
+
return c(e, "length", 0, 1e3), this.options.length = e, this;
|
|
562
|
+
}
|
|
563
|
+
setWind(e) {
|
|
564
|
+
return this.options.wind = e, this;
|
|
565
|
+
}
|
|
566
|
+
get steadyIntensities() {
|
|
567
|
+
return this.steady;
|
|
568
|
+
}
|
|
569
|
+
static attach(e, n) {__margoAssertCore(e,__margoCoreRequirement("0.1.0",import.meta.url));
|
|
570
|
+
h(e, "Flame");
|
|
571
|
+
let r = $.get(e) ?? 0, i = new t(n, r);
|
|
572
|
+
return e.use(i), $.set(e, r + 1), i;
|
|
573
|
+
}
|
|
574
|
+
release() {
|
|
575
|
+
for (let e of [
|
|
576
|
+
this.uniform,
|
|
577
|
+
this.fogBuffer,
|
|
578
|
+
...this.owned
|
|
579
|
+
]) e?.destroy();
|
|
580
|
+
this.uniform = this.fogBuffer = this.softRuntime = void 0, this.owned = [], this.groups.clear();
|
|
581
|
+
}
|
|
582
|
+
onDeviceRestored(e) {
|
|
583
|
+
this.release(), this.setup(e);
|
|
584
|
+
}
|
|
585
|
+
dispose() {
|
|
586
|
+
this.disposed || (this.disposed = !0, this.engine?.sceneOf(this)?.removePlugin(this.name), this.engine = void 0, this.group ? this.group.dispose() : this.target && !this.target.isDisposed && this.target.setLights([]), this.release());
|
|
587
|
+
}
|
|
588
|
+
[Symbol.dispose]() {
|
|
589
|
+
this.dispose();
|
|
590
|
+
}
|
|
591
|
+
};
|
|
592
|
+
function oe(e, t, n) {
|
|
593
|
+
let r = Math.sqrt(Math.max(t / b, 1)), i = structuredClone(e);
|
|
594
|
+
return i.size *= r, i.rate *= r, i.capacity = Math.ceil(i.capacity * r), i.lifetime = [i.lifetime[0] * r, i.lifetime[1] * r], i.velocity = i.velocity.map((e) => e * r), i.gravity = i.gravity.map((e) => e * r), i.turbulence *= r, i.frequency /= r, {
|
|
595
|
+
...i,
|
|
596
|
+
emissionExtent: [
|
|
597
|
+
n[0] / 2,
|
|
598
|
+
.02 * t,
|
|
599
|
+
n[1] / 2
|
|
600
|
+
],
|
|
601
|
+
schedule: { prewarm: i.lifetime[1] }
|
|
602
|
+
};
|
|
603
|
+
}
|
|
604
|
+
//#endregion
|
|
605
|
+
export { y as FLAME_FROUDE, V as FLAME_LIGHT_FILL, m as FLAME_PEAK, H as FLAME_RADIANT_FRACTION, ae as Flame, b as HEARTH_FLAME_LENGTH, w as PUFFING_CONSTANT, u as blackbodyChromaticity, f as blackbodyColor, l as blackbodyLuminanceRatio, s as blackbodyLuminousEfficacy, x as byramFlameLength, S as byramIntensity, U as flameLuminousFlux, p as flameRamp, C as heskestadFlameLength, W as heskestadHeat, E as puffingDiameter, T as puffingFrequency, oe as scaleHearthEmbers };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { VFXExtension, VFXFlipbook } from './base.js';
|
|
2
|
+
/**
|
|
3
|
+
* The `flipbook` feature: blends each lifetime atlas frame into the next instead of stepping. Pass it as `{flipbook}`
|
|
4
|
+
* to `VFX` from `@margoengine/vfx/base`; the root `VFX` has it. Needs `texture` with an atlas whose frames are
|
|
5
|
+
* `'lifetime'`.
|
|
6
|
+
*/
|
|
7
|
+
export declare const flipbook: VFXExtension<VFXFlipbook>;
|
package/dist/flipbook.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { MargoError as e } from "@margoengine/math/errors";
|
|
2
|
+
//#region src/flipbook.ts
|
|
3
|
+
function t(t, n, r) {
|
|
4
|
+
if (t.split(n).length !== 2) throw new e("unsupported", "VFX flipbook needs the atlas billboard it was written for; update the flipbook edits");
|
|
5
|
+
return t.replace(n, r);
|
|
6
|
+
}
|
|
7
|
+
var n = /o\.uv=corner;let atlasFrame=min\(u32\(max\(1-p\.positionLife\.w\/p\.rotation\.z,0\.0\)\*(\d+)\.0\),(\d+)u\);o\.cell=vec2f\(f32\(atlasFrame%(\d+)u\),f32\(atlasFrame\/\3u\)\);/, r = "mix(flipbookSample(i.cell,i.uv),flipbookSample(i.nextCell,i.uv),i.blend)", i = {
|
|
8
|
+
validate(t, n) {
|
|
9
|
+
if (!t || typeof t != "object") throw TypeError("VFX flipbook must be an object; pass {}");
|
|
10
|
+
let r = n.texture?.atlas;
|
|
11
|
+
if (!n.texture || !r || r.frames !== "lifetime") throw new e("state", "VFX flipbook needs a texture with an atlas whose frames are 'lifetime'; pass texture and {texture: textureFeature} from @margoengine/vfx/texture");
|
|
12
|
+
},
|
|
13
|
+
billboard: (i) => {
|
|
14
|
+
let a = i.match(n);
|
|
15
|
+
if (!a || Number(a[2]) !== Number(a[1]) - 1) throw new e("unsupported", "VFX flipbook needs the atlas billboard it was written for; update the flipbook edits");
|
|
16
|
+
let o = a[1], s = a[2], c = a[3], l = t(i, a[0], `o.uv=corner;let atlasPos=max(1.0-p.positionLife.w/p.rotation.z,0.0)*${o}.0;let atlasFrame=min(u32(atlasPos),${s}u);let atlasNext=min(atlasFrame+1u,${s}u);o.cell=vec2f(f32(atlasFrame%${c}u),f32(atlasFrame/${c}u));o.nextCell=vec2f(f32(atlasNext%${c}u),f32(atlasNext/${c}u));o.blend=atlasPos-f32(atlasFrame);`), u = "let t=textureSample(sprite,spriteSampler,(i.cell+", d = l.indexOf(u), f = l.indexOf("));if(t.a", d);
|
|
17
|
+
if (d < 0 || f < 0 || l.indexOf(u, d + 1) >= 0) throw new e("unsupported", "VFX flipbook needs the atlas billboard it was written for; update the flipbook edits");
|
|
18
|
+
let p = l.slice(d + 49, f).replaceAll("i.uv", "uv"), m = l.slice(0, d) + `let t=${r};` + l.slice(f + 3), h = `fn flipbookSample(cell:vec2f,uv:vec2f)->vec4f{return textureSample(sprite,spriteSampler,(cell+${p}));}\n`;
|
|
19
|
+
return t(t(m, "@location(2) @interpolate(flat) cell:vec2f}", "@location(2) @interpolate(flat) cell:vec2f,@location(4) @interpolate(flat) nextCell:vec2f,@location(5) @interpolate(flat) blend:f32}"), "@fragment fn fragment", h + "@fragment fn fragment");
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
//#endregion
|
|
23
|
+
export { i as flipbook };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { LinearToSRGB as e, parseColor as t } from "@margoengine/math/color";
|
|
2
|
+
//#region ../shared/fog.js
|
|
3
|
+
var n = (e, t) => `
|
|
4
|
+
struct MargoFog {color:vec4f,params:vec4f,eye:vec4f}
|
|
5
|
+
@group(${e}) @binding(${t}) var<uniform> margoFog:MargoFog;
|
|
6
|
+
fn fogAmount(p:vec3f)->f32 {
|
|
7
|
+
let d=distance(p,margoFog.eye.xyz);let q=margoFog.params;
|
|
8
|
+
let amount=select(clamp((d-q.x)/max(q.y-q.x,1e-6),0.0,1.0),1.0-exp(-q.z*q.z*d*d),q.z>0.0);
|
|
9
|
+
return amount*exp(-q.w*max(p.y-margoFog.color.w,0.0));
|
|
10
|
+
}
|
|
11
|
+
`, r = (e, t, n, r) => {
|
|
12
|
+
let i = e === void 0 ? t : e;
|
|
13
|
+
if (typeof i != "number" || !Number.isFinite(i) || i < r) throw RangeError(`Fog ${n} is ${String(i)}; pass a finite number${r === 0 ? ", 0 or more" : ""}`);
|
|
14
|
+
return i;
|
|
15
|
+
};
|
|
16
|
+
function i(e) {
|
|
17
|
+
if (e !== null && (typeof e != "object" || Array.isArray(e))) throw TypeError("Fog must be an object with Fog fields; pass an environment Fog, such an object or null");
|
|
18
|
+
return e && a(e, [
|
|
19
|
+
0,
|
|
20
|
+
0,
|
|
21
|
+
0
|
|
22
|
+
], /* @__PURE__ */ new Float32Array(12)), e;
|
|
23
|
+
}
|
|
24
|
+
function a(n, i, a, o = !1) {
|
|
25
|
+
let s = n.enabled ?? !0;
|
|
26
|
+
if (typeof s != "boolean") throw TypeError(`Fog enabled is ${String(s)}; pass true or false`);
|
|
27
|
+
let c = r(n.height, 0, "height", -Infinity), l = r(n.near, 1, "near", 0), u = r(n.far, 1e3, "far", 0), d = r(n.density, 0, "density", 0), f = r(n.heightFalloff, 0, "heightFalloff", 0);
|
|
28
|
+
if (t(n.color ?? "#ffffff", a, 0), o) for (let t = 0; t < 3; t++) a[t] = e(a[t]);
|
|
29
|
+
a[3] = c, a[4] = l, a[5] = u, a[6] = d, a[7] = f, s || (a.fill(0, 0, 8), a[4] = a[5] = 34028234663852886e22), a[8] = i[0], a[9] = i[1], a[10] = i[2], a[11] = 0;
|
|
30
|
+
}
|
|
31
|
+
//#endregion
|
|
32
|
+
export { n, a as r, i as t };
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
//#region src/frame.ts
|
|
2
|
+
var e = "\nstruct Particle {positionLife:vec4f,velocitySize:vec4f,color:vec4f,rotation:vec4f}\nstruct Frame {camera:mat4x4f,right:vec4f,up:vec4f,gravityDelta:vec4f,emitterTime:vec4f,velocitySpread:vec4f,color:vec4f,shape:vec4f,physics:vec4f,emissionExtent:vec4f,control:vec4u}\n@group(0) @binding(0) var<uniform> f:Frame;\n", t = "particles[at]=p.positionLife;";
|
|
3
|
+
//#endregion
|
|
4
|
+
export { t as n, e as t };
|