Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
G

go3d

> 编程语言
Open source

A performance oriented 2D/3D math package for Go

341 stars0 likes1 views
WebsiteGitHub

About

A performance oriented 2D/3D math package for Go

go3d

Performance-oriented 3D math library for Go, optimized for graphics programming, game development, and scientific computing.

Features

  • Zero-allocation value types: Stack-allocated arrays for maximum performance
  • Dual precision: Both float32 and float64 versions of all types
  • Method chaining: In-place operations return *T for fluent API
  • Immutable operations: Past-tense methods return copies without modifying originals
  • OpenGL compatible: Column-major matrices, right-handed coordinates
  • Cache-friendly: Optimized data structures for modern CPU caches
  • Well-tested: Comprehensive test coverage with benchmarks

Installation

bash
go get github.com/ungerik/go3d

Quick Start

…

Package Organization

Every type has its own sub-package and is named T. Packages under float64/ use float64 instead of float32.

Main Packages (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

Float64 Packages

All types are available in float64 precision under float64/:

  • float64/vec2, float64/vec3, float64/vec4
  • float64/mat2, float64/mat3, float64/mat4
  • float64/quaternion

Utility Packages

  • generic - Generic matrix/vector interfaces
  • hermit2 - 2D Hermite splines
  • hermit3 - 3D Hermite splines

Float32 Math Functions

This library uses github.com/chewxy/math32 for float32 math functions. Import it as:

go
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.

Core Concepts

Naming Conventions

Present tense methods modify in place and return *T for chaining:

go
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:

go
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 unchanged

Method Chaining

go
result := vec3.Zero
result.Add(&vec3.UnitX).Scale(5).Add(&vec3.UnitY)
// result = {5, 1, 0}

Matrix Layout

Matrices use column-major layout (OpenGL convention):

go
mat[column][row]

For DirectX (row-major), use Transpose().

Coordinate Systems

  • Right-handed coordinates: X × Y = Z
  • Rotation direction: Counter-clockwise (right-hand rule)
  • Angles: Radians (use math32.Pi constants)

Common Operations

Vectors

go
// 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)

Matrices

…

Quaternions

…

Performance Tips

1. Use Appropriate Precision

go
// For graphics (sufficient for most cases)
import "github.com/ungerik/go3d/vec3"

// For scientific computing (higher precision)
import "github.com/ungerik/go3d/float64/vec3"

2. Prefer In-Place Operations

go
// ✓ Good: Minimal allocations
v.Scale(2).Add(&other)

// ✗ Less efficient: Creates copies
result := v.Scaled(2).Added(&other)

3. Use LengthSqr for Comparisons

go
// ✓ Faster: No sqrt
thresholdSq := threshold * threshold
if v.LengthSqr()  1 { t = 1 }

    // Smooth interpolation
    return quaternion.Slerp(&start, &end, t)
}

Billboard Matrix

Make objects always face the camera:

…

Design Philosophy

The package is designed for performance over convenience where necessary:

  • Pointer arguments: Reduces allocations and copies
  • Stack allocation: Value types (arrays) avoid GC pressure
  • Column-major matrices: Direct GPU upload without transpose
  • Method chaining: Fluent API without sacrificing performance

Performance comparison (see mat4/mat4_test.go):

bash
cd mat4
go test -bench=BenchmarkMulAddVec4_PassBy*

Contributing

Contributions are welcome! Please ensure:

  1. All tests pass: go test ./...
  2. Code is formatted: go fmt ./...
  3. Documentation is updated
  4. Benchmarks show no regressions

License

MIT - see LICENSE file for details.

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Go

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言