124 lines
5.0 KiB
Plaintext
124 lines
5.0 KiB
Plaintext
#! 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<u64>`. 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<u64>;
|
|
pub var scenes: vector<scene>;
|
|
pub var nodes: vector<node>;
|
|
pub var meshes: vector<mesh>;
|
|
pub var materials: vector<material>;
|
|
pub var textures: vector<texture>;
|
|
pub var samplers: vector<sampler>;
|
|
pub var images: vector<image>;
|
|
pub var accessors: vector<accessor>;
|
|
pub var views: vector<buffer_view>;
|
|
pub var buffers: vector<buffer>;
|
|
pub var cameras: vector<camera>;
|
|
pub var skins: vector<skin>;
|
|
pub var animations: vector<animation>;
|
|
#! Its lights, of `KHR_lights_punctual`.
|
|
pub var lights: vector<light>;
|
|
#! The extensions it uses, those not read among them.
|
|
pub var extensions: vector<string>;
|
|
}
|
|
|
|
#! 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<asset, gltf_error> {
|
|
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<asset, gltf_error> {
|
|
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<u8>, folder: strview = ""): result<asset, gltf_error> {
|
|
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::<u8>::over(bytes.data, 0), false, folder);
|
|
}
|