savoiardi

Types

Opaque type wrapping Three.js AnimationAction.

AnimationActions schedule the performance of animations stored in AnimationClips. Actions can be played, paused, stopped, looped, crossfaded, and blended.

pub type AnimationAction

Opaque type wrapping Three.js AnimationClip.

An AnimationClip is a reusable set of keyframe tracks which represent an animation. Clips are typically loaded from GLTF/FBX models and played via AnimationAction.

pub type AnimationClip

Opaque type wrapping Three.js AnimationMixer.

The AnimationMixer is a player for animations on a particular object in the scene. When multiple objects are animated independently, one mixer per object is required. Call update_mixer with delta time each frame to advance animations.

pub type AnimationMixer

Opaque type wrapping Three.js Audio.

Creates a non-positional (global) audio object. This audio is not affected by the listener’s position - it plays at constant volume regardless of distance. Use for background music, UI sounds, etc.

pub type Audio

Opaque type wrapping the Web Audio API AudioBuffer.

Represents audio data loaded into memory. Load audio files with load_audio and assign to Audio or PositionalAudio objects with set_audio_buffer.

pub type AudioBuffer

Opaque type wrapping Three.js AudioListener.

Represents a virtual listener for all positional and non-positional audio. Typically attached to the camera. A single AudioListener is required for the Web Audio API to process audio in the scene.

pub type AudioListener

Opaque type wrapping Three.js BufferAttribute.

Stores data for an attribute (such as vertex positions, normals, colors, UVs) in a typed array buffer. Used for custom geometry and particle systems.

pub type BufferAttribute

Opaque type wrapping CSS2DObject.

A DOM element that can be positioned in 3D space and rendered by CSS2DRenderer. The element always faces the camera (billboard behavior).

pub type CSS2DObject

Opaque type wrapping CSS2DRenderer.

Renders DOM elements as 2D sprites that are positioned in 3D space but always face the camera. Useful for labels, health bars, and UI elements.

pub type CSS2DRenderer

Opaque type wrapping CSS3DObject.

A DOM element that exists in full 3D space. Unlike CSS2DObject, CSS3DObject can be rotated and transformed in 3D.

pub type CSS3DObject

Opaque type wrapping Three.js Camera.

Abstract base class for cameras. This class should not be instantiated directly. Use create_perspective_camera or create_orthographic_camera instead.

pub type Camera

Opaque type for the HTML Canvas element.

The canvas element used by the WebGL renderer for drawing.

pub type Canvas

Opaque type wrapping Three.js Color.

Represents a color. Can be created from hex values with create_color. Supports color operations like lerp_color for smooth transitions.

pub type Color

Opaque type wrapping Three.js CubeTexture.

Creates a cube texture made up of six images for skyboxes and environment maps. Load with load_cube_texture providing 6 image URLs in order: +X, -X, +Y, -Y, +Z, -Z.

pub type CubeTexture

Opaque type for an HTML DOM Element.

Represents a generic DOM element returned by CSS renderers.

pub type DomElement

Opaque type for loaded FBX data from FBXLoader.

Contains the loaded scene and animations from an FBX file. FBX is commonly exported from tools like Blender, Maya, and 3ds Max.

pub type FBXData

Opaque type wrapping JavaScript Float32Array.

A typed array of 32-bit floating point numbers. Used for buffer geometry attributes like positions, normals, colors, and UVs.

pub type Float32Array

Opaque type for fonts loaded via FontLoader.

Fonts are required for creating 3D text geometry with create_text_geometry. Fonts must be in Three.js JSON font format (converted from TTF/OTF).

pub type Font

Opaque type for loaded GLTF data from GLTFLoader.

Contains the loaded scene, animations, cameras, and other assets from a GLTF/GLB file. GLTF is the recommended format for 3D models in Three.js.

pub type GLTFData

Opaque type wrapping Three.js BufferGeometry.

A representation of mesh, line, or point geometry using typed arrays. BufferGeometry is more efficient than the deprecated Geometry class as it stores all data in buffers that can be sent directly to the GPU.

pub type Geometry

Opaque type wrapping Three.js InstancedMesh.

A special version of Mesh with instanced rendering support. Use InstancedMesh to render a large number of objects with the same geometry and material but different world transformations efficiently in a single draw call.

pub type InstancedMesh

Opaque type wrapping Three.js LOD.

Level of Detail - shows meshes with varying levels of detail based on distance from the camera. Useful for optimizing rendering of complex objects by showing simpler meshes when far away.

pub type LOD

Opaque type wrapping Three.js Light.

Abstract base class for lights. Use specific light constructors like create_ambient_light, create_directional_light, create_point_light, etc.

pub type Light

Opaque type wrapping Three.js Material.

Abstract base class for materials. Materials describe the appearance of objects and how they interact with light. Use specific material constructors like create_basic_material or create_standard_material.

pub type Material

Opaque type wrapping Three.js Matrix4.

A 4x4 matrix commonly used for 3D transformations (translation, rotation, scale). Represented internally as a column-major 16-element array.

pub type Matrix4

Opaque type wrapping Three.js Object3D.

Object3D is the base class for most objects in Three.js. It provides properties and methods for manipulating objects in 3D space including position, rotation, scale, and parent/child relationships.

Common subclasses include Mesh, Group, Camera, Light, and more.

pub type Object3D

Opaque type wrapping Three.js Points.

A class for displaying particles. Points are rendered as a cloud of points using WebGL’s POINTS primitive. Used for particle systems, point clouds, etc.

pub type Points

Opaque type wrapping Three.js PositionalAudio.

Creates a positional audio object that uses the Web Audio API’s PannerNode. Audio volume and panning are affected by the listener’s position and orientation relative to the audio source. Use for 3D sound effects attached to objects.

pub type PositionalAudio

Opaque type wrapping Three.js WebGLRenderer.

The WebGL renderer displays your scenes using WebGL. It handles all the low-level graphics operations and provides hardware-accelerated rendering.

pub type Renderer

Opaque type for WebGL renderer information from WebGLRenderer.info.

Contains memory and render statistics useful for debugging and profiling.

pub type RendererInfo

Options for creating a WebGLRenderer.

Fields

  • antialias - Whether to perform antialiasing. Smooths jagged edges but has a performance cost. Default: True
  • alpha - Whether the canvas contains an alpha (transparency) buffer. Set to True if you need the canvas background to be transparent. Default: False
  • dimensions - Fixed canvas size, or None for fullscreen mode

Example

// Fixed size renderer
let options = RendererOptions(
  antialias: True,
  alpha: False,
  dimensions: option.Some(Dimensions(800.0, 600.0)),
)
let renderer = create_renderer(options)
pub type RendererOptions {
  RendererOptions(
    antialias: Bool,
    alpha: Bool,
    dimensions: option.Option(vec2.Vec2(Float)),
  )
}

Constructors

  • RendererOptions(
      antialias: Bool,
      alpha: Bool,
      dimensions: option.Option(vec2.Vec2(Float)),
    )

    Arguments

    antialias

    Enable antialiasing (smooths jagged edges, costs performance)

    alpha

    Enable alpha channel (transparent canvas background)

    dimensions

    Canvas dimensions (None for fullscreen mode)

Opaque type wrapping Three.js Scene.

Scenes allow you to set up what is to be rendered and where. This is where you place objects, lights and cameras. The scene forms the root of the scene graph.

pub type Scene

Opaque type wrapping Three.js Sprite.

A sprite is a plane that always faces towards the camera. Commonly used for billboards, particles, and UI elements in 3D space.

pub type Sprite

Opaque type wrapping Three.js Texture.

Creates a texture to apply to a surface or as a reflection/refraction map. Textures are loaded asynchronously via load_texture.

pub type Texture

Values

pub fn add_child(
  parent parent: Object3D,
  child child: Object3D,
) -> Nil

Adds a child to an Object3D.

The child’s transform becomes relative to the parent. Moving the parent moves all children with it.

Parameters

  • parent - The parent object
  • child - The object to add as a child
pub fn add_lod_level(
  lod lod: LOD,
  object object: Object3D,
  distance distance: Float,
) -> Nil

Adds a level to a LOD object.

Objects are shown when the camera is at or beyond the specified distance (until the next level’s distance is reached).

Parameters

  • lod - The LOD object
  • object - The mesh to show at this level
  • distance - Distance threshold for this level (0.0 = closest/most detailed)
pub fn add_to_scene(
  scene scene: Scene,
  object object: Object3D,
) -> Nil

Adds an object to the scene.

Wraps Object3D.add. The object becomes a child of the scene and will be rendered.

Parameters

  • scene - The scene to add to
  • object - Any Object3D (mesh, light, group, camera, etc.)

Example

let cube = create_mesh(geometry, material)
add_to_scene(scene: scene, object: cube)
pub fn apply_material_to_object(
  object: Object3D,
  material: Material,
) -> Nil

Applies a material to all meshes in an object hierarchy.

Traverses the object and all children, replacing their materials.

pub fn apply_texture_to_object(
  object: Object3D,
  texture: Texture,
  filter_mode: String,
) -> Nil

Applies a texture to all materials in an object hierarchy.

Traverses the object and sets the texture as the map property on all materials.

Parameters

  • object - The object hierarchy
  • texture - The texture to apply
  • filter_mode - “nearest” for pixel art, “linear” for smooth textures
pub fn apply_transform(
  object object: Object3D,
  position position: vec3.Vec3(Float),
  rotation rotation: vec3.Vec3(Float),
  scale scale: vec3.Vec3(Float),
) -> Nil

Applies position, rotation, and scale to an object in one call.

More efficient than setting each property individually. Sets Object3D.position, Object3D.rotation, and Object3D.scale.

Parameters

  • object - The object to transform
  • position - Position in world units
  • rotation - Euler rotation in radians (x, y, z)
  • scale - Scale factors (1.0 = original size)
pub fn apply_transform_with_quaternion(
  object object: Object3D,
  position position: vec3.Vec3(Float),
  quaternion quaternion: quaternion.Quaternion,
  scale scale: vec3.Vec3(Float),
) -> Nil

Applies position, rotation (as quaternion), and scale to an object.

Like apply_transform but uses a Quaternion for rotation instead of Euler angles. Quaternions avoid gimbal lock and interpolate smoothly.

Parameters

  • object - The object to transform
  • position - Position in world units
  • quaternion - Rotation as Quaternion
  • scale - Scale factors
pub fn center_geometry(geometry: Geometry) -> Geometry

Centers a geometry around its bounding box center.

This is useful for STL or OBJ models that need to rotate around their geometric center rather than their original origin point.

Many CAD-exported models have their origin at an arbitrary location. Call this function after loading to ensure the model rotates around its visual center.

Example

use result <- promise.await(load_stl("/models/part.stl"))
case result {
  Ok(geometry) -> {
    // Center the geometry so it rotates around its middle
    let centered = center_geometry(geometry)
    let material = create_standard_material(0x888888, 0.5, 0.5, option.None)
    let mesh = create_mesh(centered, material)
  }
  Error(Nil) -> io.println("Failed to load")
}

Notes

  • This mutates the geometry in place and returns it for convenience
  • The geometry’s vertices are translated so the bounding box center is at origin
  • Useful for any geometry, not just STL files
pub fn clear_lod_levels(lod: LOD) -> Nil

Removes all levels from a LOD object.

pub fn clear_renderer(renderer: Renderer) -> Nil

Clears the renderer’s color, depth, and stencil buffers.

Wraps WebGLRenderer.clear. Useful when manually compositing multiple render passes.

pub fn clip_action(
  mixer: AnimationMixer,
  clip: AnimationClip,
) -> AnimationAction

Gets an AnimationAction for a clip.

Wraps AnimationMixer.clipAction. Returns an action that can be played, paused, or configured.

Parameters

  • mixer - The animation mixer
  • clip - The animation clip (from loaded GLTF/FBX)
pub fn clone_object(object: Object3D) -> Object3D

Clones an Object3D and its descendants.

Creates a deep copy of the object including all children. Geometry and materials are shared by reference (not cloned) for efficiency.

Returns

A new Object3D that is a copy of the original.

pub fn clone_texture(texture: Texture) -> Texture

Clones a texture for independent manipulation.

Wraps Texture.clone. The clone shares the underlying image but has independent UV properties.

pub fn compose_matrix(
  matrix: Matrix4,
  position: vec3.Vec3(Float),
  quaternion: quaternion.Quaternion,
  scale: vec3.Vec3(Float),
) -> Nil

Composes a transformation matrix from position, rotation, and scale.

Wraps Matrix4.compose. The resulting matrix can be used with set_instance_matrix for instanced rendering.

Parameters

  • matrix - The matrix to modify
  • position - Translation as Vec3
  • quaternion - Rotation as Quaternion
  • scale - Scale factors as Vec3
pub fn copy_position(object: Object3D, source: Object3D) -> Nil

Copies position from another object.

Sets this object’s position to match the source object’s position.

pub fn copy_rotation(object: Object3D, source: Object3D) -> Nil

Copies rotation from another object.

Sets this object’s rotation to match the source object’s rotation.

pub fn copy_scale(object: Object3D, source: Object3D) -> Nil

Copies scale from another object.

Sets this object’s scale to match the source object’s scale.

pub fn create_ambient_light(
  color: Int,
  intensity: Float,
) -> Light

Creates an AmbientLight.

Illuminates all objects equally from all directions. Provides baseline illumination so shadows aren’t completely black. Does not cast shadows.

Parameters

  • color - Light color as hex
  • intensity - Light strength (typically 0.1-1.0)

Example

// Soft ambient light
let ambient = create_ambient_light(0x404040, 0.5)
add_to_scene(scene: scene, object: ambient)
pub fn create_animation_mixer(root: Object3D) -> AnimationMixer

Creates an AnimationMixer.

The mixer is a player for animations on a particular object. Each animated object needs its own mixer. Call update_mixer each frame with delta time.

Parameters

  • root - The root object to animate (usually a loaded model)
pub fn create_audio(listener: AudioListener) -> Audio

Creates a global Audio object.

Non-positional audio that plays at constant volume regardless of camera position. Use for background music, UI sounds, ambient audio.

Parameters

  • listener - The AudioListener for the scene
pub fn create_audio_listener() -> AudioListener

Creates an AudioListener.

Represents the virtual “ears” for the scene. Usually attached to the camera. Required for any audio playback in the scene.

pub fn create_axes_helper(size: Float) -> Object3D

Creates an AxesHelper.

Displays the 3 axes in a simple way: X (red), Y (green), Z (blue). Useful for understanding object orientation and debugging transforms.

Parameters

  • size - Length of each axis line
pub fn create_basic_material(
  color: Int,
  transparent: Bool,
  opacity: Float,
  map: option.Option(Texture),
) -> Material

Creates a MeshBasicMaterial.

A simple material that is not affected by lights. Displays a flat color or texture. Useful for unlit objects, wireframes, and debugging.

Parameters

  • color - Base color as hex (e.g., 0xff0000 for red)
  • transparent - Enable transparency (required for opacity < 1.0)
  • opacity - Opacity from 0.0 (invisible) to 1.0 (opaque)
  • map - Optional color texture

Example

// Solid red material
let red = create_basic_material(0xff0000, False, 1.0, option.None)

// Textured with transparency
let textured = create_basic_material(0xffffff, True, 0.8, option.Some(texture))
pub fn create_box_geometry(
  width: Float,
  height: Float,
  depth: Float,
) -> Geometry

Creates a BoxGeometry (rectangular cuboid).

The geometry is centered at the origin. The box has 6 faces with correct normals and UVs for texturing.

Parameters

  • width - Width (X axis)
  • height - Height (Y axis)
  • depth - Depth (Z axis)

Example

let cube = create_box_geometry(1.0, 1.0, 1.0)
let mesh = create_mesh(cube, material)
pub fn create_box_helper(
  object: Object3D,
  color: Int,
) -> Object3D

Creates a BoxHelper.

A wireframe box that shows the bounding box of an object. Useful for visualizing object bounds and debugging collisions.

Parameters

  • object - The object to create a bounding box helper for
  • color - Wireframe color as hex
pub fn create_buffer_attribute(
  array: Float32Array,
  item_size: Int,
) -> BufferAttribute

Creates a BufferAttribute from a Float32Array.

BufferAttributes store data for geometry attributes like positions, normals, and colors.

Parameters

  • array - The Float32Array containing the data
  • item_size - Number of values per vertex (3 for xyz, 4 for rgba, 2 for uv)
pub fn create_buffer_geometry() -> Geometry

Creates an empty BufferGeometry.

BufferGeometry is the most efficient geometry type, storing data in typed arrays. Add attributes (position, normal, color, uv) using set_geometry_attribute.

pub fn create_circle_geometry(
  radius: Float,
  segments: Int,
) -> Geometry

Creates a CircleGeometry.

A flat circular disc in the XY plane, facing the +Z direction.

Parameters

  • radius - Circle radius
  • segments - Number of segments. Higher = smoother circle (minimum 3)
pub fn create_color(hex: Int) -> Color

Creates a Color from a hex value.

Parameters

  • hex - Color as hex integer (e.g., 0xff0000 for red)
pub fn create_cone_geometry(
  radius: Float,
  height: Float,
  segments: Int,
) -> Geometry

Creates a ConeGeometry.

The cone points up along the Y axis with its base centered at the origin.

Parameters

  • radius - Base radius
  • height - Cone height
  • segments - Number of radial segments. Higher = smoother circular base
pub fn create_css2d_object(html: String) -> CSS2DObject

Creates a CSS2DObject from HTML.

The HTML string is parsed and wrapped in a CSS2DObject that can be positioned in 3D space.

Parameters

  • html - HTML string for the label content
pub fn create_css2d_renderer() -> CSS2DRenderer

Creates a CSS2DRenderer.

CSS2D renders DOM elements in 3D space, always facing the camera. Use for labels, health bars, tooltips, and UI elements attached to 3D objects.

pub fn create_css3d_object(html: String) -> CSS3DObject

Creates a CSS3DObject from HTML.

Unlike CSS2DObject, CSS3DObject exists in full 3D space and can be rotated and scaled like any other 3D object.

pub fn create_cylinder_geometry(
  radius_top: Float,
  radius_bottom: Float,
  height: Float,
  radial_segments: Int,
) -> Geometry

Creates a CylinderGeometry.

A cylinder along the Y axis, centered at the origin. Can also create cones by setting one radius to 0.

Parameters

  • radius_top - Top cap radius (0 for a cone)
  • radius_bottom - Bottom cap radius
  • height - Cylinder height
  • radial_segments - Number of faces around the circumference

Example

// Regular cylinder
let cylinder = create_cylinder_geometry(1.0, 1.0, 2.0, 32)

// Tapered cylinder (cone-like)
let tapered = create_cylinder_geometry(0.5, 1.0, 2.0, 32)
pub fn create_directional_light(
  color: Int,
  intensity: Float,
  cast_shadow: Bool,
  shadow_resolution: Int,
  shadow_bias: Float,
) -> Light

Creates a DirectionalLight.

A light that shines in a specific direction, as if from infinitely far away (like the sun). All rays are parallel. Can cast shadows.

Parameters

  • color - Light color as hex
  • intensity - Light strength
  • cast_shadow - Enable shadow casting
  • shadow_resolution - Shadow map size (e.g., 1024, 2048). Higher = sharper shadows
  • shadow_bias - Bias to prevent shadow acne. Typical: -0.0001 to -0.001

Example

let sun = create_directional_light(0xffffff, 1.0, True, 2048, -0.0001)
set_object_position(sun, vec3.new(10.0, 20.0, 10.0))
add_to_scene(scene: scene, object: sun)
pub fn create_float32_array(size: Int) -> Float32Array

Creates a Float32Array of the specified size.

Used for storing vertex data for buffer geometry attributes.

Parameters

  • size - Number of float elements in the array
pub fn create_grid_helper(
  size: Float,
  divisions: Int,
  color: Int,
) -> Object3D

Creates a GridHelper.

A 2D grid in the XZ plane. Useful for visualizing the ground plane and understanding scale in the scene.

Parameters

  • size - Total size of the grid (extends size/2 in each direction)
  • divisions - Number of divisions (grid cells)
  • color - Grid line color as hex
pub fn create_group() -> Object3D

Creates an empty Group (Object3D container).

Groups are useful for organizing objects hierarchically. Transforming a group affects all its children. Groups have no visual representation themselves.

Example

let car = create_group()
add_child(parent: car, child: body_mesh)
add_child(parent: car, child: wheel1)
add_child(parent: car, child: wheel2)
// Now moving the car moves all parts together
set_object_position(car, vec3.new(10.0, 0.0, 0.0))
pub fn create_hemisphere_light(
  sky_color: Int,
  ground_color: Int,
  intensity: Float,
) -> Light

Creates a HemisphereLight.

A light positioned above the scene that fades from sky color (above) to ground color (below). Provides natural-looking ambient lighting for outdoor scenes. Does not cast shadows.

Parameters

  • sky_color - Color from above as hex
  • ground_color - Color from below as hex
  • intensity - Light strength

Example

// Blue sky, brown ground
let hemi = create_hemisphere_light(0x87ceeb, 0x8b4513, 0.6)
add_to_scene(scene: scene, object: hemi)
pub fn create_icosahedron_geometry(
  radius: Float,
  detail: Int,
) -> Geometry

Creates an IcosahedronGeometry (20-faced polyhedron).

An icosahedron centered at the origin. With detail > 0, becomes a good approximation of a sphere with evenly distributed vertices.

Parameters

  • radius - Radius of the circumscribed sphere
  • detail - Subdivision level (0 = 20 faces, 1 = 80 faces, etc.)
pub fn create_instanced_mesh(
  geometry: Geometry,
  material: Material,
  count: Int,
) -> InstancedMesh

Creates an InstancedMesh.

Renders many copies of the same geometry/material with different transforms in a single draw call. Extremely efficient for rendering large numbers of similar objects (trees, particles, crowd, etc.).

Parameters

  • geometry - Shared geometry for all instances
  • material - Shared material for all instances
  • count - Maximum number of instances

Example

// Render 1000 trees efficiently
let tree_geometry = create_cone_geometry(1.0, 3.0, 8)
let tree_material = create_basic_material(0x228b22, False, 1.0, option.None)
let trees = create_instanced_mesh(tree_geometry, tree_material, 1000)
pub fn create_lambert_material(
  color: Int,
  map: option.Option(Texture),
  normal_map: option.Option(Texture),
  ao_map: option.Option(Texture),
  transparent: Bool,
  opacity: Float,
  alpha_test: Float,
) -> Material

Creates a MeshLambertMaterial.

A material for non-shiny surfaces (matte). Cheaper than Phong/Standard. Uses Lambertian reflectance (diffuse only, no specular).

Parameters

  • color - Base color as hex
  • map - Color texture
  • normal_map - Normal map
  • ao_map - Ambient occlusion map
  • transparent - Enable transparency
  • opacity - Opacity value
  • alpha_test - Alpha cutoff threshold
pub fn create_line_material(
  color: Int,
  linewidth: Float,
) -> Material

Creates a LineBasicMaterial.

Material for Line and LineSegments objects.

Parameters

  • color - Line color as hex
  • linewidth - Line width in pixels (note: line width > 1 not supported on all platforms)
pub fn create_line_segments(
  geometry: Geometry,
  material: Material,
) -> Object3D

Creates LineSegments.

Draws a series of line segments. Every pair of consecutive vertices forms a separate line segment (unlike Line which connects all vertices).

Parameters

  • geometry - Geometry with vertex positions
  • material - A LineBasicMaterial (created with create_line_material)
pub fn create_lod() -> LOD

Creates a LOD (Level of Detail) object.

LOD shows different meshes based on distance from the camera. Use simpler meshes for distant objects to improve performance.

Example

let lod = create_lod()
add_lod_level(lod: lod, object: high_detail_mesh, distance: 0.0)
add_lod_level(lod: lod, object: medium_detail_mesh, distance: 50.0)
add_lod_level(lod: lod, object: low_detail_mesh, distance: 100.0)
add_to_scene(scene: scene, object: lod)
pub fn create_matrix4() -> Matrix4

Creates a new identity Matrix4.

A 4x4 transformation matrix initialized to the identity matrix. Use compose_matrix to set translation, rotation, and scale.

pub fn create_mesh(
  geometry: Geometry,
  material: Material,
) -> Object3D

Creates a Mesh from geometry and material.

A mesh is a 3D object composed of triangular polygons. It combines geometry (shape) with material (appearance) to create a renderable object.

Parameters

  • geometry - The shape of the mesh (vertices, faces, UVs, normals)
  • material - The surface appearance (color, texture, shading)

Example

let geometry = create_box_geometry(1.0, 1.0, 1.0)
let material = create_standard_material(0xff0000, 0.5, 0.5, ...)
let cube = create_mesh(geometry, material)
add_to_scene(scene: scene, object: cube)
pub fn create_orthographic_camera(
  left: Float,
  right: Float,
  top: Float,
  bottom: Float,
  near: Float,
  far: Float,
) -> Camera

Creates an OrthographicCamera.

In orthographic projection, object size stays constant regardless of distance. Useful for 2D games, isometric views, UI elements, and CAD-style applications.

Parameters

  • left - Left plane of the camera frustum
  • right - Right plane of the camera frustum
  • top - Top plane of the camera frustum
  • bottom - Bottom plane of the camera frustum
  • near - Near clipping plane
  • far - Far clipping plane

Example

// Orthographic camera for 2D game (origin at center)
let half_width = 10.0
let half_height = 10.0 /. aspect
let camera = create_orthographic_camera(
  -half_width, half_width,
  half_height, -half_height,
  0.1, 1000.0
)
pub fn create_perspective_camera(
  fov: Float,
  aspect: Float,
  near: Float,
  far: Float,
) -> Camera

Creates a PerspectiveCamera.

The most common projection mode for 3D rendering. Mimics how the human eye sees, with objects getting smaller as they move further away (perspective foreshortening).

Parameters

  • fov - Field of view in degrees (vertical). Typical values: 45-75. Higher = wider view
  • aspect - Aspect ratio (width / height). Should match canvas aspect ratio
  • near - Near clipping plane. Objects closer than this won’t render. Typical: 0.1
  • far - Far clipping plane. Objects further than this won’t render. Typical: 1000-10000

Important Notes

  • Keep near as large as possible to avoid z-fighting (depth buffer precision issues)
  • The ratio far/near should be as small as possible for best depth precision
  • After changing fov or aspect, call update_camera_projection_matrix

Example

// Standard 16:9 camera
let camera = create_perspective_camera(75.0, 16.0 /. 9.0, 0.1, 1000.0)
set_object_position(camera, vec3.new(0.0, 5.0, 10.0))
pub fn create_phong_material(
  color: Int,
  shininess: Float,
  map: option.Option(Texture),
  normal_map: option.Option(Texture),
  ao_map: option.Option(Texture),
  transparent: Bool,
  opacity: Float,
  alpha_test: Float,
) -> Material

Creates a MeshPhongMaterial.

A material for shiny surfaces with specular highlights. Less realistic than StandardMaterial but faster to render. Good for stylized graphics.

Parameters

  • color - Base color as hex
  • shininess - Specular highlight size (higher = smaller, sharper highlights)
  • map - Color texture
  • normal_map - Normal map
  • ao_map - Ambient occlusion map
  • transparent - Enable transparency
  • opacity - Opacity value
  • alpha_test - Alpha cutoff threshold (pixels below this are discarded)
pub fn create_plane_geometry(
  width: Float,
  height: Float,
  width_segments: Int,
  height_segments: Int,
) -> Geometry

Creates a PlaneGeometry.

A flat rectangular surface in the XY plane, facing the +Z direction. Commonly used for floors, walls, billboards, and UI elements.

Parameters

  • width - Width (X axis)
  • height - Height (Y axis)
  • width_segments - Subdivisions along width (1 = single quad)
  • height_segments - Subdivisions along height (1 = single quad)

Example

// Simple floor (rotate to lie flat)
let floor = create_plane_geometry(100.0, 100.0, 1, 1)
set_object_rotation(floor_mesh, vec3.new(-3.14159 /. 2.0, 0.0, 0.0))
pub fn create_point_light(
  color: Int,
  intensity: Float,
  distance: Float,
  cast_shadow: Bool,
  shadow_resolution: Int,
  shadow_bias: Float,
) -> Light

Creates a PointLight.

A light that emits in all directions from a single point (like a light bulb). Intensity falls off with distance. Can cast shadows (expensive).

Parameters

  • color - Light color as hex
  • intensity - Light strength
  • distance - Maximum range (0 = no limit, light decays naturally)
  • cast_shadow - Enable shadow casting (uses cube shadow map, expensive)
  • shadow_resolution - Shadow map size
  • shadow_bias - Bias to prevent shadow acne
pub fn create_points(
  geometry: Geometry,
  material: Material,
) -> Points

Creates a Points object (particle system).

Renders each vertex in the geometry as a point/sprite. Efficient for large particle systems with thousands of particles.

Parameters

  • geometry - Geometry containing vertex positions (and optionally colors)
  • material - A PointsMaterial (created with create_points_material)
pub fn create_points_material(
  size: Float,
  vertex_colors: Bool,
  transparent: Bool,
  opacity: Float,
  depth_write: Bool,
  blending: Int,
  size_attenuation: Bool,
) -> Material

Creates a PointsMaterial.

Material for Points (particle) objects. Each vertex is rendered as a point/sprite.

Parameters

  • size - Point size in world units (or pixels if size_attenuation is False)
  • vertex_colors - Use per-vertex colors from geometry
  • transparent - Enable transparency
  • opacity - Opacity value
  • depth_write - Write to depth buffer (False for additive particles)
  • blending - Blending mode (use get_additive_blending or get_normal_blending)
  • size_attenuation - If True, points get smaller with distance

Example

// Additive particle material
let particles = create_points_material(
  0.1, False, True, 0.8,
  False, get_additive_blending(), True
)
pub fn create_positional_audio(
  listener: AudioListener,
) -> PositionalAudio

Creates a PositionalAudio object.

3D audio that changes volume and panning based on distance and direction from the listener. Use for sound effects attached to objects.

Parameters

  • listener - The AudioListener for the scene
pub fn create_renderer(options: RendererOptions) -> Renderer

Creates a new WebGLRenderer.

The WebGL renderer uses WebGL to render your scene. It automatically creates a canvas element and appends it to the document body (in fullscreen mode).

Parameters

  • options - Renderer configuration (see RendererOptions)

Example

// Fullscreen with antialiasing
let renderer = create_renderer(default_renderer_options())

// Fixed size, transparent background
let renderer = create_renderer(RendererOptions(
  antialias: True,
  alpha: True,
  dimensions: option.Some(Dimensions(1280.0, 720.0)),
))
pub fn create_scene() -> Scene

Creates a new Three.js Scene.

The scene is the container for all objects, lights, and cameras. It forms the root of the scene graph that gets rendered.

Example

let scene = create_scene()
  |> set_scene_background_color(0x000000)
pub fn create_sphere_geometry(
  radius: Float,
  width_segments: Int,
  height_segments: Int,
) -> Geometry

Creates a SphereGeometry.

The sphere is centered at the origin. Higher segment counts produce smoother spheres but use more triangles.

Parameters

  • radius - Sphere radius
  • width_segments - Horizontal segments (longitude). Minimum 3, typical 32
  • height_segments - Vertical segments (latitude). Minimum 2, typical 16

Example

// Smooth sphere
let sphere = create_sphere_geometry(1.0, 32, 16)

// Low-poly sphere
let low_poly = create_sphere_geometry(1.0, 8, 6)
pub fn create_spot_light(
  color: Int,
  intensity: Float,
  distance: Float,
  angle: Float,
  penumbra: Float,
  cast_shadow: Bool,
  shadow_resolution: Int,
  shadow_bias: Float,
) -> Light

Creates a SpotLight.

A light that emits in a cone from a single point (like a flashlight or stage light). Can cast shadows with a single shadow map.

Parameters

  • color - Light color as hex
  • intensity - Light strength
  • distance - Maximum range (0 = no limit)
  • angle - Maximum cone angle in radians (max: π/2)
  • penumbra - Soft edge percentage (0 = hard edge, 1 = fully soft)
  • cast_shadow - Enable shadow casting
  • shadow_resolution - Shadow map size
  • shadow_bias - Bias to prevent shadow acne
pub fn create_sprite(material: Material) -> Sprite

Creates a Sprite.

A sprite is a plane that always faces the camera (billboard). Commonly used for particles, labels, lens flares, and UI elements in 3D space.

Parameters

  • material - A SpriteMaterial (created with create_sprite_material)
pub fn create_sprite_material(
  color: Int,
  transparent: Bool,
  opacity: Float,
  map: option.Option(Texture),
) -> Material

Creates a SpriteMaterial.

Material for Sprite objects (always camera-facing planes).

Parameters

  • color - Tint color as hex
  • transparent - Enable transparency
  • opacity - Opacity value
  • map - Sprite texture
pub fn create_standard_material(
  color: Int,
  metalness: Float,
  roughness: Float,
  transparent: Bool,
  opacity: Float,
  map: option.Option(Texture),
  normal_map: option.Option(Texture),
  ao_map: option.Option(Texture),
  displacement_map: option.Option(Texture),
  displacement_scale: Float,
  displacement_bias: Float,
  roughness_map: option.Option(Texture),
  metalness_map: option.Option(Texture),
  emissive: Int,
  emissive_intensity: Float,
) -> Material

Creates a MeshStandardMaterial.

A physically-based rendering (PBR) material using the metallic-roughness workflow. This is the recommended material for realistic rendering with proper lighting.

Parameters

  • color - Base color (albedo) as hex
  • metalness - How metallic the surface is (0.0 = dielectric, 1.0 = metal)
  • roughness - How rough the surface is (0.0 = mirror, 1.0 = diffuse)
  • transparent - Enable transparency
  • opacity - Opacity value
  • map - Color/albedo texture
  • normal_map - Normal map for surface detail
  • ao_map - Ambient occlusion map
  • displacement_map - Displacement map for vertex displacement
  • displacement_scale - Displacement strength
  • displacement_bias - Displacement offset
  • roughness_map - Per-pixel roughness
  • metalness_map - Per-pixel metalness
  • emissive - Emissive (glow) color
  • emissive_intensity - Emissive strength

Example

// Shiny metal
let metal = create_standard_material(
  0xcccccc, 1.0, 0.2,  // Silver, full metal, slightly rough
  False, 1.0,          // Opaque
  option.None, option.None, option.None,  // No textures
  option.None, 0.0, 0.0,  // No displacement
  option.None, option.None,  // No maps
  0x000000, 0.0         // No emission
)
pub fn create_tetrahedron_geometry(
  radius: Float,
  detail: Int,
) -> Geometry

Creates a TetrahedronGeometry (4-faced polyhedron).

A tetrahedron (triangular pyramid) centered at the origin.

Parameters

  • radius - Radius of the circumscribed sphere
  • detail - Subdivision level (0 = base shape, higher = more triangles)
pub fn create_text_geometry(
  text: String,
  font: Font,
  size: Float,
  depth: Float,
  curve_segments: Int,
  bevel_enabled: Bool,
  bevel_thickness: Float,
  bevel_size: Float,
  bevel_offset: Float,
  bevel_segments: Int,
) -> Geometry

Creates a TextGeometry for 3D text.

Generates extruded 3D text from a string. Requires a font loaded via load_font.

Parameters

  • text - The text string to render
  • font - A Font loaded via load_font
  • size - Font size
  • depth - Extrusion depth (thickness)
  • curve_segments - Curve smoothness for rounded letters
  • bevel_enabled - Whether to add beveled edges
  • bevel_thickness - Bevel depth
  • bevel_size - Bevel extent from text outline
  • bevel_offset - Bevel start offset
  • bevel_segments - Bevel smoothness

Example

load_font("/fonts/helvetiker.json")
|> promise.map(fn(result) {
  case result {
    Ok(font) -> {
      let text_geo = create_text_geometry(
        "Hello", font, 1.0, 0.2, 12,
        True, 0.03, 0.02, 0.0, 3
      )
      create_mesh(text_geo, material)
    }
    Error(Nil) -> panic
  }
})
pub fn create_toon_material(
  color: Int,
  map: option.Option(Texture),
  normal_map: option.Option(Texture),
  ao_map: option.Option(Texture),
  transparent: Bool,
  opacity: Float,
  alpha_test: Float,
) -> Material

Creates a MeshToonMaterial.

A cel-shaded (cartoon) material with hard lighting transitions. Great for stylized, anime-like graphics.

Parameters

  • color - Base color as hex
  • map - Color texture
  • normal_map - Normal map
  • ao_map - Ambient occlusion map
  • transparent - Enable transparency
  • opacity - Opacity value
  • alpha_test - Alpha cutoff threshold
pub fn create_torus_geometry(
  radius: Float,
  tube: Float,
  radial_segments: Int,
  tubular_segments: Int,
) -> Geometry

Creates a TorusGeometry (donut shape).

A torus centered at the origin, lying in the XY plane.

Parameters

  • radius - Distance from center to the middle of the tube
  • tube - Radius of the tube (thickness)
  • radial_segments - Segments around the tube cross-section
  • tubular_segments - Segments around the torus circumference
pub fn default_renderer_options() -> RendererOptions

Returns default renderer options: fullscreen, antialiased, opaque background.

Equivalent to RendererOptions(antialias: True, alpha: False, dimensions: option.None).

See WebGLRenderer for all available options in Three.js.

pub fn delete_camera_user_data(
  camera: Camera,
  key: String,
) -> Nil

Deletes user data from a camera.

Parameters

  • camera - The camera to remove data from
  • key - String key to delete
pub fn dispose_geometry(geometry: Geometry) -> Nil

Disposes of a geometry and frees GPU memory.

Wraps BufferGeometry.dispose. Call when geometry is no longer needed.

pub fn dispose_material(material: Material) -> Nil

Disposes of a material and frees GPU memory.

Wraps Material.dispose. Note: This does not dispose textures used by the material.

pub fn dispose_object(object: Object3D) -> Nil

Disposes of an Object3D and its resources.

Frees GPU memory by disposing geometry and materials. Call this when removing objects permanently to prevent memory leaks.

Important: After disposal, the object should not be used.

See How to dispose of objects.

pub fn dispose_texture(texture: Texture) -> Nil

Disposes of a texture and frees GPU memory.

Wraps Texture.dispose. Call when a texture is no longer needed to prevent memory leaks.

pub fn enable_transparency(object: Object3D) -> Nil

Enables transparency on all materials in an object hierarchy.

Traverses the object and all children, setting transparent: true on all materials.

pub fn extract_mesh_material_pairs(
  object: Object3D,
) -> #(array.Array(Geometry), array.Array(Material))

Extracts all geometries and materials from an Object3D hierarchy.

Traverses the object and its children, collecting all unique geometries and materials found on meshes.

Returns

A tuple of #(geometries, materials) as JavaScript arrays.

pub fn get_additive_blending() -> Int

Gets the AdditiveBlending constant.

Additive blending adds the source and destination colors together. Creates a “glow” effect, commonly used for particles and effects.

pub fn get_attribute(
  geometry: Geometry,
  name: String,
) -> BufferAttribute

Gets an attribute from geometry by name.

Wraps BufferGeometry.getAttribute.

Parameters

  • geometry - The geometry to query
  • name - Attribute name (“position”, “color”, “normal”, “uv”, etc.)
pub fn get_audio_context_state() -> String

Gets the current state of the Web Audio AudioContext.

Returns

One of: “suspended”, “running”, or “closed”. Browsers often start in “suspended” state until user interaction.

pub fn get_audio_loop(audio: Audio) -> Bool

Gets the loop state of an audio object.

Accesses Audio.loop.

pub fn get_camera_user_data(camera: Camera, key: String) -> a

Retrieves user data from a camera.

Wraps Object3D.userData.

Parameters

  • camera - The camera to get data from
  • key - String key for the data

Returns

The stored value, or undefined if not set.

pub fn get_canvas_dimensions(
  renderer: Renderer,
) -> vec2.Vec2(Float)

Gets the current canvas dimensions from the renderer.

Returns a tuple of #(width, height) in pixels.

See WebGLRenderer.

pub fn get_clamp_to_edge_wrapping() -> Int

Gets the ClampToEdgeWrapping constant.

Texture edge pixels stretch infinitely when UVs exceed 0-1 range.

pub fn get_clip_duration(clip: AnimationClip) -> Float

Gets the duration of an animation clip in seconds.

Accesses AnimationClip.duration.

pub fn get_clip_name(clip: AnimationClip) -> String

Gets the name of an animation clip.

Accesses AnimationClip.name.

pub fn get_color_b(color: Color) -> Float

Gets the blue component of a color (0.0 to 1.0).

Accesses Color.b.

pub fn get_color_g(color: Color) -> Float

Gets the green component of a color (0.0 to 1.0).

Accesses Color.g.

pub fn get_color_r(color: Color) -> Float

Gets the red component of a color (0.0 to 1.0).

Accesses Color.r.

pub fn get_css2d_renderer_dom_element(
  renderer: CSS2DRenderer,
) -> DomElement

Gets the DOM element of a CSS2DRenderer.

The DOM element should be positioned over the WebGL canvas.

pub fn get_geometry(points: Points) -> Geometry

Gets the geometry from a Points object.

Accesses Points.geometry.

pub fn get_linear_filter() -> Int

Gets the LinearFilter constant.

Bilinear interpolation for smooth texture sampling.

pub fn get_loop_once() -> Int

Gets the LoopOnce constant.

Animation plays once and stops at the end.

pub fn get_loop_ping_pong() -> Int

Gets the LoopPingPong constant.

Animation plays forward, then backward, then forward, etc.

pub fn get_loop_repeat() -> Int

Gets the LoopRepeat constant.

Animation plays repeatedly, restarting from the beginning each time.

pub fn get_mirrored_repeat_wrapping() -> Int

Gets the MirroredRepeatWrapping constant.

Texture repeats with alternating mirrored copies.

pub fn get_nearest_filter() -> Int

Gets the NearestFilter constant.

No interpolation, nearest pixel is used. Best for pixel art.

pub fn get_normal_blending() -> Int

Gets the NormalBlending constant.

Standard alpha blending where source replaces destination based on alpha.

pub fn get_object_geometry(object: Object3D) -> Geometry

Gets the geometry from a mesh.

Accesses Mesh.geometry.

pub fn get_object_material(object: Object3D) -> Material

Gets the material from a mesh.

Accesses Mesh.material.

pub fn get_object_position(object: Object3D) -> vec3.Vec3(Float)

Gets the object’s local position.

Accesses Object3D.position.

pub fn get_object_quaternion(
  object: Object3D,
) -> quaternion.Quaternion

Gets the object’s local rotation as a quaternion.

Accesses Object3D.quaternion.

pub fn get_object_rotation(object: Object3D) -> vec3.Vec3(Float)

Gets the object’s local rotation as Euler angles.

Accesses Object3D.rotation.

pub fn get_object_scale(object: Object3D) -> vec3.Vec3(Float)

Gets the object’s local scale.

Accesses Object3D.scale.

pub fn get_positional_audio_loop(audio: PositionalAudio) -> Bool

Gets the loop state of a positional audio source.

See: PositionalAudio.loop

Parameters

  • audio: The positional audio source to check

Returns

True if looping is enabled, False otherwise.

Example

// Toggle loop state
let current_loop = get_positional_audio_loop(music)
set_positional_audio_loop(music, !current_loop)
pub fn get_render_stats(renderer: Renderer) -> #(Int, Int)

Gets render statistics from the last frame.

Accesses WebGLRenderer.info. Useful for performance monitoring and debugging.

Returns

A tuple of #(draw_calls, triangles):

  • draw_calls - Number of WebGL draw calls
  • triangles - Number of triangles rendered

Example

let #(calls, tris) = get_render_stats(renderer)
io.println("Draw calls: " <> int.to_string(calls))
io.println("Triangles: " <> int.to_string(tris))
pub fn get_renderer_dom_element(renderer: Renderer) -> Canvas

Gets the canvas DOM element from the renderer.

Wraps WebGLRenderer.domElement. Useful for adding event listeners or appending to a specific container.

Example

let canvas = get_renderer_dom_element(renderer)
// Now you can use the canvas for input handling
pub fn get_renderer_info(renderer: Renderer) -> RendererInfo

Gets diagnostic information about the WebGL renderer.

Returns information about the WebGL context including:

  • Memory usage (geometries, textures)
  • Render statistics (draw calls, triangles, points, lines)
  • WebGL capabilities and limits

Useful for debugging performance issues and monitoring resource usage.

See: WebGLRenderer.info

Example

let info = get_renderer_info(renderer)
// Access render statistics for profiling

Notes

  • The returned object structure depends on Three.js version
  • Memory stats include counts of geometries and textures in GPU memory
  • Render stats are reset each frame by default
  • Call info.reset() in JavaScript to manually reset stats
pub fn get_repeat_wrapping() -> Int

Gets the RepeatWrapping constant.

Texture repeats infinitely when UVs exceed 0-1 range.

pub fn get_world_position(object: Object3D) -> vec3.Vec3(Float)

Gets the object’s world position.

Wraps Object3D.getWorldPosition. Returns the position in world space, accounting for all parent transforms.

pub fn get_world_quaternion(
  object: Object3D,
) -> quaternion.Quaternion

Gets the object’s world quaternion.

Wraps Object3D.getWorldQuaternion. Returns the rotation as a quaternion in world space.

pub fn has_audio_buffer(audio: Audio) -> Bool

Checks if an audio object has a buffer loaded.

Returns True if set_audio_buffer has been called with a valid buffer.

pub fn has_camera_user_data(camera: Camera, key: String) -> Bool

Checks if a camera has user data for a specific key.

Parameters

  • camera - The camera to check
  • key - String key to look for

Returns

True if the key exists in userData, False otherwise.

pub fn has_positional_audio_buffer(
  audio: PositionalAudio,
) -> Bool

Checks if a positional audio source has a buffer loaded.

Useful to verify audio is ready before attempting playback.

See: PositionalAudio.buffer

Parameters

  • audio: The positional audio source to check

Returns

True if a buffer has been set, False otherwise.

Example

// Only play if buffer is ready
case has_positional_audio_buffer(sound) {
  True -> play_positional_audio(sound)
  False -> {
    // Buffer still loading, queue for later
    io.println("Audio not ready")
  }
}
pub fn is_audio_playing(audio: Audio) -> Bool

Checks if audio is currently playing.

Accesses Audio.isPlaying.

pub fn is_context_valid(renderer: Renderer) -> Bool

Checks if the WebGL context is still valid and usable.

The WebGL context can be lost due to:

  • GPU driver crashes or resets
  • Too many active WebGL contexts
  • System resource pressure
  • Device sleep/wake cycles

See: WebGLRenderer

Example

case is_context_valid(renderer) {
  True -> render(renderer, scene, camera)
  False -> {
    // Handle context loss - may need to recreate renderer
    io.println("WebGL context lost!")
  }
}

Notes

  • Returns False if context was lost and not yet restored
  • Listen for webglcontextlost and webglcontextrestored events
  • When context is lost, all textures and buffers are invalidated
  • May need to recreate resources after context restoration
pub fn is_instanced_mesh(object: Object3D) -> Bool

Checks if an object is an InstancedMesh.

pub fn is_lod(object: Object3D) -> Bool

Checks if an object is a LOD.

pub fn is_orthographic_camera(object: Object3D) -> Bool

Checks if an object is an OrthographicCamera.

pub fn is_perspective_camera(object: Object3D) -> Bool

Checks if an object is a PerspectiveCamera.

Uses Three.js’s isPerspectiveCamera property for type checking.

Returns

True if the object is a PerspectiveCamera, False otherwise.

pub fn is_positional_audio_playing(
  audio: PositionalAudio,
) -> Bool

Checks if a positional audio source is currently playing.

See: PositionalAudio.isPlaying

Parameters

  • audio: The positional audio source to check

Returns

True if the audio is currently playing, False otherwise.

Example

case is_positional_audio_playing(music) {
  True -> pause_positional_audio(music)
  False -> play_positional_audio(music)
}
pub fn lerp_color(
  color1: Color,
  color2: Color,
  t: Float,
) -> Color

Linearly interpolates between two colors.

Wraps Color.lerp. Creates a new color that is a blend of the two input colors.

Parameters

  • color1 - Starting color (t=0)
  • color2 - Ending color (t=1)
  • t - Interpolation factor (0.0 to 1.0)
pub fn load_audio(
  url: String,
) -> promise.Promise(Result(AudioBuffer, Nil))

Loads an audio file from a URL into an AudioBuffer.

The AudioBuffer can be used with both global Audio and PositionalAudio. Supports common formats: MP3, OGG, WAV, AAC (browser-dependent).

See: AudioLoader

Parameters

  • url: Path or URL to the audio file

Example

use buffer <- promise.await(load_audio("/sounds/explosion.mp3"))
case buffer {
  Ok(audio_buffer) -> {
    set_audio_buffer(sound, audio_buffer)
    play_audio(sound)
  }
  Error(Nil) -> io.println("Failed to load audio")
}

Notes

  • Audio must be triggered by user interaction (browser autoplay policy)
  • Decoded audio is stored in memory - large files consume RAM
  • MP3 has best browser support; OGG better for looping
  • WAV files are larger but have no decoding overhead
  • Consider using compressed formats for music, WAV for short effects
pub fn load_cube_texture(
  urls: List(String),
) -> promise.Promise(Result(CubeTexture, Nil))

Loads a cube texture (skybox) from 6 image URLs.

Cube textures use 6 images representing the faces of a cube: positive X, negative X, positive Y, negative Y, positive Z, negative Z.

See: CubeTextureLoader

Parameters

  • urls: List of 6 URLs in order: [+X, -X, +Y, -Y, +Z, -Z]

Example

let skybox_urls = [
  "/skybox/px.jpg",  // positive X (right)
  "/skybox/nx.jpg",  // negative X (left)
  "/skybox/py.jpg",  // positive Y (top)
  "/skybox/ny.jpg",  // negative Y (bottom)
  "/skybox/pz.jpg",  // positive Z (front)
  "/skybox/nz.jpg",  // negative Z (back)
]

use cube_tex <- promise.await(load_cube_texture(skybox_urls))
case cube_tex {
  Ok(tex) -> set_scene_background_cube_texture(scene, tex)
  Error(Nil) -> io.println("Failed to load skybox")
}

Notes

  • All 6 images must be the same size and square
  • Power-of-two dimensions recommended (512, 1024, 2048)
  • More efficient than equirectangular for real-time skyboxes
  • Can also be used for reflection/environment mapping
  • Consider compressed textures for large skyboxes
pub fn load_equirectangular_texture(
  url: String,
) -> promise.Promise(Result(Texture, Nil))

Loads an equirectangular (360°) texture for environment maps or skyboxes.

Equirectangular images are panoramic photos mapped to a sphere. Commonly used for environment backgrounds and reflections.

See: TextureLoader

Parameters

  • url: Path or URL to the equirectangular image (PNG, JPG, WebP)

Example

use env_texture <- promise.await(load_equirectangular_texture("/env/studio.jpg"))
case env_texture {
  Ok(tex) -> {
    // Use as scene background
    set_scene_background_texture(scene, tex)
  }
  Error(Nil) -> io.println("Failed to load environment")
}

Notes

  • Supports standard image formats (PNG, JPG, WebP)
  • Large panoramas impact memory and load time
  • Consider pre-processed cubemaps for better performance
pub fn load_fbx(
  url: String,
) -> promise.Promise(Result(FBXData, Nil))

Loads an FBX model from a URL.

FBX is common in game development and supports complex animations, blend shapes, and skeletal rigs from tools like Maya and Blender.

See: FBXLoader

Parameters

  • url: Path or URL to the .fbx file

Example

use fbx <- promise.await(load_fbx("/models/animated_character.fbx"))
case fbx {
  Ok(data) -> {
    let model = get_fbx_scene(data)
    add_to_scene(scene, model)

    // Setup animation
    let mixer = create_animation_mixer(model)
    let clips = get_fbx_animations(data)
    // Play animations...
  }
  Error(Nil) -> io.println("Failed to load FBX")
}

Notes

  • FBX files can be very large (binary format recommended)
  • Supports skeletal animation, blend shapes, and morph targets
  • Materials may need adjustment for web rendering
  • Embedded textures are automatically extracted
  • Consider converting to GLTF for better web performance
pub fn load_font(
  url: String,
) -> promise.Promise(Result(Font, Nil))

Loads a font file for use with TextGeometry.

Fonts must be in Three.js JSON format (converted from TTF/OTF). Use tools like Facetype.js to convert standard fonts.

See: FontLoader

Parameters

  • url: Path or URL to the JSON font file

Example

use font <- promise.await(load_font("/fonts/helvetiker_regular.typeface.json"))
case font {
  Ok(f) -> {
    let text_geo = create_text_geometry("Hello!", f, 1.0, 0.2, 12, 1)
    let material = create_basic_material(0xffffff, False, 1.0, option.None)
    let text_mesh = create_mesh(text_geo, material)
    add_to_scene(scene, text_mesh)
  }
  Error(Nil) -> io.println("Failed to load font")
}

Notes

  • Three.js includes helvetiker font in examples
  • Convert fonts at: https://gero3.github.io/facetype.js/
  • Only outline fonts work (no bitmap/raster fonts)
  • Text geometry can have many triangles - use sparingly
  • Consider CSS2DRenderer for 2D text overlays instead
pub fn load_gltf(
  url: String,
) -> promise.Promise(Result(GLTFData, Nil))

Loads a GLTF or GLB model from a URL.

GLTF is the recommended format for web 3D - it’s efficient, well-supported, and includes materials, animations, and scene hierarchy.

See: GLTFLoader

Parameters

  • url: Path or URL to the .gltf or .glb file

Example

use gltf <- promise.await(load_gltf("/models/character.glb"))
case gltf {
  Ok(data) -> {
    let model = get_gltf_scene(data)
    add_to_scene(scene, model)

    // If model has animations
    let clips = get_gltf_animations(data)
    let mixer = create_animation_mixer(model)
    // Play animations...
  }
  Error(Nil) -> io.println("Failed to load GLTF")
}

Notes

  • GLB is binary GLTF - single file, faster to load
  • GLTF may reference external textures and .bin files
  • Materials are automatically converted to Three.js materials
  • Animations are stored in animations array of GLTFData
  • Use Draco compression for smaller file sizes (requires DRACOLoader)
  • Consider KTX2 textures for GPU-compressed textures
pub fn load_obj(
  url: String,
) -> promise.Promise(Result(Object3D, Nil))

Loads an OBJ model from a URL.

OBJ is a simple, widely-supported 3D format. It contains only geometry and basic material references (via .mtl files).

See: OBJLoader

Parameters

  • url: Path or URL to the .obj file

Example

use model <- promise.await(load_obj("/models/furniture.obj"))
case model {
  Ok(obj) -> {
    add_to_scene(scene, obj)
  }
  Error(Nil) -> io.println("Failed to load OBJ")
}

Notes

  • OBJ files are text-based and can be large
  • Materials require separate MTLLoader (not included here)
  • No animation support in OBJ format
  • Returns an Object3D that may contain multiple meshes
  • Consider GLTF for better features and smaller files
pub fn load_stl(
  url: String,
) -> promise.Promise(Result(Geometry, Nil))

Loads an STL (stereolithography) model from a URL.

STL is common for 3D printing and CAD applications. Returns only geometry data - you’ll need to create a material and mesh.

See: STLLoader

Parameters

  • url: Path or URL to the .stl file

Example

use geometry <- promise.await(load_stl("/models/part.stl"))
case geometry {
  Ok(geo) -> {
    let material = create_standard_material(0x888888, 0.5, 0.5, option.None)
    let mesh = create_mesh(geo, material)
    add_to_scene(scene, mesh)
  }
  Error(Nil) -> io.println("Failed to load STL")
}

Notes

  • STL contains only geometry, no materials or colors
  • Both ASCII and binary STL formats are supported
  • Binary STL is more compact and loads faster
  • STL has no hierarchy - always returns a single geometry
  • Consider computing vertex normals after loading for smooth shading
pub fn load_texture(
  url: String,
) -> promise.Promise(Result(Texture, Nil))

Loads a texture image from a URL asynchronously.

Supports common image formats: PNG, JPG, GIF, BMP, WebP. The texture is automatically uploaded to GPU memory when used.

See: TextureLoader

Parameters

  • url: Path or URL to the image file

Example

use texture <- promise.await(load_texture("/textures/brick.jpg"))
case texture {
  Ok(tex) -> {
    let material = create_basic_material(0xffffff, False, 1.0, option.Some(tex))
    // Use material...
  }
  Error(Nil) -> io.println("Failed to load texture")
}

Notes

  • Loading is asynchronous - texture may not be immediately available
  • Large textures impact memory; consider power-of-two dimensions
  • Set texture filtering and wrapping after loading if needed
  • CORS restrictions apply for cross-origin URLs
  • Consider using compressed textures (KTX2) for better performance
pub fn mark_attribute_needs_update(
  attribute: BufferAttribute,
) -> Nil

Marks a buffer attribute for GPU upload.

Sets BufferAttribute.needsUpdate to true. Call this after modifying attribute data.

pub fn pause_audio(audio: Audio) -> Nil

Pauses audio playback.

Wraps Audio.pause. Playback resumes from the current position when played again.

pub fn pause_positional_audio(audio: PositionalAudio) -> Nil

Pauses playback of a positional audio source.

The playback position is preserved and can be resumed with play.

See: PositionalAudio.pause

Parameters

  • audio: The positional audio source to pause

Example

// Pause when game is paused
pause_positional_audio(background_music)

// Resume when game continues
play_positional_audio(background_music)

Notes

  • Preserves playback position
  • Has no effect if audio is not playing
  • Better than stop for temporary pauses
pub fn play_action(action: AnimationAction) -> Nil

Plays an animation action.

Wraps AnimationAction.play.

pub fn play_audio(audio: Audio) -> Nil

Starts playing audio.

Wraps Audio.play. Note: Browser autoplay policies may require user interaction first.

pub fn play_positional_audio(audio: PositionalAudio) -> Nil

Starts playback of a positional audio source.

If the audio was paused, resumes from the paused position. If stopped, starts from the beginning.

See: PositionalAudio.play

Parameters

  • audio: The positional audio source to play

Example

// Start playing after buffer is loaded
case has_positional_audio_buffer(sound) {
  True -> play_positional_audio(sound)
  False -> io.println("Buffer not loaded yet")
}

Notes

  • Requires user interaction first (browser autoplay policy)
  • Buffer must be set before calling play
  • Calling play on already playing audio restarts it
  • Use is_positional_audio_playing to check state first
pub fn remove_child(parent: Object3D, child: Object3D) -> Nil

Removes a child from its parent.

Wraps Object3D.remove.

pub fn remove_from_scene(
  scene scene: Scene,
  object object: Object3D,
) -> Nil

Removes an object from the scene.

Wraps Object3D.remove. The object is detached from the scene graph and will no longer be rendered.

Note: This does not dispose of the object’s geometry or materials. Call dispose_object if you want to free GPU memory.

Parameters

  • scene - The scene to remove from
  • object - The object to remove
pub fn remove_lod_level(lod: LOD, distance: Float) -> Nil

Removes a level from a LOD object by distance.

Parameters

  • lod - The LOD object
  • distance - The distance of the level to remove
pub fn render(
  renderer: Renderer,
  scene: Scene,
  camera: Camera,
) -> Nil

Renders the scene using the given camera.

Wraps WebGLRenderer.render. This is the main render call - invoke it each frame in your render loop.

Parameters

  • renderer - The WebGL renderer
  • scene - The scene containing objects to render
  • camera - The camera defining the view

Example

// Basic render loop
fn render_loop() {
  render(renderer, scene, camera)
  request_animation_frame(render_loop)
}
pub fn render_css2d(
  renderer: CSS2DRenderer,
  scene: Scene,
  camera: Camera,
) -> Nil

Renders CSS2D objects in the scene.

Call this after render() in your render loop.

pub fn resume_audio_context() -> Nil

Resumes the Web Audio AudioContext.

Call this after user interaction (e.g., button click) to enable audio playback in browsers with autoplay restrictions.

pub fn set_action_loop(
  action: AnimationAction,
  loop_mode: Int,
) -> Nil

Sets the loop mode of an animation action.

Wraps AnimationAction.loop. Use get_loop_once, get_loop_repeat, or get_loop_ping_pong for mode values.

pub fn set_action_time_scale(
  action: AnimationAction,
  time_scale: Float,
) -> Nil

Sets the time scale (playback speed) of an animation action.

Wraps AnimationAction.timeScale. 1.0 = normal speed, 2.0 = double speed, 0.5 = half speed, negative = reverse.

pub fn set_action_weight(
  action: AnimationAction,
  weight: Float,
) -> Nil

Sets the weight of an animation action.

Wraps AnimationAction.weight. Used for blending multiple animations. 0.0 = no influence, 1.0 = full influence.

pub fn set_attribute_needs_update(
  attribute: BufferAttribute,
  needs_update: Bool,
) -> Nil

Sets the needsUpdate flag on a buffer attribute.

When True, the attribute data will be uploaded to the GPU on the next render.

pub fn set_audio_buffer(audio: Audio, buffer: AudioBuffer) -> Nil

Sets the audio buffer for an Audio object.

Wraps Audio.setBuffer. Load audio with load_audio first.

pub fn set_audio_loop(audio: Audio, loop: Bool) -> Nil

Sets whether audio should loop.

Wraps Audio.setLoop.

pub fn set_audio_playback_rate(audio: Audio, rate: Float) -> Nil

Sets the audio playback rate.

Wraps Audio.setPlaybackRate. 1.0 = normal speed, 2.0 = double speed, 0.5 = half speed.

pub fn set_audio_volume(audio: Audio, volume: Float) -> Nil

Sets the audio volume.

Wraps Audio.setVolume.

Parameters

  • audio - The audio object
  • volume - Volume from 0.0 (silent) to 1.0 (full volume)
pub fn set_buffer_attribute(
  geometry: Geometry,
  name: String,
  array: Float32Array,
  item_size: Int,
) -> Nil

Sets a buffer attribute directly on geometry.

Convenience function that creates a BufferAttribute and sets it in one call.

Parameters

  • geometry - The geometry to modify
  • name - Attribute name (“position”, “color”, etc.)
  • array - The Float32Array containing the data
  • item_size - Values per vertex (3 for xyz, 4 for rgba)
pub fn set_buffer_x(
  attribute: BufferAttribute,
  index: Int,
  value: Float,
) -> Nil

Sets a single value (X component) in a buffer attribute at an index.

Wraps BufferAttribute.setX.

pub fn set_buffer_xyz(
  attribute: BufferAttribute,
  index: Int,
  value: vec3.Vec3(Float),
) -> Nil

Sets XYZ values in a buffer attribute at a specific index.

Wraps BufferAttribute.setXYZ.

Parameters

  • attribute - The buffer attribute to modify
  • index - Vertex index
  • value - XYZ values as Vec3
pub fn set_camera_aspect(camera: Camera, aspect: Float) -> Nil

Sets the aspect ratio of a perspective camera.

Wraps PerspectiveCamera.aspect. Call this when the canvas/window is resized, followed by update_camera_projection_matrix.

Parameters

  • camera - The perspective camera to update
  • aspect - New aspect ratio (width / height)

Example

// On window resize
let aspect = int.to_float(width) /. int.to_float(height)
set_camera_aspect(camera, aspect)
update_camera_projection_matrix(camera)
pub fn set_camera_look_at(
  camera camera: Camera,
  target target: vec3.Vec3(Float),
) -> Nil

Makes a camera look at a target position.

Wraps Object3D.lookAt. Rotates the camera so its -Z axis points at the target.

Parameters

  • camera - The camera to rotate
  • target - The world position to look at
pub fn set_camera_user_data(
  camera: Camera,
  key: String,
  value: a,
) -> Nil

Stores arbitrary user data on a camera.

Wraps Object3D.userData. Useful for attaching game-specific data to cameras.

Parameters

  • camera - The camera to store data on
  • key - String key for the data
  • value - Any value to store
pub fn set_css2d_renderer_size(
  renderer: CSS2DRenderer,
  width: Int,
  height: Int,
) -> Nil

Sets the size of a CSS2DRenderer.

Should match the WebGL renderer size.

pub fn set_draw_range(
  geometry: Geometry,
  start: Int,
  count: Int,
) -> Nil

Sets the draw range for a geometry.

Wraps BufferGeometry.setDrawRange. Limits which vertices are rendered, useful for partial geometry rendering.

Parameters

  • geometry - The geometry to modify
  • start - First vertex index to render
  • count - Number of vertices to render
pub fn set_geometry_attribute(
  geometry: Geometry,
  name: String,
  attribute: BufferAttribute,
) -> Nil

Sets an attribute on a BufferGeometry.

Parameters

  • geometry - The geometry to modify
  • name - Attribute name (“position”, “normal”, “color”, “uv”, etc.)
  • attribute - The BufferAttribute to set
pub fn set_instance_matrix(
  mesh: InstancedMesh,
  index: Int,
  matrix: Matrix4,
) -> Nil

Sets the transform matrix for an instance.

Wraps InstancedMesh.setMatrixAt. After setting matrices, call update_instance_matrix to apply changes.

Parameters

  • mesh - The instanced mesh
  • index - Instance index (0 to count-1)
  • matrix - 4x4 transformation matrix
pub fn set_max_distance(
  audio: PositionalAudio,
  distance: Float,
) -> Nil

Sets the maximum distance for positional audio.

Wraps PositionalAudio.setMaxDistance. Beyond this distance, volume won’t decrease further.

Parameters

  • audio - The positional audio object
  • distance - Maximum distance in world units (default: 10000)
pub fn set_object_geometry(
  object: Object3D,
  geometry: Geometry,
) -> Nil

Sets the geometry of a mesh.

Replaces the mesh’s geometry. The old geometry is not automatically disposed.

pub fn set_object_material(
  object: Object3D,
  material: Material,
) -> Nil

Sets the material of a mesh.

Replaces the mesh’s material. The old material is not automatically disposed.

pub fn set_object_position(
  object: Object3D,
  position: vec3.Vec3(Float),
) -> Nil

Sets the object’s local position.

Wraps Object3D.position.

Parameters

  • object - The object to position
  • position - Position as Vec3 (x, y, z)
pub fn set_object_quaternion(
  object: Object3D,
  quaternion: quaternion.Quaternion,
) -> Nil

Sets the object’s rotation using a quaternion.

Sets Object3D.quaternion. Quaternions avoid gimbal lock and interpolate smoothly.

pub fn set_object_rotation(
  object: Object3D,
  rotation: vec3.Vec3(Float),
) -> Nil

Sets the object’s local rotation using Euler angles.

Wraps Object3D.rotation. Rotation order is XYZ by default.

Parameters

  • object - The object to rotate
  • rotation - Rotation in radians as Vec3 (x, y, z)
pub fn set_object_scale(
  object: Object3D,
  scale: vec3.Vec3(Float),
) -> Nil

Sets the object’s local scale.

Wraps Object3D.scale.

Parameters

  • object - The object to scale
  • scale - Scale factors as Vec3 (1.0 = original size)
pub fn set_orthographic_camera_params(
  camera: Camera,
  left: Float,
  right: Float,
  top: Float,
  bottom: Float,
  near: Float,
  far: Float,
) -> Nil

Sets all parameters of an orthographic camera.

Updates left, right, top, bottom, near, and far planes. Call update_camera_projection_matrix afterward to apply the changes.

pub fn set_perspective_camera_params(
  camera: Camera,
  fov: Float,
  aspect: Float,
  near: Float,
  far: Float,
) -> Nil

Sets all parameters of a perspective camera.

Updates fov, aspect, near, and far planes. Call update_camera_projection_matrix afterward to apply the changes.

pub fn set_positional_audio_buffer(
  audio: PositionalAudio,
  buffer: AudioBuffer,
) -> Nil

Sets the audio buffer for a positional audio source.

The buffer contains the decoded audio data to play. Must be set before playing the audio.

See: PositionalAudio.setBuffer

Parameters

  • audio: The positional audio source
  • buffer: Decoded audio buffer from load_audio

Example

use buffer <- promise.await(load_audio("/sounds/engine.mp3"))
case buffer {
  Ok(audio_buffer) -> {
    set_positional_audio_buffer(car_sound, audio_buffer)
    set_positional_audio_loop(car_sound, True)
    play_positional_audio(car_sound)
  }
  Error(Nil) -> Nil
}

Notes

  • Buffer must be set before calling play
  • Same buffer can be shared between multiple audio sources
  • Buffer stays in memory until explicitly released
pub fn set_positional_audio_loop(
  audio: PositionalAudio,
  loop: Bool,
) -> Nil

Enables or disables looping for a positional audio source.

When enabled, the audio will restart from the beginning when it reaches the end.

See: PositionalAudio.setLoop

Parameters

  • audio: The positional audio source
  • loop: True to enable looping, False for one-shot playback

Example

// Background music should loop
set_positional_audio_loop(music, True)

// Sound effects play once
set_positional_audio_loop(explosion_sound, False)

Notes

  • Can be changed while audio is playing
  • OGG format handles looping better than MP3 (no gaps)
  • For seamless loops, ensure audio file is properly trimmed
pub fn set_positional_audio_playback_rate(
  audio: PositionalAudio,
  rate: Float,
) -> Nil

Sets the playback rate for a positional audio source.

Changes both the speed and pitch of the audio playback.

See: PositionalAudio.setPlaybackRate

Parameters

  • audio: The positional audio source
  • rate: Playback speed (1.0 = normal, 2.0 = double speed, 0.5 = half speed)

Example

// Simulate engine revving up
set_positional_audio_playback_rate(engine_sound, 1.5)

// Slow motion effect
set_positional_audio_playback_rate(sound, 0.5)

Notes

  • Changes pitch proportionally (higher rate = higher pitch)
  • Values below 0.5 or above 4.0 may sound unnatural
  • Good for simulating Doppler effect or engine RPM
  • Can be changed while audio is playing for dynamic effects
pub fn set_positional_audio_volume(
  audio: PositionalAudio,
  volume: Float,
) -> Nil

Sets the volume for a positional audio source.

Volume is a multiplier for the audio output. This is the base volume before distance attenuation is applied.

See: PositionalAudio.setVolume

Parameters

  • audio: The positional audio source
  • volume: Volume level (0.0 = silent, 1.0 = full volume)

Example

// Set ambient sound to half volume
set_positional_audio_volume(forest_ambience, 0.5)

// Mute temporarily
set_positional_audio_volume(music, 0.0)

Notes

  • Values above 1.0 amplify the sound (may cause clipping)
  • Actual heard volume also depends on listener distance
  • Use fade functions for smooth volume transitions
pub fn set_ref_distance(
  audio: PositionalAudio,
  distance: Float,
) -> Nil

Sets the reference distance for positional audio attenuation.

Wraps PositionalAudio.setRefDistance. The distance at which the volume reduction starts. Audio at this distance plays at full volume; beyond this, volume decreases based on rolloff.

Parameters

  • audio - The positional audio object
  • distance - Reference distance in world units (default: 1)
pub fn set_renderer_pixel_ratio(
  renderer: Renderer,
  ratio: Float,
) -> Nil

Sets the device pixel ratio for the renderer.

Wraps WebGLRenderer.setPixelRatio. Higher values render at higher resolution for sharper images on HiDPI displays, but cost more performance.

Parameters

  • renderer - The renderer to configure
  • ratio - Pixel ratio (typically window.devicePixelRatio, usually 1.0-3.0)

Example

// Match device pixel ratio for sharp rendering
set_renderer_pixel_ratio(renderer, 2.0)  // Retina display
pub fn set_renderer_size(
  renderer: Renderer,
  width: Int,
  height: Int,
) -> Nil

Sets the renderer output canvas size.

Wraps WebGLRenderer.setSize. Call this when the window resizes to update the rendering resolution.

Parameters

  • renderer - The renderer to resize
  • width - New width in pixels
  • height - New height in pixels

Example

// Handle window resize
set_renderer_size(renderer, window_width, window_height)
set_camera_aspect(camera, int.to_float(window_width) /. int.to_float(window_height))
pub fn set_rolloff_factor(
  audio: PositionalAudio,
  factor: Float,
) -> Nil

Sets the rolloff factor for positional audio attenuation.

Wraps PositionalAudio.setRolloffFactor. Controls how quickly volume decreases with distance. Higher values = faster falloff.

Parameters

  • audio - The positional audio object
  • factor - Rolloff factor (default: 1, range: 0-∞)
pub fn set_scene_background_color(
  scene: Scene,
  color: Int,
) -> Scene

Sets the scene background to a solid color.

Wraps Scene.background with a Three.js Color.

Parameters

  • scene - The scene to modify
  • color - Hex color value (e.g., 0x1a1a2e for dark blue)

Example

let scene = create_scene()
  |> set_scene_background_color(0x87ceeb)  // Sky blue
pub fn set_scene_background_cube_texture(
  scene: Scene,
  cube_texture: CubeTexture,
) -> Scene

Sets the scene background to a cube texture (skybox).

Wraps Scene.background with a CubeTexture. Creates an immersive 360° environment.

Parameters

  • scene - The scene to modify
  • cube_texture - A cube texture loaded via load_cube_texture

Example

let urls = [
  "/skybox/px.jpg", "/skybox/nx.jpg",  // +X, -X
  "/skybox/py.jpg", "/skybox/ny.jpg",  // +Y, -Y
  "/skybox/pz.jpg", "/skybox/nz.jpg",  // +Z, -Z
]
load_cube_texture(urls)
|> promise.map(fn(result) {
  case result {
    Ok(cube) -> set_scene_background_cube_texture(scene, cube)
    Error(Nil) -> scene
  }
})
pub fn set_scene_background_texture(
  scene: Scene,
  texture: Texture,
) -> Scene

Sets the scene background to a texture (e.g., an image).

Wraps Scene.background. The texture will be stretched to fill the entire background.

Parameters

  • scene - The scene to modify
  • texture - A texture loaded via load_texture or load_equirectangular_texture

Example

load_texture("/images/background.jpg")
|> promise.map(fn(result) {
  case result {
    Ok(texture) -> set_scene_background_texture(scene, texture)
    Error(Nil) -> scene
  }
})
pub fn set_scissor(
  renderer: Renderer,
  x: Int,
  y: Int,
  width: Int,
  height: Int,
) -> Nil

Sets the scissor region for rendering.

Wraps WebGLRenderer.setScissor. When scissor test is enabled, only pixels within this region are affected. Use with set_scissor_test(renderer, True).

Parameters

  • renderer - The renderer
  • x - Left edge in pixels
  • y - Bottom edge in pixels
  • width - Scissor region width
  • height - Scissor region height
pub fn set_scissor_test(renderer: Renderer, enabled: Bool) -> Nil

Enables or disables the scissor test.

Wraps WebGLRenderer.setScissorTest. When enabled, only the scissor region defined by set_scissor is rendered.

Parameters

  • renderer - The renderer
  • enabled - True to enable scissor test, False to disable
pub fn set_shadow_properties(
  object object: Object3D,
  cast_shadow cast_shadow: Bool,
  receive_shadow receive_shadow: Bool,
) -> Nil

Sets shadow casting and receiving properties on an object.

For shadows to work, you need:

  1. A light with cast_shadow: True
  2. Objects with cast_shadow: True
  3. Objects with receive_shadow: True
  4. renderer.shadowMap.enabled = true (automatic in create_renderer)

Parameters

  • object - The object to configure
  • cast_shadow - Whether this object casts shadows
  • receive_shadow - Whether this object receives shadows from other objects
pub fn set_texture_filter_mode(
  texture: Texture,
  min_filter: Int,
  mag_filter: Int,
) -> Nil

Sets the texture filtering mode.

Wraps Texture.minFilter and Texture.magFilter. Use get_nearest_filter for pixel art or get_linear_filter for smooth textures.

pub fn set_texture_offset(
  texture: Texture,
  x: Float,
  y: Float,
) -> Nil

Sets the UV offset of a texture.

Wraps Texture.offset. Useful for sprite sheet animation or tiling effects.

Parameters

  • texture - The texture to modify
  • x - Horizontal offset (0.0-1.0)
  • y - Vertical offset (0.0-1.0)
pub fn set_texture_repeat(
  texture: Texture,
  x: Float,
  y: Float,
) -> Nil

Sets the UV repeat (tiling) of a texture.

Wraps Texture.repeat.

Parameters

  • texture - The texture to modify
  • x - Horizontal repeat count
  • y - Vertical repeat count
pub fn set_texture_wrap_mode(
  texture: Texture,
  wrap_s: Int,
  wrap_t: Int,
) -> Nil

Sets the texture wrapping mode.

Wraps Texture.wrapS and Texture.wrapT. Use get_repeat_wrapping, get_clamp_to_edge_wrapping, or get_mirrored_repeat_wrapping.

pub fn set_viewport(
  renderer: Renderer,
  x: Int,
  y: Int,
  width: Int,
  height: Int,
) -> Nil

Sets the viewport for rendering.

Wraps WebGLRenderer.setViewport. Defines the rectangular region of the canvas to render to. Useful for split-screen, picture-in-picture, or multiple views.

Parameters

  • renderer - The renderer
  • x - Left edge in pixels
  • y - Bottom edge in pixels (WebGL uses bottom-left origin)
  • width - Viewport width in pixels
  • height - Viewport height in pixels

Example

// Render to left half of screen
set_viewport(renderer, 0, 0, width / 2, height)
render(renderer, scene, left_camera)

// Render to right half
set_viewport(renderer, width / 2, 0, width / 2, height)
render(renderer, scene, right_camera)
pub fn stop_action(action: AnimationAction) -> Nil

Stops an animation action.

Wraps AnimationAction.stop. Resets the action to the beginning.

pub fn stop_audio(audio: Audio) -> Nil

Stops audio playback.

Wraps Audio.stop. Resets playback position to the beginning.

pub fn stop_positional_audio(audio: PositionalAudio) -> Nil

Stops playback of a positional audio source.

Resets the playback position to the beginning.

See: PositionalAudio.stop

Parameters

  • audio: The positional audio source to stop

Example

// Stop sound effect
stop_positional_audio(explosion_sound)

// Play again from start
play_positional_audio(explosion_sound)

Notes

  • Resets position to beginning (unlike pause)
  • Has no effect if audio is not playing
  • Use for one-shot sounds or complete restarts
pub fn update_camera_projection_matrix(camera: Camera) -> Nil

Updates the camera’s projection matrix.

See PerspectiveCamera or OrthographicCamera. Call this after changing camera parameters (fov, aspect, near, far, etc.).

pub fn update_css2d_object_html(
  object: CSS2DObject,
  html: String,
) -> Nil

Updates the HTML content of a CSS2DObject.

Parameters

  • object - The CSS2DObject to update
  • html - New HTML content
pub fn update_css3d_object_html(
  object: CSS3DObject,
  html: String,
) -> Nil

Updates the HTML content of a CSS3DObject.

pub fn update_group_instanced_meshes(
  group: Object3D,
  instances: List(
    #(vec3.Vec3(Float), vec3.Vec3(Float), vec3.Vec3(Float)),
  ),
) -> Nil

Updates all instanced meshes within a group with the same transforms.

Applies the same instance transforms to all InstancedMesh children in a group.

Parameters

  • group - The group containing instanced meshes
  • instances - List of transform tuples (position, rotation, scale)
pub fn update_instance_matrix(mesh: InstancedMesh) -> Nil

Marks the instance matrix buffer for GPU upload.

Wraps InstancedMesh.instanceMatrix.needsUpdate. Call this after modifying instance matrices with set_instance_matrix.

pub fn update_instanced_mesh_transforms(
  mesh: InstancedMesh,
  instances: List(
    #(vec3.Vec3(Float), vec3.Vec3(Float), vec3.Vec3(Float)),
  ),
) -> Nil

Updates all instance transforms in an instanced mesh.

Efficient batch update for all instances. Each instance is specified as a tuple of #(position, rotation, scale) where rotation is Euler angles.

Parameters

  • mesh - The instanced mesh
  • instances - List of transform tuples
pub fn update_matrix_world(object: Object3D) -> Nil

Updates the object’s world matrix.

Wraps Object3D.updateMatrixWorld. Computes the world matrix from local position/rotation/scale and parent transforms. Usually called automatically during rendering, but needed for manual matrix access.

pub fn update_matrix_world_force(
  object: Object3D,
  force: Bool,
) -> Nil

Updates the object’s world matrix with optional force flag.

Wraps Object3D.updateMatrixWorld.

Parameters

  • object - The object to update
  • force - If True, forces update of all descendants even if not dirty
pub fn update_mixer(
  mixer: AnimationMixer,
  delta_time: Float,
) -> Nil

Updates the animation mixer.

Wraps AnimationMixer.update. Call this every frame with the time delta in seconds.

Parameters

  • mixer - The animation mixer
  • delta_time - Time since last update in seconds
Search Document