meta-next
Version:
88 lines (73 loc) • 2 kB
JavaScript
import Engine from "../Engine"
import Renderer from "./Renderer"
import DrawCommand from "./DrawCommand"
import Material from "../resources/Material"
import Mesh from "../mesh/Mesh"
import Vector3 from "../math/Vector3"
import debugVS from "../../shaders/debug.vs"
import debugFS from "../../shaders/debug.fs"
let material = null
const color = new Vector3(1, 1, 1)
class DebugRenderer extends Renderer
{
constructor()
{
super()
this.mesh = null
this.uniforms = { color }
this.drawCommand = null
}
setup()
{
if(!material) {
material = new Material({ vertexSource: debugVS, fragmentSource: debugFS })
}
this.mesh = new Mesh(tempBuffer, null)
this.drawCommand = new DrawCommand(null, this.mesh, material, this.uniforms, Engine.gl.LINE_STRIP)
}
render(entity, r, g, b)
{
if(r === undefined) { r = 1 }
if(g === undefined) { g = 1 }
if(b === undefined) { b = 1 }
if(entity.needUpdateOffset) {
this.updateMesh(entity)
}
color.set(r, g, b)
this.drawCommand.transform = entity.transform
this.drawCommand.mesh = entity.drawCommand.mesh
super.render(this.drawCommand)
if(entity.children) {
const children = entity.children
for(let n = 0; n < children.length; n++) {
this.render(children[n])
}
}
}
updateMesh(entity)
{
const gl = Engine.gl
const pivot = entity._pivot
const sizeX = entity._size.x
const sizeY = entity._size.y
const offsetX = -sizeX * pivot.x
const offsetY = -sizeY * pivot.y
tempBuffer[0] = sizeX + offsetX
tempBuffer[1] = sizeY + offsetY
tempBuffer[2] = offsetX
tempBuffer[3] = sizeY + offsetY
tempBuffer[4] = offsetX
tempBuffer[5] = offsetY
tempBuffer[6] = sizeX + offsetX
tempBuffer[7] = offsetY
this.mesh.upload(tempBuffer)
}
}
const tempBuffer = new Float32Array([
1.0, 1.0,
0.0, 1.0,
0.0, 0.0,
1.0, 0.0
])
const instance = new DebugRenderer()
export default instance