Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ 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

Auto Save

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

              
                <script id="shader-fs" type="x-shader/x-fragment">

  #define PI 3.14159265359
  #define TWO_PI PI * 2.0
  #define HALF_PI PI / 2.0


  precision mediump float;

  uniform float u_time;
  uniform vec2 u_mouse;
  uniform vec2 u_resolution;

  struct Camera {
      vec3 position;
      vec3 ray;
  };

  struct Light {
      vec3 position;
      vec3 ambientWeight;
      vec3 diffuseWeight;
      vec3 specularWeight;
  };

  float random(float v) {
      return fract(sin(v) * 43758.5453123);
  }

  float smoothmin(float d1, float d2, float k) {
      return -log(exp(-k * d1) + exp(-k * d2)) / k;
  }

  vec3 repeat(vec3 p, vec3 interval) {
      return mod(p, interval) - interval / 2.0;
  }

  vec3 repeatX(vec3 p, float interval) {
      return vec3(mod(p.x, interval) - interval / 2.0, p.y, p.z);
  }

  vec3 repeatY(vec3 p, float interval) {
      return vec3(p.x, mod(p.y, interval) - interval / 2.0, p.z);
  }

  vec3 repeatZ(vec3 p, float interval) {
      return vec3(p.x, p.y, mod(p.z, interval) - interval / 2.0);
  }

  vec3 rotateX(vec3 p, float theta) {
      float c = cos(-theta);
      float s = sin(-theta);

      mat3 m = mat3(vec3(1, 0.0, 0.0),
                    vec3(0.0, c, -s),
                    vec3(0.0, s, c));
      return m * p;
  }


  vec3 rotateY(vec3 p, float theta) {
      float c = cos(-theta);
      float s = sin(-theta);

      mat3 m = mat3(vec3(c, 0.0, s),
                    vec3(0.0, 1.0, 0.0),
                    vec3(-s, 0.0, c));
      return m * p;
  }

  vec3 rotateZ(vec3 p, float theta) {
      float c = cos(-theta);
      float s = sin(-theta);

      mat3 m = mat3(vec3(c, -s, 0.0),
                    vec3(s, c, 0.0),
                    vec3(0.0, 0.0, 1.0));
      return m * p;
  }

  vec3 translate(vec3 p, vec3 t) {
      mat4 m = mat4(vec4(1.0, 0.0, 0.0, 0.0),
                    vec4(0.0, 1.0, 0.0, 0.0),
                    vec4(0.0, 0.0, 1.0, 0.0),
                    vec4(-t.x, -t.y, -t.z, 1.0));

      return (m * vec4(p, 1.0)).xyz;
  }

  float calcBoxDistance(vec3 p, vec3 size) {
      return length(max(abs(p) - size, 0.0));
  }

  float calcRoundBoxDistance(vec3 p, vec3 size, float r) {
      return calcBoxDistance(p, size) - r;
  }

  float calcSphereDistance(vec3 p, float size) {
      return length(p) - size;
  }

  float calcPlainDistance(vec3 p, vec3 n) {
      return dot(p, n);
  }

  float calcTorusDistance(vec3 p, vec2 size) {
      vec2 q = vec2(length(p.xz) - size.x, p.y);
      return length(q) - size.y;
  }

  float calcCylinderDistance(vec3 p, vec3 size) {
      return length(p.xz - size.xy) - size.z;
  }

  float calcDistance(vec3 p) {
      float d = 10000000.0;
      d = min(d, calcSphereDistance(translate(p, vec3(0.0, 3.0, 0.0)), 1.0));
      d = min(d, calcPlainDistance(p, vec3(0.0, 1.0, 0.0)));
      return d;
  }

  vec3 calcNormal(vec3 p) {
      float delta = 0.00001;
      return normalize(vec3(
          calcDistance(p + vec3(delta, 0.0, 0.0)) - calcDistance(p - vec3(delta, 0.0, 0.0)),
          calcDistance(p + vec3(0.0, delta, 0.0)) - calcDistance(p - vec3(0.0, delta, 0.0)),
          calcDistance(p + vec3(0.0, 0.0, delta)) - calcDistance(p - vec3(0.0, 0.0, delta))
      ));
  }

  Camera getPerspectiveCamera(vec2 pos, vec3 eye, vec3 center, vec3 top, float fov) {
      float camRadian = fov / 2.0 * PI / 180.0;
      vec3 viewDir = normalize(center - eye);
      vec3 camSide = cross(viewDir, top);
      vec3 camTop = cross(camSide, viewDir);

      Camera camera;
      camera.position = eye;
      camera.ray = normalize(camTop * sin(camRadian * pos.y) + camSide * sin(camRadian * pos.x) + viewDir * cos(camRadian * pos.x));
      return camera;
  }

  Camera getOrthographicCamera(vec2 pos, vec3 eye, vec3 center, vec3 top, float width, float height) {
      vec3 viewDir = normalize(center - eye);
      vec3 camSide = cross(viewDir, top);
      vec3 camTop = cross(camSide, viewDir);

      Camera camera;
      camera.position = eye + vec3(camTop * pos.y * height / 2.0 + camSide * pos.x * width / 2.0);
      camera.ray = viewDir;
      return camera;
  }

  float calcShadow(vec3 pos, vec3 light) {
      vec3 lightDir = normalize(light - pos);
      float depth = 0.01;
      float d;
      for (int i = 0; i < 16; i++) {
          d = calcDistance(pos + lightDir * depth);
          depth += d;
      }
      return d < 0.001 ? 0.5 : 1.0;
  }

  float calcSoftShadow(vec3 pos, vec3 light) {
      vec3 lightDir = normalize(light - pos);
      float depth = 0.001;
      float bright = 1.0;
      float d;

      float shadowIntensity = 0.7;
      float shadowSharpness = 8.0;

      for (int i = 0; i < 32; i++) {
          d = calcDistance(pos + lightDir * depth);
          if (d < 0.001) {
              return 1.0 - shadowIntensity;
          }
          bright = min(bright, shadowSharpness * d / depth);
          depth += d;
      }
      return 1.0 - (1.0 - bright) * shadowIntensity;
  }

  void main(void) {
      vec2 st = (gl_FragCoord.xy * 2.0 - u_resolution) / min(u_resolution.x, u_resolution.y);
      vec2 mouse = (u_mouse * 2.0 - u_resolution) / min(u_resolution.x, u_resolution.y);
      float time = u_time * 0.001;

      vec3 cameraPosition = vec3(20.0 * cos(mouse.x * PI), 10.0 + 5.0 * mouse.y, 20.0 * sin(mouse.x * PI));
      vec3 viewCenter = vec3(0.0);
      vec3 cameraTop = vec3(0.0, 1.0, 0.0);
      Camera camera = getPerspectiveCamera(st, cameraPosition, viewCenter, vec3(0.0, 1.0, 0.0), 60.0);
      // Camera camera = getOrthographicCamera(st, cameraPosition, viewCenter, vec3(0.0, 1.0, 0.0), 50.0, 50.0);
      vec3 rayPosition = camera.position;
      vec3 rayDirection = camera.ray;

      vec3 color = vec3(0.0);
      float d;
      for (int i = 0; i < 256; i++) {
          d = calcDistance(rayPosition);
          rayPosition += rayDirection * d;
      }


      Light lights[3];
      lights[0].position = vec3(10.0 * cos(time),10.0 + 3.0 * sin(time / 3.0), 10.0 * sin(time));
      lights[0].ambientWeight = vec3(1.0, 0.0, 0.0);
      lights[0].diffuseWeight = vec3(1.0, 0.0, 0.0);
      lights[0].specularWeight = vec3(1.0, 0.0, 0.0);

      lights[1].position = vec3(10.0 * cos(time * 2.0), 10.0 + 3.0 * sin(time / 2.0), 10.0 * sin(time * 2.0));
      lights[1].ambientWeight = vec3(0.0, 1.0, 0.0);
      lights[1].diffuseWeight = vec3(0.0, 1.0, 0.0);
      lights[1].specularWeight = vec3(0.0, 1.0, 0.0);

      lights[2].position = vec3(10.0 * cos(time * 3.0), 10.0 + 3.0 * sin(time), 10.0 * sin(time * 3.0));
      lights[2].ambientWeight = vec3(0.0, 0.0, 1.0);
      lights[2].diffuseWeight = vec3(0.0, 0.0, 1.0);
      lights[2].specularWeight = vec3(0.0, 0.0, 1.0);

      if (d < 0.0001) {
          vec3 normal = calcNormal(rayPosition);


          color = vec3(0.0);
          for (int i = 0; i < 3; i++) {


              Light light = lights[i];

              vec3 vecToLight = normalize(light.position - rayPosition);
              float diffuseWeight = max(dot(normal, vecToLight), 0.0);

              vec3 reflectVec = normalize(reflect(-vecToLight, normal));
              float specularWeight = pow(max(dot(reflectVec, -rayDirection), 0.0), 16.0);

              float shadow = calcSoftShadow(rayPosition + normal * 0.01, light.position);

              vec3 c = vec3(0.3, 0.3, 0.3) * light.ambientWeight + vec3(0.5, 0.5, 0.5) * light.diffuseWeight * diffuseWeight * shadow + vec3(1.0, 1.0, 1.0) * light.specularWeight * specularWeight;
              // c *= shadow;
              color += c;
              // color *= shadow;
          }
      }

      gl_FragColor = vec4(color, 1.0);
  }

</script>
<script id="shader-vs" type="x-shader/x-vertex">
  attribute vec3 position;

  void main(void) {
    gl_Position = vec4(position, 1.0);
  }
</script>
<canvas id="canvas">
</canvas>
              
            
!

CSS

              
                    body {
      margin: 0;
      overflow: hidden;
    }
              
            
!

JS

              
                var canvas;
var gl;

window.onload = function() {
  var fragmentShader, vertexShader;
  var shaderProgram;
  var mouse;
  var startTime;
  var positionAttribute;
  var timeUniformLocatoin, mouseUniformLocation, resolutionUniformLocation;
  var verticesBuffer;
  var vertices;

  canvas = document.getElementById("canvas");
  canvas.width = window.innerWidth;
  canvas.height = window.innerHeight;
  gl = canvas.getContext("webgl") || canvas.getContext("experimental-webgl");

  if (!gl) {
    console.error('can not get context');
    return;
  }

  canvas.addEventListener('mousemove', function(e) {
    mouse = [e.offsetX, canvas.height - e.offsetY];
  });
  mouse = [0, 0];
  startTime = new Date().getTime();

  fragmentShader = getShader('shader-fs');
  vertexShader = getShader('shader-vs');
  shaderProgram = gl.createProgram();
  gl.attachShader(shaderProgram, vertexShader);
  gl.attachShader(shaderProgram, fragmentShader);
  gl.linkProgram(shaderProgram);

  if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
    console.error('can not initialize shader program');
    return;
  }

  gl.useProgram(shaderProgram);
  positionAttribute = gl.getAttribLocation(shaderProgram, 'position');
  gl.enableVertexAttribArray(positionAttribute);

  timeUniformLocatoin = gl.getUniformLocation(shaderProgram, 'u_time');
  mouseUniformLocation = gl.getUniformLocation(shaderProgram, 'u_mouse');
  resolutionUniformLocation = gl.getUniformLocation(shaderProgram, 'u_resolution');

  verticesBuffer = gl.createBuffer();
  gl.bindBuffer(gl.ARRAY_BUFFER, verticesBuffer);
  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
  ];
  gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);

  gl.clearColor(0.0, 0.0, 0.0, 1.0);
  render();

  function getShader(id) {
    var shaderScript, shader;

    shaderScript = document.getElementById(id);
    if(!shaderScript) {
      return null;
    }

    if (shaderScript.type == 'x-shader/x-fragment') {
      shader = gl.createShader(gl.FRAGMENT_SHADER);
    } else if (shaderScript.type == 'x-shader/x-vertex') {
      shader = gl.createShader(gl.VERTEX_SHADER);
    } else {
      return null;
    }

    gl.shaderSource(shader, shaderScript.text);
    gl.compileShader(shader);

    if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
      console.error('can not comple shader source');
      console.error(gl.getShaderInfoLog(shader));
      return null;
    }

    return shader;
  }

  function render() {
    var time, resolution;

    requestAnimationFrame(render);
    gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);

    time = (new Date().getTime() - startTime);
    resolution = [canvas.width, canvas.height];
    gl.uniform1f(timeUniformLocatoin, time);
    gl.uniform2fv(mouseUniformLocation, mouse);
    gl.uniform2fv(resolutionUniformLocation, resolution);

    gl.bindBuffer(gl.ARRAY_BUFFER, verticesBuffer);
    gl.vertexAttribPointer(verticesBuffer, 3, gl.FLOAT, false, 0, 0);
    gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
    gl.flush();
  }
}

window.onresize = function() {
    canvas.width = window.innerWidth;
    canvas.height = window.innerHeight;
    gl.viewport(0, 0, canvas.width, canvas.height);
}

              
            
!
999px

Console