Skip to content

Instantly share code, notes, and snippets.

View patskovn's full-sized avatar

Nikita patskovn

View GitHub Profile
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)]]) {
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,
struct SceneConstants {
...
float t;
};
vertex VertexOut vertex_main(uint vertexID [[vertex_id]],
constant VertexIn *vertices [[buffer(0)]],
constant ModelConstants &modelConstants [[buffer(1)]],
constant SceneConstants &sceneConstants [[buffer(2)]]) {
struct SceneConstants {
simd_float4x4 viewMatrix;
simd_float4x4 projection;
float noiseDencity;
float noiseStrength;
};
vertex VertexOut vertex_main(uint vertexID [[vertex_id]],
constant VertexIn *vertices [[buffer(0)]],
constant ModelConstants &modelConstants [[buffer(1)]],
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,
struct SceneConstants {
simd_float4x4 viewMatrix;
simd_float4x4 projection;
};
vertex VertexOut vertex_main(uint vertexID [[vertex_id]],
constant VertexIn *vertices [[buffer(0)]],
constant ModelConstants &modelConstants [[buffer(1)]],
constant SceneConstants &sceneConstants [[buffer(2)]]) {
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)
struct ModelConstants {
simd_float4x4 modelMatrix;
}
vertex VertexOut vertex_main(uint vertexID [[vertex_id]],
constant VertexIn *vertices [[buffer(0)]],
constant ModelConstants &modelConstants [[buffer(1)]],
constant SceneConstants &sceneConstants [[buffer(2)]]) {
VertexIn in = vertices[vertexID];
VertexOut out;
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()!
// 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))