smooth transition

This commit is contained in:
ajuvercr 2019-09-19 18:27:56 +02:00
parent 195df9da38
commit 89eae79eca
3 changed files with 28 additions and 9 deletions

View file

@ -78,10 +78,20 @@ impl Game {
} }
fn update_planet_colours(&mut self) { fn update_planet_colours(&mut self) {
self.current_planet_colours = self.states[self.turn].planets let mut new_vec = Vec::new();
.iter() let planets_now = self.states[self.turn].planets.iter();
.map(|p| utils::COLORS[p.owner.unwrap_or(0) as usize % utils::COLORS.len()].into()) let planets_later = self.states[(self.turn + 1).min(self.states.len() - 1)].planets.iter();
.collect();
for (p1, p2) in planets_now.zip(planets_later) {
new_vec.push(
utils::COLORS[p1.owner.unwrap_or(0) as usize % utils::COLORS.len()].into()
);
new_vec.push(
utils::COLORS[p2.owner.unwrap_or(0) as usize % utils::COLORS.len()].into()
);
}
self.current_planet_colours = new_vec;
} }
fn update_ship_locations(&mut self) { fn update_ship_locations(&mut self) {

View file

@ -104,22 +104,26 @@ class GameInstance {
} }
render(time: number) { render(time: number) {
if (time > this.last_time + 100) { if (time > this.last_time + 1000) {
this.last_time = time; this.last_time = time;
this.frame ++; this.frame ++;
this.game.update_turn(this.frame); this.game.update_turn(this.frame);
const colours = f32v(this.game.get_planet_colors(), this.planet_count * 3); const colours = f32v(this.game.get_planet_colors(), this.planet_count * 6);
for(let i=0; i < this.planet_count; i++){ for(let i=0; i < this.planet_count; i++){
const u = new Uniform3f(colours[i*3], colours[i*3 + 1], colours[i*3 + 2]); const u = new Uniform3f(colours[i*6], colours[i*6 + 1], colours[i*6 + 2]);
this.renderer.updateUniform(i, (us) => us["u_color"] = u); this.renderer.updateUniform(i, (us) => us["u_color"] = u);
const u2 = new Uniform3f(colours[i*6 + 3], colours[i*6 + 4], colours[i*6 + 5]);
this.renderer.updateUniform(i, (us) => us["u_color_next"] = u2);
} }
} }
GL.bindFramebuffer(GL.FRAMEBUFFER, null); GL.bindFramebuffer(GL.FRAMEBUFFER, null);
GL.viewport(0, 0, GL.canvas.width, GL.canvas.height); GL.viewport(0, 0, GL.canvas.width, GL.canvas.height);
GL.clear(GL.COLOR_BUFFER_BIT | GL.DEPTH_BUFFER_BIT); GL.clear(GL.COLOR_BUFFER_BIT | GL.DEPTH_BUFFER_BIT);
this.shader.uniform(GL, "u_time", new Uniform1f(time * 0.001)); this.shader.uniform(GL, "u_step_interval", new Uniform1f(1000));
this.shader.uniform(GL, "u_time", new Uniform1f(time));
this.shader.uniform(GL, "u_mouse", new Uniform2f(this.resizer.get_mouse_pos())); 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_viewbox", new Uniform4f(this.resizer.get_viewbox()));
this.shader.uniform(GL, "u_resolution", new Uniform2f(RESOLUTION)); this.shader.uniform(GL, "u_resolution", new Uniform2f(RESOLUTION));

View file

@ -2,8 +2,13 @@
precision mediump float; precision mediump float;
#endif #endif
uniform float u_step_interval;
uniform float u_time;
uniform vec3 u_color; uniform vec3 u_color;
uniform vec3 u_color_next;
void main() { void main() {
gl_FragColor = vec4(u_color, 1.0); float part = fract(u_time / u_step_interval);
vec3 color = mix(u_color, u_color_next, part);
gl_FragColor = vec4(color, 1.0);
} }