initial lighting

This commit is contained in:
2024-08-18 01:10:25 +10:00
parent 55d47a2d2f
commit f7255c473c
3 changed files with 26 additions and 10 deletions

View File

@ -344,13 +344,16 @@ public class Renderer {
assert(self._encoder != nil, "batch can't be called outside of a frame being rendered")
assert(instances.count < 52)
var uniforms = ShaderUniforms(projView: camera.viewProjection)
var uniforms = ShaderUniforms(
projView: camera.viewProjection,
directionalLight: normalize(.init(0.75, -1, 0.5)))
let instances = instances.map { (instance: Instance) -> ShaderInstance in
ShaderInstance(
model:
.translate(instance.position) *
matrix_float4x4(instance.rotation) *
.scale(instance.scale),
let model =
.translate(instance.position) *
matrix_float4x4(instance.rotation) *
.scale(instance.scale)
return ShaderInstance(
model: model, normalModel: model.inverse.transpose,
color: SIMD4(Color<UInt8>(instance.color)))
}
@ -361,6 +364,9 @@ public class Renderer {
self._encoder.setVertexBytes(&uniforms,
length: MemoryLayout<ShaderUniforms>.stride,
index: ShaderInputIdx.uniforms.rawValue)
self._encoder.setFragmentBytes(&uniforms,
length: MemoryLayout<ShaderUniforms>.stride,
index: ShaderInputIdx.uniforms.rawValue)
self._encoder.drawIndexedPrimitives(
type: .triangle,