-
Notifications
You must be signed in to change notification settings - Fork 0
/
ani-test.html
417 lines (384 loc) · 10.7 KB
/
ani-test.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html" charset="utf-8"/>
<title>tileMap</title>
<script src="https://cdn.babylonjs.com/babylon.worker.js"></script>
<style>
html, body {
overflow: hidden;
width : 100%;
height : 100%;
margin : 0;
padding : 0;
font-size:16px;
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
body *{
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
*:before, *:after{
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
#renderCanvas {
width : 100%;
height : 100%;
touch-action: none;
}
</style>
</head>
<body>
<canvas id="renderCanvas"></canvas>
<script>
TM = function(target, args){
this.canvas = document.getElementById(target) || target;
args = args || {};
this.tileScale = args.tileScale || 1.0;
this.tileSize = args.tileSize || 16.0;
this.offset = args.offset || new BABYLON.Vector2.Zero();
this.layers = [];
this.time = 0;
this.init(args);
return this;
};
TM.prototype = {
init : function(args){
this._loadingBuild(args);
this._asycBuffer();
this._startScene();
this._buildShader();
//this._startRender();
},
_loadingBuild : function(){
this._tasks = 1;
this._ready = false;
this._ls = document.createElement('div');
this._ls.setAttribute('style', 'position:absolute;display:block;left:50%;top:50%;transform:translate(-50% , -50%);');
this._ls.innerHTML = 'LOADING!';
document.body.appendChild(this._ls);
},
_asycBuffer : function(){
this._ready = true;
if(!this._atlas){this._ready=false;}
if(this._tasks>0){this._ready=false;}
for(i=0; i<this.layers.length; i++){
var l = this.layers[i];
if(!l._atlasSet || !l._tilesSet){this._ready=false;}
}
if(this._ready){
this._ls.remove();
this._startRender();
this._resize();
//console.log('READY!');
}else{
//console.log('NOT READY!');
var self = this;
setTimeout(function(){self._asycBuffer();}, 1000/30);
}
},
_startScene : function(){
var canvas = this.canvas;
var engine = new BABYLON.Engine(canvas, true);
var scene = new BABYLON.Scene(engine);
scene.clearColor = new BABYLON.Color3(1, 1, 1);
var camera = new BABYLON.FreeCamera("camera1", new BABYLON.Vector3(0, 0, -10), scene);
camera.setTarget(BABYLON.Vector3.Zero());
this.camera = camera;
var light = new BABYLON.HemisphericLight("light1", new BABYLON.Vector3(0, 1, 0), scene);
light.intensity = .5;
this.engine = engine;
this.scene = scene;
},
_buildShader : function(){
var _vs =
`precision highp float;
// Attributes
attribute vec3 position;
attribute vec3 normal;
attribute vec2 uv;
// Uniforms
uniform mat4 worldViewProjection;
uniform float time;
uniform vec2 viewOffset;
uniform vec2 viewportSize;
uniform vec2 inverseTileTextureSize;
uniform float layerHeight;
uniform float tileSize;
// Varying
varying vec3 vPosition;
varying vec2 vUV;
varying vec2 pixelCoord;
varying vec2 texCoord;
void main() {
vec4 p = vec4( position, 1. );
vPosition = p.xyz;
gl_Position = worldViewProjection * p;
vUV = uv;
vUV.y = vUV.y * -1.;
pixelCoord = (vUV * viewportSize) + viewOffset;
pixelCoord.y = pixelCoord.y + (viewportSize.y*0.5) + ((layerHeight*tileSize)/2.0);
texCoord = pixelCoord * inverseTileTextureSize * (1.0/tileSize);
}
`;
var _fs =
`precision highp float;
uniform float time;
uniform float fps;
uniform sampler2D tiles;
uniform sampler2D animationMap;
uniform sampler2D sprites;
uniform vec2 viewportSize;
uniform float tileSize;
uniform vec2 inverseSpriteTextureSize;
uniform vec2 inverseTileTextureSize;
uniform vec3 globalLight;
varying vec3 vPosition;
varying vec2 vUV;
varying vec2 pixelCoord;
varying vec2 texCoord;
const float pU = 1./255.;
void main(){
vec4 tile = texture2D(tiles, texCoord);
if((tile.x == 1.0 && tile.y == 1.0) ) { discard; }
vec2 aLoc = (tile.xy/pU)/4.0;
vec4 aTile = texture2D(animationMap, aLoc);
if(aTile.a > 0.0){
float t = (time*aTile.a)/aTile.b;
int _t;
/*
x:1 -
[1][2]
x:2 -
[1]
[2]
x:3 - does not work?
x:4 -
[1][2][3][4]
x:5 -
[1][2]
[3][4]
x:5 -
[1][2][3]
[4][5][6]
x:6,7 - do not work?
x:8 -
[1][2]
[3][4]
[5][6]
x:9 -
[1][2][3]
[4][5][6]
[7][8][9]
*/
//x:1
if(aTile.x == pU){
_t = int(mod(t, 2.));
if(_t == 1){tile.x+=pU;}
}
//x:2
if(aTile.x == pU*2.){
_t = int(mod(t, 2.));
if(_t == 1){tile.y+=pU;}
}
//Why does three not work?
//x:4
if(aTile.x == pU*4.){
_t = int(mod(t, 4.));
if(_t == 1){tile.x+=pU;}
if(_t == 2){tile.x+=pU*2.;}
if(_t == 3){tile.x+=pU*3.;}
}
//x:5
if(aTile.x == pU*5.){
_t = int(mod(t, 6.));
if(_t == 1){tile.x+=pU;}
if(_t == 2){tile.x+=pU*2.;}
if(_t == 3){tile.y+=pU;}
if(_t == 4){tile.x+=pU;tile.y+=pU;}
if(_t == 5){tile.x+=pU*2.;tile.y+=pU;}
}
//6, 7 also do not work?
//x:8
if(aTile.x == pU*8.){
_t = int(mod(t, 6.));
if(_t == 1){tile.x+=pU;}
if(_t == 2){tile.y+=pU;}
if(_t == 3){tile.x+=pU;tile.y+=pU;}
if(_t == 4){tile.y+=pU*2.;}
if(_t == 5){tile.x+=pU;tile.y+=pU*2.;}
}
//x:9
if(aTile.x == pU*9.){
_t = int(mod(t, 9.));
if(_t == 1){tile.x+=pU;}
if(_t == 2){tile.x+=pU*2.;}
if(_t == 3){tile.y+=pU;}
if(_t == 4){tile.x+=pU;tile.y+=pU;}
if(_t == 5){tile.x+=pU*2.;tile.y+=pU;}
if(_t == 6){tile.y+=pU*2.;}
if(_t == 7){tile.x+=pU;tile.y+=pU*2.;}
if(_t == 8){tile.x+=pU*2.;tile.y+=pU*2.;}
}
}
vec2 spriteOffset = floor(tile.xy * 256.0) * tileSize;
vec2 spriteCoord = mod(pixelCoord, tileSize);
vec4 tSample = texture2D(sprites, (spriteOffset + spriteCoord) * inverseSpriteTextureSize);
vec3 color = tSample.xyz;
float alpha = tSample.a;
color = color * globalLight;
gl_FragColor = vec4(color, alpha);
}
`;
BABYLON.Effect.ShadersStore["basicVertexShader"] = _vs;
BABYLON.Effect.ShadersStore["basicFragmentShader"] = _fs;
this._tasks--;
var self = this;
this.scene.registerBeforeRender(()=>{
self._update();
});
},
_startRender : function(){
var self = this;
self.engine.runRenderLoop(function () {
self.scene.render();
self.time+=0.01;
});
window.addEventListener("resize", function () {
self.engine.resize();
self._resize();
});
},
setAtlas : function(url){
var self = this;
this._tasks++;
this._atlas = new BABYLON.Texture(url, this.scene, true, false, 1, ()=>{
self.inverseSpriteTextureSize = new BABYLON.Vector2(1/self._atlas._texture._width, 1/self._atlas._texture._height);
console.log("inverseSpriteTextureSize", self.inverseSpriteTextureSize);
self._tasks--;
});
},
setAnimations : function(url){
var self = this;
this._tasks++;
this._atlasAnimationSheet = new BABYLON.Texture(url, this.scene, true, false, 1, ()=>{
self._tasks--;
});
},
addLayer : function(url, args){
this._tasks++;
var layer = new TM.LAYER(url, args || {}, this);
this.layers.push(layer);
return layer;
},
_resize : function(){
console.log('RESIZE!');
for(var i=0; i<this.layers.length; i++){
var l = this.layers[i];
l.viewportSize = new BABYLON.Vector2(this.canvas.width, this.canvas.height);
l.viewportScaled = new BABYLON.Vector2(this.canvas.width/(this.tileScale+l.scale), this.canvas.height/(this.tileScale+l.scale));
l._build();
}
},
_update : function(){
for(var i=0; i<this.layers.length; i++){
var l = this.layers[i];
l.shader.setVector2('viewOffset', this.offset.multiply(l.scrollSpeed));
l.shader.setFloat('time' ,this.time);
}
}
};
TM.LAYER = function(url, args, parent){
this.parent = parent;
this.repeat = args.repeat || false;
this.scale = args.scale || 0;
this.scrollSpeed = args.scrollSpeed || new BABYLON.Vector2(1.0,1.0);
this.viewportSize = new BABYLON.Vector2(this.parent.canvas.width, this.parent.canvas.height);
this.viewportScaled = new BABYLON.Vector2(this.parent.canvas.width/(this.parent.tileScale+this.scale), this.parent.canvas.height/(this.parent.tileScale+this.scale));
this._atlasSet = false;
this._animationsSet = false;
this._tilesSet = false;
var self = this;
this._tiles = new BABYLON.Texture(url, this.scene, true, false, 1, ()=>{
self.inverseTextureSize = new BABYLON.Vector2(1/self._tiles._texture._width, 1/self._tiles._texture._height);
console.log("inverseTextureSize", self.inverseTextureSize);
self._setShader();
self.parent._tasks--;
});
if(!this.repeat){
this._tiles.wrapU = BABYLON.Texture.CLAMP_ADDRESSMODE;
this._tiles.wrapV = BABYLON.Texture.CLAMP_ADDRESSMODE;
}
};
TM.LAYER.prototype = {
_build : function(){
this._setShader();
this._createPlane();
},
_createPlane : function(){
if(this.plane)this.plane.dispose();
var scene = this.parent.scene;
var engine = this.parent.engine;
var c = scene.activeCamera;
var fov = c.fov;
var aspectRatio = engine.getAspectRatio(c);
var d = c.position.length();
var y = 2 * d * Math.tan(fov / 2);
var x = y * aspectRatio;
this.plane = BABYLON.MeshBuilder.CreatePlane("plane", {width: x, height:y}, scene);
this.plane.material = this.shader;
},
_setShader : function(){
if(!this.shader){
this.shader = new BABYLON.ShaderMaterial("basicShader", this.parent.scene, {
vertex: "basic",
fragment: "basic",
},{
attributes: ["position", "normal", "uv"],
uniforms: ["world",
"worldView",
"worldViewProjection",
"view", "viewOffset",
"viewportSize",
"inverseTileTextureSize",
"inverseSpriteTextureSize","layerHeight",
"tileSize", "time", "projection",
"globalLight", "fps"]
});
}
if(!this._atlasSet){
this.shader.setTexture("sprites", this.parent._atlas);
this._atlasSet = true;
}
if(!this._animationsSet){
this.shader.setTexture('animationMap', this.parent._atlasAnimationSheet);
this._animationsSet = true;
}
if(!this._tilesSet){
this.shader.setTexture("tiles", this._tiles);
this._tilesSet = true;
}
this.shader.setVector2('viewportSize', this.viewportScaled);
this.shader.setVector2('inverseTileTextureSize', this.inverseTextureSize);
this.shader.setVector2('inverseSpriteTextureSize', this.parent.inverseSpriteTextureSize);
this.shader.setVector3('globalLight', new BABYLON.Vector3(1.0, 1.0, 1.0));
this.shader.setFloat('tileSize', this.parent.tileSize);
this.shader.setFloat('layerHeight', this._tiles._texture._baseHeight);
this.shader.setFloat('fps', 60);
},
};
document.addEventListener("DOMContentLoaded", () => {
tm = new TM('renderCanvas', {tileScale:2, tileSize:16});
tm.setAtlas('./imgs/number_atlas.png');
tm.setAnimations('./imgs/ani-map-test2.png');
//tm.addLayer('layer0.png', {repeat:true, scale:-0.5, scrollSpeed:new BABYLON.Vector2(0.8, 0.8)});
var l1 = tm.addLayer('./imgs/ani-test-layer.png', {repeat:false});
},false);
</script>
</body>
</html>