Skip to content

Commit

Permalink
Merge pull request #364 from tsherif/texture-preload
Browse files Browse the repository at this point in the history
Texture preload
  • Loading branch information
xeolabs committed Jun 11, 2015
2 parents ddf2e91 + 410aad4 commit 15c175f
Show file tree
Hide file tree
Showing 10 changed files with 460 additions and 78 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
.DS_Store
150 changes: 126 additions & 24 deletions api/latest/scenejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -12862,39 +12862,83 @@ new (function () {
*/

var src = layer.uri || layer.src;
var preloadSrc = layer.preloadURI || layer.preloadSrc;
var preloadColor = layer.preloadColor || { r: 0.57735, g: 0.57735, b: 0.57735 };
preloadColor.a = preloadColor.a === undefined ? 1 : preloadColor.a;

preloadColor = new Uint8Array([
Math.floor(preloadColor.r * 255),
Math.floor(preloadColor.g * 255),
Math.floor(preloadColor.b * 255),
Math.floor(preloadColor.a * 255)
]);

var taskId = SceneJS_sceneStatusModule.taskStarted(this, "Loading texture");

var image = new Image();

image.onload = function () {
var texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
var texture = gl.createTexture();

var maxTextureSize = SceneJS_configsModule.configs.maxTextureSize;
if (maxTextureSize) {
image = SceneJS._webgl.clampImageSize(image, maxTextureSize);
}
var loaded = false;
var taskFinished = false;

gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 1, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, preloadColor);
self._setLayerTexture(gl, layer, texture);

if (preloadSrc) {
var preloadImage = new Image();

preloadImage.onload = function () {
if (!loaded) {
self._setTextureImage(gl, texture, preloadImage);
self._setLayerTexture(gl, layer, texture);
SceneJS_sceneStatusModule.taskFinished(taskId);
taskFinished = true;
}
};

self._fetchImage(preloadImage, preloadSrc);
}

gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, self._ensureImageSizePowerOfTwo(image));
image.onload = function () {
self._setTextureImage(gl, texture, image);
self._setLayerTexture(gl, layer, texture);
SceneJS_sceneStatusModule.taskFinished(taskId);
self._engine.display.imageDirty = true;
if (!taskFinished) {
SceneJS_sceneStatusModule.taskFinished(taskId);
}
loaded = true;
};

image.onerror = function () {
SceneJS_sceneStatusModule.taskFailed(taskId);
};

if (src.indexOf("data") == 0) { // Image data
image.src = src;
} else { // Image file
image.crossOrigin = "Anonymous";
image.src = src;
}
self._fetchImage(image, src);
}
};

SceneJS.Texture.prototype._fetchImage = function (image, src) {
if (src.indexOf("data") == 0) { // Image data
image.src = src;
} else { // Image file
image.crossOrigin = "Anonymous";
image.src = src;
}
};

SceneJS.Texture.prototype._setTextureImage = function (gl, texture, image) {
gl.bindTexture(gl.TEXTURE_2D, texture);

var maxTextureSize = SceneJS_configsModule.configs.maxTextureSize;
if (maxTextureSize) {
image = SceneJS._webgl.clampImageSize(image, maxTextureSize);
}

gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, this._ensureImageSizePowerOfTwo(image));
this._engine.display.imageDirty = true;
};

SceneJS.Texture.prototype._ensureImageSizePowerOfTwo = function (image) {

if (!this._isPowerOfTwo(image.width) || !this._isPowerOfTwo(image.height)) {
Expand Down Expand Up @@ -13241,13 +13285,16 @@ new (function () {

buildMatrix.call(this._core);


if (params.src) { // Load from URL
var texture = this._initTexture(params.preloadColor);
this._core.src = params.src;
this._loadTexture(params.src);
this._loadTexture(texture, params.src, params.preloadSrc);

} else if (params.image) { // Create from image
var texture = this._initTexture(params.preloadColor);
this._core.image = params.image;
this._initTexture(params.image);
this._setTextureImage(texture, params.image);

} else if (params.target) { // Render to this texture
this.getScene().getNode(params.target,
Expand All @@ -13257,11 +13304,14 @@ new (function () {
}

this._core.webglRestored = function () {

if (self._core.image) {
self._initTexture(self._core.image);
var texture = this._initTexture(params.preloadColor);
self._setTextureImage(texture, self._core.image);

} else if (self._core.src) {
self._loadTexture(self._core.src);
var texture = this._initTexture(params.preloadColor);
self._loadTexture(texture, self._core.src);

} else if (self._core.target) {
// self.getScene().getNode(params.target,
Expand Down Expand Up @@ -13298,18 +13348,64 @@ new (function () {
this._matrixDirty = false;
}

SceneJS.TextureMap.prototype._loadTexture = function (src) {
SceneJS.TextureMap.prototype._initTexture = function (preloadColor) {
var gl = this._engine.canvas.gl;

preloadColor = preloadColor || { r: 0.57735, g: 0.57735, b: 0.57735 };
preloadColor.a = preloadColor.a === undefined ? 1 : preloadColor.a;

preloadColor = new Uint8Array([
Math.floor(preloadColor.r * 255),
Math.floor(preloadColor.g * 255),
Math.floor(preloadColor.b * 255),
Math.floor(preloadColor.a * 255)
]);

var texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 1, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, preloadColor);
this._setCoreTexture(texture);

return texture;
};

SceneJS.TextureMap.prototype._loadTexture = function (texture, src, preloadSrc) {
var self = this;
var taskId = SceneJS_sceneStatusModule.taskStarted(this, "Loading texture");
var image = new Image();
var loaded = false;
var taskFinished = false;

if (preloadSrc) {
var preloadImage = new Image();

preloadImage.onload = function () {
if (!loaded) {
self._setTextureImage(texture, preloadImage);
SceneJS_sceneStatusModule.taskFinished(taskId);
taskFinished = true;
self._engine.display.imageDirty = true;
}
};

this._fetchImage(preloadImage, preloadSrc);
}

image.onload = function () {
self._initTexture(image);
SceneJS_sceneStatusModule.taskFinished(taskId);
self._setTextureImage(texture, image);
if (!taskFinished) {
SceneJS_sceneStatusModule.taskFinished(taskId);
}
loaded = true;
self._engine.display.imageDirty = true;
};
image.onerror = function () {
SceneJS_sceneStatusModule.taskFailed(taskId);
};
this._fetchImage(image, src);
};

SceneJS.TextureMap.prototype._fetchImage = function (image, src) {
if (src.indexOf("data") == 0) { // Image data
image.src = src;
} else { // Image file
Expand All @@ -13318,7 +13414,7 @@ new (function () {
}
};

SceneJS.TextureMap.prototype._initTexture = function (image) {
SceneJS.TextureMap.prototype._setTextureImage = function (texture, image) {

var gl = this._engine.canvas.gl;

Expand All @@ -13334,6 +13430,12 @@ new (function () {

this._core.image = image;

this._setCoreTexture(texture);
};

SceneJS.TextureMap.prototype._setCoreTexture = function (texture) {
var gl = this._engine.canvas.gl;

this._core.texture = new SceneJS._webgl.Texture2D(gl, {
texture: texture, // WebGL texture object
minFilter: this._getGLOption("minFilter", gl.LINEAR_MIPMAP_NEAREST),
Expand Down
6 changes: 3 additions & 3 deletions api/latest/scenejs.min.js

Large diffs are not rendered by default.

Loading

0 comments on commit 15c175f

Please sign in to comment.