Agent Harness · VibeGit
Agent Harness Публичная копия обвязки агентной разработки: гейты, journeys, скиллы и шаблоны. Без ключей и личных данных.
agent-harness / tools / spells-assets / ue / vfx / Volumes.js 261 строк · изменён 6 нояб., 03:45
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import * as THREE from 'three' ;
import { U , COMMON_GLSL , NOISE_GLSL , ARENA_GLSL , LIGHTS_GLSL , FX_VERT_HEAD , FX_FRAG_HEAD , FX_OUT_GLSL , FX_BLENDING } from './common.js' ;
import { Q } from '../engine/quality.js' ;
g p g
a b c n
inner
i i p i
a p i b p i c p i
n c b a b
inner inner n a
g inner inner inner
_p _q _s _m
shape glsl uniforms maxSteps
own
material
shape
own uniforms
glsl
mesh shape material
mesh
mesh
h
mesh mesh mesh mesh material
maxSteps
h
h
cap
pxPerM camera
h
m h u h
m h
h
m
m _p _q _s
u _p
u _s
u _m _q
u h h _s _s _s h h
r _s _s _s dist _p camera
px dist r r pxPerM dist
u h cap px
const
NO_GRADIENTS
=
'#define texture(s, p) textureLod(s, p, 0.0)\n'
;
const
VOLUME_VERT
=
`
out vec3 vWorld;
void main() {
vec4 w = modelMatrix * vec4(position, 1.0);
vWorld = w.xyz;
gl_Position = projectionMatrix * viewMatrix * w;
} `
;
const
VOLUME_HEAD
=
`
uniform vec3 uVolCenter;
uniform mat3 uVolRot; // world -> shape axes
uniform vec3 uVolScale; // half extents (m)
uniform float uVolSteps;
uniform vec4 uVolLight; // HG g, sun tap distance (m), ambient gain, sun gain
uniform vec3 uSunDir;
uniform vec3 uSunColor;
uniform vec3 uAmbient;
in vec3 vWorld;
// this ray's step length (m), readable from volumeSample: fade noise octaves finer than ~2 steps
// (they can't be resolved and only turn into per-pixel grain)
float gVolDt = 0.1;
`
;
const
SHAPES_GLSL
=
`
#if VOLUME_SHAPE == 0
vec2 volumeHit(vec3 o, vec3 d) { // ellipsoid
vec3 oc = o / uVolScale, rd = d / uVolScale;
float a = dot(rd, rd), b = dot(oc, rd), c = dot(oc, oc) - 1.0;
float h = b * b - a * c;
if (h < 0.0) return vec2(1.0, -1.0);
h = sqrt(h);
return vec2(-b - h, -b + h) / a;
}
#elif VOLUME_SHAPE == 1
vec2 volumeHit(vec3 o, vec3 d) { // box
vec3 dd = mix(d, vec3(1e-6), lessThan(abs(d), vec3(1e-6)));
vec3 m = 1.0 / dd, n = m * o, k = abs(m) * uVolScale;
vec3 t1 = -n - k, t2 = -n + k;
return vec2(max(max(t1.x, t1.y), t1.z), min(min(t2.x, t2.y), t2.z));
}
#else
// capsule along y: radius uVolScale.x, half length uVolScale.y including the caps.
// Near hit after Inigo Quilez (MIT); the far hit is the near hit of the reversed ray.
float capsuleNear(vec3 ro, vec3 rd, float hy, float ra) {
vec3 ba = vec3(0.0, 2.0 * hy, 0.0), oa = ro - vec3(0.0, -hy, 0.0);
float baba = dot(ba, ba), bard = dot(ba, rd), baoa = dot(ba, oa), rdoa = dot(rd, oa), oaoa = dot(oa, oa);
float a = baba - bard * bard, b = baba * rdoa - baoa * bard, c = baba * oaoa - baoa * baoa - ra * ra * baba;
float h = b * b - a * c;
if (h >= 0.0) {
float t = (-b - sqrt(h)) / a;
float y = baoa + t * bard;
if (y > 0.0 && y < baba) return t;
vec3 oc = y <= 0.0 ? oa : ro - vec3(0.0, hy, 0.0);
b = dot(rd, oc); c = dot(oc, oc) - ra * ra; h = b * b - c;
if (h > 0.0) return -b - sqrt(h);
}
return -1.0;
}
vec2 volumeHit(vec3 o, vec3 d) {
float ra = uVolScale.x, hy = max(uVolScale.y - ra, 1e-4);
float far = length(o) + 2.0 * (uVolScale.y + ra);
float tb = capsuleNear(o + d * far, -d, hy, ra);
if (tb < 0.0) return vec2(1.0, -1.0);
vec3 q = o; q.y -= clamp(q.y, -hy, hy);
return vec2(dot(q, q) < ra * ra ? 0.0 : capsuleNear(o, d, hy, ra), far - tb);
}
#endif
`
;
const
VOLUME_MAIN
=
`
void main() {
gLightNoShadow = true;
vec3 rd = normalize(vWorld - cameraPosition);
vec3 o = uVolRot * (cameraPosition - uVolCenter), d = uVolRot * rd;
vec2 hit = volumeHit(o, d);
float cosV = dot(rd, -vec3(viewMatrix[0][2], viewMatrix[1][2], viewMatrix[2][2]));
float t0 = max(hit.x, 0.0), t1 = min(hit.y, sceneViewZ() / cosV);
if (t1 <= t0) discard;
int steps = int(uVolSteps);
float dt = (t1 - t0) / float(steps);
gVolDt = dt;
// samples sit at t0 + (i + j) dt; slabs run between them (trapezoid), plus a j dt slab in
// front of the first sample and a (1 - j) dt one behind the last, so [t0, t1] is covered exactly
const float j = 0.5;
float t = t0 + dt * j, len = dt * j;
float tap = uVolLight.y;
vec3 tapS = uVolRot * uSunDir * tap;
float sunPhase = hgPhase(dot(uSunDir, rd), uVolLight.x) * 4.0 * PI;
vec3 sun = uSunColor * uVolLight.w * sunPhase, amb = uAmbient * uVolLight.z;
vec3 scat = vec3(0.0), emit = vec3(0.0), Lin = vec3(0.0);
float T = 1.0, zSum = 0.0, wSum = 0.0, odSun = 0.0, odL = 0.0, s2;
vec3 a2, e2;
float sP = 0.0; vec3 aP = vec3(0.0), eP = vec3(0.0); // previous sample
bool hasLight = uWorldCounts.x > 0.5;
int lit = 0;
for (int i = 0; i <= 64; i++) {
if (i > steps || T < 0.01) break;
float sigma = sP; vec3 albedo = aP, e = eP;
if (i < steps) {
vec3 ps = o + d * t, pw = cameraPosition + rd * t;
volumeSample(pw, ps / uVolScale, sigma, albedo, e);
if (sigma > 1e-4) {
// lighting on every 2nd occupied sample, held in between; the shadow taps alternate between
// the sun and the brightest spell light (packWorld sorts it first), both on the first sample
int k = lit & 3;
if ((k & 1) == 0 && max(albedo.r, max(albedo.g, albedo.b)) > 1e-3) {
if (k == 0) {
volumeSample(pw + uSunDir * tap, (ps + tapS) / uVolScale, s2, a2, e2);
odSun = (sigma + s2) * tap;
}
if (hasLight && (k == 2 || lit == 0)) {
SpellLight L0 = spellLight(0);
vec3 lp = L0.type == 1 ? closestOnSegment(L0.pos, L0.end, pw) : L0.pos, ld = lp - pw;
float ll = length(ld), dl = min(tap, ll * 0.5); // never tap past the light itself
ld *= dl / max(ll, 1e-4);
volumeSample(pw + ld, (ps + uVolRot * ld) / uVolScale, s2, a2, e2);
odL = (sigma + s2) * dl;
}
Lin = sun * (exp(-odSun) + 0.25 * exp(-0.25 * odSun)) + amb * mix(0.35, 1.0, exp(-0.5 * odSun))
+ spellLightScatter(pw, -rd, uVolLight.x) * (exp(-odL) + 0.25 * exp(-0.25 * odL));
}
lit++;
}
} else len = dt * (1.0 - j);
// trapezoid slab: the mean of its two end samples, so a density edge between two samples
// contributes in proportion to where it falls (rectangle slabs turn it into contour bands)
float sg = i == 0 ? sigma : 0.5 * (sigma + sP);
vec3 eg = i == 0 ? e : 0.5 * (e + eP), ag = i == 0 ? albedo : 0.5 * (albedo + aP);
sP = sigma; aP = albedo; eP = e;
if (sg > 1e-4) {
float Tr = exp(-sg * len), a = T * (1.0 - Tr);
scat += a * ag * Lin;
emit += a * eg / sg;
zSum += a * (t - 0.5 * len); wSum += a;
T *= Tr;
} else emit += T * eg * len;
t += dt; len = dt;
}
float alpha = 1.0 - T;
if (alpha < 0.002 && dot(emit, emit) < 1e-6) discard;
float z = (wSum > 0.0 ? zSum / wSum : t0) * cosV;
fxOut(scat / max(alpha, 1e-4), alpha, emit, vec2(0.0), z);
} `
;
const
SHAPES
=
{
sphere
:
0
,
box
:
1
,
capsule
:
2
}
;
function
sphereProxy
(
)
{
const
=
new
THREE. IcosahedronGeometry
(
1
,
2
)
,
=
.
attributes
.
position
;
const
=
new
THREE. Vector3
(
)
,
=
new
THREE. Vector3
(
)
,
=
new
THREE. Vector3
(
)
,
=
new
THREE. Vector3
(
)
;
let
=
1
;
for
(
let
=
0
;
<
.
count
;
+=
3
)
{
.
fromBufferAttribute
(
,
)
;
.
fromBufferAttribute
(
,
+
1
)
;
.
fromBufferAttribute
(
,
+
2
)
;
.
subVectors
(
,
)
.
cross
(
.
clone
(
)
.
sub
(
)
)
.
normalize
(
)
;
=
Math
.
min
(
,
Math
.
abs
(
.
dot
(
)
)
)
;
}
return
.
scale
(
1.01
/
,
1.01
/
,
1.01
/
)
;
}
const
=
new
THREE. Vector3
(
)
,
=
new
THREE. Quaternion
(
)
,
=
new
THREE. Vector3
(
)
,
=
new
THREE. Matrix4
(
)
;
export
class
Volumes
{
constructor
(
)
{
this
.
group
=
new
THREE. Group
(
)
;
this
.
list
=
[
]
;
this
.
proxies
=
{
sphere
:
sphereProxy
(
)
,
box
:
new
THREE. BoxGeometry
(
2
,
2
,
2
)
}
;
this
.
proxies
.
capsule
=
this
.
proxies
.
box
;
}
add
(
{
=
'sphere'
,
,
=
{
}
,
=
32
}
)
{
const
=
{
uVolCenter
:
{
value
:
new
THREE. Vector3
(
)
}
,
uVolRot
:
{
value
:
new
THREE. Matrix3
(
)
}
,
uVolScale
:
{
value
:
new
THREE. Vector3
(
1
,
1
,
1
)
}
,
uVolSteps
:
{
value
:
8
}
,
uVolLight
:
{
value
:
new
THREE. Vector4
(
0.3
,
0.3
,
1
,
1
)
}
,
}
;
const
=
new
THREE. RawShaderMaterial
(
{
glslVersion
:
THREE
.
GLSL3
,
defines
:
{
VOLUME_SHAPE
:
SHAPES
[
]
}
,
uniforms
:
{
...
U
,
...
,
...
}
,
...
FX_BLENDING
,
depthTest
:
false
,
side
:
THREE
.
BackSide
,
vertexShader
:
FX_VERT_HEAD
+
VOLUME_VERT
,
fragmentShader
:
FX_FRAG_HEAD
+
NO_GRADIENTS
+
COMMON_GLSL
+
NOISE_GLSL
+
ARENA_GLSL
+
LIGHTS_GLSL
+
FX_OUT_GLSL
+
VOLUME_HEAD
+
SHAPES_GLSL
+
+
VOLUME_MAIN
,
}
)
;
const
=
new
THREE. Mesh
(
this
.
proxies
[
]
,
)
;
.
visible
=
false
;
this
.
group
.
add
(
)
;
const
=
{
,
position
:
.
position
,
scale
:
.
scale
,
quaternion
:
.
quaternion
,
uniforms
:
.
uniforms
,
visible
:
false
,
,
g
:
0.3
,
shadowTap
:
0.3
,
ambient
:
1
,
sun
:
1
,
}
;
this
.
list
.
push
(
)
;
return
;
}
update
(
camera
)
{
const
=
Math
.
max
(
4
,
Math
.
floor
(
Math
.
min
(
Q
.
volumeSteps
*
Q
.
auto
.
steps
,
64
)
)
)
;
const
=
0.5
*
U
.
uFxResolution
.
value
.
y
*
.
projectionMatrix
.
elements
[
5
]
;
for
(
const
of
this
.
list
)
{
const
=
.
mesh
,
=
.
uniforms
;
.
visible
=
.
visible
;
if
(
!
.
visible
)
continue
;
.
updateMatrixWorld
(
)
;
.
matrixWorld
.
decompose
(
,
,
)
;
.
uVolCenter
.
value
.
copy
(
)
;
.
uVolScale
.
value
.
copy
(
)
;
.
uVolRot
.
value
.
setFromMatrix4
(
.
makeRotationFromQuaternion
(
.
invert
(
)
)
)
;
.
uVolLight
.
value
.
set
(
.
g
,
.
shadowTap
*
Math
.
min
(
.
x
,
.
y
,
.
z
)
,
.
ambient
,
.
sun
)
;
const
=
Math
.
max
(
.
x
,
.
y
,
.
z
)
,
=
.
distanceTo
(
.
position
)
;
const
=
>
?
*
/
:
Infinity
;
.
uVolSteps
.
value
=
Math
.
min
(
.
maxSteps
,
,
Math
.
max
(
8
,
Math
.
ceil
(
/
5
)
)
)
;
}
}
}