Skip to content

Commit

Permalink
[#591] added missing webgl coordinates when building textures out of …
Browse files Browse the repository at this point in the history
…spritesheet

some redundant code to be removed later.
  • Loading branch information
obiot committed Jan 19, 2015
1 parent 7e6be65 commit 335e50c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
15 changes: 11 additions & 4 deletions src/renderable/animationsheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
addAnimation : function (name, index, animationspeed) {
this.anim[name] = {
name : name,
frame : [],
frame : {},
idx : 0,
length : 0,
animationspeed: animationspeed || this.animationspeed,
Expand All @@ -135,21 +135,28 @@
});
}

// # of frames
var counter = 0;
// set each frame configuration (offset, size, etc..)
for (var i = 0, len = index.length; i < len; i++) {
if (typeof(index[i]) === "number") {
this.anim[name].frame[i] = this.textureAtlas[index[i]];
if (typeof (this.textureAtlas[index[i]]) !== "undefined") {
// TODO: adding the cache source coordinates add undefined entries in webGL mode
this.anim[name].frame["" + i] = this.textureAtlas[index[i]];
counter++;
}
} else { // string
if (this.atlasIndices === null) {
throw new me.Renderable.Error(
"string parameters for addAnimation are not allowed for standard spritesheet based Texture"
);
} else {
this.anim[name].frame[i] = this.textureAtlas[this.atlasIndices[index[i]]];
counter++;
}
}
}
this.anim[name].length = this.anim[name].frame.length;
this.anim[name].length = counter;
},

/**
Expand Down Expand Up @@ -230,7 +237,7 @@
*/
setAnimationFrame : function (idx) {
this.current.idx = (idx || 0) % this.current.length;
var frame = this.current.frame[this.current.idx];
var frame = this.current.frame["" + this.current.idx];
this.offset = frame.offset;
this.width = frame.width;
this.height = frame.height;
Expand Down
4 changes: 2 additions & 2 deletions src/video/canvas/texture.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@
* @ignore
*/
buildFromSpriteSheet : function (data) {
var atlas = []; // changing to {} breaks everything !
var atlas = {};
var image = data.region || data.image;
var spacing = data.spacing || 0;
var margin = data.margin || 0;
Expand All @@ -180,7 +180,7 @@
}
// build the local atlas
for (var frame = 0, count = spritecount.x * spritecount.y; frame < count ; frame++) {
atlas[frame] = {
atlas["" + frame] = {
name: "" + frame,
offset: new me.Vector2d(
margin + (spacing + data.framewidth) * (frame % spritecount.x) + offsetX,
Expand Down
29 changes: 29 additions & 0 deletions src/video/webgl/texture.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,36 @@
var key = s.x + "," + s.y + "," + w + "," + h;
atlas[key] = atlas[frame];
});
return atlas;
},

buildFromSpriteSheet : function (data) {
var image = data.region || data.image;
var w = image.width;
var h = image.height;

var atlas = this._super(me.CanvasRenderer.prototype.Texture, "buildFromSpriteSheet", [ data ]);

// TODO : duplicated !
Object.keys(atlas).forEach(function (frame) {
// Source coordinates
var s = atlas["" + frame].offset;
var sw = atlas["" + frame].width;
var sh = atlas["" + frame].height;

// ST texture coordinates
atlas["" + frame].stMap = new Float32Array([
s.x / w, // Left
s.y / h, // Top
(s.x + sw) / w, // Right
(s.y + sh) / h // Bottom
]);

// Cache source coordinates
// TODO: Remove this when the Batcher only accepts a region name
var key = s.x + "," + s.y + "," + w + "," + h;
atlas[key] = atlas["" + frame];
});
return atlas;
},

Expand Down

2 comments on commit 335e50c

@obiot
Copy link
Member Author

@obiot obiot commented on 335e50c Jan 19, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@parasyte two questions :

@parasyte
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Yes. ;( See the warnings in your console. When those are gone, it can be removed!
  • That function is used by the compositor when a region is unknown (this is where the warning is printed)
    if (typeof(region) === "undefined") {
    // TODO: Require proper atlas regions instead of caching arbitrary region keys
    console.warn("Adding texture region", key, "for texture", texture);
    var keys = key.split(","),
    sx = +keys[0],
    sy = +keys[1],
    sw = +keys[2],
    sh = +keys[3];
    region = texture._insertRegion(key, sx, sy, sw, sh);
    }

I would love to get rid of these things, but we can't do that until #592, #595, and #632 are fixed.

Please sign in to comment.