From f8c86487d5618163d83a026bafce273492d4b379 Mon Sep 17 00:00:00 2001 From: ajuvercr Date: Mon, 20 Apr 2020 11:15:18 +0200 Subject: [PATCH] resize voronoi with resize + squash all in one buffer --- frontend/www/src/index.ts | 17 ++--- frontend/www/src/voronoi/voronoi.ts | 98 +++++++++++++++-------------- frontend/www/src/webgl/renderer.ts | 2 - 3 files changed, 60 insertions(+), 57 deletions(-) diff --git a/frontend/www/src/index.ts b/frontend/www/src/index.ts index 942b8f9..a3ab978 100644 --- a/frontend/www/src/index.ts +++ b/frontend/www/src/index.ts @@ -1,14 +1,11 @@ import { Game } from "planetwars"; import { memory } from "planetwars/planetwars_bg"; import { Resizer, resizeCanvasToDisplaySize, FPSCounter, url_to_mesh, Mesh } from "./webgl/util"; -import { Shader, Uniform4f, Uniform2fv, Uniform3fv, Uniform1i, Uniform1f, Uniform2f, ShaderFactory, Uniform3f, UniformMatrix3fv, UniformBool } from './webgl/shader'; +import { Shader, Uniform4f, Uniform3fv, Uniform1f, Uniform2f, ShaderFactory, Uniform3f, UniformMatrix3fv, UniformBool } from './webgl/shader'; import { Renderer } from "./webgl/renderer"; import { VertexBuffer, IndexBuffer } from "./webgl/buffer"; import { VertexBufferLayout, VertexArray } from "./webgl/vertexBufferLayout"; -import { Texture } from "./webgl/texture"; -import { callbackify } from "util"; import { defaultLabelFactory, LabelFactory, Align, Label } from "./webgl/text"; -import Voronoi = require("./voronoi/voronoi-core"); import { VoronoiBuilder } from "./voronoi/voronoi"; const LAYERS = { @@ -63,7 +60,7 @@ var ms_per_frame = parseInt(SPEED.value); resizeCanvasToDisplaySize(CANVAS); -GL.clearColor(0, 0, 0, 0); +GL.clearColor(0, 0, 0, 1); GL.clear(GL.COLOR_BUFFER_BIT); GL.enable(GL.BLEND); @@ -212,14 +209,20 @@ class GameInstance { this.renderer.addRenderable(label.getRenderable(), LAYERS.ship_label) } - // this.vor_shader.uniform(GL, "u_planets", new Uniform3fv(planets)); - // Set slider correctly SLIDER.max = this.turn_count - 1 + ''; } on_resize() { this.resizer = new Resizer(CANVAS, [...f32v(this.game.get_viewbox(), 4)], true); + + const _bbox = this.resizer.get_viewbox(); + const bbox = { + 'xl': _bbox[0], 'xr': _bbox[0] + _bbox[2], + 'yt': _bbox[1], 'yb': _bbox[1] + _bbox[3] + }; + + this.vor_builder.resize(GL, bbox); } _update_state() { diff --git a/frontend/www/src/voronoi/voronoi.ts b/frontend/www/src/voronoi/voronoi.ts index 2d31a81..8b896a6 100644 --- a/frontend/www/src/voronoi/voronoi.ts +++ b/frontend/www/src/voronoi/voronoi.ts @@ -58,39 +58,63 @@ function dist(a: Point, b: Point, norm = false): number { export class VoronoiBuilder { inner: DefaultRenderable; - vor: VoronoiDiagram; - a_own: number[]; + vor: Voronoi; + planets: Point[]; + constructor(gl: WebGLRenderingContext, shader: Shader, planets: Point[], bbox: BBox) { - const voronoi = new Voronoi(); + this.vor = new Voronoi(); + this.planets = planets; + + const ib = new IndexBuffer(gl, []); + const vb = new VertexBuffer(gl, []); + + const layout = new VertexBufferLayout(); + layout.push(gl.FLOAT, 2, 4, "a_pos"); + layout.push(gl.FLOAT, 1, 4, "a_own"); + layout.push(gl.FLOAT, 1, 4, "a_intensity"); + + const vao = new VertexArray(); + vao.addBuffer(vb, layout); + + this.inner = new DefaultRenderable(ib, vao, shader, [], {}); + + this.resize(gl, bbox); + } + + getRenderable(): DefaultRenderable { + return this.inner; + } + + resize(gl: WebGLRenderingContext, bbox: BBox) { + const start = new Date().getTime(); // This voronoi sorts the planets, then owners don't align anymore const own_map = {}; - planets.forEach((p, i) => own_map[to_key(p)] = i); + this.planets.forEach((p, i) => own_map[to_key(p)] = i); - this.vor = voronoi.compute(planets, bbox); + const vor = this.vor.compute(this.planets, bbox); - const a_pos = []; - const a_own = []; + const attrs = []; const ids = []; let vertCount = 0; - for (let i = 0; i < this.vor.cells.length; i++) { - const cell = this.vor.cells[i]; + for (let i = 0; i < vor.cells.length; i++) { + const cell = vor.cells[i]; const planetId = own_map[to_key(cell.site)]; const point_map = build_point_map(cell.halfedges); const centerId = vertCount++; - a_pos.push(cell.site.x, cell.site.y); - a_own.push(planetId); - a_own.push(1); + attrs.push(cell.site.x, cell.site.y); + attrs.push(planetId); + attrs.push(1); const dist_mean = cell.halfedges.map(e => { const start = e.getStartpoint(); const end = e.getEndpoint(); - return dist(cell.site, start, true) + dist(cell.site, {'x': (start.x + end.x) / 2, 'y': (start.y + end.y) / 2}, true) + return dist(cell.site, start, true) + dist(cell.site, { 'x': (start.x + end.x) / 2, 'y': (start.y + end.y) / 2 }, true) }).reduce((a, b) => a + b, 0) / cell.halfedges.length / 2; const round_fn = get_round_fn(dist_mean); @@ -107,51 +131,29 @@ export class VoronoiBuilder { ids.push(centerId); ids.push(vertCount++); - a_pos.push(start.x, start.y); - a_own.push(planetId); - a_own.push(0); + attrs.push(start.x, start.y); + attrs.push(planetId); + attrs.push(0); ids.push(vertCount++); - a_pos.push(center.x, center.y); - a_own.push(planetId); - a_own.push(0); + attrs.push(center.x, center.y); + attrs.push(planetId); + attrs.push(0); ids.push(centerId); ids.push(vertCount - 1); ids.push(vertCount++); - a_pos.push(end.x, end.y); - a_own.push(planetId); - a_own.push(0); + attrs.push(end.x, end.y); + attrs.push(planetId); + attrs.push(0); } } - const ib = new IndexBuffer(gl, ids); - const vb_pos = new VertexBuffer(gl, a_pos); - const vb_own = new VertexBuffer(gl, a_own); + this.inner.updateIndexBuffer(gl, ids); + this.inner.updateVAOBuffer(gl, 0, attrs); + // this.inner.updateVAOBuffer(gl, 1, attrs); - const layout_pos = new VertexBufferLayout(); - layout_pos.push(gl.FLOAT, 2, 4, "a_pos"); - - const layout_own = new VertexBufferLayout(); - layout_own.push(gl.FLOAT, 1, 4, "a_own"); - layout_own.push(gl.FLOAT, 1, 4, "a_intensity"); - - const vao = new VertexArray(); - vao.addBuffer(vb_pos, layout_pos); - vao.addBuffer(vb_own, layout_own); - - this.inner = new DefaultRenderable(ib, vao, shader, [], {}); - - this.a_own = a_own; - } - - getRenderable(): DefaultRenderable { - return this.inner; - } - - updateOwners(gl: WebGLRenderingContext, planets_owners: number[]) { - // planets_owners.forEach((own, i) => this.a_own[i] = own); - // this.inner.updateVAOBuffer(gl, 1, this.a_own); + console.log(`Vor things took ${new Date().getTime() - start} ms!`) } } diff --git a/frontend/www/src/webgl/renderer.ts b/frontend/www/src/webgl/renderer.ts index 3f3bf0d..8d73501 100644 --- a/frontend/www/src/webgl/renderer.ts +++ b/frontend/www/src/webgl/renderer.ts @@ -141,8 +141,6 @@ 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);