Giter Club home page Giter Club logo

munum's Introduction

πŸ…ΌπŸ†„πŸ…½πŸ†„πŸ…Ό

ΞΌNum - Micro 3D Math Library


license: MIT Docs.rs Crates.io npm codecov build

Overview

munum is a minimalistic numerical library for high-performance 3D math with Rust, WebAssembly and JavaScript bindings.

Documentation

Install

[JavaScript] Install via npm:

npm install --save munum

[Rust] Install as Cargo dependency:

cargo add munum

Features:

  • std - enables std support. enabled by default.
  • libm - enables trigonometry related functions in no_std environment using libm.
  • jsmath - enables trigonometry related functions in no_std WebAssembly environment using JS Math binding.
  • serde - enables serde serialize/deserialize implementations
  • wasm - produces WebAssembly module and WebAssembly component (WIP)

Usage (JavaScript WebAssembly binding)

Sample usage to build a perspective camera view-projection matrix below:

(Try it yourself here)

import { lookAt, perspective, Mat4, Vec3 } from 'munum'; // Or load from CDN, e.g. 'https://unpkg.com/munum@latest'

using eye = new Vec3(1, 1, 1);
using target = new Vec3(0, 0, 0);
using up = new Vec3(0, 1, 0);
const view = lookAt(eye, target, up);

const aspectRatio = width / height;
const yfov = Math.PI / 4;
const znear = 1;
const zfar = 100;

using viewProj = perspective(aspectRatio, yfov, znear, zfar).mul(view);

Note the use of using (which automatically calls .free() when out of scope). When using JavaScript binding, munum resources are allocated on WebAssembly memory which need to be deallocated later. munum uses FinalizationRegistry for automatic memory management, so explicit memory management with using or .free() is not required through recommended.

Usage (Pure JavaScript)

Import from munum/js for pure JavaScript implementation.

Usage (Rust)

Sample usage to build a perspective camera view-projection matrix:

use core::f32::{consts::PI, INFINITY};
use munum::{transform, vec3};

let eye = vec3(0_f32, 2., 0.);
let target = vec3(0., 0.6, 0.);
let up = vec3(0., 0., -1.);
let view = transform::look_at(eye, target, up);

let proj = transform::perspective(2., PI/2., 1., INFINITY);

let view_proj = proj * view;

License

This repository and the code inside it is licensed under the MIT License. Read LICENSE for more information.

munum's People

Contributors

andykswong avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

munum's Issues

More convenience functions

Functions that may be useful:

  • vec.dist = len(sub(v1, v2))
  • vec3.mmul4(Mat4, Vec3)
  • vec2.mmul3(Mat3, Vec2)
  • vec[234].set(v, a, b, c ,d)

SIMD Support

Support the use of SIMD v128 in WASM for performance

Optimize WASM performance

Currently WASM build is slower than JS, need more investigation on optimizing it:

  • Use unchecked for array access for much better performance
  • Using local variable seems faster
  • Use native math functions instead of Math.*

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.