Skip to content

Commit d0e4f59

Browse files
author
Dylan McGannon
committed
Add rotating color cycle and expanded quad to fill screen.
1 parent 51f46ab commit d0e4f59

File tree

4 files changed

+42
-8
lines changed

4 files changed

+42
-8
lines changed

js/gl.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export class GL {
2727
this.zoom_target = new Vector(0, 0, 0, 1);
2828
this.zoom_speed = 0;
2929
this.zoom_level = 1;
30+
this.color_cycle = 0;
3031

3132
// Add resize listener
3233
window.addEventListener('resize', () => {
@@ -260,7 +261,7 @@ export class GL {
260261
render() {
261262
this.gl.viewport(0, 0, this.gl.canvas.width, this.gl.canvas.height);
262263

263-
this.gl.clearColor(58 / 255, 0, 2 / 15, 1);
264+
this.gl.clearColor(0, 0, 0, 0);
264265
this.gl.clear(this.gl.COLOR_BUFFER_BIT);
265266

266267
this.gl.useProgram(this.get_shader_program().ref());
@@ -318,6 +319,22 @@ export class GL {
318319
return true;
319320
}
320321

322+
/**
323+
* Cycle the color rotation float.
324+
*
325+
* @param {integer} frame_delta
326+
*/
327+
cycle(frame_delta) {
328+
this.color_cycle = (this.color_cycle + frame_delta / 200) % 1024.0;
329+
330+
this.get_shader_program().set_uniform_float(
331+
'continuous_cycle',
332+
this.color_cycle
333+
);
334+
335+
return true;
336+
}
337+
321338
/**
322339
* The event loop executed for each tick.
323340
*/
@@ -335,6 +352,7 @@ export class GL {
335352

336353
scene_dirty |= this.resize();
337354
scene_dirty |= this.zoom(frame_delta);
355+
scene_dirty |= this.cycle(frame_delta);
338356

339357
if (scene_dirty) {
340358
this.render();

js/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ async function mandelbrot(canvas) {
1717

1818
gl.set_shader_program(shader_program);
1919

20-
const quad = gl.create_quad(-2, -1, 3, 2);
20+
const quad = gl.create_quad(-5, -3, 10, 6);
2121
const mesh = gl.create_mesh([quad]);
2222
gl.add_object_to_scene(mesh);
2323

js/shader.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,18 @@ export class ShaderProgram {
8383
this.gl.uniformMatrix4fv(location, false, value);
8484
}
8585

86+
/**
87+
* Set a float uniform value.
88+
*
89+
* @param {string} name
90+
* @param {float} value
91+
*/
92+
set_uniform_float(name, value) {
93+
const location = this.gl.getUniformLocation(this.program, name);
94+
this.gl.useProgram(this.program);
95+
this.gl.uniform1f(location, value);
96+
}
97+
8698
/**
8799
* @return {WebGLProgram}
88100
*/

shaders/fragment.glsl

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,23 @@ precision highp float;
44

55
in vec4 vertex;
66
out vec4 output_colour;
7+
uniform float continuous_cycle;
78

89
void main() {
910
vec2 p = vertex.xy;
1011
vec2 c = p;
1112

12-
vec3 color=vec3(0.0,0.0,0.0);
13+
vec3 color = vec3(0.0, 0.0, 0.0);
1314

14-
for(int i=0;i<512;i++){
15-
p= vec2(p.x*p.x-p.y*p.y,2.0*p.x*p.y)+c;
15+
for(int i=0;i<1024;i++){
16+
p = vec2(
17+
p.x * p.x - p.y * p.y,
18+
2.0 * p.x * p.y
19+
) + c;
1620

17-
if (dot(p,p)>4.0){
18-
float colorRegulator = float(i-1)-log(((log(dot(p,p)))/log(2.0)))/log(2.0);
19-
color = vec3(0.95 + .012*colorRegulator , 1.0, .2+.4*(1.0+sin(.3*colorRegulator)));
21+
if (dot(p, p) > 4.0) {
22+
float regulator = float(i) - continuous_cycle - log(log(dot(p, p)) / log(2.0)) / log(2.0);
23+
color = vec3(0.95 + .012 * regulator, 1.0, .1 + .4 * (1.0 + sin(.3 * regulator)));
2024
break;
2125
}
2226
}

0 commit comments

Comments
 (0)