MetalByTutorials/12-environment/skybox.playground/Sources/MathLibrary.swift

131 lines
4.0 KiB
Swift

/**
* Copyright (c) 2018 Razeware LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* Notwithstanding the foregoing, you may not use, copy, modify, merge, publish,
* distribute, sublicense, create a derivative work, and/or sell copies of the
* Software in any work that is designed, intended, or marketed for pedagogical or
* instructional purposes related to programming, coding, application development,
* or information technology. Permission for such use, copying, modification,
* merger, publication, distribution, sublicensing, creation of derivative works,
* or sale is expressly withheld.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
import simd
let π = Float.pi
public typealias int2 = SIMD2<Int32>
public typealias float2 = SIMD2<Float>
public typealias float3 = SIMD3<Float>
public typealias float4 = SIMD4<Float>
extension Float {
var radiansToDegrees: Float {
return (self / π) * 180
}
var degreesToRadians: Float {
return (self / 180) * π
}
}
extension float4x4 {
init(translation: float3) {
let matrix = float4x4(
[ 1, 0, 0, 0],
[ 0, 1, 0, 0],
[ 0, 0, 1, 0],
[translation.x, translation.y, translation.z, 1]
)
self = matrix
}
init(scaling: float3) {
let matrix = float4x4(
[scaling.x, 0, 0, 0],
[ 0, scaling.y, 0, 0],
[ 0, 0, scaling.z, 0],
[ 0, 0, 0, 1]
)
self = matrix
}
public init(scaling: Float) {
self = matrix_identity_float4x4
columns.3.w = 1 / scaling
}
init(rotationX angle: Float) {
let matrix = float4x4(
[1, 0, 0, 0],
[0, cos(angle), sin(angle), 0],
[0, -sin(angle), cos(angle), 0],
[0, 0, 0, 1]
)
self = matrix
}
init(rotationY angle: Float) {
let matrix = float4x4(
[cos(angle), 0, -sin(angle), 0],
[ 0, 1, 0, 0],
[sin(angle), 0, cos(angle), 0],
[ 0, 0, 0, 1]
)
self = matrix
}
init(rotationZ angle: Float) {
let matrix = float4x4(
[ cos(angle), sin(angle), 0, 0],
[-sin(angle), cos(angle), 0, 0],
[ 0, 0, 1, 0],
[ 0, 0, 0, 1]
)
self = matrix
}
init(rotation angle: float3) {
let rotationX = float4x4(rotationX: angle.x)
let rotationY = float4x4(rotationY: angle.y)
let rotationZ = float4x4(rotationZ: angle.z)
self = rotationX * rotationY * rotationZ
}
public static func identity() -> float4x4 {
let matrix:float4x4 = matrix_identity_float4x4
return matrix
}
public init(projectionFov fov: Float, near: Float, far: Float, aspect: Float) {
let y = 1 / tan(fov * 0.5)
let x = y / aspect
let z = far / (near - far)
self.init(columns: (
float4( x, 0, 0, 0),
float4( 0, y, 0, 0),
float4( 0, 0, z, -1),
float4( 0, 0, z * near, 0)
))
}
}