Skip to content

Commit 9a1bb50

Browse files
committed
Camera Scroll added to Tile map
1 parent ea71909 commit 9a1bb50

3 files changed

Lines changed: 90 additions & 86 deletions

File tree

v3/src/checksum.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
var CHECKSUM = {
2-
build: '3a8f5ea0-4711-11e7-9525-fb6db2cab82a'
2+
build: '1ced6ae0-471b-11e7-aeea-e52f23bf6487'
33
};
44
module.exports = CHECKSUM;

v3/src/gameobjects/tilemap/static/StaticTilemap.js

Lines changed: 88 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -49,98 +49,102 @@ var StaticTilemap = new Class({
4949
this.setSize(tileWidth * mapWidth, tileHeight * mapHeight);
5050
},
5151

52-
upload: function ()
52+
upload: function (scrollX, scrollY)
5353
{
54-
if (this.dirty && this.gl)
54+
if (this.gl)
5555
{
56-
var gl = this.gl;
57-
var vbo = this.vbo;
58-
var mapWidth = this.mapWidth;
59-
var mapHeight = this.mapHeight;
60-
var tileWidth = this.tileWidth;
61-
var tileHeight = this.tileHeight;
62-
var bufferData = this.bufferData;
63-
var bufferF32, bufferU32;
64-
var voffset = 0;
65-
var vertexCount = 0;
66-
var width = this.texture.source[0].width;
67-
var height = this.texture.source[0].height;
68-
var setWidth = width / tileWidth;
69-
var mapData = this.mapData;
70-
71-
if (this.vbo === null)
56+
if (this.dirty)
7257
{
73-
vbo = this.resourceManager.createBuffer(gl.ARRAY_BUFFER, (4 * 6 * (mapWidth * mapHeight)) * 4, gl.STATIC_DRAW);
74-
vbo.addAttribute(this.tilemapRenderer.shader.getAttribLocation('a_position'), 2, gl.FLOAT, false, CONST.VERTEX_SIZE, 0);
75-
vbo.addAttribute(this.tilemapRenderer.shader.getAttribLocation('a_tex_coord'), 2, gl.FLOAT, false, CONST.VERTEX_SIZE, 8);
76-
bufferData = this.bufferData = new ArrayBuffer((4 * 6 * (mapWidth * mapHeight)) * 4);
77-
this.vbo = vbo;
78-
}
58+
var gl = this.gl;
59+
var vbo = this.vbo;
60+
var mapWidth = this.mapWidth;
61+
var mapHeight = this.mapHeight;
62+
var tileWidth = this.tileWidth;
63+
var tileHeight = this.tileHeight;
64+
var bufferData = this.bufferData;
65+
var bufferF32, bufferU32;
66+
var voffset = 0;
67+
var vertexCount = 0;
68+
var width = this.texture.source[0].width;
69+
var height = this.texture.source[0].height;
70+
var setWidth = width / tileWidth;
71+
var mapData = this.mapData;
72+
73+
if (this.vbo === null)
74+
{
75+
vbo = this.resourceManager.createBuffer(gl.ARRAY_BUFFER, (4 * 6 * (mapWidth * mapHeight)) * 4, gl.STATIC_DRAW);
76+
vbo.addAttribute(this.tilemapRenderer.shader.getAttribLocation('a_position'), 2, gl.FLOAT, false, CONST.VERTEX_SIZE, 0);
77+
vbo.addAttribute(this.tilemapRenderer.shader.getAttribLocation('a_tex_coord'), 2, gl.FLOAT, false, CONST.VERTEX_SIZE, 8);
78+
bufferData = this.bufferData = new ArrayBuffer((4 * 6 * (mapWidth * mapHeight)) * 4);
79+
this.vbo = vbo;
80+
}
7981

80-
bufferF32 = new Float32Array(bufferData);
82+
bufferF32 = new Float32Array(bufferData);
8183

82-
for (var y = 0; y < mapHeight; ++y)
83-
{
84-
for (var x = 0; x < mapWidth; ++x)
84+
for (var y = 0; y < mapHeight; ++y)
8585
{
86-
var tileId = mapData[y * mapWidth + x];
87-
var rectx = ((tileId % setWidth)|0) * tileWidth;
88-
var recty = ((tileId / setWidth)|0) * tileHeight;
89-
var tx = x * tileWidth;
90-
var ty = y * tileHeight;
91-
var txw = tx + tileWidth;
92-
var tyh = ty + tileHeight;
93-
var u0 = rectx / width;
94-
var v0 = recty / height;
95-
var u1 = u0 + (tileWidth / width);
96-
var v1 = v0 + (tileHeight / height);
97-
var tx0 = tx;
98-
var ty0 = ty;
99-
var tx1 = tx;
100-
var ty1 = tyh;
101-
var tx2 = txw;
102-
var ty2 = tyh;
103-
var tx3 = txw;
104-
var ty3 = ty;
105-
106-
bufferF32[voffset + 0] = tx0;
107-
bufferF32[voffset + 1] = ty0;
108-
bufferF32[voffset + 2] = u0;
109-
bufferF32[voffset + 3] = v0;
110-
111-
bufferF32[voffset + 4] = tx1;
112-
bufferF32[voffset + 5] = ty1;
113-
bufferF32[voffset + 6] = u0;
114-
bufferF32[voffset + 7] = v1;
115-
116-
bufferF32[voffset + 8] = tx2;
117-
bufferF32[voffset + 9] = ty2;
118-
bufferF32[voffset + 10] = u1;
119-
bufferF32[voffset + 11] = v1;
120-
121-
bufferF32[voffset + 12] = tx0;
122-
bufferF32[voffset + 13] = ty0;
123-
bufferF32[voffset + 14] = u0;
124-
bufferF32[voffset + 15] = v0;
125-
126-
bufferF32[voffset + 16] = tx2;
127-
bufferF32[voffset + 17] = ty2;
128-
bufferF32[voffset + 18] = u1;
129-
bufferF32[voffset + 19] = v1;
130-
131-
bufferF32[voffset + 20] = tx3;
132-
bufferF32[voffset + 21] = ty3;
133-
bufferF32[voffset + 22] = u1;
134-
bufferF32[voffset + 23] = v0;
135-
136-
voffset += 24;
137-
vertexCount += 6;
86+
for (var x = 0; x < mapWidth; ++x)
87+
{
88+
var tileId = mapData[y * mapWidth + x];
89+
var rectx = ((tileId % setWidth)|0) * tileWidth;
90+
var recty = ((tileId / setWidth)|0) * tileHeight;
91+
var tx = x * tileWidth;
92+
var ty = y * tileHeight;
93+
var txw = tx + tileWidth;
94+
var tyh = ty + tileHeight;
95+
var u0 = rectx / width;
96+
var v0 = recty / height;
97+
var u1 = u0 + (tileWidth / width);
98+
var v1 = v0 + (tileHeight / height);
99+
var tx0 = tx;
100+
var ty0 = ty;
101+
var tx1 = tx;
102+
var ty1 = tyh;
103+
var tx2 = txw;
104+
var ty2 = tyh;
105+
var tx3 = txw;
106+
var ty3 = ty;
107+
108+
bufferF32[voffset + 0] = tx0;
109+
bufferF32[voffset + 1] = ty0;
110+
bufferF32[voffset + 2] = u0;
111+
bufferF32[voffset + 3] = v0;
112+
113+
bufferF32[voffset + 4] = tx1;
114+
bufferF32[voffset + 5] = ty1;
115+
bufferF32[voffset + 6] = u0;
116+
bufferF32[voffset + 7] = v1;
117+
118+
bufferF32[voffset + 8] = tx2;
119+
bufferF32[voffset + 9] = ty2;
120+
bufferF32[voffset + 10] = u1;
121+
bufferF32[voffset + 11] = v1;
122+
123+
bufferF32[voffset + 12] = tx0;
124+
bufferF32[voffset + 13] = ty0;
125+
bufferF32[voffset + 14] = u0;
126+
bufferF32[voffset + 15] = v0;
127+
128+
bufferF32[voffset + 16] = tx2;
129+
bufferF32[voffset + 17] = ty2;
130+
bufferF32[voffset + 18] = u1;
131+
bufferF32[voffset + 19] = v1;
132+
133+
bufferF32[voffset + 20] = tx3;
134+
bufferF32[voffset + 21] = ty3;
135+
bufferF32[voffset + 22] = u1;
136+
bufferF32[voffset + 23] = v0;
137+
138+
voffset += 24;
139+
vertexCount += 6;
140+
}
138141
}
139-
}
140-
this.vertexCount = vertexCount;
141-
vbo.updateResource(bufferData, 0);
142+
this.vertexCount = vertexCount;
143+
vbo.updateResource(bufferData, 0);
142144

143-
this.dirty = false;
145+
this.dirty = false;
146+
}
147+
this.tilemapRenderer.shader.setConstantFloat2(this.tilemapRenderer.scrollLocation, -scrollX, -scrollY);
144148
}
145149
}
146150

v3/src/gameobjects/tilemap/static/StaticTilemapWebGLRenderer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ var StaticTilemapWebGLRenderer = function (renderer, src, interpolationPercentag
1111

1212
renderer.setRenderer(gameObject.tilemapRenderer, frame.texture.source[frame.sourceIndex].glTexture, gameObject.renderTarget);
1313
gameObject.tilemapRenderer.bind();
14-
gameObject.upload();
14+
gameObject.upload(camera.scrollX, camera.scrollY);
1515
gameObject.vbo.bind();
1616
gl.drawArrays(gl.TRIANGLES, 0, gameObject.vertexCount);
1717
};

0 commit comments

Comments
 (0)