Initial Commit
This commit is contained in:
36 files changed
+8436
No files matched your search
@@ -0,0 +1,964 @@
|
||||
#! Contains writing an asset as glTF 2.0: its parts as JSON, each member glTF
|
||||
#! gives a default left out where it's that default, and its buffers' bytes, as
|
||||
#! a `.glb` file's binary chunk, `data:` URIs, or files beside it; and what an
|
||||
#! asset is built of by hand: bytes of its first buffer, views of them, and
|
||||
#! accessors of numbers, with their least and most, and images.
|
||||
#!
|
||||
#! What's read is written as it was read, so an asset read and written reads
|
||||
#! back the same, but for the extensions it used that aren't read, which it
|
||||
#! no longer holds, and a node's matrix, which glTF holds is no more than its
|
||||
#! translation, rotation, and scale, which are written in its place.
|
||||
|
||||
import std::io;
|
||||
import std::json;
|
||||
import std::math;
|
||||
import malunal::linear;
|
||||
module malunal::gltf;
|
||||
|
||||
#! An asset of nothing yet, of glTF 2.0, saying what wrote it.
|
||||
#! @param generator What wrote it, as its JSON says.
|
||||
pub def asset::new(generator: strview = "malunal gltf"): asset {
|
||||
var made := asset{};
|
||||
made.version = string::from("2.0", nothing{});
|
||||
made.generator = string::from(generator, nothing{});
|
||||
return made;
|
||||
}
|
||||
|
||||
#! Adds bytes to the asset's first buffer, which it's given where it has none,
|
||||
#! and a view of them, begun at a multiple of 4 bytes, as accessors need. A
|
||||
#! `.glb` file writes that buffer as its binary chunk, and a `.gltf` file as a
|
||||
#! `data:` URI.
|
||||
#! @param this The asset.
|
||||
#! @param bytes The bytes.
|
||||
#! @param name The view's name, or none.
|
||||
#! @returns The view, by its place among the asset's.
|
||||
pub def asset::add_view(&mut this, bytes: span<u8>, name: strview = ""): u64 {
|
||||
if this.buffers.length == 0 {
|
||||
this.buffers.append(buffer{});
|
||||
}
|
||||
val held := &mut this.buffers.at(0);
|
||||
while held.bytes.length % 4 != 0 {
|
||||
held.bytes.append(0);
|
||||
}
|
||||
val offset := held.bytes.length;
|
||||
held.bytes.extend(bytes);
|
||||
held.length = held.bytes.length;
|
||||
this.views.append(buffer_view{ name = string::from(name, nothing{}), buffer = 0, offset = offset, length = bytes.length });
|
||||
return this.views.length - 1;
|
||||
}
|
||||
|
||||
#! Adds an accessor of floats, of their own view, with the least and the most
|
||||
#! of each of their numbers, as glTF asks of a position's.
|
||||
#! @param this The asset.
|
||||
#! @param values The numbers, each element's in turn.
|
||||
#! @param kind What each element is, of how many of them.
|
||||
#! @param name The accessor's name, or none.
|
||||
#! @returns The accessor, by its place among the asset's.
|
||||
pub def asset::add_floats(&mut this, values: span<f32>, kind: element, name: strview = ""): u64 {
|
||||
val width := kind.components;
|
||||
var bytes := vector::<u8>::new();
|
||||
for i in 0..values.length {
|
||||
put_word(&mut bytes, math::bits_of(values[i]));
|
||||
}
|
||||
val view := this.add_view(bytes.span);
|
||||
var made := accessor{ name = string::from(name, nothing{}), view = view, kind = kind, count = values.length / width };
|
||||
for k in 0..width {
|
||||
var least := math::infinity::<f64>();
|
||||
var most := -math::infinity::<f64>();
|
||||
var i := k;
|
||||
while i < values.length {
|
||||
least = math::min(least, values[i] as f64);
|
||||
most = math::max(most, values[i] as f64);
|
||||
i += width;
|
||||
}
|
||||
made.min.append(least);
|
||||
made.max.append(most);
|
||||
}
|
||||
this.accessors.append(move(made));
|
||||
return this.accessors.length - 1;
|
||||
}
|
||||
|
||||
#! Adds an accessor of indices, as a primitive draws its vertices by: unsigned
|
||||
#! shorts where each is less than 65535, and unsigned ints otherwise.
|
||||
#! @param this The asset.
|
||||
#! @param values The indices.
|
||||
#! @param name The accessor's name, or none.
|
||||
#! @returns The accessor, by its place among the asset's.
|
||||
pub def asset::add_indices(&mut this, values: span<u32>, name: strview = ""): u64 {
|
||||
var most: u32 = 0;
|
||||
for i in 0..values.length {
|
||||
most = math::max(most, values[i]);
|
||||
}
|
||||
val short := most < 65535;
|
||||
var bytes := vector::<u8>::new();
|
||||
for i in 0..values.length {
|
||||
if short {
|
||||
bytes.append((values[i] & 0xFF) as u8);
|
||||
bytes.append((values[i] >> 8) as u8);
|
||||
} else {
|
||||
put_word(&mut bytes, values[i]);
|
||||
}
|
||||
}
|
||||
val view := this.add_view(bytes.span);
|
||||
this.accessors.append(accessor{
|
||||
name = string::from(name, nothing{}), view = view, count = values.length,
|
||||
component_type = short ? component::unsigned_short : component::unsigned_int,
|
||||
});
|
||||
return this.accessors.length - 1;
|
||||
}
|
||||
|
||||
#! Adds an image, of its file's bytes, as encoded, in a view of the first
|
||||
#! buffer.
|
||||
#! @param this The asset.
|
||||
#! @param bytes The file's bytes: PNG or JPEG.
|
||||
#! @param mime Its type, as `image/png`, or none, for what its first bytes say.
|
||||
#! @param name Its name, or none.
|
||||
#! @returns The image, by its place among the asset's.
|
||||
pub def asset::add_image(&mut this, bytes: span<u8>, mime: strview = "", name: strview = ""): u64 {
|
||||
val view := this.add_view(bytes);
|
||||
var made := image{ name = string::from(name, nothing{}), view = view };
|
||||
made.mime_type = string::from(mime.length != 0 ? mime : mime_of(bytes), nothing{});
|
||||
made.bytes.extend(bytes);
|
||||
this.images.append(move(made));
|
||||
return this.images.length - 1;
|
||||
}
|
||||
|
||||
# The type of an image's bytes, by its first: PNG's or JPEG's, or PNG's where
|
||||
# it's neither.
|
||||
def mime_of(bytes: span<u8>): strview {
|
||||
return "image/jpeg" if bytes.length >= 2 && bytes[0] == 0xFF && bytes[1] == 0xD8;
|
||||
return "image/png";
|
||||
}
|
||||
|
||||
#! Writes an asset as glTF: a `.glb` file, its JSON and its first buffer as its
|
||||
#! binary chunk where that names no file, or a `.gltf` file's JSON. Each other
|
||||
#! buffer, and an image of neither a view nor a file, is a `data:` URI; one
|
||||
#! naming a file names it still, which `save` writes, and this leaves to whoever
|
||||
#! writes it.
|
||||
#! @param made The asset.
|
||||
#! @param binary Whether it's a `.glb` file.
|
||||
#! @returns The file's bytes, or why it couldn't be written: `malformed` where a
|
||||
#! number is infinite or NaN, or a name isn't UTF-8.
|
||||
pub def encode(made: &asset, binary: bool = true): result<vector<u8>, gltf_error> {
|
||||
val chunked := binary && made.buffers.length != 0 && !names_file(made.buffers[0].uri.span);
|
||||
var w := json::writer::new();
|
||||
write_asset(&mut w, made, chunked);
|
||||
val text := w.finish() else (_) {
|
||||
return failed(failure::malformed, "A number of it is infinite or NaN, or a name of it isn't UTF-8.");
|
||||
};
|
||||
var out := vector::<u8>::new();
|
||||
if !binary {
|
||||
out.extend(text);
|
||||
return out;
|
||||
}
|
||||
# The header, then the JSON padded with spaces, then the binary chunk
|
||||
# padded with zeros, each chunk a multiple of 4 bytes.
|
||||
var json_bytes := vector::<u8>::new();
|
||||
json_bytes.extend(text);
|
||||
while json_bytes.length % 4 != 0 {
|
||||
json_bytes.append(' ');
|
||||
}
|
||||
var bin := vector::<u8>::new();
|
||||
if chunked {
|
||||
bin.extend(made.buffers[0].bytes.span);
|
||||
while bin.length % 4 != 0 {
|
||||
bin.append(0);
|
||||
}
|
||||
}
|
||||
var total := 12 + 8 + json_bytes.length;
|
||||
if chunked {
|
||||
total += 8 + bin.length;
|
||||
}
|
||||
put_word(&mut out, glb_magic);
|
||||
put_word(&mut out, 2);
|
||||
put_word(&mut out, total as u32);
|
||||
put_word(&mut out, json_bytes.length as u32);
|
||||
put_word(&mut out, json_chunk);
|
||||
out.extend(json_bytes.span);
|
||||
if chunked {
|
||||
put_word(&mut out, bin.length as u32);
|
||||
put_word(&mut out, bin_chunk);
|
||||
out.extend(bin.span);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
#! Writes an asset to a file: `.glb` where its name ends so, and `.gltf`
|
||||
#! otherwise, as `encode` writes it; and the files its buffers and images name,
|
||||
#! beside it, of their bytes, its folder made where it isn't.
|
||||
#! @param made The asset.
|
||||
#! @param at The file's path.
|
||||
#! @returns Nothing, or why it couldn't be written: `unreadable` where a file
|
||||
#! couldn't be written, as a file is read.
|
||||
pub def save(made: &asset, at: &path): result<void, gltf_error> {
|
||||
val binary := at.span.ends_with(".glb") || at.span.ends_with(".GLB");
|
||||
var folder := string::new(nothing{});
|
||||
if at.parent() is some(&above) {
|
||||
folder.append(above.span);
|
||||
_ = io::create_directories(above);
|
||||
}
|
||||
for i in 0..made.buffers.length {
|
||||
val uri := made.buffers[i].uri.span;
|
||||
continue if !names_file(uri);
|
||||
write_beside(folder.span, uri, made.buffers[i].bytes.span) else (e) { return e; };
|
||||
}
|
||||
for i in 0..made.images.length {
|
||||
val uri := made.images[i].uri.span;
|
||||
continue if made.images[i].view is some(_) || !names_file(uri);
|
||||
write_beside(folder.span, uri, made.images[i].bytes.span) else (e) { return e; };
|
||||
}
|
||||
val bytes := encode(made, binary) else (e) { return e; };
|
||||
io::write_file(at, bytes.span) else (_) { return failed(failure::unreadable, $"{at.span} couldn't be written.".span); };
|
||||
return;
|
||||
}
|
||||
|
||||
#! Writes an asset to a file named by text, as `save` does.
|
||||
pub def save(made: &asset, at: strview): result<void, gltf_error> {
|
||||
val named := path::parse(at) else (_) { return failed(failure::unreadable, $"{at} isn't a path.".span); };
|
||||
return save(made, &named);
|
||||
}
|
||||
|
||||
# Whether a URI names a file, rather than nothing or data.
|
||||
def names_file(uri: strview): bool {
|
||||
return uri.length != 0 && !uri.starts_with("data:");
|
||||
}
|
||||
|
||||
# Writes the bytes of a file a URI names, beside the asset's.
|
||||
def write_beside(folder: strview, uri: strview, bytes: span<u8>): result<void, gltf_error> {
|
||||
val named := percent_decoded(uri) else { return failed(failure::malformed, $"{uri} isn't percent-encoded right.".span); };
|
||||
val at := folder.length == 0 ? string::from(named.span, nothing{}) : $"{folder}/{named.span}";
|
||||
val file := path::parse(at.span) else (_) { return failed(failure::unreadable, $"{at.span} isn't a path.".span); };
|
||||
if file.parent() is some(&above) {
|
||||
_ = io::create_directories(above);
|
||||
}
|
||||
io::write_file(&file, bytes) else (_) { return failed(failure::unreadable, $"{at.span} couldn't be written.".span); };
|
||||
return;
|
||||
}
|
||||
|
||||
# A little-endian `u32`, added to bytes.
|
||||
def put_word(bytes: &mut vector<u8>, value: u32): void {
|
||||
for i in 0..4u {
|
||||
bytes.append(((value >> (8 * i)) & 0xFF) as u8);
|
||||
}
|
||||
}
|
||||
|
||||
# Bytes as base64, padded.
|
||||
def base64_text(bytes: span<u8>): string {
|
||||
val digits: strview = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
var made := string::new(nothing{});
|
||||
var i: u64 = 0;
|
||||
while i < bytes.length {
|
||||
val left := bytes.length - i;
|
||||
val a := bytes[i] as u32;
|
||||
val b: u32 = left > 1 ? bytes[i + 1] as u32 : 0;
|
||||
val c: u32 = left > 2 ? bytes[i + 2] as u32 : 0;
|
||||
val all := (a << 16) | (b << 8) | c;
|
||||
made.push(digits[((all >> 18) & 63) as u64]);
|
||||
made.push(digits[((all >> 12) & 63) as u64]);
|
||||
made.push(left > 1 ? digits[((all >> 6) & 63) as u64] : '=');
|
||||
made.push(left > 2 ? digits[(all & 63) as u64] : '=');
|
||||
i += 3;
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
# A `data:` URI of bytes of a type.
|
||||
def data_uri_of(mime: strview, bytes: span<u8>): string {
|
||||
val digits := base64_text(bytes);
|
||||
return $"data:{mime};base64,{digits.span}";
|
||||
}
|
||||
|
||||
# A float, as the shortest text that reads back as it, or `null` where it's
|
||||
# infinite or NaN, which the writer keeps as its failure.
|
||||
def put_float(w: &mut json::writer, value: f32): void {
|
||||
if !(value - value == 0) {
|
||||
w.real(value as f64);
|
||||
return;
|
||||
}
|
||||
val text := $"{value}";
|
||||
w.raw(text.span);
|
||||
}
|
||||
|
||||
# A least or most of an accessor, as a float's text where it's a float's.
|
||||
def put_real(w: &mut json::writer, value: f64): void {
|
||||
if (value as f32) as f64 == value {
|
||||
put_float(w, value as f32);
|
||||
} else {
|
||||
w.real(value);
|
||||
}
|
||||
}
|
||||
|
||||
# A member of text, where it's given.
|
||||
def put_text(w: &mut json::writer, key: strview, value: &string): void {
|
||||
return if value.length == 0;
|
||||
w.member(key);
|
||||
w.text(value.span);
|
||||
}
|
||||
|
||||
# A member of a whole number.
|
||||
def put_count(w: &mut json::writer, key: strview, value: u64): void {
|
||||
w.member(key);
|
||||
w.unsigned(value);
|
||||
}
|
||||
|
||||
# A member of an index, where it's given.
|
||||
def put_index(w: &mut json::writer, key: strview, value: maybe<u64>): void {
|
||||
if value is some(at) {
|
||||
put_count(w, key, at);
|
||||
}
|
||||
}
|
||||
|
||||
# A member of indices, where there are any.
|
||||
def put_indices(w: &mut json::writer, key: strview, values: &vector<u64>): void {
|
||||
return if values.length == 0;
|
||||
w.member(key);
|
||||
w.begin_array();
|
||||
for i in 0..values.length {
|
||||
w.unsigned(values[i]);
|
||||
}
|
||||
w.end_array();
|
||||
}
|
||||
|
||||
# A member of floats.
|
||||
def put_floats(w: &mut json::writer, key: strview, values: span<f32>): void {
|
||||
w.member(key);
|
||||
w.begin_array();
|
||||
for i in 0..values.length {
|
||||
put_float(w, values[i]);
|
||||
}
|
||||
w.end_array();
|
||||
}
|
||||
|
||||
# A member of a float, where it isn't its default.
|
||||
def put_number(w: &mut json::writer, key: strview, value: f32, given: f32): void {
|
||||
return if value == given;
|
||||
w.member(key);
|
||||
put_float(w, value);
|
||||
}
|
||||
|
||||
# Writes an asset's JSON, its first buffer the binary chunk where `chunked`.
|
||||
def write_asset(w: &mut json::writer, made: &asset, chunked: bool): void {
|
||||
w.begin_object();
|
||||
w.member("asset");
|
||||
w.begin_object();
|
||||
w.member("version");
|
||||
w.text(made.version.length != 0 ? made.version.span : "2.0");
|
||||
put_text(w, "generator", &made.generator);
|
||||
w.end_object();
|
||||
|
||||
# The extensions it uses, of those written.
|
||||
var used := vector::<strview>::new();
|
||||
var required := vector::<strview>::new();
|
||||
if made.lights.length != 0 {
|
||||
used.append("KHR_lights_punctual");
|
||||
}
|
||||
var unlit := false;
|
||||
var strong := false;
|
||||
for i in 0..made.materials.length {
|
||||
unlit = unlit || made.materials[i].unlit;
|
||||
strong = strong || brightest(&made.materials[i]) > 1;
|
||||
}
|
||||
if unlit {
|
||||
used.append("KHR_materials_unlit");
|
||||
}
|
||||
if strong {
|
||||
used.append("KHR_materials_emissive_strength");
|
||||
}
|
||||
for i in 0..made.extensions.length {
|
||||
if made.extensions[i].span == "KHR_mesh_quantization" {
|
||||
used.append("KHR_mesh_quantization");
|
||||
required.append("KHR_mesh_quantization");
|
||||
}
|
||||
}
|
||||
if used.length != 0 {
|
||||
w.member("extensionsUsed");
|
||||
w.begin_array();
|
||||
for i in 0..used.length {
|
||||
w.text(used[i]);
|
||||
}
|
||||
w.end_array();
|
||||
}
|
||||
if required.length != 0 {
|
||||
w.member("extensionsRequired");
|
||||
w.begin_array();
|
||||
for i in 0..required.length {
|
||||
w.text(required[i]);
|
||||
}
|
||||
w.end_array();
|
||||
}
|
||||
|
||||
put_index(w, "scene", made.scene);
|
||||
write_scenes(w, made);
|
||||
write_nodes(w, made);
|
||||
write_meshes(w, made);
|
||||
write_materials(w, made);
|
||||
write_textures(w, made);
|
||||
write_images(w, made);
|
||||
write_accessors(w, made);
|
||||
write_views(w, made);
|
||||
write_buffers(w, made, chunked);
|
||||
write_cameras(w, made);
|
||||
write_skins(w, made);
|
||||
write_animations(w, made);
|
||||
if made.lights.length != 0 {
|
||||
w.member("extensions");
|
||||
w.begin_object();
|
||||
w.member("KHR_lights_punctual");
|
||||
w.begin_object();
|
||||
write_lights(w, made);
|
||||
w.end_object();
|
||||
w.end_object();
|
||||
}
|
||||
w.end_object();
|
||||
}
|
||||
|
||||
# The most of a material's emission, which more than 1 of is its strength.
|
||||
def brightest(m: &material): f32 {
|
||||
return math::max(m.emissive[0], math::max(m.emissive[1], m.emissive[2]));
|
||||
}
|
||||
|
||||
def write_scenes(w: &mut json::writer, made: &asset): void {
|
||||
return if made.scenes.length == 0;
|
||||
w.member("scenes");
|
||||
w.begin_array();
|
||||
for i in 0..made.scenes.length {
|
||||
val each := &made.scenes[i];
|
||||
w.begin_object();
|
||||
put_text(w, "name", &each.name);
|
||||
put_indices(w, "nodes", &each.nodes);
|
||||
w.end_object();
|
||||
}
|
||||
w.end_array();
|
||||
}
|
||||
|
||||
def write_nodes(w: &mut json::writer, made: &asset): void {
|
||||
return if made.nodes.length == 0;
|
||||
w.member("nodes");
|
||||
w.begin_array();
|
||||
for i in 0..made.nodes.length {
|
||||
val each := &made.nodes[i];
|
||||
w.begin_object();
|
||||
put_text(w, "name", &each.name);
|
||||
put_indices(w, "children", &each.children);
|
||||
put_index(w, "mesh", each.mesh);
|
||||
put_index(w, "camera", each.camera);
|
||||
put_index(w, "skin", each.skin);
|
||||
if each.weights.length != 0 {
|
||||
put_floats(w, "weights", each.weights.span);
|
||||
}
|
||||
val t := each.translation;
|
||||
if t.x != 0 || t.y != 0 || t.z != 0 {
|
||||
val numbers: f32[3] = [t.x, t.y, t.z];
|
||||
put_floats(w, "translation", numbers[:]);
|
||||
}
|
||||
val r := each.rotation;
|
||||
if r.x != 0 || r.y != 0 || r.z != 0 || r.w != 1 {
|
||||
val numbers: f32[4] = [r.x, r.y, r.z, r.w];
|
||||
put_floats(w, "rotation", numbers[:]);
|
||||
}
|
||||
val s := each.scale;
|
||||
if s.x != 1 || s.y != 1 || s.z != 1 {
|
||||
val numbers: f32[3] = [s.x, s.y, s.z];
|
||||
put_floats(w, "scale", numbers[:]);
|
||||
}
|
||||
if each.light is some(lit) {
|
||||
w.member("extensions");
|
||||
w.begin_object();
|
||||
w.member("KHR_lights_punctual");
|
||||
w.begin_object();
|
||||
put_count(w, "light", lit);
|
||||
w.end_object();
|
||||
w.end_object();
|
||||
}
|
||||
w.end_object();
|
||||
}
|
||||
w.end_array();
|
||||
}
|
||||
|
||||
# A primitive's attributes, or a morph target's, as an object of each's name.
|
||||
def write_attributes(w: &mut json::writer, list: &vector<attribute>): void {
|
||||
w.begin_object();
|
||||
for i in 0..list.length {
|
||||
put_count(w, list[i].name.span, list[i].accessor);
|
||||
}
|
||||
w.end_object();
|
||||
}
|
||||
|
||||
def write_meshes(w: &mut json::writer, made: &asset): void {
|
||||
return if made.meshes.length == 0;
|
||||
w.member("meshes");
|
||||
w.begin_array();
|
||||
for i in 0..made.meshes.length {
|
||||
val each := &made.meshes[i];
|
||||
w.begin_object();
|
||||
put_text(w, "name", &each.name);
|
||||
w.member("primitives");
|
||||
w.begin_array();
|
||||
for k in 0..each.primitives.length {
|
||||
val part := &each.primitives[k];
|
||||
w.begin_object();
|
||||
w.member("attributes");
|
||||
write_attributes(w, &part.attributes);
|
||||
put_index(w, "indices", part.indices);
|
||||
put_index(w, "material", part.material);
|
||||
if part.mode != topology::triangles {
|
||||
put_count(w, "mode", part.mode as u64);
|
||||
}
|
||||
if part.targets.length != 0 {
|
||||
w.member("targets");
|
||||
w.begin_array();
|
||||
for t in 0..part.targets.length {
|
||||
write_attributes(w, &part.targets[t]);
|
||||
}
|
||||
w.end_array();
|
||||
}
|
||||
w.end_object();
|
||||
}
|
||||
w.end_array();
|
||||
if each.weights.length != 0 {
|
||||
put_floats(w, "weights", each.weights.span);
|
||||
}
|
||||
w.end_object();
|
||||
}
|
||||
w.end_array();
|
||||
}
|
||||
|
||||
# A texture a material names, with its scale or strength where it has one.
|
||||
def write_texture_ref(w: &mut json::writer, key: strview, held: maybe<texture_ref>, scale_key: strview): void {
|
||||
val used := held else { return; };
|
||||
w.member(key);
|
||||
w.begin_object();
|
||||
put_count(w, "index", used.texture);
|
||||
if used.set != 0 {
|
||||
put_count(w, "texCoord", used.set);
|
||||
}
|
||||
if scale_key.length != 0 {
|
||||
put_number(w, scale_key, used.scale, 1);
|
||||
}
|
||||
w.end_object();
|
||||
}
|
||||
|
||||
def write_materials(w: &mut json::writer, made: &asset): void {
|
||||
return if made.materials.length == 0;
|
||||
w.member("materials");
|
||||
w.begin_array();
|
||||
for i in 0..made.materials.length {
|
||||
val each := &made.materials[i];
|
||||
w.begin_object();
|
||||
put_text(w, "name", &each.name);
|
||||
w.member("pbrMetallicRoughness");
|
||||
w.begin_object();
|
||||
val c := each.base_color;
|
||||
if c[0] != 1 || c[1] != 1 || c[2] != 1 || c[3] != 1 {
|
||||
put_floats(w, "baseColorFactor", c[:]);
|
||||
}
|
||||
write_texture_ref(w, "baseColorTexture", each.base_color_texture, "");
|
||||
put_number(w, "metallicFactor", each.metallic, 1);
|
||||
put_number(w, "roughnessFactor", each.roughness, 1);
|
||||
write_texture_ref(w, "metallicRoughnessTexture", each.metallic_roughness_texture, "");
|
||||
w.end_object();
|
||||
write_texture_ref(w, "normalTexture", each.normal_texture, "scale");
|
||||
write_texture_ref(w, "occlusionTexture", each.occlusion_texture, "strength");
|
||||
write_texture_ref(w, "emissiveTexture", each.emissive_texture, "");
|
||||
# Emission past 1 is written as its strength, by the extension.
|
||||
val strength := brightest(each);
|
||||
val by: f32 = strength > 1 ? strength : 1;
|
||||
if each.emissive[0] != 0 || each.emissive[1] != 0 || each.emissive[2] != 0 {
|
||||
val numbers: f32[3] = [each.emissive[0] / by, each.emissive[1] / by, each.emissive[2] / by];
|
||||
put_floats(w, "emissiveFactor", numbers[:]);
|
||||
}
|
||||
if each.alpha_mode == alpha_mode::mask {
|
||||
w.member("alphaMode");
|
||||
w.text("MASK");
|
||||
put_number(w, "alphaCutoff", each.alpha_cutoff, 0.5);
|
||||
} else if each.alpha_mode == alpha_mode::blend {
|
||||
w.member("alphaMode");
|
||||
w.text("BLEND");
|
||||
}
|
||||
if each.double_sided {
|
||||
w.member("doubleSided");
|
||||
w.boolean(true);
|
||||
}
|
||||
if each.unlit || strength > 1 {
|
||||
w.member("extensions");
|
||||
w.begin_object();
|
||||
if each.unlit {
|
||||
w.member("KHR_materials_unlit");
|
||||
w.begin_object();
|
||||
w.end_object();
|
||||
}
|
||||
if strength > 1 {
|
||||
w.member("KHR_materials_emissive_strength");
|
||||
w.begin_object();
|
||||
w.member("emissiveStrength");
|
||||
put_float(w, strength);
|
||||
w.end_object();
|
||||
}
|
||||
w.end_object();
|
||||
}
|
||||
w.end_object();
|
||||
}
|
||||
w.end_array();
|
||||
}
|
||||
|
||||
# glTF's code of a filter, or 0 for one unset.
|
||||
def filter_code(f: filter): u64 {
|
||||
return match f {
|
||||
unset => 0,
|
||||
nearest => 9728,
|
||||
linear => 9729,
|
||||
};
|
||||
}
|
||||
|
||||
# glTF's code of a minifying filter, between mip levels as it says.
|
||||
def min_filter_code(min: filter, mip: filter): u64 {
|
||||
return filter_code(min) if mip == filter::unset;
|
||||
return 0 if min == filter::unset;
|
||||
val linear := min == filter::linear;
|
||||
if mip == filter::nearest {
|
||||
return linear ? 9985 : 9984;
|
||||
}
|
||||
return linear ? 9987 : 9986;
|
||||
}
|
||||
|
||||
def wrap_code(how: wrap): u64 {
|
||||
return match how {
|
||||
repeat => 10497,
|
||||
clamp => 33071,
|
||||
mirror => 33648,
|
||||
};
|
||||
}
|
||||
|
||||
def write_textures(w: &mut json::writer, made: &asset): void {
|
||||
if made.textures.length != 0 {
|
||||
w.member("textures");
|
||||
w.begin_array();
|
||||
for i in 0..made.textures.length {
|
||||
val each := &made.textures[i];
|
||||
w.begin_object();
|
||||
put_text(w, "name", &each.name);
|
||||
put_index(w, "sampler", each.sampler);
|
||||
put_index(w, "source", each.source);
|
||||
w.end_object();
|
||||
}
|
||||
w.end_array();
|
||||
}
|
||||
return if made.samplers.length == 0;
|
||||
w.member("samplers");
|
||||
w.begin_array();
|
||||
for i in 0..made.samplers.length {
|
||||
val each := &made.samplers[i];
|
||||
w.begin_object();
|
||||
put_text(w, "name", &each.name);
|
||||
val mag := filter_code(each.mag_filter);
|
||||
if mag != 0 {
|
||||
put_count(w, "magFilter", mag);
|
||||
}
|
||||
val min := min_filter_code(each.min_filter, each.mip_filter);
|
||||
if min != 0 {
|
||||
put_count(w, "minFilter", min);
|
||||
}
|
||||
if each.wrap_s != wrap::repeat {
|
||||
put_count(w, "wrapS", wrap_code(each.wrap_s));
|
||||
}
|
||||
if each.wrap_t != wrap::repeat {
|
||||
put_count(w, "wrapT", wrap_code(each.wrap_t));
|
||||
}
|
||||
w.end_object();
|
||||
}
|
||||
w.end_array();
|
||||
}
|
||||
|
||||
def write_images(w: &mut json::writer, made: &asset): void {
|
||||
return if made.images.length == 0;
|
||||
w.member("images");
|
||||
w.begin_array();
|
||||
for i in 0..made.images.length {
|
||||
val each := &made.images[i];
|
||||
w.begin_object();
|
||||
put_text(w, "name", &each.name);
|
||||
if each.view is some(v) {
|
||||
put_count(w, "bufferView", v);
|
||||
w.member("mimeType");
|
||||
w.text(each.mime_type.length != 0 ? each.mime_type.span : mime_of(each.bytes.span));
|
||||
} else if names_file(each.uri.span) {
|
||||
w.member("uri");
|
||||
w.text(each.uri.span);
|
||||
put_text(w, "mimeType", &each.mime_type);
|
||||
} else {
|
||||
val mime := each.mime_type.length != 0 ? each.mime_type.span : mime_of(each.bytes.span);
|
||||
val uri := data_uri_of(mime, each.bytes.span);
|
||||
w.member("uri");
|
||||
w.text(uri.span);
|
||||
}
|
||||
w.end_object();
|
||||
}
|
||||
w.end_array();
|
||||
}
|
||||
|
||||
# glTF's code of the type of an accessor's numbers.
|
||||
def component_code(c: component): u64 {
|
||||
return match c {
|
||||
byte => 5120,
|
||||
unsigned_byte => 5121,
|
||||
short => 5122,
|
||||
unsigned_short => 5123,
|
||||
unsigned_int => 5125,
|
||||
float => 5126,
|
||||
};
|
||||
}
|
||||
|
||||
# glTF's name of what an accessor's elements are.
|
||||
def element_name(e: element): strview {
|
||||
return match e {
|
||||
scalar => "SCALAR",
|
||||
vec2 => "VEC2",
|
||||
vec3 => "VEC3",
|
||||
vec4 => "VEC4",
|
||||
mat2 => "MAT2",
|
||||
mat3 => "MAT3",
|
||||
mat4 => "MAT4",
|
||||
};
|
||||
}
|
||||
|
||||
# A member of an accessor's least or most, where it has them.
|
||||
def put_bounds(w: &mut json::writer, key: strview, values: &vector<f64>): void {
|
||||
return if values.length == 0;
|
||||
w.member(key);
|
||||
w.begin_array();
|
||||
for i in 0..values.length {
|
||||
put_real(w, values[i]);
|
||||
}
|
||||
w.end_array();
|
||||
}
|
||||
|
||||
def write_accessors(w: &mut json::writer, made: &asset): void {
|
||||
return if made.accessors.length == 0;
|
||||
w.member("accessors");
|
||||
w.begin_array();
|
||||
for i in 0..made.accessors.length {
|
||||
val each := &made.accessors[i];
|
||||
w.begin_object();
|
||||
put_text(w, "name", &each.name);
|
||||
put_index(w, "bufferView", each.view);
|
||||
if each.offset != 0 {
|
||||
put_count(w, "byteOffset", each.offset);
|
||||
}
|
||||
put_count(w, "componentType", component_code(each.component_type));
|
||||
if each.normalized {
|
||||
w.member("normalized");
|
||||
w.boolean(true);
|
||||
}
|
||||
put_count(w, "count", each.count);
|
||||
w.member("type");
|
||||
w.text(element_name(each.kind));
|
||||
put_bounds(w, "min", &each.min);
|
||||
put_bounds(w, "max", &each.max);
|
||||
if each.sparse is some(s) {
|
||||
w.member("sparse");
|
||||
w.begin_object();
|
||||
put_count(w, "count", s.count);
|
||||
w.member("indices");
|
||||
w.begin_object();
|
||||
put_count(w, "bufferView", s.indices_view);
|
||||
if s.indices_offset != 0 {
|
||||
put_count(w, "byteOffset", s.indices_offset);
|
||||
}
|
||||
put_count(w, "componentType", component_code(s.indices_type));
|
||||
w.end_object();
|
||||
w.member("values");
|
||||
w.begin_object();
|
||||
put_count(w, "bufferView", s.values_view);
|
||||
if s.values_offset != 0 {
|
||||
put_count(w, "byteOffset", s.values_offset);
|
||||
}
|
||||
w.end_object();
|
||||
w.end_object();
|
||||
}
|
||||
w.end_object();
|
||||
}
|
||||
w.end_array();
|
||||
}
|
||||
|
||||
def write_views(w: &mut json::writer, made: &asset): void {
|
||||
return if made.views.length == 0;
|
||||
w.member("bufferViews");
|
||||
w.begin_array();
|
||||
for i in 0..made.views.length {
|
||||
val each := &made.views[i];
|
||||
w.begin_object();
|
||||
put_text(w, "name", &each.name);
|
||||
put_count(w, "buffer", each.buffer);
|
||||
if each.offset != 0 {
|
||||
put_count(w, "byteOffset", each.offset);
|
||||
}
|
||||
put_count(w, "byteLength", each.length);
|
||||
if each.stride != 0 {
|
||||
put_count(w, "byteStride", each.stride);
|
||||
}
|
||||
w.end_object();
|
||||
}
|
||||
w.end_array();
|
||||
}
|
||||
|
||||
def write_buffers(w: &mut json::writer, made: &asset, chunked: bool): void {
|
||||
return if made.buffers.length == 0;
|
||||
w.member("buffers");
|
||||
w.begin_array();
|
||||
for i in 0..made.buffers.length {
|
||||
val each := &made.buffers[i];
|
||||
w.begin_object();
|
||||
put_text(w, "name", &each.name);
|
||||
put_count(w, "byteLength", each.bytes.length);
|
||||
if names_file(each.uri.span) {
|
||||
w.member("uri");
|
||||
w.text(each.uri.span);
|
||||
} else if !(chunked && i == 0) {
|
||||
val uri := data_uri_of("application/octet-stream", each.bytes.span);
|
||||
w.member("uri");
|
||||
w.text(uri.span);
|
||||
}
|
||||
w.end_object();
|
||||
}
|
||||
w.end_array();
|
||||
}
|
||||
|
||||
def write_cameras(w: &mut json::writer, made: &asset): void {
|
||||
return if made.cameras.length == 0;
|
||||
w.member("cameras");
|
||||
w.begin_array();
|
||||
for i in 0..made.cameras.length {
|
||||
val each := &made.cameras[i];
|
||||
w.begin_object();
|
||||
put_text(w, "name", &each.name);
|
||||
if each.kind == camera_kind::perspective {
|
||||
w.member("type");
|
||||
w.text("perspective");
|
||||
w.member("perspective");
|
||||
w.begin_object();
|
||||
w.member("yfov");
|
||||
put_float(w, each.fov_y);
|
||||
if each.aspect is some(aspect) {
|
||||
w.member("aspectRatio");
|
||||
put_float(w, aspect);
|
||||
}
|
||||
} else {
|
||||
w.member("type");
|
||||
w.text("orthographic");
|
||||
w.member("orthographic");
|
||||
w.begin_object();
|
||||
w.member("xmag");
|
||||
put_float(w, each.x_mag);
|
||||
w.member("ymag");
|
||||
put_float(w, each.y_mag);
|
||||
}
|
||||
w.member("znear");
|
||||
put_float(w, each.near);
|
||||
if each.far is some(far) {
|
||||
w.member("zfar");
|
||||
put_float(w, far);
|
||||
}
|
||||
w.end_object();
|
||||
w.end_object();
|
||||
}
|
||||
w.end_array();
|
||||
}
|
||||
|
||||
def write_skins(w: &mut json::writer, made: &asset): void {
|
||||
return if made.skins.length == 0;
|
||||
w.member("skins");
|
||||
w.begin_array();
|
||||
for i in 0..made.skins.length {
|
||||
val each := &made.skins[i];
|
||||
w.begin_object();
|
||||
put_text(w, "name", &each.name);
|
||||
put_indices(w, "joints", &each.joints);
|
||||
put_index(w, "inverseBindMatrices", each.inverse_binds);
|
||||
put_index(w, "skeleton", each.skeleton);
|
||||
w.end_object();
|
||||
}
|
||||
w.end_array();
|
||||
}
|
||||
|
||||
def write_animations(w: &mut json::writer, made: &asset): void {
|
||||
return if made.animations.length == 0;
|
||||
w.member("animations");
|
||||
w.begin_array();
|
||||
for i in 0..made.animations.length {
|
||||
val each := &made.animations[i];
|
||||
w.begin_object();
|
||||
put_text(w, "name", &each.name);
|
||||
w.member("samplers");
|
||||
w.begin_array();
|
||||
for k in 0..each.samplers.length {
|
||||
val keys := &each.samplers[k];
|
||||
w.begin_object();
|
||||
put_count(w, "input", keys.input);
|
||||
put_count(w, "output", keys.output);
|
||||
if keys.interpolation == interpolation::step {
|
||||
w.member("interpolation");
|
||||
w.text("STEP");
|
||||
} else if keys.interpolation == interpolation::cubic_spline {
|
||||
w.member("interpolation");
|
||||
w.text("CUBICSPLINE");
|
||||
}
|
||||
w.end_object();
|
||||
}
|
||||
w.end_array();
|
||||
w.member("channels");
|
||||
w.begin_array();
|
||||
for k in 0..each.channels.length {
|
||||
val moved := &each.channels[k];
|
||||
w.begin_object();
|
||||
put_count(w, "sampler", moved.sampler);
|
||||
w.member("target");
|
||||
w.begin_object();
|
||||
put_index(w, "node", moved.node);
|
||||
w.member("path");
|
||||
w.text(match moved.path {
|
||||
translation => "translation",
|
||||
rotation => "rotation",
|
||||
scale => "scale",
|
||||
weights => "weights",
|
||||
});
|
||||
w.end_object();
|
||||
w.end_object();
|
||||
}
|
||||
w.end_array();
|
||||
w.end_object();
|
||||
}
|
||||
w.end_array();
|
||||
}
|
||||
|
||||
def write_lights(w: &mut json::writer, made: &asset): void {
|
||||
w.member("lights");
|
||||
w.begin_array();
|
||||
for i in 0..made.lights.length {
|
||||
val each := &made.lights[i];
|
||||
w.begin_object();
|
||||
put_text(w, "name", &each.name);
|
||||
w.member("type");
|
||||
w.text(match each.kind {
|
||||
directional => "directional",
|
||||
point => "point",
|
||||
spot => "spot",
|
||||
});
|
||||
if each.color[0] != 1 || each.color[1] != 1 || each.color[2] != 1 {
|
||||
put_floats(w, "color", each.color[:]);
|
||||
}
|
||||
put_number(w, "intensity", each.intensity, 1);
|
||||
if each.range is some(far) {
|
||||
w.member("range");
|
||||
put_float(w, far);
|
||||
}
|
||||
if each.kind == light_kind::spot {
|
||||
w.member("spot");
|
||||
w.begin_object();
|
||||
put_number(w, "innerConeAngle", each.inner_cone, 0);
|
||||
put_number(w, "outerConeAngle", each.outer_cone, 0.7853982);
|
||||
w.end_object();
|
||||
}
|
||||
w.end_object();
|
||||
}
|
||||
w.end_array();
|
||||
}
|
||||
Reference in new issue
Block a user