meta-next
Version:
96 lines (78 loc) • 2 kB
JavaScript
import Node from "./Node"
import Vector4 from "../math/Vector4"
import Mesh from "../mesh/Mesh"
import Resources from "../resources/Resources"
import Material from "../resources/Material"
import DrawCommand from "../renderer/DrawCommand"
import spriteVS from "../../shaders/sprite.vs"
import spriteFS from "../../shaders/sprite.fs"
let material = null
class RenderNode extends Node
{
constructor()
{
super()
if(!material) {
material = new Material({ vertexSource: spriteVS, fragmentSource: spriteFS })
}
this._texture = null
this.color = new Vector4(1, 1, 1, 1)
const mesh = new Mesh(defaultBuffer, defaultIndices)
const uniforms = Object.assign({
color: this.color
}, material.uniforms)
this.drawCommand = new DrawCommand(this._transform, mesh, material, uniforms)
}
updateMesh() {}
set texture(texture)
{
if(typeof texture === "string") {
const newTexture = Resources.get(texture)
if(!newTexture) {
console.warn(`(Sprite.texture) Could not find resource with id: ${texture}`)
texture = null
}
else {
texture = newTexture
}
}
this._texture = texture
if(texture) {
this.size.set(texture.width, texture.height)
this.drawCommand.uniforms.albedo = texture.instance
}
else {
this.size.set(1, 1)
this.drawCommand.uniforms.albedo = null
}
this.needUpdateMesh = true
}
get texture() {
return this._texture
}
set material(material) {
this.drawCommand.material = material
}
get material() {
return this.drawCommand.material
}
tint(r, g, b) {
this.color.set(r, g, b, this.color.w)
}
set alpha(value) {
this.color.w = value
}
get alpha() {
return this.color.w
}
}
const defaultBuffer = new Float32Array([
1.0, 1.0, 1.0, 1.0,
0.0, 1.0, 0.0, 1.0,
0.0, 0.0, 0.0, 0.0,
1.0, 0.0, 1.0, 0.0
])
const defaultIndices = new Uint16Array([
0, 2, 1, 0, 3, 2
])
export default RenderNode