cione-comp-lib
Version:
`$ yarn add cione-comp-lib` 或者 `$ npm i cione-comp-lib -S`
171 lines (170 loc) • 5.39 kB
JavaScript
import Texture from "./Texture.mjs";
import glenum from "./core/glenum.mjs";
import util from "./core/util.mjs";
import mathUtil from "./math/util.mjs";
import vendor from "./core/vendor.mjs";
var isPowerOfTwo = mathUtil.isPowerOfTwo;
var targetList = ["px", "nx", "py", "ny", "pz", "nz"];
var TextureCube = Texture.extend(function() {
return {
image: {
px: null,
nx: null,
py: null,
ny: null,
pz: null,
nz: null
},
pixels: {
px: null,
nx: null,
py: null,
ny: null,
pz: null,
nz: null
},
mipmaps: []
};
}, {
textureType: "textureCube",
update: function(renderer) {
var _gl = renderer.gl;
_gl.bindTexture(_gl.TEXTURE_CUBE_MAP, this._cache.get("webgl_texture"));
this.updateCommon(renderer);
var glFormat = this.format;
var glType = this.type;
_gl.texParameteri(_gl.TEXTURE_CUBE_MAP, _gl.TEXTURE_WRAP_S, this.getAvailableWrapS());
_gl.texParameteri(_gl.TEXTURE_CUBE_MAP, _gl.TEXTURE_WRAP_T, this.getAvailableWrapT());
_gl.texParameteri(_gl.TEXTURE_CUBE_MAP, _gl.TEXTURE_MAG_FILTER, this.getAvailableMagFilter());
_gl.texParameteri(_gl.TEXTURE_CUBE_MAP, _gl.TEXTURE_MIN_FILTER, this.getAvailableMinFilter());
var anisotropicExt = renderer.getGLExtension("EXT_texture_filter_anisotropic");
if (anisotropicExt && this.anisotropic > 1) {
_gl.texParameterf(_gl.TEXTURE_CUBE_MAP, anisotropicExt.TEXTURE_MAX_ANISOTROPY_EXT, this.anisotropic);
}
if (glType === 36193) {
var halfFloatExt = renderer.getGLExtension("OES_texture_half_float");
if (!halfFloatExt) {
glType = glenum.FLOAT;
}
}
if (this.mipmaps.length) {
var width = this.width;
var height = this.height;
for (var i = 0; i < this.mipmaps.length; i++) {
var mipmap = this.mipmaps[i];
this._updateTextureData(_gl, mipmap, i, width, height, glFormat, glType);
width /= 2;
height /= 2;
}
} else {
this._updateTextureData(_gl, this, 0, this.width, this.height, glFormat, glType);
if (!this.NPOT && this.useMipmap) {
_gl.generateMipmap(_gl.TEXTURE_CUBE_MAP);
}
}
_gl.bindTexture(_gl.TEXTURE_CUBE_MAP, null);
},
_updateTextureData: function(_gl, data, level, width, height, glFormat, glType) {
for (var i = 0; i < 6; i++) {
var target = targetList[i];
var img = data.image && data.image[target];
if (img) {
_gl.texImage2D(_gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, level, glFormat, glFormat, glType, img);
} else {
_gl.texImage2D(_gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, level, glFormat, width, height, 0, glFormat, glType, data.pixels && data.pixels[target]);
}
}
},
generateMipmap: function(renderer) {
var _gl = renderer.gl;
if (this.useMipmap && !this.NPOT) {
_gl.bindTexture(_gl.TEXTURE_CUBE_MAP, this._cache.get("webgl_texture"));
_gl.generateMipmap(_gl.TEXTURE_CUBE_MAP);
}
},
bind: function(renderer) {
renderer.gl.bindTexture(renderer.gl.TEXTURE_CUBE_MAP, this.getWebGLTexture(renderer));
},
unbind: function(renderer) {
renderer.gl.bindTexture(renderer.gl.TEXTURE_CUBE_MAP, null);
},
isPowerOfTwo: function() {
if (this.image.px) {
return isPowerOfTwo(this.image.px.width) && isPowerOfTwo(this.image.px.height);
} else {
return isPowerOfTwo(this.width) && isPowerOfTwo(this.height);
}
},
isRenderable: function() {
if (this.image.px) {
return isImageRenderable(this.image.px) && isImageRenderable(this.image.nx) && isImageRenderable(this.image.py) && isImageRenderable(this.image.ny) && isImageRenderable(this.image.pz) && isImageRenderable(this.image.nz);
} else {
return !!(this.width && this.height);
}
},
load: function(imageList, crossOrigin) {
var loading = 0;
var self = this;
util.each(imageList, function(src, target) {
var image = vendor.createImage();
if (crossOrigin) {
image.crossOrigin = crossOrigin;
}
image.onload = function() {
loading--;
if (loading === 0) {
self.dirty();
self.trigger("success", self);
}
};
image.onerror = function() {
loading--;
};
loading++;
image.src = src;
self.image[target] = image;
});
return this;
}
});
Object.defineProperty(TextureCube.prototype, "width", {
get: function() {
if (this.image && this.image.px) {
return this.image.px.width;
}
return this._width;
},
set: function(value) {
if (this.image && this.image.px) {
console.warn("Texture from image can't set width");
} else {
if (this._width !== value) {
this.dirty();
}
this._width = value;
}
}
});
Object.defineProperty(TextureCube.prototype, "height", {
get: function() {
if (this.image && this.image.px) {
return this.image.px.height;
}
return this._height;
},
set: function(value) {
if (this.image && this.image.px) {
console.warn("Texture from image can't set height");
} else {
if (this._height !== value) {
this.dirty();
}
this._height = value;
}
}
});
function isImageRenderable(image) {
return image.width > 0 && image.height > 0;
}
var TextureCube$1 = TextureCube;
export { TextureCube$1 as default };