Files
2026-11-14 15:40:07 -06:00

244 lines
8.2 KiB
Plaintext

#! Contains the parts of an asset, as glTF lays them out: scenes and their nodes,
#! meshes and their primitives, materials, textures, samplers, images, cameras,
#! skins, animations, and lights, each with glTF's defaults where its JSON
#! leaves a member out.
import malunal::linear;
module malunal::gltf;
#! A scene: the nodes at its roots.
pub struct scene {
pub var name: string;
pub var nodes: vector<u64>;
}
#! A node of the tree a scene holds: where it is within its parent, and what's
#! there, a mesh, a camera, a light, or nothing but its children.
pub struct node {
pub var name: string;
pub var children: vector<u64>;
#! The node it's a child of, or none at a root.
pub var parent: maybe<u64>;
pub var mesh: maybe<u64>;
pub var camera: maybe<u64>;
#! The skin its mesh is bound to.
pub var skin: maybe<u64>;
#! Its light, of `KHR_lights_punctual`.
pub var light: maybe<u64>;
#! Where it is within its parent: its translation, rotation, and scale, and
#! the matrix of them, whichever the JSON gave, the other worked out of it.
pub var translation := vec3::zero;
pub var rotation := quat::identity;
pub var scale := vec3::one;
pub var matrix := mat4::identity;
#! The weights of its mesh's morph targets, where it gives its own.
pub var weights: vector<f32>;
}
#! What a primitive's vertices make.
pub enum topology: u8 { points, lines, line_loop, line_strip, triangles, triangle_strip, triangle_fan }
#! An attribute of a primitive's vertices: its name, as `POSITION`, `NORMAL`,
#! `TEXCOORD_0`, or `COLOR_0`, and the accessor holding it.
pub struct attribute {
pub var name: string;
pub var accessor: u64;
}
#! A part of a mesh drawn at once: its vertices' attributes, the indices of
#! them it draws, or none to draw them in order, and its material.
pub struct primitive {
pub var attributes: vector<attribute>;
pub var indices: maybe<u64>;
#! Its material, or none for glTF's default: white, not metallic, rough.
pub var material: maybe<u64>;
pub var mode := topology::triangles;
#! Its morph targets, each the attributes it moves.
pub var targets: vector<vector<attribute>>;
}
#! The accessor of an attribute of a primitive, by its name, where it has it.
pub def primitive::find(&this, name: strview): maybe<u64> {
for i in 0..this.attributes.length {
return this.attributes[i].accessor if this.attributes[i].name.span == name;
}
return nothing{};
}
#! A mesh: its primitives, each drawn with its own material.
pub struct mesh {
pub var name: string;
pub var primitives: vector<primitive>;
#! The weights of its morph targets, where it gives them.
pub var weights: vector<f32>;
}
#! A texture as a material samples it: which, by which of its primitives'
#! `TEXCOORD_n`, and, of a normal map, its scale, or of occlusion, its strength.
pub struct texture_ref {
pub var texture: u64;
pub var set: u64;
pub var scale: f32 = 1;
}
#! How a material's alpha is taken.
pub enum alpha_mode: u8 {
#! Not at all: it's opaque.
opaque,
#! As a cutout: opaque where alpha is at least its cutoff, and not drawn where
#! it's below.
mask,
#! Blended over what's behind it.
blend,
}
#! A material, of glTF's metallic and rough model. Its colors are linear, as
#! glTF's factors are, and its textures of color are sRGB, as glTF's images are.
pub struct material {
pub var name: string;
#! The color its base color texture is multiplied by, and alpha.
pub var base_color: f32[4] = [1, 1, 1, 1];
pub var base_color_texture: maybe<texture_ref>;
pub var metallic: f32 = 1;
pub var roughness: f32 = 1;
#! Its roughness in green and metalness in blue.
pub var metallic_roughness_texture: maybe<texture_ref>;
#! Its normal map: which way each texel faces, across the surface's tangent
#! and the way up its texture, as OpenGL's normal maps hold it.
pub var normal_texture: maybe<texture_ref>;
#! How much light reaches it, in red.
pub var occlusion_texture: maybe<texture_ref>;
#! The light it gives, by `KHR_materials_emissive_strength` where it says.
pub var emissive: f32[3];
pub var emissive_texture: maybe<texture_ref>;
pub var alpha_mode := alpha_mode::opaque;
pub var alpha_cutoff: f32 = 0.5;
#! Whether its faces are seen from behind too.
pub var double_sided: bool;
#! Whether it's drawn as its colors are, unlit, by `KHR_materials_unlit`.
pub var unlit: bool;
}
#! A texture: an image, sampled by a sampler, or by repeating and blending
#! where it names none.
pub struct texture {
pub var name: string;
pub var sampler: maybe<u64>;
pub var source: maybe<u64>;
}
#! How a sampler picks between texels, where it says.
pub enum filter: u8 { unset, nearest, linear }
#! What a sampler reads past a texture's edge.
pub enum wrap: u8 { repeat, clamp, mirror }
#! How a texture is sampled.
pub struct sampler {
pub var name: string;
pub var mag_filter: filter;
pub var min_filter: filter;
#! Between mip levels, where the minifying filter uses them; unset where it
#! doesn't.
pub var mip_filter: filter;
pub var wrap_s := wrap::repeat;
pub var wrap_t := wrap::repeat;
}
#! An image, encoded as its file is, PNG or JPEG: its bytes, read from its file,
#! a `data:` URI, or a buffer view.
pub struct image {
pub var name: string;
#! Its URI, as the JSON writes it, or empty where it's in a buffer view.
pub var uri: string;
#! Its buffer view, where it's in one.
pub var view: maybe<u64>;
#! Its type, as `image/png`, where the JSON or its `data:` URI says.
pub var mime_type: string;
pub var bytes: vector<u8>;
}
#! How a camera projects.
pub enum camera_kind: u8 { perspective, orthographic }
#! A camera, looking down its node's -z.
pub struct camera {
pub var name: string;
pub var kind := camera_kind::perspective;
#! Of a perspective: how far it sees up and down, in radians, and its width
#! by its height, where it says.
pub var fov_y: f32;
pub var aspect: maybe<f32>;
#! Of an orthographic one: half its width and half its height.
pub var x_mag: f32;
pub var y_mag: f32;
pub var near: f32;
#! The furthest it draws, or none, of a perspective, for no end.
pub var far: maybe<f32>;
}
#! A skin: the nodes of its skeleton, its joints, and the inverses of where
#! each was as the mesh was bound to them.
pub struct skin {
pub var name: string;
pub var joints: vector<u64>;
#! An accessor of a matrix for each joint, or none for none but the identity.
pub var inverse_binds: maybe<u64>;
#! The skeleton's root, where it says.
pub var skeleton: maybe<u64>;
}
#! What an animation's channel moves.
pub enum target_path: u8 { translation, rotation, scale, weights }
#! How an animation's values change between its keys.
pub enum interpolation: u8 { linear, step, cubic_spline }
#! A channel of an animation: what it moves of a node, by which of its samplers.
pub struct channel {
pub var sampler: u64;
#! The node, or none where an extension names what it moves.
pub var node: maybe<u64>;
pub var path: target_path;
}
#! The keys of an animation: the accessor of their times, in seconds, and of
#! their values.
pub struct animation_sampler {
pub var input: u64;
pub var output: u64;
pub var interpolation := interpolation::linear;
}
#! An animation: channels, each moving a node by a sampler's keys.
pub struct animation {
pub var name: string;
pub var channels: vector<channel>;
pub var samplers: vector<animation_sampler>;
}
#! What a light is, of `KHR_lights_punctual`.
pub enum light_kind: u8 {
#! Shining down its node's -z, everywhere.
directional,
#! Shining every way from its node.
point,
#! Shining down its node's -z in a cone.
spot,
}
#! A light, of `KHR_lights_punctual`: its color, linear, and its intensity, in
#! lux for a directional light and in candela for the others.
pub struct light {
pub var name: string;
pub var kind := light_kind::point;
pub var color: f32[3] = [1, 1, 1];
pub var intensity: f32 = 1;
#! How far it reaches, or none where it reaches as far as its intensity does.
pub var range: maybe<f32>;
#! Of a spot light: the angles from its middle where it begins to dim, and
#! where it's dark, in radians.
pub var inner_cone: f32;
pub var outer_cone: f32 = 0.7853982;
}