A performance oriented 2D/3D math package for Go
Performance-oriented 3D math library for Go, optimized for graphics programming, game development, and scientific computing.
*T for fluent APIgo get github.com/ungerik/go3d…Every type has its own sub-package and is named T. Packages under float64/ use float64 instead of float32.
| Package | Description | Type Size |
|---|---|---|
vec2 |
2D vectors | 8 bytes |
vec3 |
3D vectors | 12 bytes |
vec4 |
4D vectors / homogeneous coordinates | 16 bytes |
mat2 |
2×2 matrices | 16 bytes |
mat3 |
3×3 matrices | 36 bytes |
mat4 |
4×4 matrices | 64 bytes (one cache line!) |
quaternion |
Quaternions for 3D rotations | 16 bytes |
All types are available in float64 precision under float64/:
float64/vec2, float64/vec3, float64/vec4float64/mat2, float64/mat3, float64/mat4float64/quaterniongeneric - Generic matrix/vector interfaceshermit2 - 2D Hermite splineshermit3 - 3D Hermite splinesThis library uses github.com/chewxy/math32 for float32 math functions. Import it as:
import math "github.com/chewxy/math32"The math32 package provides float32 versions of Go's standard math library functions, offering better performance for float32-based graphics calculations compared to converting to/from float64.
Present tense methods modify in place and return *T for chaining:
v := vec3.T{1, 2, 3}
v.Scale(2) // v is now {2, 4, 6}
v.Add(&vec3.UnitX) // v is now {3, 4, 6}Past tense methods return a modified copy:
v := vec3.T{1, 2, 3}
v2 := v.Scaled(2) // v2 = {2, 4, 6}, v unchanged
v3 := v.Added(&vec3.UnitX) // v3 = {2, 2, 3}, v unchangedresult := vec3.Zero
result.Add(&vec3.UnitX).Scale(5).Add(&vec3.UnitY)
// result = {5, 1, 0}Matrices use column-major layout (OpenGL convention):
mat[column][row]For DirectX (row-major), use Transpose().
math32.Pi constants)// Construction
v := vec3.T{1, 2, 3}
// Length
length := v.Length() // sqrt(x² + y² + z²)
lengthSq := v.LengthSqr() // x² + y² + z² (faster, no sqrt)
// Normalization
v.Normalize() // In-place
unit := v.Normalized() // Copy
// Arithmetic
v.Add(&other) // v += other
v.Sub(&other) // v -= other
v.Scale(2.5) // v *= 2.5
// Dot and cross products
dot := vec3.Dot(&a, &b)
cross := vec3.Cross(&a, &b)
// Distance
dist := vec3.Distance(&a, &b)……// For graphics (sufficient for most cases)
import "github.com/ungerik/go3d/vec3"
// For scientific computing (higher precision)
import "github.com/ungerik/go3d/float64/vec3"// ✓ Good: Minimal allocations
v.Scale(2).Add(&other)
// ✗ Less efficient: Creates copies
result := v.Scaled(2).Added(&other)// ✓ Faster: No sqrt
thresholdSq := threshold * threshold
if v.LengthSqr() 1 { t = 1 }
// Smooth interpolation
return quaternion.Slerp(&start, &end, t)
}Make objects always face the camera:
…The package is designed for performance over convenience where necessary:
Performance comparison (see mat4/mat4_test.go):
cd mat4
go test -bench=BenchmarkMulAddVec4_PassBy*Contributions are welcome! Please ensure:
go test ./...go fmt ./...MIT - see LICENSE file for details.
No open issues yet, or sync has not completed.