This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct SceneConstants { | |
... | |
float3 bottomColor; | |
float bottomColorBorder; | |
}; | |
vertex VertexOut vertex_main(uint vertexID [[vertex_id]], | |
constant VertexIn *vertices [[buffer(0)]], | |
constant ModelConstants &modelConstants [[buffer(1)]], | |
constant SceneConstants &sceneConstants [[buffer(2)]]) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var sceneConstants: SceneConstants = ... | |
public func draw(in view: MTKView) { | |
guard let drawable = view.currentDrawable else { return } | |
... | |
let deltaTime = 1 / Float(view.preferredFramesPerSecond) | |
sceneConstants.t += deltaTime | |
commandEncoder.setVertexBytes(&sceneConstants, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Camera { | |
private var _zoom: Float = 45.0 | |
var position: SIMD3<Float> = .init(0, 0, 3) | |
var rotation: SIMD3<Float> = .init(repeating: 0) | |
var scale: SIMD3<Float> = .init(repeating: 1) | |
var projectionMatrix: matrix_float4x4 { | |
return Matrix.perspective(fov: Angle(degrees: Double(self._zoom)), | |
aspectRatio: Renderer.aspectRatio, | |
near: 0.1, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func draw(in view: MTKView) { | |
guard let drawable = view.currentDrawable else { return } | |
let renderPassDescriptor = MTLRenderPassDescriptor() | |
renderPassDescriptor.colorAttachments[0].texture = drawable.texture | |
renderPassDescriptor.colorAttachments[0].loadAction = .clear | |
renderPassDescriptor.colorAttachments[0].storeAction = .store | |
renderPassDescriptor.colorAttachments[0].clearColor = MTLClearColorMake(0.0, 0.0, 0.0, 1.0) | |
let commandBuffer = commandQueue.makeCommandBuffer()! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Color is (r, g, b, a) components | |
static func complexPlane() -> [VertexIn] { | |
var result: [VertexIn] = [] | |
let size = SIMD2<Float>(10, 2) | |
for x in stride(from: 0, to: size.x, by: 1) { | |
for y in stride(from: 0, to: size.y, by: 1) { | |
let position = SIMD3(.init(x / size.x, y).metal, 0) | |
result.append( | |
.init(position: .init(position, 1), | |
color: .init(1, 1, 1, 1)) |
NewerOlder