Skip to content

Commit

Permalink
Merge pull request #3823 from lasalvavida/texture-map-fix
Browse files Browse the repository at this point in the history
Missing Textures Fix
  • Loading branch information
bagnell committed May 12, 2016
2 parents f7abdd4 + 2a0f13c commit ca88219
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 58 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Change Log
* Fixed infinite horizontal 2D scrolling in IE/Edge. [#3893](https://github.com/AnalyticalGraphicsInc/cesium/issues/3893)
* Fixed a bug that could cause incorrect normals to be computed for exaggerated terrain, especially for low-detail tiles. [#3904](https://github.com/AnalyticalGraphicsInc/cesium/pull/3904)
* Fixed a bug that was causing errors to be thrown when picking and terrain was enabled. [#3779](https://github.com/AnalyticalGraphicsInc/cesium/issues/3779)
* Fixed issue where labels were disappearing. [3730](https://github.com/AnalyticalGraphicsInc/cesium/issues/3730)

### 1.21 - 2016-05-02

Expand Down
62 changes: 20 additions & 42 deletions Source/Scene/TextureAtlas.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,35 +88,9 @@ define([
this._textureCoordinates = [];
this._guid = createGuid();
this._idHash = {};
this._initialSize = initialSize;

// Create initial texture and root.
this._texture = new Texture({
context : this._context,
width : initialSize.x,
height : initialSize.y,
pixelFormat : this._pixelFormat
});
this._root = new TextureAtlasNode(new Cartesian2(), new Cartesian2(initialSize.x, initialSize.y));

var that = this;
var uniformMap = {
u_texture : function() {
return that._texture;
}
};

var fs =
'uniform sampler2D u_texture;\n' +
'varying vec2 v_textureCoordinates;\n' +
'void main()\n' +
'{\n' +
' gl_FragColor = texture2D(u_texture, v_textureCoordinates);\n' +
'}\n';


this._copyCommand = this._context.createViewportQuadCommand(fs, {
uniformMap : uniformMap
});
this._root = undefined;
}

defineProperties(TextureAtlas.prototype, {
Expand Down Expand Up @@ -151,6 +125,14 @@ define([
*/
texture : {
get : function() {
if(!defined(this._texture)) {
this._texture = new Texture({
context : this._context,
width : this._initialSize.x,
height : this._initialSize.y,
pixelFormat : this._pixelFormat
});
}
return this._texture;
}
},
Expand Down Expand Up @@ -202,7 +184,6 @@ define([
var nodeBottomHalf = new TextureAtlasNode(new Cartesian2(), new Cartesian2(atlasWidth, oldAtlasHeight), textureAtlas._root, nodeBottomRight);
var nodeTopHalf = new TextureAtlasNode(new Cartesian2(0.0, oldAtlasHeight + textureAtlas._borderWidthInPixels), new Cartesian2(atlasWidth, atlasHeight));
var nodeMain = new TextureAtlasNode(new Cartesian2(), new Cartesian2(atlasWidth, atlasHeight), nodeBottomHalf, nodeTopHalf);
textureAtlas._root = nodeMain;

// Resize texture coordinates.
for (var i = 0; i < textureAtlas._textureCoordinates.length; i++) {
Expand All @@ -225,30 +206,27 @@ define([

var framebuffer = new Framebuffer({
context : context,
colorTextures : [newTexture],
colorTextures : [textureAtlas._texture],
destroyAttachments : false
});

var command = textureAtlas._copyCommand;
var renderState = {
viewport : new BoundingRectangle(0, 0, oldAtlasWidth, oldAtlasHeight)
};
command.renderState = RenderState.fromCache(renderState);

// Copy by rendering a viewport quad, instead of using Texture.copyFromFramebuffer,
// to workaround a Chrome 45 issue, https://github.com/AnalyticalGraphicsInc/cesium/issues/2997
framebuffer._bind();
command.execute(textureAtlas._context);
newTexture.copyFromFramebuffer(0, 0, 0, 0, atlasWidth, atlasHeight);
framebuffer._unBind();
framebuffer.destroy();
textureAtlas._texture = textureAtlas._texture && textureAtlas._texture.destroy();
textureAtlas._texture = newTexture;

RenderState.removeFromCache(renderState);
command.renderState = undefined;
textureAtlas._root = nodeMain;
} else {
// First image exceeds initialSize
var initialWidth = scalingFactor * (image.width + textureAtlas._borderWidthInPixels);
var initialHeight = scalingFactor * (image.height + textureAtlas._borderWidthInPixels);
if(initialWidth < textureAtlas._initialSize.x) {
initialWidth = textureAtlas._initialSize.x;
}
if(initialHeight < textureAtlas._initialSize.y) {
initialHeight = textureAtlas._initialSize.y;
}
textureAtlas._texture = textureAtlas._texture && textureAtlas._texture.destroy();
textureAtlas._texture = new Texture({
context : textureAtlas._context,
Expand Down
32 changes: 16 additions & 16 deletions Specs/Scene/TextureAtlasSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ void main() {\n\
expect(atlas.borderWidthInPixels).toEqual(0);

var texture = atlas.texture;
var atlasWidth = 1.0;
var atlasHeight = 1.0;
var atlasWidth = 2.0;
var atlasHeight = 2.0;
expect(texture.pixelFormat).toEqual(PixelFormat.RGBA);
expect(texture.width).toEqual(atlasWidth);
expect(texture.height).toEqual(atlasHeight);
Expand Down Expand Up @@ -228,8 +228,8 @@ void main() {\n\

var texture = atlas.texture;

var atlasWidth = 1.0;
var atlasHeight = 5.0;
var atlasWidth = 2.0;
var atlasHeight = 8.0;
expect(texture.width).toEqual(atlasWidth);
expect(texture.height).toEqual(atlasHeight);

Expand Down Expand Up @@ -451,8 +451,8 @@ void main() {\n\
var texture = atlas.texture;
var coordinates = atlas.textureCoordinates;

var atlasWidth = 1.0;
var atlasHeight = 1.0;
var atlasWidth = 2.0;
var atlasHeight = 2.0;
expect(texture.width).toEqual(atlasWidth);
expect(texture.height).toEqual(atlasHeight);

Expand All @@ -469,8 +469,8 @@ void main() {\n\
var texture = atlas.texture;
var coordinates = atlas.textureCoordinates;

var atlasWidth = 10.0;
var atlasHeight = 10.0;
var atlasWidth = 12.0;
var atlasHeight = 12.0;
expect(texture.width).toEqual(atlasWidth);
expect(texture.height).toEqual(atlasHeight);

Expand All @@ -482,7 +482,7 @@ void main() {\n\

// big green image
expect(coordinates[greenIndex].x).toEqual(0.0 / atlasWidth);
expect(coordinates[greenIndex].y).toEqual(1.0 / atlasHeight);
expect(coordinates[greenIndex].y).toEqual(2.0 / atlasHeight);
expect(coordinates[greenIndex].width).toEqual(4.0 / atlasWidth);
expect(coordinates[greenIndex].height).toEqual(4.0 / atlasHeight);
});
Expand Down Expand Up @@ -577,8 +577,8 @@ void main() {\n\
var texture = atlas.texture;
var coordinates = atlas.textureCoordinates;

var atlasWidth = 10.0;
var atlasHeight = 10.0;
var atlasWidth = 6.0;
var atlasHeight = 6.0;
expect(atlas.borderWidthInPixels).toEqual(2);
expect(atlas.numberOfImages).toEqual(2);
expect(texture.width).toEqual(atlasWidth);
Expand All @@ -589,7 +589,7 @@ void main() {\n\
expect(coordinates[greenIndex].width).toEqual(1.0 / atlasWidth);
expect(coordinates[greenIndex].height).toEqual(1.0 / atlasHeight);

expect(coordinates[blueIndex].x).toEqual(4.0 / atlasWidth);
expect(coordinates[blueIndex].x).toEqual(3.0 / atlasWidth);
expect(coordinates[blueIndex].y).toEqual(0.0 / atlasHeight);
expect(coordinates[blueIndex].width).toEqual(1.0 / atlasWidth);
expect(coordinates[blueIndex].height).toEqual(1.0 / atlasHeight);
Expand Down Expand Up @@ -742,8 +742,8 @@ void main() {\n\
expect(atlas.numberOfImages).toEqual(5);

var coordinates = atlas.textureCoordinates;
var atlasWidth = 1.0;
var atlasHeight = 1.0;
var atlasWidth = 2.0;
var atlasHeight = 2.0;

expect(coordinates[index1].x).toEqual(0.0 / atlasWidth);
expect(coordinates[index1].y).toEqual(0.0 / atlasHeight);
Expand Down Expand Up @@ -793,8 +793,8 @@ void main() {\n\
expect(atlas.numberOfImages).toEqual(6);

var coordinates = atlas.textureCoordinates;
var atlasWidth = 4.0;
var atlasHeight = 4.0;
var atlasWidth = 2.0;
var atlasHeight = 2.0;

expect(coordinates[index1].x).toEqual(0.0 / atlasWidth);
expect(coordinates[index1].y).toEqual(0.0 / atlasHeight);
Expand Down

0 comments on commit ca88219

Please sign in to comment.