do the voronoi dance
This commit is contained in:
parent
720520f4b7
commit
b8fc4e1710
4 changed files with 37 additions and 56 deletions
|
@ -121,28 +121,6 @@ class GameInstance {
|
|||
this.renderer = new Renderer();
|
||||
this.game.update_turn(0);
|
||||
|
||||
|
||||
|
||||
// const indexBuffer = new IndexBuffer(GL, [
|
||||
// 0, 1, 2,
|
||||
// 1, 2, 3,
|
||||
// ]);
|
||||
|
||||
// const positionBuffer = new VertexBuffer(GL, [
|
||||
// -1, -1,
|
||||
// -1, 1,
|
||||
// 1, -1,
|
||||
// 1, 1,
|
||||
// ]);
|
||||
|
||||
// const layout = new VertexBufferLayout();
|
||||
// layout.push(GL.FLOAT, 2, 4, "a_pos");
|
||||
|
||||
// const vao = new VertexArray();
|
||||
// vao.addBuffer(positionBuffer, layout);
|
||||
|
||||
// this.renderer.addToDraw(indexBuffer, vao, this.vor_shader);
|
||||
|
||||
// Setup key handling
|
||||
document.addEventListener('keydown', this.handleKey.bind(this));
|
||||
|
||||
|
@ -153,7 +131,7 @@ class GameInstance {
|
|||
for(let i = 0; i < planets.length; i += 3) {
|
||||
planet_points.push({'x': planets[i], 'y': planets[i+1]});
|
||||
}
|
||||
const _bbox = f32v(game.get_viewbox(), 4);
|
||||
const _bbox = this.resizer.get_viewbox();
|
||||
const bbox = {
|
||||
'xl': _bbox[0], 'xr': _bbox[0] + _bbox[2],
|
||||
'yt': _bbox[1], 'yb': _bbox[1] + _bbox[3]
|
||||
|
@ -161,9 +139,6 @@ class GameInstance {
|
|||
|
||||
this.vor_builder = new VoronoiBuilder(GL, this.vor_shader, planet_points, bbox);
|
||||
this.renderer.addRenderable(this.vor_builder.getRenderable());
|
||||
const players= new Array(this.planet_count).fill(undefined).map((_, i) => i);
|
||||
console.log(players);
|
||||
this.vor_builder.updateOwners(GL, players);
|
||||
|
||||
for (let i = 0; i < this.planet_count; i++) {
|
||||
{
|
||||
|
@ -458,16 +433,3 @@ function step(time: number) {
|
|||
// set_loading(false);
|
||||
|
||||
requestAnimationFrame(step);
|
||||
|
||||
(function() {
|
||||
console.log(Voronoi);
|
||||
console.log(new Voronoi());
|
||||
|
||||
var sites = [{x:300,y:300}, {x:100,y:100}, {x:200,y:500}, {x:250,y:450}, {x:600,y:150}];
|
||||
var bbox = {xl:0, xr:800, yt:0, yb:600};
|
||||
|
||||
const voronoi = new Voronoi();
|
||||
const result = voronoi.compute(sites, bbox);
|
||||
|
||||
console.log(result);
|
||||
})();
|
||||
|
|
|
@ -17,29 +17,46 @@ export class VoronoiBuilder {
|
|||
|
||||
constructor(gl: WebGLRenderingContext, shader: Shader, planets: Point[], bbox: BBox) {
|
||||
const voronoi = new Voronoi();
|
||||
|
||||
const own_map = {};
|
||||
planets.forEach((p, i)=>own_map[to_key(p)] = i);
|
||||
|
||||
// This voronoi sorts the planets, then owners don't align anymore
|
||||
this.vor = voronoi.compute(planets, bbox);
|
||||
|
||||
const a_pos = planets.concat(this.vor.vertices).reduce((a, b) => a.concat([-b.x, -b.y]), []);
|
||||
const a_own = new Array(planets.length + this.vor.vertices.length).fill(-1);
|
||||
|
||||
const vert_indcs = {};
|
||||
planets.concat(this.vor.vertices).forEach((p, i) => vert_indcs[to_key(p)] = i);
|
||||
|
||||
const a_pos = [];
|
||||
const a_own = [];
|
||||
const ids = [];
|
||||
|
||||
for (let cell of this.vor.cells) {
|
||||
const baseIndx = vert_indcs[to_key(cell.site)];
|
||||
console.log(baseIndx);
|
||||
for (let edge of cell.halfedges) {
|
||||
ids.push(baseIndx);
|
||||
let vertCount = 0;
|
||||
|
||||
ids.push(vert_indcs[to_key(edge.getStartpoint())]);
|
||||
ids.push(vert_indcs[to_key(edge.getEndpoint())])
|
||||
for (let i = 0; i < this.vor.cells.length; i++) {
|
||||
const cell = this.vor.cells[i];
|
||||
const planetId = own_map[to_key(cell.site)];
|
||||
|
||||
const centerId = vertCount++;
|
||||
a_pos.push(cell.site.x, cell.site.y);
|
||||
a_own.push(planetId);
|
||||
|
||||
for (let edge of cell.halfedges) {
|
||||
ids.push(centerId);
|
||||
|
||||
const start = edge.getStartpoint();
|
||||
ids.push(vertCount++);
|
||||
|
||||
a_pos.push(start.x, start.y);
|
||||
a_own.push(planetId);
|
||||
|
||||
const end = edge.getEndpoint();
|
||||
ids.push(vertCount++);
|
||||
|
||||
a_pos.push(end.x, end.y);
|
||||
a_own.push(planetId);
|
||||
}
|
||||
}
|
||||
|
||||
const ib = new IndexBuffer(gl, ids);
|
||||
const vb_pos = new VertexBuffer(gl, a_pos);
|
||||
const vb_pos = new VertexBuffer(gl, a_pos.map((x) => -1 * x));
|
||||
const vb_own = new VertexBuffer(gl, a_own);
|
||||
|
||||
const layout_pos = new VertexBufferLayout();
|
||||
|
@ -62,7 +79,7 @@ export class VoronoiBuilder {
|
|||
}
|
||||
|
||||
updateOwners(gl: WebGLRenderingContext, planets_owners: number[]) {
|
||||
planets_owners.forEach((own, i) => this.a_own[i] = own);
|
||||
this.inner.updateVAOBuffer(gl, 1, this.a_own);
|
||||
// planets_owners.forEach((own, i) => this.a_own[i] = own);
|
||||
// this.inner.updateVAOBuffer(gl, 1, this.a_own);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -121,6 +121,8 @@ export class Renderer {
|
|||
}
|
||||
|
||||
render(gl: WebGLRenderingContext, frameBuffer?: WebGLFramebuffer, width?: number, height?: number) {
|
||||
gl.clearColor(0,0,0,1);
|
||||
|
||||
gl.bindFramebuffer(gl.FRAMEBUFFER, frameBuffer);
|
||||
gl.viewport(0, 0, width || gl.canvas.width, height || gl.canvas.height);
|
||||
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
|
||||
|
|
|
@ -12,5 +12,5 @@ varying vec3 v_color;
|
|||
varying vec2 v_pos;
|
||||
|
||||
void main() {
|
||||
gl_FragColor = vec4(v_color, 0.8);
|
||||
gl_FragColor = vec4(v_color, 0.3);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue