Files
models/tests/writing.sakura
T

240 lines
13 KiB
Plaintext
Raw Normal View History

2026-10-30 12:04:54 -05:00
#! Contains the tests of writing assets: one built by hand, as `.glb` and as
#! `.gltf` JSON, read back; one read, written, and read again, its every part;
#! and files saved, beside it where they're named so.
import std::io;
import std::testing;
import malunal::linear;
import malunal::gltf;
# A quad built by hand, of positions, normals, and indices, a red material, a
# node placed and turned, and a PNG's first bytes as an image.
def built_quad(): gltf::asset {
var made := gltf::asset::new("gltf tests");
val places: f32[12] = [-0.5, -0.5, 0, 0.5, -0.5, 0, 0.5, 0.5, 0, -0.5, 0.5, 0];
val normals: f32[12] = [0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1];
val indices: u32[6] = [0, 1, 2, 0, 2, 3];
val p := made.add_floats(places[:], gltf::element::vec3, "places");
val n := made.add_floats(normals[:], gltf::element::vec3);
val i := made.add_indices(indices[:]);
var part := gltf::primitive{ indices = i, material = 0 };
part.attributes.append(gltf::attribute{ name = string::from("POSITION", nothing{}), accessor = p });
part.attributes.append(gltf::attribute{ name = string::from("NORMAL", nothing{}), accessor = n });
var shape := gltf::mesh{ name = string::from("quad", nothing{}) };
shape.primitives.append(move(part));
made.meshes.append(move(shape));
var red := gltf::material{ name = string::from("red", nothing{}), metallic = 0, double_sided = true };
red.base_color = [1, 0, 0, 1];
red.emissive = [4, 2, 0];
made.materials.append(move(red));
val picture: u8[8] = [0x89, 'P', 'N', 'G', 13, 10, 26, 10];
_ = made.add_image(picture[:]);
made.textures.append(gltf::texture{ source = 0 });
var square := gltf::node{ name = string::from("Square", nothing{}), mesh = 0 };
square.translation = vec3{ 1, 2, 3 };
square.rotation = quat::axis_angle(vec3::up, 0.5);
made.nodes.append(move(square));
var whole := gltf::scene{ name = string::from("Quad", nothing{}) };
whole.nodes.append(0);
made.scenes.append(move(whole));
made.scene = 0;
return made;
}
# Checks what the quad built holds, read back.
def expect_built(t: &mut tester, read: &gltf::asset): void {
t.expects(read.version.span == "2.0" && read.generator.span == "gltf tests");
t.expects(read.scene is some(s) && s == 0 && read.scenes[0].name.span == "Quad" && read.scenes[0].nodes.length == 1);
val node := &read.nodes[0];
t.expects(node.name.span == "Square" && node.mesh is some(m) && m == 0 && near(node.translation.z, 3));
val turned := quat::axis_angle(vec3::up, 0.5);
t.expects(near(node.rotation.y, turned.y) && near(node.rotation.w, turned.w) && near(node.scale.x, 1));
val part := &read.meshes[0].primitives[0];
t.expects(read.meshes[0].name.span == "quad" && part.find("POSITION") is some(at) && at == 0 && part.material is some(k) && k == 0);
val places := read.floats(0);
t.expects(places.length == 12 && near(places[3], 0.5) && near(places[10], 0.5));
t.expects(read.accessors[0].name.span == "places" && read.accessors[0].min.length == 3 && read.accessors[0].min[0] == -0.5 && read.accessors[0].max[1] == 0.5);
val normals := read.floats(1);
t.expects(normals.length == 12 && near(normals[11], 1));
val indices := read.integers(2);
t.expects(indices.length == 6 && indices[2] == 2 && indices[5] == 3 && read.accessors[2].component_type == gltf::component::unsigned_short);
val red := &read.materials[0];
t.expects(red.name.span == "red" && red.double_sided && near(red.base_color[0], 1) && near(red.base_color[1], 0) && near(red.metallic, 0));
# Emission past 1 comes back by its strength.
t.expects(near(red.emissive[0], 4) && near(red.emissive[1], 2) && near(red.emissive[2], 0));
t.expects(read.images.length == 1 && read.images[0].bytes.length == 8 && read.images[0].bytes[1] == 'P' && read.images[0].mime_type.span == "image/png");
t.expects(read.textures[0].source is some(src) && src == 0);
}
@test def writes_what_it_built_as_glb_and_json(t: &mut tester): void {
val built := built_quad();
val glb := gltf::encode(&built, true) else (e) {
t.expects(false);
io::println(e.describe().span);
return;
};
t.expects(glb.length % 4 == 0 && glb[0] == 'g' && glb[1] == 'l' && glb[2] == 'T' && glb[3] == 'F');
val from_glb := gltf::decode(glb.span) else (e) {
t.expects(false);
io::println(e.describe().span);
return;
};
expect_built(t, &from_glb);
t.expects(from_glb.buffers.length == 1 && from_glb.buffers[0].uri.length == 0);
val json := gltf::encode(&built, false) else (e) {
t.expects(false);
io::println(e.describe().span);
return;
};
val text := string::from(strview::over(json.data, json.length), nothing{});
t.expects(text.span.find("\"uri\":\"data:application/octet-stream;base64,") is some(_));
t.expects(text.span.find("KHR_materials_emissive_strength") is some(_) && text.span.find("\"emissiveStrength\":4") is some(_));
val from_json := gltf::decode(json.span) else (e) {
t.expects(false);
io::println(e.describe().span);
return;
};
expect_built(t, &from_json);
}
@test def writes_what_it_read_as_it_was(t: &mut tester): void {
# Every part, read of JSON: lights, a camera of each kind, a skin, an
# animation, a sampler, textures of a material, and a node's children.
# The quad's buffer, and the animation's keys, two times and two rotations,
# in a buffer of their own, each a data URI.
val bytes := quad_buffer();
val quad_uri := data_uri(bytes.span);
var keys := packed::new();
val times: f32[2] = [0, 1];
val turns: f32[8] = [0, 0, 0, 1, 0, 0.7071068, 0, 0.7071068];
keys.floats(times[:]);
keys.floats(turns[:]);
val keys_uri := data_uri(keys.bytes.span);
val whole := string::from("""
{
"asset": { "version": "2.0", "generator": "gltf tests" },
"extensionsUsed": ["KHR_lights_punctual", "KHR_materials_unlit", "EXT_unknown"],
"scene": 0,
"scenes": [{ "name": "Quad", "nodes": [0] }],
"nodes": [
{ "name": "Square", "mesh": 0, "translation": [1, 2, 3], "children": [1], "skin": 0 },
{ "name": "Lamp", "scale": [2, 2, 2], "extensions": { "KHR_lights_punctual": { "light": 1 } } }
],
"meshes": [{ "name": "quad", "primitives": [{
"attributes": { "POSITION": 0, "NORMAL": 1 }, "indices": 2, "material": 0
}] }],
"materials": [{ "name": "red", "doubleSided": true,
"pbrMetallicRoughness": { "baseColorFactor": [1, 0, 0, 1], "metallicFactor": 0 },
"normalTexture": { "index": 0, "scale": 0.5 }, "extensions": { "KHR_materials_unlit": {} } }],
"textures": [{ "sampler": 0, "source": 0 }],
"samplers": [{ "magFilter": 9728, "minFilter": 9987, "wrapS": 33071 }],
"images": [{ "uri": "data:image/png;base64,iVBORw0KGgo=" }],
"accessors": [
{ "bufferView": 0, "componentType": 5126, "count": 4, "type": "VEC3", "min": [-0.5, -0.5, 0], "max": [0.5, 0.5, 0] },
{ "bufferView": 0, "byteOffset": 48, "componentType": 5126, "count": 4, "type": "VEC3" },
{ "bufferView": 1, "componentType": 5123, "count": 6, "type": "SCALAR" },
{ "bufferView": 2, "componentType": 5126, "count": 2, "type": "SCALAR" },
{ "bufferView": 2, "byteOffset": 8, "componentType": 5126, "count": 2, "type": "VEC4" }
],
"bufferViews": [
{ "buffer": 0, "byteOffset": 0, "byteLength": 96 },
{ "buffer": 0, "byteOffset": 96, "byteLength": 12 },
{ "buffer": 1, "byteLength": 40 }
],
"buffers": [{ "byteLength": 108, "uri": "QUAD" }, { "byteLength": 40, "uri": "KEYS" }],
"cameras": [
{ "name": "Eye", "type": "perspective", "perspective": { "yfov": 0.8, "znear": 0.1 } },
{ "type": "orthographic", "orthographic": { "xmag": 2, "ymag": 1, "znear": 0, "zfar": 50 } }
],
"skins": [{ "joints": [0] }],
"animations": [{ "name": "Spin",
"samplers": [{ "input": 3, "output": 4, "interpolation": "STEP" }],
"channels": [{ "sampler": 0, "target": { "node": 0, "path": "rotation" } }] }],
"extensions": { "KHR_lights_punctual": { "lights": [
{ "name": "Sun", "type": "directional", "intensity": 3 },
{ "type": "spot", "color": [1, 0.5, 0], "range": 10, "spot": { "innerConeAngle": 0.2, "outerConeAngle": 0.4 } }
] } }
}
""", nothing{});
val json := replaced(replaced(whole.span, "QUAD", quad_uri.span).span, "KEYS", keys_uri.span);
val read := gltf::decode(json.span) else (e) {
t.expects(false);
io::println(e.describe().span);
return;
};
for binary in [false, true] {
val written := gltf::encode(&read, binary) else (e) {
t.expects(false);
io::println(e.describe().span);
return;
};
val again := gltf::decode(written.span) else (e) {
t.expects(false);
io::println(e.describe().span);
return;
};
t.expects(again.generator.span == "gltf tests" && again.scene is some(s) && s == 0 && again.scenes[0].name.span == "Quad");
t.expects(again.nodes[0].name.span == "Square" && near(again.nodes[0].translation.y, 2) && again.nodes[0].children.length == 1);
val places := again.floats(0);
val indices := again.integers(2);
t.expects(places.length == 12 && near(places[3], 0.5) && indices.length == 6 && indices[5] == 3);
t.expects(again.accessors[0].max.length == 3 && again.accessors[0].max[0] == 0.5 && again.accessors[1].offset == 48);
val red := &again.materials[0];
t.expects(red.name.span == "red" && red.double_sided && near(red.base_color[1], 0) && near(red.metallic, 0) && near(red.roughness, 1));
t.expects(again.lights.length == 2 && again.lights[0].name.span == "Sun" && again.lights[0].kind == gltf::light_kind::directional && near(again.lights[0].intensity, 3));
t.expects(again.lights[1].kind == gltf::light_kind::spot && near(again.lights[1].color[1], 0.5) && again.lights[1].range is some(r) && near(r, 10) && near(again.lights[1].outer_cone, 0.4));
t.expects(again.cameras.length == 2 && near(again.cameras[0].fov_y, 0.8) && again.cameras[0].far is none && again.cameras[1].kind == gltf::camera_kind::orthographic);
t.expects(again.cameras[1].far is some(f) && near(f, 50) && near(again.cameras[1].x_mag, 2));
t.expects(again.samplers[0].mag_filter == gltf::filter::nearest && again.samplers[0].min_filter == gltf::filter::linear && again.samplers[0].mip_filter == gltf::filter::linear);
t.expects(again.samplers[0].wrap_s == gltf::wrap::clamp && again.samplers[0].wrap_t == gltf::wrap::repeat);
t.expects(again.images[0].bytes.length == 8 && again.images[0].mime_type.span == "image/png");
t.expects(again.materials[0].unlit && again.materials[0].normal_texture is some(nt) && near(nt.scale, 0.5));
t.expects(again.skins.length == 1 && again.skins[0].joints[0] == 0 && again.nodes[0].skin is some(sk) && sk == 0);
t.expects(again.nodes.length == 2 && again.nodes[1].parent is some(up) && up == 0 && again.nodes[1].light is some(l) && l == 1 && near(again.nodes[1].scale.y, 2));
val spin := &again.animations[0];
t.expects(spin.name.span == "Spin" && spin.samplers[0].interpolation == gltf::interpolation::step && spin.channels[0].path == gltf::target_path::rotation);
val turned := again.floats(spin.samplers[0].output);
t.expects(turned.length == 8 && near(turned[5], 0.7071068));
# The extension it didn't read isn't written.
val text := string::from(strview::over(written.data, written.length), nothing{});
t.expects(text.span.find("EXT_unknown") is none && text.span.find("KHR_materials_unlit") is some(_));
# A .glb file's first buffer, the keys', is its binary chunk; the quad's
# is a data URI.
if binary {
t.expects(again.buffers[0].uri.length == 0 && again.buffers[1].uri.span.starts_with("data:"));
}
}
}
@test def saves_files_and_those_beside_them(t: &mut tester): void {
var built := built_quad();
# Its buffer is a file beside it, named with a space.
built.buffers.at(0).uri = string::from("quad%20data.bin", nothing{});
gltf::save(&built, ".blossom/gltf tests/saved/quad.gltf") else (e) {
t.expects(false);
io::println(e.describe().span);
return;
};
t.expects(io::read_file(&path::parse(".blossom/gltf tests/saved/quad data.bin").expect("A path.")) is value(_));
val read := gltf::load(".blossom/gltf tests/saved/quad.gltf") else (e) {
t.expects(false);
io::println(e.describe().span);
return;
};
expect_built(t, &read);
# As a .glb file, the buffer named a file still is one.
gltf::save(&built, ".blossom/gltf tests/saved/quad.glb") else (e) {
t.expects(false);
io::println(e.describe().span);
return;
};
val glb := gltf::load(".blossom/gltf tests/saved/quad.glb") else (e) {
t.expects(false);
io::println(e.describe().span);
return;
};
expect_built(t, &glb);
t.expects(glb.buffers[0].uri.span == "quad%20data.bin");
}