mirror of
				https://github.com/GayPizzaSpecifications/voxelotl-engine.git
				synced 2025-11-04 10:59:39 +00:00 
			
		
		
		
	initial modelbatch
This commit is contained in:
		
							
								
								
									
										73
									
								
								Sources/Voxelotl/Renderer/ModelBatch.swift
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										73
									
								
								Sources/Voxelotl/Renderer/ModelBatch.swift
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,73 @@
 | 
			
		||||
import simd
 | 
			
		||||
 | 
			
		||||
public struct ModelBatch {
 | 
			
		||||
  private let _renderer: Renderer
 | 
			
		||||
  private var _active = false
 | 
			
		||||
  private var _cam: Camera!
 | 
			
		||||
  private var _env: Environment!
 | 
			
		||||
 | 
			
		||||
  internal init(_ renderer: Renderer) {
 | 
			
		||||
    self._renderer = renderer
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  //TODO: Sort, Blend
 | 
			
		||||
  mutating func begin(camera: Camera, environment: Environment) {
 | 
			
		||||
    self._active = true
 | 
			
		||||
    self._cam = camera
 | 
			
		||||
    self._env = environment
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  mutating func end() {
 | 
			
		||||
    self._active = false
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  mutating func draw(_ model: ModelInstance, position: SIMD3<Float>, color: Color<Float> = .white
 | 
			
		||||
  ) {
 | 
			
		||||
    self.draw(model, world: .translate(position), color: color)
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  mutating func draw(_ model: ModelInstance,
 | 
			
		||||
    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)
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  mutating func draw(_ model: ModelInstance,
 | 
			
		||||
    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)
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  mutating func draw(_ model: ModelInstance, world: simd_float4x4, color: Color<Float> = .white) {
 | 
			
		||||
    assert(self._active)
 | 
			
		||||
    self._renderer.draw(
 | 
			
		||||
      model: world,
 | 
			
		||||
      color: color.linear,
 | 
			
		||||
      mesh: model.mesh,
 | 
			
		||||
      material: model.material,
 | 
			
		||||
      environment: self._env,
 | 
			
		||||
      camera: self._cam)
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  internal struct Instance {
 | 
			
		||||
    let model: simd_float4x4
 | 
			
		||||
    let color: Color<Float>
 | 
			
		||||
 | 
			
		||||
    init(model: simd_float4x4, color: Color<Float> = .white) {
 | 
			
		||||
      self.model = model
 | 
			
		||||
      self.color = color
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
//TODO: delet
 | 
			
		||||
public struct ModelInstance {
 | 
			
		||||
  let mesh: RendererMesh
 | 
			
		||||
  let material: Material
 | 
			
		||||
}
 | 
			
		||||
@ -415,7 +415,11 @@ public class Renderer {
 | 
			
		||||
      indexBufferOffset: 0)
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  func batch(instances: [Instance], mesh: RendererMesh, material: Material, environment: Environment, camera: Camera) {
 | 
			
		||||
  func createModelBatch() -> ModelBatch {
 | 
			
		||||
    return ModelBatch(self)
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  func batch(instances: [ModelBatch.Instance], mesh: RendererMesh, material: Material, environment: Environment, camera: Camera) {
 | 
			
		||||
    assert(self._encoder != nil, "batch can't be called outside of a frame being rendered")
 | 
			
		||||
 | 
			
		||||
    var vertUniforms = VertexShaderUniforms(projView: camera.viewProjection)
 | 
			
		||||
@ -447,12 +451,9 @@ public class Renderer {
 | 
			
		||||
    instanceBuffer.contents().withMemoryRebound(to: VertexShaderInstance.self, capacity: numInstances) { data in
 | 
			
		||||
      for i in 0..<numInstances {
 | 
			
		||||
        let instance = instances[i]
 | 
			
		||||
        let model =
 | 
			
		||||
          .translate(instance.position) *
 | 
			
		||||
          matrix_float4x4(instance.rotation) *
 | 
			
		||||
          .scale(instance.scale)
 | 
			
		||||
        data[i] = VertexShaderInstance(
 | 
			
		||||
          model: model, normalModel: model.inverse.transpose,
 | 
			
		||||
          model: instance.model,
 | 
			
		||||
          normalModel: instance.model.inverse.transpose,
 | 
			
		||||
          color: SIMD4(instance.color))
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user