All Projects → haxiomic → vector-math

haxiomic / vector-math

Licence: MIT license
Shader-math in haxe: library for GLSL vector operations, complete with swizzles and all

Programming Languages

haxe
709 projects

Projects that are alternatives of or similar to vector-math

Joml
A Java math library for OpenGL rendering calculations
Stars: ✭ 479 (+1496.67%)
Mutual labels:  math, vector
Algebra
means completeness and balancing, from the Arabic word الجبر
Stars: ✭ 92 (+206.67%)
Mutual labels:  math, vector
Cglm
📽 Highly Optimized Graphics Math (glm) for C
Stars: ✭ 887 (+2856.67%)
Mutual labels:  math, vector
Webgl Fundamentals
WebGL lessons that start with the basics
Stars: ✭ 3,315 (+10950%)
Mutual labels:  math, glsl
Gpu.js
GPU Accelerated JavaScript
Stars: ✭ 13,427 (+44656.67%)
Mutual labels:  math, glsl
Webclgl
GPGPU Javascript library 🐸
Stars: ✭ 313 (+943.33%)
Mutual labels:  math, glsl
Sharpmath
A small .NET math library.
Stars: ✭ 36 (+20%)
Mutual labels:  math, vector
SdfFontDesigner
Offline font tuning/bitmap generation via shaders
Stars: ✭ 56 (+86.67%)
Mutual labels:  math, glsl
Math Php
Powerful modern math library for PHP: Features descriptive statistics and regressions; Continuous and discrete probability distributions; Linear algebra with matrices and vectors, Numerical analysis; special mathematical functions; Algebra
Stars: ✭ 2,009 (+6596.67%)
Mutual labels:  math, vector
Hlslpp
Math library using hlsl syntax with SSE/NEON support
Stars: ✭ 153 (+410%)
Mutual labels:  math, vector
hypVR-Ray
Hyperbolic VR using Raymarching
Stars: ✭ 81 (+170%)
Mutual labels:  math, glsl
matrixgl
Yet another matrix library for WebGL
Stars: ✭ 25 (-16.67%)
Mutual labels:  math, vector
mightyscape-1.X
A maintained extension collection for Inkscape 1.0+, working on Windows and Linux
Stars: ✭ 23 (-23.33%)
Mutual labels:  math, vector
Mango
mango fun framework
Stars: ✭ 343 (+1043.33%)
Mutual labels:  math, vector
vecti
A tiny TypeScript library for 2D vector math.
Stars: ✭ 14 (-53.33%)
Mutual labels:  math, vector
Coord Rs
[deprecated] A simple, ergonomic vector mathematics crate for Rust
Stars: ✭ 18 (-40%)
Mutual labels:  math, vector
vec-la-fp
↗️ A tiny (functional) 2d linear algebra library
Stars: ✭ 21 (-30%)
Mutual labels:  math, vector
vector2d
2D Vector Library. Operates using Objects, Array or Float32Array types to allow flexible performance.
Stars: ✭ 28 (-6.67%)
Mutual labels:  math, vector
Numphp
Mathematical PHP library for scientific computing
Stars: ✭ 120 (+300%)
Mutual labels:  math, vector
PyGLM
Fast OpenGL Mathematics (GLM) for Python
Stars: ✭ 167 (+456.67%)
Mutual labels:  vector, glsl

Haxe Vector Math

Requires haxe 4.2+

Haxe vector math library that enables GLSL vector and matrix operations to compile in haxe

Features

GLSL Built-in Functions

All GLSL built-in functions are available after import VectorMath;

import VectorMath;

var direction = normalize(velocity);
var speed = length(velocity);

Vector and Matrix Constructors

Vectors and matrices can be constructed with the same patterns as GLSL

// new keyword not required
vec2(1, 2);

// single argument sets all components
vec2(0.0);

// vector composition
var color = vec3(0, 1, 0);
vec4(color, a);

// matrices can be composed from vectors
mat2(
	vec2(1, 0), // column 0
	vec2(0, 1)  // column 1
);

// a single argument sets the diagonal components (which creates a scale matrix)
mat2(scale);

Operator Overloads

All vector, matrix and scalar operations available in GLSL are supported

// vectors can multiply with scalars
vec2(1, 2) * 0.5;
mat2(1) * 0.5; // return a new mat2 after multiplying each component with the scalar

// vectors can be multiplied with compatible matrices
mat2(2) * vec2(3, 4);
var position = projection * view * model * vec4(xyz, 1.0);

// +=, *= etc work
var dt = 1/60;
var position = vec2(0.0);
var velocity = vec2(0.3, 0.4);
position += velocity * dt;

// component-wise comparison
vec2(1, 2) == vec2(1, 2) // true

Swizzles

Supports all possible read and write swizzle operations, including aliases rgba and stpq

vec4(1, 2, 3, 4).wzyx == vec4(4, 3, 2, 1); // true

// set xy components to (1, 2)
var position = vec4(0.0);
position.xy = vec2(1, 2);

// set rgb components to green
var color4 = vec4(1.0); // white
color4.rgb = vec3(0., 1., 0); // green

Performance

All operations are inlined so that vector objects are rarely constructed, instead vector operations compile to stack variables (meaning we can avoid the GC completely for most vector operations!). For example, the following compiled with -D analyzer-optimize

trace(length(mat2(2) * vec2(3, 4) * 0.5 - vec2(0.5)));

Generates

console.log(Math.sqrt(18.5));

(Cool right?)

Furthermore, because vector components are just stack variables these operations are easily auto-vectorized (SIMD) on compiled targets like cpp

Usage

Install with haxelib install vector-math

Add --library vector-math to your hxml commands

Then simply import the VectorMath class: import VectorMath;

import VectorMath;

function main() {
	var normal = normalize(vec2(Math.random(), Math.random()));
	var tangent = normal.yx * vec2(-1, 1);
	var transformedTangent = mat2(2) * tangent;
}

Add --dce full and -D analyzer-optimize to your hxml for clean output!

Q/A

  • What are the differences to GLSL?

    • Direct vector assignment is reference rather than copy, that is: in the following statement: var ref = original, 'ref' represents the same underlying vector as 'original', whereas in GLSL it would be a copy. To copy a vector you can do any of var copy = original.clone(), var copy = vec3(original) or var copy = original.xyz
    • You can call methods via dot syntax in addition to regular calls, for example: vec3(1).length() and length(vec(1)) are equivalent
    • Boolean and integer vector types are not yet included (bvec and ivec). These may come in the future if there's a desire for them
  • Which haxe targets does this work on?

    All haxe targets are supported with the exception of macros, this is an optimization to improve compile-time performance

  • Can this be used to generate shader code?

    Yes! @rainyt has developed a VectorMath -> GLSL translator for use with OpenFL: github.com/rainyt/openfl-glsl

    In the future I hope to work on a general VectorMath to shader translator to support multiple ouput shading languages

  • What makes this different from HXSL?

    HXSL is the haxe-based shading language in Heaps, it has similar aims but different implementation – HXSL works at the syntax level and does not support autocomplete or compiling to CPU platform code (like js or C++), whereas VectorMath code is executable as regular haxe code as well as shader code

  • Which specification is this based on?

    GLSL ES 1.0 Specification

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].