Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <canvas id="webgl-contents"></canvas>
<script id="vs" type="x-shader/x-vertex">
  precision highp float;

  attribute vec3 position;

  void main(void) {
    gl_Position = vec4(position, 1.0);
  }
</script>
<script id="fs" type="x-shader/x-fragment">
  #extension GL_OES_standard_derivatives : enable

  precision highp float;

  uniform float time;
  uniform vec2 mouse;
  uniform vec2 resolution;

  const float PI = 3.14159265;
  const float angle = 60.0;
  const float fov = angle * 0.5 * PI / 180.0;

  const vec3 lightDir = vec3(0.577, -0.577, 0.577);

  vec3 hsv2rgb(vec3 c){
    vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
    vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
    return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
  }

  //
  // Description : Array and textureless GLSL 2D/3D/4D simplex
  //               noise functions.
  //      Author : Ian McEwan, Ashima Arts.
  //  Maintainer : ijm
  //     Lastmod : 20110822 (ijm)
  //     License : Copyright (C) 2011 Ashima Arts. All rights reserved.
  //               Distributed under the MIT License. See LICENSE file.
  //               https://github.com/ashima/webgl-noise
  //
  vec3 mod289(vec3 x) {
     return x - floor(x * (1.0 / 289.0)) * 289.0;
  }
  vec4 mod289(vec4 x) {
     return x - floor(x * (1.0 / 289.0)) * 289.0;
  }
  vec4 permute(vec4 x) {
     return mod289(((x*34.0)+1.0)*x);
  }
  vec4 taylorInvSqrt(vec4 r) {
     return 1.79284291400159 - 0.85373472095314 * r;
  }
  float snoise(vec3 v) {
     const vec2  C = vec2(1.0/6.0, 1.0/3.0) ;
     const vec4  D = vec4(0.0, 0.5, 1.0, 2.0);

     vec3 i  = floor(v + dot(v, C.yyy) );
     vec3 x0 =   v - i + dot(i, C.xxx) ;

     vec3 g = step(x0.yzx, x0.xyz);
     vec3 l = 1.0 - g;
     vec3 i1 = min( g.xyz, l.zxy );
     vec3 i2 = max( g.xyz, l.zxy );

     vec3 x1 = x0 - i1 + C.xxx;
     vec3 x2 = x0 - i2 + C.yyy; // 2.0*C.x = 1/3 = C.y
     vec3 x3 = x0 - D.yyy;      // -1.0+3.0*C.x = -0.5 = -D.y

     i = mod289(i);
     vec4 p = permute( permute( permute(
     i.z + vec4(0.0, i1.z, i2.z, 1.0 ))
     + i.y + vec4(0.0, i1.y, i2.y, 1.0 ))
     + i.x + vec4(0.0, i1.x, i2.x, 1.0 ));

     float n_ = 0.142857142857; // 1.0/7.0
     vec3  ns = n_ * D.wyz - D.xzx;

     vec4 j = p - 49.0 * floor(p * ns.z * ns.z);  //  mod(p,7*7)

     vec4 x_ = floor(j * ns.z);
     vec4 y_ = floor(j - 7.0 * x_ );    // mod(j,N)

     vec4 x = x_ *ns.x + ns.yyyy;
     vec4 y = y_ *ns.x + ns.yyyy;
     vec4 h = 1.0 - abs(x) - abs(y);

     vec4 b0 = vec4( x.xy, y.xy );
     vec4 b1 = vec4( x.zw, y.zw );

     vec4 s0 = floor(b0)*2.0 + 1.0;
     vec4 s1 = floor(b1)*2.0 + 1.0;
     vec4 sh = -step(h, vec4(0.0));

     vec4 a0 = b0.xzyw + s0.xzyw*sh.xxyy ;
     vec4 a1 = b1.xzyw + s1.xzyw*sh.zzww ;

     vec3 p0 = vec3(a0.xy,h.x);
     vec3 p1 = vec3(a0.zw,h.y);
     vec3 p2 = vec3(a1.xy,h.z);
     vec3 p3 = vec3(a1.zw,h.w);

     vec4 norm = taylorInvSqrt(vec4(dot(p0,p0), dot(p1,p1), dot(p2, p2), dot(p3,p3)));
     p0 *= norm.x;
     p1 *= norm.y;
     p2 *= norm.z;
     p3 *= norm.w;

     vec4 m = max(0.6 - vec4(dot(x0,x0), dot(x1,x1), dot(x2,x2), dot(x3,x3)), 0.0);
     m = m * m;
     return 42.0 * dot( m*m, vec4( dot(p0,x0), dot(p1,x1), dot(p2,x2), dot(p3,x3) ) );
  }

  float dSphere(vec3 p, float r) {
    return length(p) - r;
  }
  
  float dsBox(vec3 p, vec3 size, float v) {
    return length(max(abs(p) - size, 0.0)) - v;
  }

  vec3 sphericalPolarCoord(float radius, float radian1, float radian2) {
    return vec3(
      radius * sin(radian1) * cos(radian2),
      radius * sin(radian1) * sin(radian2),
      radius * cos(radian1)
    );
  }

  vec3 rotate(vec3 p, float radian_x, float radian_y, float radian_z) {
    mat3 mx = mat3(
      1.0, 0.0, 0.0,
      0.0, cos(radian_x), -sin(radian_x),
      0.0, sin(radian_x), cos(radian_x)
    );
    mat3 my = mat3(
      cos(radian_y), 0.0, sin(radian_y),
      0.0, 1.0, 0.0,
      -sin(radian_y), 0.0, cos(radian_y)
    );
    mat3 mz = mat3(
      cos(radian_z), -sin(radian_z), 0.0,
      sin(radian_z), cos(radian_z), 0.0,
      0.0, 0.0, 1.0
    );
    return mx * my * mz * p;
  }

  float distanceFunc(vec3 p) {
    float n1 = snoise(p * 0.3 + time / 100.0);
    vec3 p1 = rotate(p, radians(time), radians(time * 2.0), radians(time));
    float d1 = dsBox(p1, vec3(2.0), 0.3);
    float d2 = dSphere(p, 3.2) - n1;
    float d3 = dSphere(p, 1.8) - n1;
    return min(max(d1, -d2), d3);
  }

  vec3 getNormal(vec3 p) {
    const float d = 0.01;
    return normalize(vec3(
      distanceFunc(p + vec3(d, 0.0, 0.0)) - distanceFunc(p + vec3(-d, 0.0, 0.0)),
      distanceFunc(p + vec3(0.0, d, 0.0)) - distanceFunc(p + vec3(0.0, -d, 0.0)),
      distanceFunc(p + vec3(0.0, 0.0, d)) - distanceFunc(p + vec3(0.0, 0.0, -d))
    ));
  }

void main() {
  vec2 p = (gl_FragCoord.xy * 2.0 - resolution) / min(resolution.x, resolution.y);

  vec3 cPos = vec3(0.0, 0.0, 10.0);
  vec3 cDir = vec3(0.0, 0.0, -1.0);
  vec3 cUp  = vec3(0.0, 1.0, 0.0);
  vec3 cSide = cross(cDir, cUp);
  float targetDepth = 1.8;

  vec3 ray = normalize(cSide * p.x + cUp * p.y + cDir * targetDepth);

  float distance = 0.0;
  float rLen = 0.0;
  vec3  rPos = cPos;
  for(int i = 0; i < 64; i++){
      distance = distanceFunc(rPos);
      rLen += distance;
      rPos = cPos + ray * rLen * 0.1;
  }

  vec3 normal = getNormal(rPos);
  if(abs(distance) < 1.0){
    float n = snoise(rPos * 0.3 + time / 100.0);
    vec3 p = rotate(rPos, radians(time * -2.0), radians(time * 2.0), radians(time * -2.0));
    float d = dSphere(p, 1.6) - n;
    if (d > 1.0) {
      gl_FragColor = vec4(hsv2rgb(vec3(dot(normal, cUp) * 0.8 + time / 200.0, 0.2, dot(normal, cUp) * 0.8 + 0.1)), 1.0);
    } else {
      gl_FragColor = vec4(hsv2rgb(vec3(dot(normal, cUp) * 0.1 + time / 200.0, 0.8, dot(normal, cUp) * 0.2 + 0.8)), 1.0);
    }
    
  }else{
    gl_FragColor = vec4(0.9, 0.9, 0.9, 1.0);
  }
}
</script>

              
            
!

CSS

              
                html {
  height: 100%;
}
body {
  min-height: 100%;
  margin: 0;
  padding: 0;
  overflow: hidden;
}
              
            
!

JS

              
                const canvas = document.getElementById('webgl-contents');
const gl = canvas.getContext('webgl');

const loadProgram = (gl, vs_src, fs_src) => {
  const program = gl.createProgram();
  const vs = loadShader(gl.VERTEX_SHADER, gl, vs_src);
  const fs = loadShader(gl.FRAGMENT_SHADER, gl, fs_src);

  gl.attachShader(program, vs);
  gl.attachShader(program, fs);
  gl.linkProgram(program);
  gl.useProgram(program);

  return program;
};
const loadShader = (type, gl, shader_src) => {
  const shader = gl.createShader(type);
  gl.shaderSource(shader, shader_src);
  gl.compileShader(shader);
  return shader;
};
const createVBO = (gl, array) => {
  const buffer = gl.createBuffer();
  gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
  gl.bufferData(gl.ARRAY_BUFFER, array, gl.STATIC_DRAW);
  gl.bindBuffer(gl.ARRAY_BUFFER, null);
  return buffer;
};
const createIBO = (gl, array) => {
  const buffer = gl.createBuffer();
  gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, buffer);
  gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, array, gl.STATIC_DRAW);
  gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
  return buffer;
};
const setArrayBuffer = (gl, buffer, array, length) => {
  gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
  gl.enableVertexAttribArray(array);
  gl.vertexAttribPointer(array, length, gl.FLOAT, false, 0, 0);
};
const vertices = [
  -1.0,  1.0, 0.0,
  1.0,  1.0, 0.0,
  -1.0, -1.0, 0.0,
  1.0, -1.0, 0.0,
];
const indecies = [
  0, 2, 1,
  1, 2, 3,
];
const init = () => {
   canvas.width = window.innerWidth;
   canvas.height = window.innerHeight;
   canvas.style.width = `${window.innerWidth}px`;
   canvas.style.height = `${window.innerHeight}px`;
  if(!gl.getExtension('OES_standard_derivatives')){
    console.log('OES_standard_derivatives is not supported');
    return;
  }
  gl.clearColor(0.0, 0.0, 0.0, 1.0);
  gl.enable(gl.DEPTH_TEST);
  gl.enable(gl.CULL_FACE);
  gl.clear(gl.COLOR_BUFFER_BIT);
  gl.viewport(0, 0, canvas.width, canvas.height);

  let time = 0;

  const program = loadProgram(gl, document.getElementById('vs').textContent, document.getElementById('fs').textContent);

  const uni_time = gl.getUniformLocation(program, 'time');
  gl.uniform1f(uni_time, time);

  const uni_mouse = gl.getUniformLocation(program, 'mouse');
  gl.uniform2fv(uni_mouse, [0, 0]);

  const uni_resolution = gl.getUniformLocation(program, 'resolution');
  gl.uniform2fv(uni_resolution, [window.innerWidth, window.innerHeight]);

  const attr_position = gl.getAttribLocation(program, 'position');
  const vertex_buffer = createVBO(gl, new Float32Array(vertices));

  gl.bindBuffer(gl.ARRAY_BUFFER, null);

  const attr_index = gl.getAttribLocation(program, 'index');
  const index_buffer = createIBO(gl, new Uint16Array(indecies));

  gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);

  const render = () => {
    time ++;

    gl.clearColor(0.0, 0.0, 0.0, 1.0);
    gl.clearDepth(1.0);
    gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);

    gl.useProgram(program);

    setArrayBuffer(gl, vertex_buffer, attr_position, 3);
    gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, index_buffer);

    gl.uniform1f(uni_time, time);
    gl.uniform2fv(uni_resolution, [window.innerWidth, window.innerHeight]);

    gl.drawElements(gl.TRIANGLES, indecies.length, gl.UNSIGNED_SHORT, 0);

    gl.bindTexture(gl.TEXTURE_2D, null);
  };
  const renderLoop = () => {
    render();
    requestAnimationFrame(renderLoop);
  };
  renderLoop();
};
init();
              
            
!
999px

Console