51 lines
1.2 KiB
JavaScript
51 lines
1.2 KiB
JavaScript
import Resource from '../resource'
|
|
|
|
class Texture {
|
|
static createTexture2D (gl, img) {
|
|
let tex = gl.createTexture()
|
|
gl.bindTexture(gl.TEXTURE_2D, tex)
|
|
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true)
|
|
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, img)
|
|
|
|
gl.generateMipmap(gl.TEXTURE_2D)
|
|
gl.bindTexture(gl.TEXTURE_2D, null)
|
|
|
|
let oTex = new Texture()
|
|
oTex.type = gl.TEXTURE_2D
|
|
oTex.id = tex
|
|
|
|
return oTex
|
|
}
|
|
}
|
|
|
|
class Material {
|
|
async loadTextures (gl) {
|
|
if (this.textures) {
|
|
for (let ti in this.textures) {
|
|
let tex = this.textures[ti]
|
|
if (!(tex instanceof Texture)) {
|
|
let teximg = await Resource.loadImage(tex)
|
|
let result = Texture.createTexture2D(gl, teximg)
|
|
this.textures[ti] = result
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
apply (gl, shader) {
|
|
// TODO: lighting related things
|
|
|
|
// Load textures
|
|
if (!this.textures || !this.textures.length) return
|
|
for (let i in this.textures) {
|
|
let tex = this.textures[i]
|
|
if (tex && tex instanceof Texture) {
|
|
gl.activeTexture(gl.TEXTURE0 + parseInt(i))
|
|
gl.bindTexture(tex.type, tex.id)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
export { Texture, Material }
|