A wake, a drawn line or a comet’s path follows recent positions. Rebuilding ribbons on the CPU requires vertices for every trail each , with joins that can crack at sharp turns. Store each trail as a ring of recent points on the GPU. Push points from a or write a whole line from the CPU. One draw turns all trails into lines with joins and caps.
Four kernel-pushed trails and one you draw
push once a
on a screen of
a full trail
0.80 s
stroke
join
caps
blend
Try Drag to write a whole trail, or compare the four kernel-pushed orbits. Fixed-step pushes keep a trail's duration on every display; per-frame pushes shorten it at higher rates.
Creates trails as rings of recent points, pushed by kernels or written on the CPU, and draws
them as lines. Throws unless capacity is an integer of at least 1 and length is an integer
of at least 2.
@injects ― member push, reset, pointAt and held, which read its own buffers in any
pipeline
@binds ― 2 storage: push or pointAt, the points and the rings
@binds ― 1 storage: held or reset alone, the rings
@binds ― 2 storage, 2 uniform: drawing, the points and the rings, the settings and
passes.viewport
@owns ― 3 buffers, a lines: the points, the rings and the settings, and the lines that draw them
@records ― drawing, added to passes, after the passes that push; clear whenever you like.
@param ― options.width a 'use gpu' function; it and shade carry the fills of what they read,
such as tgpu.fn(swell).pipe(noise.inject()). The ribbons face the camera of the pass
description they are drawn in, which width and shade read too
@returns ― a set of trails you destroy
create(
constroot:TgpuRoot
root,{
Options.capacity: number
Sets the number of trails to an integer of at least 1.
capacity:64,
Options.length: number
Sets the number of points per trail to an integer of at least 2. Pushing beyond this count
drops the oldest point.
Describes the targets and outputs for scene passes. With a surface such as the canvas, writes
only each renderer's colour. With a target set, writes colour into the target marked color,
depth into the target marked depth, and other outputs into their named targets. Multisampled
targets resolve according to resolve. Targets with layers draw one pass per layer, and every
target the description draws into needs the same layers. add builds a renderer's drawing.
begin starts a pass. pipe supplies fills such as a camera.
@param ― screen the surface, or the targets, which give the formats; each one the description
names is drawn into
@param ― writes what fills each target, by its key in screen
@returns ― a description you destroy, which holds the pipelines it builds
Adds fill after existing fills and returns a description of the same passes. Piped fills
take precedence over a drawing's own. For example, passes.create(…).pipe(view.inject()) uses
the camera from view.
pipe(
constview:cameras.OrbitCamera
view.
CameraBase.inject(): Inject
Fills cameras.current for the pipelines created from the configuration it is piped into.
Builds a pipeline for drawing that writes every described output and returns its draw
methods. If the drawing cannot supply the fields an output reads, type checking fails with
that target's name and those fields. Use without to leave it out or instead to replace
it.
add(
constwakes:trails.Trails
wakes.
Trails.drawing: passes.Drawing<d.WgslStruct<{
position:d.Vec4f;
world:d.Vec3f;
normal:d.Vec3f;
color:d.Vec4f;
along:d.F32;
segment:d.Vec3f;
radii:d.Vec2f;
rim:d.Vec4f;
ends:d.U32;
line:d.U32;
}>,{
world:d.v3f;
normal: d.v3f;
color: d.v4f;
}>&{
inject(): Inject;
}
Draws trails with at least two points, newest first, as lines when added to a pass
description. It tests depth without writing it.
Pushes a point to the newest end from a shader. Once the trail holds length points, it drops the oldest. Only one thread may push a given
trail per dispatch; concurrent pushes race.
Runs the loop on each scheduled frame. Time advances by at most maxDt per frame, never steps
back, and stops while the surface is paused. Destroying the animation keeps the loop alive.
Another animation on the same loop continues from its recorded time. A frame error, or a run
that returns false because the loop was destroyed, stops the animation.
@param ― loop the loop to run on each unpaused frame. Continues from its seconds if it ran before
@returns ― an animation you destroy to stop the frames
Records a frame into one encoder and submits once, with a clock. Borrows the root. The
synchronous recorder borrows each frame's encoder.
@submits ― always once a frame after your recorder returns, and again at each frame.submit()
@param ― record your frame, recorded into each frame's encoder
@returns ― a loop you run with each frame's time and destroy
create(
constroot:TgpuRoot
root,(
frame:frames.Frame
frame)=>{
const
constcompute:TgpuComputePass
compute=
frame:frames.Frame
frame.
Frame.encoder: TgpuCommandEncoder
Records commands for submission after the recorder returns. submit() replaces this encoder.
Read it at each call instead of keeping it, and let the frame submit it.
Adds dt seconds and returns the number of steps to run. Each step lasts step seconds, so a
GPU kernel can use a fixed duration. All steps in a frame see the same queued CPU writes. Keep
a step counter on the GPU for values that must change per step.
update(
frame:frames.Frame
frame.
Frame.dt: number
Reports seconds since the previous submitted frame. The first frame has a dt of zero.
Begins a pass, draws drawn or each drawing in a list in order, then ends the pass. Reuse the
list to avoid allocating each frame. Call begin yourself when you need to choose drawings
per frame. Records nothing while enabled is false.
record(
frame:frames.Frame
frame,
constdrawn:passes.Drawn
drawn);
}),
);
Pushing A push adds a point and drops the oldest once the trail holds length points. Push once per . Pushing once per frame makes trails shorter on faster displays.
One thread Allow only one to push a given trail per . Multiple threads would race to update its ring.
Resettingreset(trail) clears one trail from a kernel and counts as its push for that dispatch. The same thread can push after resetting. Do this when respawning an object to avoid a streak between its old and respawn positions. clear(frame) clears all trails.
Drawingcanvas.add(wakes.drawing) builds the pipeline into a pass description, shaped for the camera the injects. One indexed draw renders all trails with at least two points, using one per segment.
Blending Ribbons blend and test depth without writing it. Draw them after opaque , or pass blend: 'translucent' to draw them into the translucency targets in any order. See Passes for their target .
Default ribbon The default ribbon starts at settings.width and tapers to zero at the oldest point a full trail can hold. It uses settings.color and is brightest at its centre. Change width with wakes.settings.patch({ width: 0.4 }).
The head Points land on fixed steps, while particles draw between them, ahead of the last step. head draws a trail’s newest end where its particle is drawn instead: head: sparks.drawnAt. The fireworks streaks push a point every few steps and stay on their stars this way.
The solar system example pushes each comet’s trail from the kernel that moves it.
write(trail, positions) replaces a trail with x, y and z values ordered oldest first. It keeps the newest length points. write(trail, []) clears it. Rewrite a player-drawn line whenever it changes:
Writes x, y and z coordinates from oldest to newest, keeping only the newest length
points. Use write(trail, []) to empty a trail. Queue writes run before frame commands, so
kernel pushes in that frame follow these points.
width(point) returns the width in world units. shade(point, across) returns premultiplied fragment colour, with across from -1 to 1 across the ribbon. Both receive a trails.Sample with position, trail and along. along is 0 at the newest point and 1 at the oldest point a full trail can hold.
Allocates memory on the GPU, allows passing data between host and shader.
Read-only on the GPU, optimized for large data. For a general-purpose buffer,
use
TgpuRoot.createBuffer
.
@param ― typeSchema The type of data that this buffer will hold.
@param ― initial Either initial value of the buffer, or an initializer to execute on the mapped buffer. (optional)
Creates trails as rings of recent points, pushed by kernels or written on the CPU, and draws
them as lines. Throws unless capacity is an integer of at least 1 and length is an integer
of at least 2.
@injects ― member push, reset, pointAt and held, which read its own buffers in any
pipeline
@binds ― 2 storage: push or pointAt, the points and the rings
@binds ― 1 storage: held or reset alone, the rings
@binds ― 2 storage, 2 uniform: drawing, the points and the rings, the settings and
passes.viewport
@owns ― 3 buffers, a lines: the points, the rings and the settings, and the lines that draw them
@records ― drawing, added to passes, after the passes that push; clear whenever you like.
@param ― options.width a 'use gpu' function; it and shade carry the fills of what they read,
such as tgpu.fn(swell).pipe(noise.inject()). The ribbons face the camera of the pass
description they are drawn in, which width and shade read too
@returns ― a set of trails you destroy
create(
constroot:TgpuRoot
root,{
Options.capacity: number
Sets the number of trails to an integer of at least 1.
capacity:8,
Options.length: number
Sets the number of points per trail to an integer of at least 2. Pushing beyond this count
drops the oldest point.
Returns the trail width in world units at a point. Default settings.width at the newest
point, tapering with along to 0 at the oldest point of a full trail.
Returns premultiplied fragment colour. across ranges from -1 to 1 across the width. The
drawing multiplies it by how much of the pixel the trail covers. Default settings.color,
with soft edges and a fade towards the oldest point.
Both functions can read cameras.current.$. If one reads another , fill that dependency. For example, a width function that samples cached Perlin noise uses width: tgpu.fn(swell).pipe(noise.inject()).
Creates trails as rings of recent points, pushed by kernels or written on the CPU, and draws
them as lines. Throws unless capacity is an integer of at least 1 and length is an integer
of at least 2.
@injects ― member push, reset, pointAt and held, which read its own buffers in any
pipeline
@binds ― 2 storage: push or pointAt, the points and the rings
@binds ― 1 storage: held or reset alone, the rings
@binds ― 2 storage, 2 uniform: drawing, the points and the rings, the settings and
passes.viewport
@owns ― 3 buffers, a lines: the points, the rings and the settings, and the lines that draw them
@records ― drawing, added to passes, after the passes that push; clear whenever you like.
@param ― options.width a 'use gpu' function; it and shade carry the fills of what they read,
such as tgpu.fn(swell).pipe(noise.inject()). The ribbons face the camera of the pass
description they are drawn in, which width and shade read too
@returns ― a set of trails you destroy
create(
constroot:TgpuRoot
root,{
Options.capacity: number
Sets the number of trails to an integer of at least 1.
capacity:16,
Options.length: number
Sets the number of points per trail to an integer of at least 2. Pushing beyond this count
drops the oldest point.
Any shader reads trail points. held(trail) returns the point count. pointAt(trail, k) reads point k counting backward from the newest, with k below held. This kernel finds trail directions for arrowheads or sprites:
Length All trails in a set have the same length. Size it for the longest trail.
Stopped trails A stopped trail keeps its points until pushes or a write replace them. To fade it, keep each trail’s stop time in a buffer of your own and read it by point.trail in shade.
Dashesalong counts points rather than distance. Distance-based dashes and textures need your own calculation.
Facing Ribbons are joined on the screen, so they always face the camera.
Near plane A segment that crosses the near plane is cut there with no cap.
Repeated points A point pushed where the last one lies ends the ribbon there with endCap and starts it again with startCap. Unless both caps are round, push only points that moved.
frameA frame is one turn of the loop, and the object your function gets for it, which holds the encoder, the time and the time since the last frame.You own the frame →
kernelA kernel is a compute function that the GPU runs many times at once, each run on its own thread.A frame in plain TypeGPU →
fixed stepA fixed step advances a simulation by one set length of time, and each frame runs a whole number of them, so the result is the same at any frame rate.A fixed step →
threadA thread is one run of a kernel, and the GPU starts many at once, each with its own number.Slime mould →
dispatchA dispatch runs a kernel once over a number of threads, inside a compute pass.Words →
pass descriptionA pass description says once which textures a pass draws into and what each holds, and builds the pipeline for every drawing added to it.Scenes and passes →
instanceAn instance is one copy of a mesh with its own position, rotation, scale and material, kept as a row of a table.Instances →
rendererA renderer draws one sort of thing, such as instances or particles, by handing a pass description a drawing.Every renderer is a drawing →
outputAn output is a value every drawing writes at each pixel beside its colour, such as motion or an id.Outputs every drawing writes →
moduleA module is one piece of GPU work, made by a factory that borrows your root and returns resources, shader functions and methods that record into your frame.A module borrows →
storage bufferA storage buffer is GPU memory that shaders read by index and kernels can write, which is where the kit keeps rows.Words →
slotA slot is a row's place in its buffers, which a new row can take once the first is removed.Row ids →
a 'use gpu' function; it and shade carry the fills of what they read, such as tgpu.fn(swell).pipe(noise.inject()). The ribbons face the camera of the pass description they are drawn in, which width and shade read too
Returns the trail width in world units at a point.Default settings.width at the newest point, tapering with along to 0 at the oldest point of a full trail.
Returns where a trail's newest end is drawn, in place of its newest point, such as where a particle system draws the particle that pushes it: sparks.drawnAt.Default the newest point.