#! Contains `asset`, a model read from glTF 2.0, why one couldn't be, and #! reading one, from a file or from bytes. #! #! A `.gltf` file is JSON, naming its buffers and images as files beside it or #! as data in `data:` URIs; a `.glb` file is the same JSON and its first buffer #! in one file, as chunks. Either is read whole: its JSON into the parts of an #! `asset`, each index between them checked, its buffers' and images' bytes read, #! and every accessor checked to lie within its buffer, so what an asset holds #! is read without failing after. What an accessor holds is read as numbers by #! `asset::floats` and `asset::integers`. #! #! Indices into an asset's lists are `u64`s, as glTF's are, and one that's #! optional a `maybe`. Each part keeps glTF's defaults where its JSON leaves #! them out, and a node its translation, rotation, and scale and its matrix both, #! whichever the JSON gave. import std::io; import linear; module gltf; #! What kind of thing kept an asset from being read. pub enum failure: u8 { unreadable, #! A file couldn't be read: the asset's, or a buffer's or an image's it names. malformed, #! The data breaks glTF: JSON that isn't, a member of the wrong kind or missing, an index past what it names, an accessor past its buffer. unsupported, #! It's well formed, but asks for what isn't read: a version but 2, or an extension it requires that isn't read. } #! Why an asset couldn't be read: the kind of thing, and what, as #! `meshes[2].primitives[0]: its material is 9, of the 4 there are`. It holds #! its text itself, its first 240 bytes, so it's plain data, as std's errors #! are. pub struct gltf_error { pub var kind: failure; var text: u8[240]; var length: u64; } #! What was wrong, and where. pub def gltf_error::describe(&this): string { return string::from(strview::over(&this.text[0] as *u8, this.length), nothing{}); } # An error of a kind, saying why. def failed(kind: failure, why: strview): gltf_error { var made := gltf_error{ kind = kind }; made.length = why.length < 240 ? why.length : 240; for i in 0..made.length { made.text[i] = why[i]; } return made; } #! The extensions read. An asset requiring another is `unsupported`; one using #! another, without requiring it, is read without what it adds. pub const extensions_read: strview[4] = [ "KHR_lights_punctual", "KHR_materials_unlit", "KHR_materials_emissive_strength", "KHR_mesh_quantization", ]; #! A model read from glTF 2.0: its scenes, the tree of nodes they hold, and #! what those draw with, each part in the list glTF keeps it in, by index. pub struct asset { #! The glTF version it says it's of, as `2.0`. pub var version: string; #! What wrote it, where it says. pub var generator: string; #! The scene shown at first, where it says. pub var scene: maybe; pub var scenes: vector; pub var nodes: vector; pub var meshes: vector; pub var materials: vector; pub var textures: vector; pub var samplers: vector; pub var images: vector; pub var accessors: vector; pub var views: vector; pub var buffers: vector; pub var cameras: vector; pub var skins: vector; pub var animations: vector; #! Its lights, of `KHR_lights_punctual`. pub var lights: vector; #! The extensions it uses, those not read among them. pub var extensions: vector; } #! Reads an asset from a file: `.gltf` or `.glb`, whichever its first bytes #! say, the files it names read from beside it. #! @param at The file's path. #! @returns The asset, or why it couldn't be read: `unreadable` where the file, #! or one it names, couldn't be. pub def load(at: &path): result { val bytes := io::read_file(at) else (_) { return failed(failure::unreadable, $"{at.view()} couldn't be read.".view()); }; var folder := path::new(); if at.parent() is some(&above) { folder = path::copy_of(&above); } return decode(bytes.view(), folder.view()); } #! Reads an asset from a file named by text, as `load` does. pub def load(at: strview): result { val named := path::parse(at) else (_) { return failed(failure::unreadable, $"{at} isn't a path.".view()); }; return load(&named); } #! Reads an asset from bytes: a `.glb` file's, whose first four are `glTF`, or #! else a `.gltf` file's JSON. #! @param bytes The bytes. #! @param folder The folder the files it names are found in, or empty where #! it names none but data, as a `.glb` file of one buffer. #! @returns The asset, or why it couldn't be read. pub def decode(bytes: span, folder: strview = ""): result { if is_binary(bytes) { val chunks := binary_chunks(bytes) else (e) { return e; }; return read_json(chunks.json, chunks.bin, chunks.has_bin, folder); } val text := strview::over(bytes.data, bytes.length); return read_json(text, span::::over(bytes.data, 0), false, folder); }