import*asTHREEfrom'three';import{U,COMMON_GLSL,FX_VERT_HEAD,FX_FRAG_HEAD,FX_OUT_GLSL,FX_BLENDING,LINES_BLENDING}from'./common.js';// Instanced camera-facing strips (PLAN 2.5 "Ribbons and bolts"). One draw per material for every// ribbon it owns; nothing is allocated after construction. Strips wind either way depending on// the path direction, so every strip material is DoubleSide.// TrailRibbons history ribbons: a points x max float texture of head positions, a ring per row// AnalyticRibbons stateless: the centreline is user GLSL evaluated per vertex// Bolts.js builds on STRIP_GLSL too.//// Distortion amounts everywhere are metres of apparent displacement at the strip's depth, so a far// effect bends the background less than a near one.// Needs projectionMatrix, viewMatrix and cameraPosition (three's prefix, or FX_VERT_HEAD).exportconstSTRIP_GLSL=/* glsl */`
// Humus "phone-wire" AA: a strip is never drawn thinner than 1.5 px; the width it lacks comes back
// as intensity (aa = trueWidth / clampedWidth), so thin wires fade out instead of breaking into dashes.
// Returns the world half width to expand by.
float wireHalfWidth(float width, float viewZ, float pxPerM, out float aa) {
float px = width * pxPerM / max(viewZ, 1e-3);
float clamped = max(px, 1.5);
aa = px / clamped;
return 0.5 * width * clamped / max(px, 1e-6);
}
// pixels per metre at view depth 1, for a target resY pixels tall
float pxPerMetre(float resY) { return 0.5 * resY * projectionMatrix[1][1]; }
// unit side vector of a strip through p with tangent t, facing the camera
vec3 stripSide(vec3 p, vec3 t) {
vec3 s = cross(t, p - cameraPosition);
float l = length(s);
return l > 1e-7 ? s / l : vec3(1.0, 0.0, 0.0);
}
// screen-UV offset of 1 m of apparent displacement along world dir at p (dir ~ perpendicular to the view ray)
vec2 distortPerMetre(vec3 p, vec3 dir, float viewZ, vec2 res) {
mat4 vp = projectionMatrix * viewMatrix;
vec4 a = vp * vec4(p, 1.0), b = vp * vec4(p + dir * 0.01 * viewZ, 1.0);
vec2 px = (b.xy / b.w - a.xy / a.w) * res;
vec2 d = dot(px, px) > 1e-12 ? normalize(px) : vec2(1.0, 0.0);
float h = 0.5 * projectionMatrix[1][1] / max(viewZ, 1e-3); // metres -> fraction of screen height
return d * vec2(h * res.y / res.x, h);
}
i i segments i
a base i
indexa a a a a a
arro x arro y arro z arro w
arro x arro y arro z arro w
arr o v v v v
attr count size size
attr
geoname attr
attr
max
points
points max
tex points max
tex tex
tex
max
geo
pos index
i i points i posi i
index points
geo pos
geoindex
geo max
geo max
geo max
geo
geo
tex
geo
geo
max
owner
row
p
owner
owner
p
p
owner p
owner
t
t
t
t t t t
t
t
i row slot o i d
do p do p do p do
r
r o r
dt
n
t
t
t t t t
o n t
o t o t o t o t
o t o t o t o t
o t o t o t o t
n
n
n
n
tex
tex
tex
tex
i i i texi
tex
max segments pathGLSL shadeGLSL target uniforms
max
geo
pos index
i i segments i posi segments i segments
index segments
geo pos
geoindex
geo n max
geo
geo
uniforms segments
head pathGLSL
material target
head
shadeGLSL
head shadeGLSL
geo material
max
index
r
r
r r r
r r r r
r
top dirty
a0 a1 a2 a3 ac
r
o r on r r
on top r
dirty ac o r r r r r r on dirty
on
dirty a0 o r dirty
dirty a1 o r dirty
dirty a2 o r dirty
dirty a3 o r dirty
dirty a a
top
top
// --- history ribbons ---------------------------------------------------------------------
const
TRAIL_VERT
=
/* glsl */
`
uniform highp sampler2D uTrailTex; // x = point slot (ring), y = trail row; xyz pos, w push time
uniform float uTrailTime;
in vec4 aTrail; // row, head slot, point count, fade time
in vec4 aLook; // width, glow (fx width multiplier), taper, distortion (m)
in vec4 aColor; // rgb * intensity, glow intensity (fraction of the core's)
out vec2 vUv; // along (0 head .. 1 tail), across (-1..1)
out vec3 vColor;
out float vViewZ;
out vec2 vDist;
vec4 trailPoint(int row, int head, int i, int P) { return texelFetch(uTrailTex, ivec2((head - i + P) % P, row), 0); }
void main() {
int n = int(aTrail.z);
vColor = vec3(0.0); vUv = vec2(0.0); vViewZ = 1.0; vDist = vec2(0.0);
if (n < 2) { ${COLLAPSE} return; }
int P = textureSize(uTrailTex, 0).x, row = int(aTrail.x), head = int(aTrail.y);
int i = min(int(position.x), n - 1);
vec4 c = trailPoint(row, head, i, P);
vec3 tangent = trailPoint(row, head, max(i - 1, 0), P).xyz - trailPoint(row, head, min(i + 1, n - 1), P).xyz;
float u = float(i) / float(n - 1);
// past the last point the strip folds onto it with zero width (zero-area triangles)
float alive = position.x > float(n - 1) ? 0.0 : 1.0;
float fade = clamp(1.0 - (uTrailTime - c.w) / max(aTrail.w, 1e-3), 0.0, 1.0);
fade *= fade;
vec4 view = viewMatrix * vec4(c.xyz, 1.0);
vViewZ = -view.z;
vec3 side = stripSide(c.xyz, tangent);
float width = aLook.x * mix(1.0, 1.0 - u, aLook.z) * alive, aa;
#ifdef TRAIL_FX
// the wide glow would end in a square cap at the head: fade it in over the first two points
float cap = smoothstep(0.0, 2.0, float(i));
float hw = wireHalfWidth(width * aLook.y * mix(0.4, 1.0, cap), vViewZ, pxPerMetre(uFxResolution.y), aa);
vColor = aColor.rgb * aColor.a * fade * aa * cap;
vDist = distortPerMetre(c.xyz, side, vViewZ, uFxResolution) * aLook.w * fade;
#else
float hw = wireHalfWidth(width, vViewZ, pxPerMetre(uResolution.y), aa);
vColor = aColor.rgb * fade * aa;
#endif
vUv = vec2(u, position.y);
gl_Position = projectionMatrix * viewMatrix * vec4(c.xyz + side * position.y * hw, 1.0);
}`
;
const
TRAIL_LINES_FRAG
=
/* glsl */
`
in vec2 vUv;
in vec3 vColor;
out vec4 fragColor;
void main() {
float v = 1.0 - vUv.y * vUv.y;
fragColor = vec4(vColor * v * v, 1.0);
}`
;
const
TRAIL_FX_FRAG
=
/* glsl */
`
in vec2 vUv;
in vec3 vColor;
in float vViewZ;
in vec2 vDist;
void main() {
float v = vUv.y;
float g = max(exp(-4.0 * v * v) - 0.0183, 0.0); // gaussian, zero at the strip edge
// derivative-of-gaussian profile: the centreline acts as a cylindrical lens (pushes outward)
vec2 dist = vDist * (v * exp(-3.0 * v * v) * 2.33);
fxOut(vec3(0.0), 0.0, vColor * g * softFade(vViewZ, 0.4), dist, vViewZ);
}`
;
// per-trail look, reset on acquire. spacing: metres between committed points (points * spacing
// caps the trail's length; fadeTime its duration)
const
TRAIL_DEFAULTS
=
{
width
:
0.06
,
intensity
:
4
,
glow
:
6
,
glowIntensity
:
0.3
,
distortion
:
0
,
taper
:
1
,
fadeTime
:
0.4
,
spacing
:
0.08
}
;
export
class
TrailRibbons
{
constructor
(
{ max =64, points =48}={}
)
{
this
.
max
=
;
this
.
points
=
;
this
.
time
=
0
;
this
.
data
=
new
Float32Array
(
*
*
4
)
;
const
=
this
.
texture
=
new
THREE.DataTexture
(
this
.
data
,
,
,
THREE
.
RGBAFormat
,
THREE
.
FloatType
)
;
.
minFilter
=
.
magFilter
=
THREE
.
NearestFilter
;
.
needsUpdate
=
true
;
// one texel per push; preallocated so pushing into texture.updateRanges allocates nothing
this
.
_ranges
=
Array
.
from
(
{
length
:
*
4
}
,
(
)
=>
(
{
start
:
0
,
count
:
0
}
)
)
;
this
.
_nRanges
=
0
;
this
.
_full
=
false
;
const
=
new
THREE.InstancedBufferGeometry
(
)
;
const
=
[
]
,
=
[
]
;
for
(
let
=
0
;
<
;
++
)
.
push
(
,
-
1
,
0
,
,
1
,
0
)
;
stripIndex
(
,
0
,
-
1
)
;
.
setAttribute
(
'position'
,
new
THREE.Float32BufferAttribute
(
,
3
)
)
;
.
setIndex
(
)
;
this
.
aTrail
=
instanced
(
,
'aTrail'
,
)
;
this
.
aLook
=
instanced
(
,
'aLook'
,
)
;
this
.
aColor
=
instanced
(
,
'aColor'
,
)
;
.
instanceCount
=
0
;
this
.
geometry
=
;
this
.
uniforms
=
{
...
U
,
uTrailTex
:
{
value
:
}
,
uTrailTime
:
{
value
:
0
}
}
;
this
.
lines
=
new
THREE.Mesh
(
,
new
THREE.ShaderMaterial
(
{
glslVersion
:
THREE
.
GLSL3
,
uniforms
:
this
.
uniforms
,
...
LINES_BLENDING
,
side
:
THREE
.
DoubleSide
,
vertexShader
:
COMMON_GLSL
+
STRIP_GLSL
+
TRAIL_VERT
,
fragmentShader
:
TRAIL_LINES_FRAG
,
}
)
)
;
this
.
fx
=
new
THREE.Mesh
(
,
new
THREE.RawShaderMaterial
(
{
glslVersion
:
THREE
.
GLSL3
,
uniforms
:
this
.
uniforms
,
...
FX_BLENDING
,
side
:
THREE
.
DoubleSide
,
defines
:
{
TRAIL_FX
:
1
}
,
vertexShader
:
FX_VERT_HEAD
+
COMMON_GLSL
+
STRIP_GLSL
+
TRAIL_VERT
,
fragmentShader
:
FX_FRAG_HEAD
+
COMMON_GLSL
+
FX_OUT_GLSL
+
TRAIL_FX_FRAG
,
}
)
)
;
this
.
lines
.
frustumCulled
=
this
.
fx
.
frustumCulled
=
false
;
this
.
lines
.
visible
=
this
.
fx
.
visible
=
false
;
this
.
trails
=
Array
.
from
(
{
length
:
}
,
(
_, row
)
=>
{
const
=
this
;
return
{
,
active
:
false
,
released
:
false
,
head
:
0
,
count
:
0
,
lastPush
:
0
,
last
:
new
THREE.Vector3
(
)
,
color
:
new
THREE.Color
(
1
,
1
,
1
)
,
...
TRAIL_DEFAULTS
,
// the newest point follows pos; a new point is committed once it is `spacing` from the last
push
(
p
)
{
if
(
this
.
count
>
0
&&
this
.
last
.
distanceToSquared
(
)
>=
this
.
spacing
*
this
.
spacing
)
{
this
.
head
=
(
this
.
head
+
1
)
%
.
points
;
this
.
count
=
Math
.
min
(
this
.
count
+
1
,
.
points
)
;
this
.
last
.
copy
(
)
;
}
else
if
(
this
.
count
===
0
)
{
this
.
count
=
1
;
this
.
last
.
copy
(
)
;
}
.
_write
(
this
.
row
,
this
.
head
,
)
;
this
.
lastPush
=
.
time
;
}
,
release
(
)
{
this
.
released
=
true
;
}
,
}
;
}
)
;
}
// released trails keep drawing until their newest point has faded (fadeTime), then free
acquire
(
)
{
for
(
const
of
this
.
trails
)
{
if
(
.
active
)
continue
;
Object
.
assign
(
,
TRAIL_DEFAULTS
)
;
.
active
=
true
;
.
released
=
false
;
.
head
=
0
;
.
count
=
0
;
.
color
.
setRGB
(
1
,
1
,
1
)
;
return
;
}
return
null
;
}
_write
(
row, slot, p
)
{
const
=
*
this
.
points
+
,
=
*
4
,
=
this
.
data
;
[
]
=
.
x
;
[
+
1
]
=
.
y
;
[
+
2
]
=
.
z
;
[
+
3
]
=
this
.
time
;
if
(
this
.
_nRanges
<
this
.
_ranges
.
length
)
{
const
=
this
.
_ranges
[
this
.
_nRanges
++
]
;
.
start
=
;
.
count
=
4
;
}
else
this
.
_full
=
true
;
}
update
(
dt
)
{
this
.
time
+=
;
this
.
uniforms
.
uTrailTime
.
value
=
this
.
time
;
const
A
=
this
.
aTrail
.
array
,
B
=
this
.
aLook
.
array
,
C
=
this
.
aColor
.
array
;
let
=
0
;
for
(
const
of
this
.
trails
)
{
if
(
!
.
active
)
continue
;
if
(
.
released
&&
this
.
time
-
.
lastPush
>
.
fadeTime
)
{
.
active
=
false
;
continue
;
}
const
=
*
4
,
I
=
.
intensity
;
A
[
]
=
.
row
;
A
[
+
1
]
=
.
head
;
A
[
+
2
]
=
.
count
;
A
[
+
3
]
=
.
fadeTime
;
B
[
]
=
.
width
;
B
[
+
1
]
=
.
glow
;
B
[
+
2
]
=
.
taper
;
B
[
+
3
]
=
.
distortion
;
C
[
]
=
.
color
.
r
*
I
;
C
[
+
1
]
=
.
color
.
g
*
I
;
C
[
+
2
]
=
.
color
.
b
*
I
;
C
[
+
3
]
=
.
glowIntensity
;
++
;
}
this
.
geometry
.
instanceCount
=
;
this
.
lines
.
visible
=
this
.
fx
.
visible
=
>
0
;
if
(
)
this
.
aTrail
.
needsUpdate
=
this
.
aLook
.
needsUpdate
=
this
.
aColor
.
needsUpdate
=
true
;
// texel uploads: only what was pushed. Ranges still queued from a frame that never drew mean
// the GPU copy is behind, so send the whole texture once (48 KB at 64 x 48).