forked from regl-project/regl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tile.js
83 lines (69 loc) · 1.8 KB
/
tile.js
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
/*
tags: basic
<p>This example implements a simple 2D tiled sprite renderer.</p>
*/
const regl = require('../regl')()
const mouse = require('mouse-change')()
require('resl')({
manifest: {
map: {
type: 'text',
src: 'assets/map.json',
parser: JSON.parse
},
tiles: {
type: 'image',
src: 'assets/tiles.png',
parser: regl.texture
}
},
onDone: ({ map, tiles }) => {
const drawBackground = regl({
frag: `
precision mediump float;
uniform sampler2D map, tiles;
uniform vec2 mapSize, tileSize;
varying vec2 uv;
void main() {
vec2 tileCoord = floor(255.0 * texture2D(map, floor(uv) / mapSize).ra);
gl_FragColor = texture2D(tiles, (tileCoord + fract(uv)) / tileSize);
}`,
vert: `
precision mediump float;
attribute vec2 position;
uniform vec4 view;
varying vec2 uv;
void main() {
uv = mix(view.xw, view.zy, 0.5 * (1.0 + position));
gl_Position = vec4(position, 1, 1);
}`,
depth: { enable: false },
uniforms: {
tiles,
tileSize: [16.0, 16.0],
map: regl.texture(map),
mapSize: [map[0].length, map.length],
view: regl.prop('view')
},
attributes: {
position: [ -1, -1, 1, -1, -1, 1, 1, 1, -1, 1, 1, -1 ]
},
count: 6
})
regl.frame(({ viewportWidth, viewportHeight }) => {
const { x, y } = mouse
const boxX = map[0].length * x / viewportWidth
const boxY = map.length * y / viewportHeight
const boxH = 10
const boxW = viewportWidth / viewportHeight * boxH
drawBackground({
view: [
boxX - 0.5 * boxW,
boxY - 0.5 * boxH,
boxX + 0.5 * boxW,
boxY + 0.5 * boxH
]
})
})
}
})