Classes
A class is a struct with functions around it. Dispatch is static, so inheritance, a mixin and a generic are settled while the file is compiled.
Fields and constructors
-
Derived value in a base position
The compiler builds the base representation once when it proves read-only access and equivalent method dispatch. Overrides, receiver writes and observable alias mutation remain outside that proof.
TypeScriptclass Material {color: f32 = 0.5;response(): f32 { return this.color; }}class LeafMaterial extends Material {thickness: f32 = 1.;}function response(material: Material): f32 {return material.response();}export function leafResponse(): f32 {const material = new LeafMaterial();return response(material);}Emitted WGSL struct Material {color: f32,}struct LeafMaterial {color: f32,thickness: f32,}fn response(material: Material) -> f32 {return Material_response(material);}fn leafResponse() -> f32 {var material: LeafMaterial;LeafMaterial_new_into(&material);return response(LeafMaterial_as_Material(&material));}Emitted GLSL ES 3.00 struct Material {float color;};struct LeafMaterial {float color;float thickness;};float response(Material material) {return Material_response(material);}float leafResponse() {LeafMaterial material = LeafMaterial_new();return response(LeafMaterial_as_Material(material));} -
Class without fields
Construction and method calls work with no instance fields. The host value is
{}, and a result is an instance of the class when the module exports it (Rule 8.21); the GPU receives an internal storage member that is absent from the source value.TypeScriptclass Counter {next(value: f32): f32 { return value + 1.; }}export function next(value: f32): f32 {return new Counter().next(value);}Emitted WGSL struct Counter {_empty: u32,}fn Counter_new() -> Counter {return Counter(0u);}fn Counter_next(self_: Counter, value: f32) -> f32 {return (value + 1.0);}fn next(value: f32) -> f32 {return Counter_next(Counter_new(), value);}Emitted GLSL ES 3.00 struct Counter {uint _empty;};Counter Counter_new() {return Counter(0u);}float Counter_next(Counter self_, float value) {return (value + 1.0);}float next(float value) {return Counter_next(Counter_new(), value);} -
constructorandnewnewis a call ofRay_new, which builds the struct and hands it back. WGSL spells itRay_new_intoand writes the struct into the variable that receives it (change 0104); GLSL returns it.TypeScriptclass Ray {origin: vec3dir: vec3constructor(origin: vec3, dir: vec3) {this.origin = originthis.dir = dir}}Emitted WGSL fn Ray_new_into(_dst: ptr<function, Ray>, origin: vec3<f32>, dir: vec3<f32>) {(*_dst) = Ray(origin, dir);}Emitted GLSL ES 3.00 Ray Ray_new(vec3 origin, vec3 dir) {return Ray(origin, dir);}
Methods
-
Method
A function whose first parameter is the struct, and
thisreads as that parameter.TypeScriptclass Ray {origin: vec3dir: vec3at(t: f32): vec3 {return this.origin + this.dir * t}}Emitted WGSL fn Ray_at(self_: Ray, t: f32) -> vec3<f32> {return (self_.origin + (self_.dir * t));}fn hit(r: Ray) -> vec3<f32> {return Ray_at(r, 1.0);}Emitted GLSL ES 3.00 vec3 Ray_at(Ray self_, float t) {return (self_.origin + (self_.dir * t));}vec3 hit(Ray r) {return Ray_at(r, 1.0);} -
staticmethodA function with no receiver, under the class name.
TypeScriptclass Ray {origin: vec3dir: vec3static up(): vec3 {return vec3(0., 1., 0.)}}Emitted WGSL fn Ray_up() -> vec3<f32> {return vec3<f32>(0.0, 1.0, 0.0);}Emitted GLSL ES 3.00 vec3 Ray_up() {return vec3(0.0, 1.0, 0.0);} -
Method that writes
thisA method that writes its object takes it by pointer on WGSL, and by
inouton GLSL ES 3.00. orbit-inoutTypeScriptclass Body {pos: vec3vel: vec3step(dt: f32): void {this.pos = this.pos + this.vel * dt}}Emitted WGSL fn Body_step(self_: ptr<function, Body>, dt: f32) {(*self_).pos = ((*self_).pos + ((*self_).vel * dt));}Emitted GLSL ES 3.00 void Body_step(inout Body self_, float dt) {self_.pos = (self_.pos + (self_.vel * dt));}
Inheritance and interfaces
-
extendsandsuperThe base's fields come first, an inherited method is lowered again, and
superis a function of its own. shape-inheritanceTypeScriptclass Ring extends Circle {width: f32sdf(p: vec2): f32 {return abs(super.sdf(p)) - this.width}}Emitted WGSL struct Ring {center: vec2<f32>,radius: f32,width: f32,}fn Ring_sdf(self_: ptr<function, Ring>, p: vec2<f32>) -> f32 {return (abs(Ring_super_Circle_sdf(self_, p)) - (*self_).width);}Emitted GLSL ES 3.00 struct Ring {vec2 center;float radius;float width;};float Ring_sdf(Ring self_, vec2 p) {return (abs(Ring_super_Circle_sdf(self_, p)) - self_.width);} -
abstractclassNo struct for the abstract class. Each subclass carries the fields and its own copy of the method.
TypeScriptabstract class Shape {center: vec2abstract sdf(p: vec2): f32offset(p: vec2): vec2 {return p - this.center}}Emitted WGSL struct Disc {center: vec2<f32>,radius: f32,}fn Disc_offset(self_: ptr<function, Disc>, p: vec2<f32>) -> vec2<f32> {return (p - (*self_).center);}Emitted GLSL ES 3.00 struct Disc {vec2 center;float radius;};vec2 Disc_offset(Disc self_, vec2 p) {return (p - self_.center);} -
implementsChecked by TypeScript alone. The struct is the class's own fields.
TypeScriptclass Disc implements Placed {center: vec2radius: f32}Emitted WGSL struct Disc {center: vec2<f32>,radius: f32,}Emitted GLSL ES 3.00 struct Disc {vec2 center;float radius;};
Access and accessors
-
Access modifiers
Accepted, and they mean nothing to the shader. TypeScript is what enforces them.
TypeScriptclass Sphere {private center: vec3readonly radius: f32public hit(p: vec3): f32 {return length(p - this.center) - this.radius}}Emitted WGSL struct Sphere {center: vec3<f32>,radius: f32,}fn Sphere_hit(self_: Sphere, p: vec3<f32>) -> f32 {return (length((p - self_.center)) - self_.radius);}Emitted GLSL ES 3.00 struct Sphere {vec3 center;float radius;};float Sphere_hit(Sphere self_, vec3 p) {return (length((p - self_.center)) - self_.radius);} -
Getter and setter
Each half is a function of the module,
Disc_get_areaandDisc_set_area, and a read or a write calls it. class-syntaxTypeScriptclass Disc {radius: f32get area(): f32 {return this.radius * this.radius}}Emitted WGSL struct Disc {radius: f32,}fn Disc_get_area(self_: Disc) -> f32 {return (self_.radius * self_.radius);}Emitted GLSL ES 3.00 struct Disc {float radius;};float Disc_get_area(Disc self_) {return (self_.radius * self_.radius);}
Mixins and generics
-
Mixin function
The function runs while the file is compiled. Its members are spliced in, and
Tintedis emitted nowhere. mixin-surfaceTypeScriptfunction Tinted<T extends AnyClass>(Base: T) {return class extends Base {tint: vec3lit(cover: f32): vec3 {return this.tint * cover}}}class TintedDisc extends Tinted(Disc) {}Emitted WGSL struct TintedDisc {center: vec2<f32>,radius: f32,tint: vec3<f32>,}fn TintedDisc_lit(self_: ptr<function, TintedDisc>, cover: f32) -> vec3<f32> {return ((*self_).tint * cover);}Emitted GLSL ES 3.00 struct TintedDisc {vec2 center;float radius;vec3 tint;};vec3 TintedDisc_lit(TintedDisc self_, float cover) {return (self_.tint * cover);} -
Generic function
One function per set of type arguments the program uses, and nothing called
pick. generic-helpersTypeScriptfunction pick<T>(a: T, b: T, c: bool): T {return c ? a : b}Emitted WGSL fn pick_f32(a: f32, b: f32, c: bool) -> f32 {return select(b, a, c);}fn pick_vec3(a: vec3<f32>, b: vec3<f32>, c: bool) -> vec3<f32> {return select(b, a, c);}Emitted GLSL ES 3.00 float pick_f32(float a, float b, bool c) {return mix(b, a, c);}vec3 pick_vec3(vec3 a, vec3 b, bool c) {return mix(b, a, bvec3(c));} -
Generic class
One struct per set of type arguments, each with its own copy of every method. generic-class
TypeScriptclass Slot<T> {a: Tb: Tfirst(): T {return this.a}}Emitted WGSL struct Slot_f32 {a: f32,b: f32,}struct Slot_vec3 {a: vec3<f32>,b: vec3<f32>,}Emitted GLSL ES 3.00 struct Slot_f32 {float a;float b;};struct Slot_vec3 {vec3 a;vec3 b;};