Skip to content

Instantly share code, notes, and snippets.

@patskovn
Last active March 3, 2024 20:18
Show Gist options
  • Save patskovn/0963941892ab3ce3a5c357716f8afb75 to your computer and use it in GitHub Desktop.
Save patskovn/0963941892ab3ce3a5c357716f8afb75 to your computer and use it in GitHub Desktop.
enum Axis {
case x
case y
case z
var simd: SIMD3<Float> {
switch self {
case .x: return .init(1, 0, 0)
case .y: return .init(0, 1, 0)
case .z: return .init(0, 0, 1)
}
}
}
constants.modelMatrix = matrix_identity_float4x4
* Matrix.translation(direction: position)
* Matrix.rotation(angle: .degrees(rotation.x), axis: Axis.x.simd)
* Matrix.rotation(angle: .degrees(rotation.y), axis: Axis.y.simd)
* Matrix.rotation(angle: .degrees(rotation.z), axis: Axis.z.simd)
* Matrix.scale(axis: scale)
struct Matrix {
static func translation(direction d: SIMD3<Float>) -> matrix_float4x4 {
return .init(columns: (
.init( 1, 0, 0, 0),
.init( 0, 1, 0, 0),
.init( 0, 0, 1, 0),
.init(d.x, d.y, d.z, 1)
))
}
static func scale(axis a: SIMD3<Float>) -> matrix_float4x4 {
return .init(columns: (
.init(a.x, 0, 0, 0),
.init( 0, a.y, 0, 0),
.init( 0, 0, a.z, 0),
.init( 0, 0, 0, 1)
))
}
static func rotation(angle: Angle, axis: SIMD3<Float>) -> matrix_float4x4 {
.init(simd_quatf(angle: Float(angle.radians), axis: axis))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment