-
Notifications
You must be signed in to change notification settings - Fork 0
/
terrain.js
240 lines (163 loc) · 4.67 KB
/
terrain.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
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
function Terrain(assets, atlas) {
this.last = {
center: [0,0],
size: [0,0], // zero size will force an update on the first frame
}
this.prog = loadProgram2(assets.vertexShader.terrain, assets.fragmentShader.terrain);
this.pos_ul = gl.getUniformLocation(this.prog, "position");
this.vaoInfo = makeVAO([
{ buf: 0, name: 'vpos', loc: 0, count: 3, type: gl.FLOAT, norm: false },
{ buf: 0, name: 'vtex', loc: 1, count: 2, type: gl.FLOAT, norm: false },
{ buf: 1, name: 'pos_ind', loc: 2, count: 3, type: gl.FLOAT, norm: false, divisor: 1 },
]);
this.meshVBO = this.makeTerrainMeshVBO();
this.instVBO = this.makeInstVBO();
this.instances = [];
this.tiles = { /* fill with tile atlas info */};
this.blocks = {
};
this.dataTex = makeTexArray(gl.R8, 64,64, 16);
this.map = new Map();
var map = this.map;
map.fillRandom(pt(0,0), pt(63, 63), [0,0,1]);
// main street
map.placeRoad(pt(0,36), pt(63, 36), [3,2,2,2,3]);
// side streets
for(var i = 0; i < 3; i++) {
map.placeRoad(pt(5 + i*15,36), pt(5 + i*15, 20), [3,2,2,2,3]);
}
// // clear the map first
// for(var y = 0; y < 64; y++) {
// for(var x = 0; x < 64; x++) {
// var d = (Math.random() * 5) | 0;
// //console.log(d);
// map[x + (y * 64)] = d
// }
// }
//
this.dataTex.updateLayer(0, map.getBlock(0,0).tiles);
this.tilesTex = assets.imageArray['terraintex'];
return this;
}
Terrain.prototype.loadTiles = function(tiles) {
for(var t in tiles) {
}
}
Terrain.prototype.setTile = function(pos, tile) {
}
Terrain.prototype.makeBlock = function(tl) {
var arr = new Uint16Array(64*64);
arr.fill(0);
return {
aabb: [tl[0], tl[1], tl[0] + 64, tl[1] + 64],
data: arr,
meshVBO: this.makeTerrainVBO(arr),
kids: {
tl: null,
tr: null,
bl: null,
br: null,
},
};
};
Terrain.prototype.makeTerrainMeshVBO = function(arr) {
var buf = [];
for(var y = 0; y < 64; y++) {
for(var x = 0; x < 64; x++) {
buf.push(x - 0.5);
buf.push(y - 0.5);
buf.push(1);
buf.push(0);
buf.push(0);
buf.push(x - 0.5);
buf.push(y + 0.5);
buf.push(1);
buf.push(0);
buf.push(1);
buf.push(x + 0.5);
buf.push(y - 0.5);
buf.push(1);
buf.push(1);
buf.push(0);
// ------------------
buf.push(x - 0.5);
buf.push(y + 0.5);
buf.push(1);
buf.push(0);
buf.push(1);
buf.push(x + 0.5);
buf.push(y - 0.5);
buf.push(1);
buf.push(1);
buf.push(0);
buf.push(x + 0.5);
buf.push(y + 0.5);
buf.push(1);
buf.push(1);
buf.push(1);
}
}
// console.log("vbo len", buf.length);
return makeVBO(new Float32Array(buf), this.vaoInfo, 0, gl.DYNAMIC_DRAW);
}
Terrain.prototype.makeInstVBO = function(arr) {
var buf = [];
// need to track which layers of the tile info texture are used and which map to what block
// use sync objects to know when it's safe to use and recycle
// TODO: need to get view area from matrices
// var blocks = this.map.blocksInArea();
buf.push(0);
buf.push(0);
buf.push(0);
// console.log("vbo len", buf.length);
return makeVBO(new Float32Array(buf), this.vaoInfo, 1, gl.DYNAMIC_DRAW);
}
/*
Terrain.prototype.makeTerrainVBO = function(arr) {
var buf = [];
var sz = 10;
for(var y = 0; y < 64; y++) {
for(var x = 0; x < 64; x++) {
var n = arr[x + (y * 64)];
var t = this.tiles[n];
// position
buf.push(x * sz);
buf.push(y * sz);
buf.push(1);
// quad size
buf.push(sz);
buf.push(sz);
// tex array index
buf.push(t.texIndex);
// tex offset, size
buf.push(t.normOffset[0]);
buf.push(t.normOffset[1]);
buf.push(t.normSize[0]);
buf.push(t.normSize[1]);
}
}
return makeVBO(buf, this.vaoInfo, 1, gl.DYNAMIC_DRAW);
}*/
Terrain.prototype.render = function(game) {
gl.useProgram(this.prog);
var vp_ul = gl.getUniformLocation(this.prog, "mViewProj");
gl.uniformMatrix4fv(vp_ul, false, game.vp);
gl.activeTexture(gl.TEXTURE0 + 0);
gl.bindTexture(gl.TEXTURE_2D_ARRAY, this.tilesTex.tex);
// gl.bindTexture(gl.TEXTURE_2D_ARRAY, this.dataTex.tex);
var tex_ul = gl.getUniformLocation(this.prog, "sTex");
gl.uniform1i(tex_ul, 0);
gl.activeTexture(gl.TEXTURE0 + 1);
gl.bindTexture(gl.TEXTURE_2D_ARRAY, this.dataTex.tex);
var lookup_tex_ul = gl.getUniformLocation(this.prog, "sTileLookup");
gl.uniform1i(lookup_tex_ul, 1);
gl.bindVertexArray(this.vaoInfo.vao);
gl.bindBuffer(gl.ARRAY_BUFFER, this.meshVBO.vbo);
gl.bindBuffer(gl.ARRAY_BUFFER, this.instVBO.vbo);
// console.log(this.meshVBO, this.instVBO);
var primitiveType = gl.TRIANGLES;
var offset = 0;
var count = 6 * 64 * 64;
var instances = 1;
gl.drawArraysInstanced(primitiveType, offset, count, instances);
}