Recommendation
More Effects...
JS
1
// Function to make the canvas retina ready
2
const retinaFy = function retinaFy (canvas) {
3
  const height = document.body.clientHeight;
4
  const width = document.body.clientWidth;
5
 
6
  canvas.width = width * window.devicePixelRatio;
7
  canvas.height = height * window.devicePixelRatio;
8
 
9
  canvas.style.width = `${width}px`;
10
  canvas.style.height = `${height}px`;
11
}
12
 
13
// Define the canvas and webgl context
14
const canvas = document.querySelector('#canvas');
15
retinaFy(canvas);
16
 
17
window.addEventListener('resize', () => {
18
  retinaFy(canvas);
19
});
20
 
21
// Retinafy the canvas before getting the webgl context
22
const gl = canvas.getContext('webgl');
23
 
24
// Define the time, which is used to calculate progression
25
let lastUpdate = new Date().getTime();
26
let timer = 0.0;
27
 
28
const uniformPreset = 'u_';
29
const uniforms = [
30
  'time',
31
  'resolution',
32
];
33
 
34
// Function to compile the shader
35
const compileShader = function compileShader(type, shaderString, gl) {
36
  const shader = gl.createShader(type);
37
  gl.shaderSource(shader, shaderString);
38
  gl.compileShader(shader);
39
 
40
  return shader;
41
}
42
 
43
// Function to create the program
44
const createProgram = function createProgram (gl, vertexShader, fragmentShader) {
45
  const program = gl.createProgram();
46
  gl.attachShader(program, vertexShader);
47
  gl.attachShader(program, fragmentShader);
48
  gl.linkProgram(program);
49
 
50
  uniforms.map((uniform) => (
51
    uniforms[uniform] = gl.getUniformLocation(program, `${uniformPreset}${uniform}`)
52
  ));
53
 
54
  return program;
55
};
56
 
57
const vertex = `
58
  attribute vec4 a_position;
59
 
60
  void main () {
61
    gl_Position = vec4(a_position);
62
  }
63
`;
64
 
65
const fragment = `
66
  precision highp float;
67
 
68
  uniform vec2 u_resolution;
69
  uniform float u_time;
70
 
71
  //
72
  // Description : Array and textureless GLSL 2D/3D/4D simplex
73
  //               noise functions.
74
  //      Author : Ian McEwan, Ashima Arts.
75
  //  Maintainer : stegu
76
  //     Lastmod : 20110822 (ijm)
77
  //     License : Copyright (C) 2011 Ashima Arts. All rights reserved.
78
  //               Distributed under the MIT License. See LICENSE file.
79
  //               https://github.com/ashima/webgl-noise
80
  //               https://github.com/stegu/webgl-noise
81
  //
82
 
83
  vec3 mod289(vec3 x) {
84
      return x - floor(x * (1.0 / 289.0)) * 289.0;
85
  }
86
 
87
  vec4 mod289(vec4 x) {
88
      return x - floor(x * (1.0 / 289.0)) * 289.0;
89
  }
90
 
91
  vec4 permute(vec4 x) {
92
      return mod289(((x*34.0)+1.0)*x);
93
  }
94
 
95
  vec4 taylorInvSqrt(vec4 r)
96
  {
97
      return 1.79284291400159 - 0.85373472095314 * r;
98
  }
99
 
100
  float snoise(vec3 v)
101
  {
102
      const vec2  C = vec2(1.0/6.0, 1.0/3.0) ;
103
      const vec4  D = vec4(0.0, 0.5, 1.0, 2.0);
104
 
105
      // First corner
106
      vec3 i  = floor(v + dot(v, C.yyy) );
107
      vec3 x0 =   v - i + dot(i, C.xxx) ;
108
 
109
      // Other corners
110
      vec3 g = step(x0.yzx, x0.xyz);
111
      vec3 l = 1.0 - g;
112
      vec3 i1 = min( g.xyz, l.zxy );
113
      vec3 i2 = max( g.xyz, l.zxy );
114
 
115
      //   x0 = x0 - 0.0 + 0.0 * C.xxx;
116
      //   x1 = x0 - i1  + 1.0 * C.xxx;
117
      //   x2 = x0 - i2  + 2.0 * C.xxx;
118
      //   x3 = x0 - 1.0 + 3.0 * C.xxx;
119
      vec3 x1 = x0 - i1 + C.xxx;
120
      vec3 x2 = x0 - i2 + C.yyy; // 2.0*C.x = 1/3 = C.y
121
      vec3 x3 = x0 - D.yyy;      // -1.0+3.0*C.x = -0.5 = -D.y
122
 
123
      // Permutations
124
      i = mod289(i);
125
      vec4 p = permute( permute( permute(
126
      i.z + vec4(0.0, i1.z, i2.z, 1.0 ))
127
      + i.y + vec4(0.0, i1.y, i2.y, 1.0 ))
128
      + i.x + vec4(0.0, i1.x, i2.x, 1.0 ));
129
 
130
      // Gradients: 7x7 points over a square, mapped onto an octahedron.
131
      // The ring size 17*17 = 289 is close to a multiple of 49 (49*6 = 294)
132
      float n_ = 0.142857142857; // 1.0/7.0
133
      vec3  ns = n_ * D.wyz - D.xzx;
134
 
135
      vec4 j = p - 49.0 * floor(p * ns.z * ns.z);  //  mod(p,7*7)
136
 
137
      vec4 x_ = floor(j * ns.z);
138
      vec4 y_ = floor(j - 7.0 * x_ );    // mod(j,N)
139
 
140
      vec4 x = x_ *ns.x + ns.yyyy;
141
      vec4 y = y_ *ns.x + ns.yyyy;
142
      vec4 h = 1.0 - abs(x) - abs(y);
143
 
144
      vec4 b0 = vec4( x.xy, y.xy );
145
      vec4 b1 = vec4( x.zw, y.zw );
146
 
147
      //vec4 s0 = vec4(lessThan(b0,0.0))*2.0 - 1.0;
148
      //vec4 s1 = vec4(lessThan(b1,0.0))*2.0 - 1.0;
149
      vec4 s0 = floor(b0)*2.0 + 1.0;
150
      vec4 s1 = floor(b1)*2.0 + 1.0;
151
      vec4 sh = -step(h, vec4(0.0));
152
 
153
      vec4 a0 = b0.xzyw + s0.xzyw*sh.xxyy ;
154
      vec4 a1 = b1.xzyw + s1.xzyw*sh.zzww ;
155
 
156
      vec3 p0 = vec3(a0.xy,h.x);
157
      vec3 p1 = vec3(a0.zw,h.y);
158
      vec3 p2 = vec3(a1.xy,h.z);
159
      vec3 p3 = vec3(a1.zw,h.w);
160
 
161
      //Normalise gradients
162
      vec4 norm = taylorInvSqrt(vec4(dot(p0,p0), dot(p1,p1), dot(p2, p2), dot(p3,p3)));
163
      p0 *= norm.x;
164
      p1 *= norm.y;
165
      p2 *= norm.z;
166
      p3 *= norm.w;
167
 
168
      // Mix final noise value
169
      vec4 m = max(0.6 - vec4(dot(x0,x0), dot(x1,x1), dot(x2,x2), dot(x3,x3)), 0.0);
170
      m = m * m;
171
      return 42.0 * dot( m*m, vec4( dot(p0,x0), dot(p1,x1),
172
      dot(p2,x2), dot(p3,x3) ) );
173
  }
174
 
175
  float perlin3( vec3 coord ) {
176
      float n = 0.0;
177
      n = 140.0 * abs( snoise( coord ));
178
      return n;
179
  }
180
 
181
  void main( void ) {
182
 
183
    float ab = 50.0;
184
 
185
    vec2 uv = gl_FragCoord.xy / vec2(ab, ab);
186
    vec3 coord = vec3(uv.xy, u_time / 2.0);
187
    float n = perlin3(coord);
188
 
189
    vec2 st = gl_FragCoord.xy/u_resolution.xy;
190
    st.x *= u_resolution.x/u_resolution.y;
191
 
192
    // color = vec3(st.x * n, st.y * n, abs(cos(u_time)));
193
    vec3 color = vec3(st.x * n, st.y * n, 0.9);
194
 
195
    // vec2 uv = ( gl_FragCoord.xy + u_resolution.xy );
196
    // gl_FragColor = vec4((cos(u_time))  * 0.05 + 0.1, 0.0, cos(u_time)    * 0.05 + 0.1, 1.0);
197
    gl_FragColor = vec4(
198
      color,
199
      1.0
200
    );
201
  }
202
`;
203
 
204
// Create the shaders
205
const vertexShader = compileShader(gl.VERTEX_SHADER, vertex, gl);
206
const fragmentShader = compileShader(gl.FRAGMENT_SHADER, fragment, gl);
207
 
208
const program = createProgram(gl, vertexShader, fragmentShader);
209
 
210
const array = [
211
  -1.0, -1.0,
212
  1.0, -1.0,
213
  -1.0, 1.0,
214
  1.0, 1.0,
215
  -1.0, 1.0,
216
  1.0, -1.0,
217
];
218
 
219
// Create array for buffer
220
const bufferArray = new Float32Array(array);
221
 
222
// Create a buffer
223
const buffer = gl.createBuffer();
224
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
225
gl.bufferData(gl.ARRAY_BUFFER, bufferArray, gl.STATIC_DRAW);
226
 
227
// look up where the vertex data needs to go.
228
const positionLocation = gl.getAttribLocation(program, 'a_position');
229
const dimensionCount = 2; // 2D
230
 
231
gl.enableVertexAttribArray(positionLocation);
232
gl.vertexAttribPointer(positionLocation, dimensionCount, gl.FLOAT, false, 0, 0);
233
 
234
const draw = function draw() {
235
  gl.useProgram(program);
236
 
237
  gl.uniform2f(uniforms.resolution, canvas.width, canvas.height);
238
 
239
  const currentTime = new Date().getTime();
240
  const timeSinceLastUpdate = currentTime - lastUpdate;
241
  lastUpdate = currentTime;
242
 
243
  timer += (timeSinceLastUpdate ? timeSinceLastUpdate / 1000 : 0.05);
244
 
245
  // console.log((Math.cos(timer)) * 0.1 + 0.1);
246
  // Pass time to the vertex and fragment shader
247
  gl.uniform1fv(uniforms.time, [timer]);
248
 
249
  gl.drawArrays(gl.TRIANGLES, 0, array.length / dimensionCount);
250
 
251
  // Requestanimationframe
252
  window.requestAnimationFrame(draw);
253
}
254
 
255
// draw
256
draw();
257
 
 
CSS
1
html, body, canvas {
2
  width: 100%;
3
  height: 100%;
4
  margin: 0;
5
  overflow: hidden;
6
}
7
body {
8
  background: radial-gradient(hsl(255, 18%, 35%), hsl(198, 32%, 8%));
9
}
 
HTML
1
<canvas id='canvas'></canvas>
 

Join Effecthub.com

Working with Global Gaming Artists and Developers!


Or Sign Up with Your Email Address:
This field must contain a valid email
Password should be at least 1 character