From 09711466ac0ce3e62711c750f491be18f3ed6cbb Mon Sep 17 00:00:00 2001 From: ajuvercr Date: Sun, 29 Mar 2020 07:15:50 +0200 Subject: [PATCH] update voronoi for weak hardware --- backend/templates/visualizer.html.tera | 7 ------- frontend/www/index.ts | 16 +++++++++++++++- frontend/www/static/shaders/frag/vor.glsl | 16 ++++++++++------ frontend/www/webgl/shader.ts | 11 +++++++++++ frontend/www/webgl/util.ts | 11 +++++++++++ 5 files changed, 47 insertions(+), 14 deletions(-) diff --git a/backend/templates/visualizer.html.tera b/backend/templates/visualizer.html.tera index 7ed2e83..e393e6e 100644 --- a/backend/templates/visualizer.html.tera +++ b/backend/templates/visualizer.html.tera @@ -31,18 +31,11 @@

{{game.name}}

Turns: {{game.turns}}

- {# {% else %} -
- No options available -
#} {% endfor %} - {% endblock %} diff --git a/frontend/www/index.ts b/frontend/www/index.ts index 67e2866..a34d535 100644 --- a/frontend/www/index.ts +++ b/frontend/www/index.ts @@ -1,7 +1,7 @@ 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 } from './webgl/shader'; +import { Shader, Uniform4f, Uniform2fv, Uniform3fv, Uniform1i, 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"; @@ -79,6 +79,8 @@ class GameInstance { renderer: Renderer; planet_count: number; + vor_counter = 3; + use_vor = true; playing = true; // 0 is paused, 1 is playing but not rerendered, 2 is playing and rerendered time_stopped_delta = 0; last_time = 0; @@ -223,6 +225,16 @@ class GameInstance { render(time: number) { COUNTER.frame(time); + if (COUNTER.delta(time) < 30) { + this.vor_counter = Math.min(3, this.vor_counter + 1); + } else { + this.vor_counter = Math.max(-3, this.vor_counter - 1); + } + + if (this.vor_counter < -2) { + this.use_vor = false; + } + if (!this.playing) { this.last_time = time; @@ -244,11 +256,13 @@ class GameInstance { this.vor_shader.uniform(GL, "u_viewbox", new Uniform4f(this.resizer.get_viewbox())); this.vor_shader.uniform(GL, "u_resolution", new Uniform2f(RESOLUTION)); + this.vor_shader.uniform(GL, "u_vor", new UniformBool(this.use_vor)); this.shader.uniform(GL, "u_time", new Uniform1f((time - this.last_time) / ms_per_frame)); this.shader.uniform(GL, "u_mouse", new Uniform2f(this.resizer.get_mouse_pos())); this.shader.uniform(GL, "u_viewbox", new Uniform4f(this.resizer.get_viewbox())); this.shader.uniform(GL, "u_resolution", new Uniform2f(RESOLUTION)); + this.shader.uniform(GL, "u_vor", new UniformBool(true)); this.renderer.render(GL); } diff --git a/frontend/www/static/shaders/frag/vor.glsl b/frontend/www/static/shaders/frag/vor.glsl index bfb4968..62e33f5 100644 --- a/frontend/www/static/shaders/frag/vor.glsl +++ b/frontend/www/static/shaders/frag/vor.glsl @@ -7,18 +7,22 @@ uniform vec3 u_planet_colours[$PLANETS * 2]; uniform float u_step_interval; uniform float u_time; +uniform bool u_vor; varying vec2 v_pos; void main() { vec3 color = vec3(0.2); - float dis = 1000000.0; - for(int i = 0; i < $PLANETS; i++) { - float d = distance(v_pos, u_planets[i].xy); - if (d < dis) { - dis = d; - color = u_planet_colours[2 * i]; + if (u_vor) { + float dis = 1000000.0; + + for(int i = 0; i < $PLANETS; i++) { + float d = distance(v_pos, u_planets[i].xy); + if (d < dis) { + dis = d; + color = u_planet_colours[2 * i]; + } } } diff --git a/frontend/www/webgl/shader.ts b/frontend/www/webgl/shader.ts index 31c2bf7..ffe1ee0 100644 --- a/frontend/www/webgl/shader.ts +++ b/frontend/www/webgl/shader.ts @@ -312,3 +312,14 @@ export class UniformMatrix3fv implements Uniform { gl.uniformMatrix3fv(location, false, this.data); } } + +export class UniformBool implements Uniform { + data: boolean; + constructor(data: boolean) { + this.data = data; + } + + setUniform(gl: WebGLRenderingContext, location: WebGLUniformLocation) { + gl.uniform1i(location, this.data ? 1 : 0); + } +} diff --git a/frontend/www/webgl/util.ts b/frontend/www/webgl/util.ts index e36ef15..3130405 100644 --- a/frontend/www/webgl/util.ts +++ b/frontend/www/webgl/util.ts @@ -35,19 +35,30 @@ export function resizeCanvasToDisplaySize( export class FPSCounter { last: number; count: number; + _delta: number; + _prev: number constructor() { this.last = 0; this.count = 0; + this._delta = 0; + this._prev = 0; } frame(now: number) { this.count += 1; + this._delta = now - this._prev; + this._prev = now; + if (now - this.last > 1000) { this.last = now; console.log(this.count + " fps"); this.count = 0; } } + + delta(now: number): number { + return this._delta; + } } export class Resizer {