Skip to content

Repository files navigation

three.zig

CI Zig License: MIT

A Zig library for loading and processing meshes.

Requires Zig 0.16.0.

Roadmap

Under active development, APIs may change.

  • More loaders: OBJ, PLY, glTF...
  • Writers for supported formats
  • Mesh processing: normals, transforms, simplification, validation
  • Compile to C compatible library
  • Compile to Wasm with JS glue

Install

Fetch and save the dependency:

zig fetch --save git+https://github.com/Sheol27/three.zig

Wire it into your build.zig:

const three = b.dependency("three", .{
    .target = target,
    .optimize = optimize,
});

exe.root_module.addImport("three", three.module("three"));

Usage

const std = @import("std");
const three = @import("three");

pub fn main(init: std.process.Init) !void {
    const allocator = init.arena.allocator();

    // Format inferred from the extension
    const mesh: three.Mesh = try three.formats.load(init.io, allocator, "model.stl");
    defer mesh.deinit(allocator);

    std.debug.print("triangles: {}\n", .{mesh.triangles_count});

    const bb = mesh.computeBoundingBox();
    std.debug.print("center: {any}\n", .{bb.center()});

    // Format-specific options ride in the union payload
    try three.formats.save(&mesh, init.io, "out.stl", .{ .stl = .{ .encoding = .ascii } });
}

You can also parse from any std.Io.Reader via formats.loadFromReader, or use a format directly: formats.Stl.loadFromReader (auto-detects ascii/binary), formats.Stl.parseAscii / parseBinary, formats.Stl.saveToWriter.

Example

Run the bundled example against an STL file:

zig build run -- path/to/model.stl

Test

zig build test

Contributing

Contributions are welcome, open an issue or PR!

License

MIT

About

Zig 3D library

Resources

Stars

Watchers

Forks

Releases

Contributors

Languages