123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- 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);
-
- }
|