Skip to content

Commit

Permalink
Texture2D now accepts preMultiplyAlpha argument, enables by default. …
Browse files Browse the repository at this point in the history
…Texture2D now by default disables mip-mapping for textures that are not derived from images.
  • Loading branch information
kbirk committed Feb 17, 2016
1 parent d5566af commit b36e261
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 28 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "esper",
"version": "0.6.12",
"version": "0.6.13",
"homepage": "https://github.com/kbirk/esper",
"authors": [
"kbirk <birk.kevin@gmail.com>"
Expand Down
42 changes: 29 additions & 13 deletions build/esper.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/esper.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "esper",
"version": "0.6.12",
"version": "0.6.13",
"description": "A low-level WebGL rendering framework",
"author": "Kevin Birk <birk.kevin@gmail.com>",
"main": "./src/exports.js",
Expand Down
40 changes: 28 additions & 12 deletions src/core/Texture2D.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
this.filter = spec.filter || "LINEAR";
this.invertY = spec.invertY !== undefined ? spec.invertY : true;
this.mipMap = spec.mipMap !== undefined ? spec.mipMap : true;
this.preMultiplyAlpha = spec.preMultiplyAlpha !== undefined ? spec.preMultiplyAlpha : true;
// buffer the texture based on arguments
if ( spec.image ) {
// use existing Image object
Expand All @@ -99,28 +100,38 @@
};
image.src = spec.url;
} else {
// assume this texture will be rendered to. In this case disable
// mipmapping, there is no need and it will only introduce very
// peculiar rendering bugs in which the texture 'transforms' at
// certain angles / distances to the mipmapped (empty) portions.
this.mipMap = false;
// buffer data
if ( spec.format === "DEPTH_COMPONENT" ) {
// depth texture
var depthTextureExt = WebGLContext.checkExtension( "WEBGL_depth_texture" );
if( !depthTextureExt ) {
console.log( "Cannot create Texture2D of format " +
console.warn( "Cannot create Texture2D of format " +
"gl.DEPTH_COMPONENT as WEBGL_depth_texture is " +
"unsupported by this browser, command ignored" );
return;
}
// set format
this.format = spec.format;
if ( !spec.type ||
spec.type === "UNSIGNED_SHORT" ||
spec.type === "UNSIGNED_INT" ) {
// set type
if ( !spec.type ) {
// default to unsigned int for higher precision
this.type = "UNSIGNED_INT";
} else if ( spec.type === "UNSIGNED_SHORT" || spec.type === "UNSIGNED_INT" ) {
// set to accept types
this.type = spec.type;
} else {
console.log( "Depth textures do not support type'" +
spec.type + "', defaulting to 'UNSIGNED_SHORT'.");
this.type = "UNSIGNED_SHORT";
// error
console.warn( "Depth textures do not support type'" +
spec.type + "', defaulting to 'UNSIGNED_INT'.");
// default
this.type = "UNSIGNED_INT";
}
// disable mip mapping for depth texture
this.mipMap = false;
// always disable mip mapping for depth texture
} else {
// other
this.format = spec.format || "RGBA";
Expand Down Expand Up @@ -185,13 +196,17 @@
Texture2D.prototype.bufferData = function( data, width, height ) {
var gl = this.gl;
this.push();
// invert y if specified
gl.pixelStorei( gl.UNPACK_FLIP_Y_WEBGL, this.invertY );
// premultiple alpha if specified
gl.pixelStorei( gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, this.preMultiplyAlpha );
// buffer texture based on type of data
if ( data instanceof HTMLImageElement ) {
// set dimensions before resizing
// set dimensions of original image before resizing
this.width = data.width;
this.height = data.height;
data = ensurePowerOfTwo( data );
this.image = data;
gl.pixelStorei( gl.UNPACK_FLIP_Y_WEBGL, this.invertY );
gl.texImage2D(
gl.TEXTURE_2D,
0, // level
Expand All @@ -203,7 +218,6 @@
this.data = data;
this.width = width || this.width;
this.height = height || this.height;
gl.pixelStorei( gl.UNPACK_FLIP_Y_WEBGL, this.invertY );
gl.texImage2D(
gl.TEXTURE_2D,
0, // level
Expand Down Expand Up @@ -280,6 +294,8 @@
*/
Texture2D.prototype.resize = function( width, height ) {
if ( this.image ) {
// there is no need to ever resize a texture that is based
// of an actual image. That is what sampling is for.
console.error( "Cannot resize image based Texture2D" );
return;
}
Expand Down

0 comments on commit b36e261

Please sign in to comment.