<sk-custom-wgsl-color-source>

← prev index next →

Paints a material's base color from a WGSL fragment you write yourself — the most general color-source, the escape hatch for any spatial → color rule the built-in sources cannot express.

Remarks

This is the author-WGSL member of the ColorSource family: like every color-source it gives a material a base color that varies across the surface, but here you supply the evaluator itself as WGSL rather than composing built-in pieces. It is the color-source analogue of the wider custom-WGSL family (see AbstractComputeNode).

The contract. The body must define an evaluateColorSourceRaw entry function returning straight-alpha RGBA in LINEAR space (you may declare additional helper functions alongside it): ``wgsl fn evaluateColorSourceRaw(worldPos: vec3<f32>, uv: vec2<f32>, strokeCoord: vec2<f32>, strokeLength: f32, fragCoord: vec2<f32>) -> vec4<f32> ` Each input is explicitly named for what it carries: worldPos and fragCoord are always meaningful; uv is the mesh texture coordinate (surface and edge paints); strokeCoord/strokeLength are the stroke parametrization — arc-length progress in .x, cross position in .y, total arc length in strokeLength (stroke paints). The framework declares a module-scope constant SK_EVAL_SURFACE: booltrue when the shader is generated for a surface/edge pipeline, false for a stroke pipeline — so one body serving both topologies can branch on its context (never on coordinate values, which cannot discriminate). Masking is not your concern — the applicability field and the mix(baseColor, source, weight) blend are applied outside the raw evaluator by the shared wrapper. You declare no bind groups; instead you read your own parameters through generated skUniform_<name>()` accessors, one per named sk-wgsl-uniform child, so a uniform can be animated to drive the shader without hand-packing a buffer.

This element's own world matrix. The kernel can read this element's own transform: skWorldMatrix() -> mat4x4<f32> (parent chain included) and skWorldMatrixInverse(). Author a spatial uniform (a position, a direction) in the element's LOCAL frame and lift it with (skWorldMatrix() * vec4<f32>(localPos, 1.0)).xyz (a direction uses w = 0), or map worldPos back to source-local with the inverse — so the effect moves WITH the geometry when the source (or an ancestor) is transformed. It is this ELEMENT's transform, not the painted mesh's, so one source painting several meshes exposes one consistent frame; place the source under the node whose motion it should follow. Resolves in every pass — surface, edge, stroke, stacked paint, and the shadow pipelines.

Key attributes. wgsl supplies the body inline; src loads it from an external URL (src wins over inline, with no fallback on failure), and the source is inert — the flat base-color shows through — until it resolves. alpha-mode declares how the returned alpha is classified for compositing.

Related. A material selects it with base-color-source="#id" (see sk-plain-material); its parameters are sk-wgsl-uniform, sk-wgsl-vec2-uniform, sk-wgsl-vec3-uniform, sk-wgsl-vec4-uniform, sk-wgsl-color-uniform (a colour authored as a string), and sk-wgsl-f32-array-uniform children. For a precomputed color array from a compute shader instead of a per-fragment formula, use sk-compute-buffer-color-source; to layer it, use sk-composite-color-source.

Serve these docs to run the live example. WebGPU needs a secure context — open this page via grunt serve rather than double-clicking the file. The Markup tab works from disk.

Attributes

AttributeTypeDefaultDescription
id <id> The element's unique identifier — the standard HTML global id attribute.
wgsl <string> "" Inline WGSL shader body. Ignored while src is set.
src <string> External WGSL source URL (src), or null. Assigning starts a load.
alpha-mode <custom-wgsl-alpha-mode> ("auto" | "opaque" | "translucent" | "cutout") "auto" Author-declared fragment classification (alpha-mode). Drives
fieldanimatable <id-ref> Gets the optional applicability field that masks where this source reference by id
xanimatable <number> 0 Gets the X position component.
yanimatable <number> 0 Gets the Y position component.
zanimatable <number> 0 Gets the Z position component.
hanimatable <angle> 0 Gets the heading (Y-axis) rotation in radians.
panimatable <angle> 0 Gets the pitch (X-axis) rotation in radians.
banimatable <angle> 0 Gets the bank (Z-axis) rotation in radians.
visualize <boolean> false Whether the visualization is visible (wireframe splines + center marker).
interactive-controls <boolean> false Whether interactive controls are shown (draggable markers for adjusting parameters).

Event handler attributes

Standard DOM event-handler content attributes — the value is JavaScript run when the event fires. They behave exactly as on any HTML element.

onload, onerror