2024-09-06 06:37:19 +10:00
|
|
|
import simd
|
|
|
|
|
|
|
|
public struct ModelBatch {
|
|
|
|
private let _renderer: Renderer
|
|
|
|
private var _active = false
|
|
|
|
private var _cam: Camera!
|
|
|
|
private var _env: Environment!
|
2024-09-06 23:07:53 +10:00
|
|
|
private var _prev: ModelInstance!
|
|
|
|
private var _instances: [Instance]
|
2024-09-06 06:37:19 +10:00
|
|
|
|
|
|
|
internal init(_ renderer: Renderer) {
|
|
|
|
self._renderer = renderer
|
2024-09-06 23:07:53 +10:00
|
|
|
self._instances = Array()
|
2024-09-06 06:37:19 +10:00
|
|
|
}
|
|
|
|
|
2024-09-06 23:07:53 +10:00
|
|
|
public mutating func begin(camera: Camera, environment: Environment) {
|
2024-09-06 06:37:19 +10:00
|
|
|
self._active = true
|
|
|
|
self._cam = camera
|
|
|
|
self._env = environment
|
2024-09-06 23:07:53 +10:00
|
|
|
self._prev = nil
|
2024-09-06 23:54:23 +10:00
|
|
|
self._renderer.setupBatch(material: Game.material, environment: environment, camera: camera)
|
2024-09-06 06:37:19 +10:00
|
|
|
}
|
|
|
|
|
2024-09-06 23:07:53 +10:00
|
|
|
private mutating func flush() {
|
|
|
|
assert(self._instances.count > 0)
|
2024-09-06 23:54:23 +10:00
|
|
|
self._renderer.submitBatch(mesh: self._prev.mesh, instances: self._instances)
|
2024-09-06 23:07:53 +10:00
|
|
|
self._instances.removeAll(keepingCapacity: true)
|
|
|
|
self._prev = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
public mutating func end() {
|
|
|
|
if !self._instances.isEmpty {
|
|
|
|
self.flush()
|
|
|
|
}
|
|
|
|
self._cam = nil
|
|
|
|
self._env = nil
|
2024-09-06 06:37:19 +10:00
|
|
|
self._active = false
|
|
|
|
}
|
|
|
|
|
2024-09-06 23:07:53 +10:00
|
|
|
public mutating func draw(_ model: ModelInstance, position: SIMD3<Float>, color: Color<Float> = .white
|
2024-09-06 06:37:19 +10:00
|
|
|
) {
|
|
|
|
self.draw(model, world: .translate(position), color: color)
|
|
|
|
}
|
|
|
|
|
2024-09-06 23:07:53 +10:00
|
|
|
public mutating func draw(_ model: ModelInstance,
|
2024-09-06 06:37:19 +10:00
|
|
|
position: SIMD3<Float>, scale: Float, rotation: simd_quatf,
|
|
|
|
color: Color<Float> = .white
|
|
|
|
) {
|
|
|
|
self.draw(model, position: position, scale: .init(repeating: scale), rotation: rotation, color: color)
|
|
|
|
}
|
|
|
|
|
2024-09-06 23:07:53 +10:00
|
|
|
public mutating func draw(_ model: ModelInstance,
|
2024-09-06 06:37:19 +10:00
|
|
|
position: SIMD3<Float>, scale: SIMD3<Float>, rotation: simd_quatf,
|
|
|
|
color: Color<Float> = .white
|
|
|
|
) {
|
|
|
|
let world =
|
|
|
|
.translate(position) *
|
|
|
|
simd_float4x4(rotation) *
|
|
|
|
.scale(scale)
|
|
|
|
self.draw(model, world: world, color: color)
|
|
|
|
}
|
|
|
|
|
2024-09-06 23:07:53 +10:00
|
|
|
public mutating func draw(_ model: ModelInstance, world: simd_float4x4, color: Color<Float> = .white) {
|
2024-09-06 06:37:19 +10:00
|
|
|
assert(self._active)
|
2024-09-06 23:07:53 +10:00
|
|
|
if self._prev == nil {
|
|
|
|
self._prev = model
|
|
|
|
} else if model != self._prev {
|
|
|
|
self.flush()
|
|
|
|
self._prev = model
|
|
|
|
}
|
|
|
|
|
|
|
|
self._instances.append(.init(
|
|
|
|
world: world,
|
|
|
|
color: color.linear))
|
2024-09-06 06:37:19 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
internal struct Instance {
|
2024-09-06 23:07:53 +10:00
|
|
|
let world: simd_float4x4
|
2024-09-06 06:37:19 +10:00
|
|
|
let color: Color<Float>
|
|
|
|
|
2024-09-06 23:07:53 +10:00
|
|
|
init(world: simd_float4x4, color: Color<Float> = .white) {
|
|
|
|
self.world = world
|
2024-09-06 06:37:19 +10:00
|
|
|
self.color = color
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//TODO: delet
|
2024-09-06 23:07:53 +10:00
|
|
|
public struct ModelInstance: Hashable {
|
2024-09-06 06:37:19 +10:00
|
|
|
let mesh: RendererMesh
|
|
|
|
let material: Material
|
|
|
|
}
|