webgl/public/gl/core/Material.js

46 lines
1.2 KiB
JavaScript
Raw Normal View History

2023-01-19 03:43:02 +00:00
export class Material {
static #id = 0
constructor(shader, data = {}) {
this.shader = shader
this.data = data
this.id = Material.#id
Material.#id++
}
set(data) {
this.data = data
return this
}
bind() {
this.shader.start()
for (const key in this.data) {
const value = this.data[key]
if (typeof value == 'number' && !isNaN(x)) {
if (Number.isInteger(value)) {
this.shader.loadInt(key, value)
} else {
this.shader.loadFloat(key, value)
}
} else if (typeof value == 'boolean') {
this.shader.loadBool(key, value)
} else if (typeof value == 'object') {
const name = value.constructor.name
if (name == 'Vec2') {
this.shader.loadVec2(key, value)
} else if (name == 'Vec3') {
this.shader.loadVec3(key,value)
} else if (name == 'Mat4') {
this.shader.loadMat4(key, value)
}
}
}
}
unbind() {
this.shader.stop()
}
}