cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

            
              <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;

vec3 repeat(vec3 p, vec3 interval) {
    return mod(p, 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 calcSphereDistance(vec3 p, float size) {
    return length(p) - size;
}

float calcDistance(vec3 p) {
    vec3 q = repeat(p, vec3(5.0));
    return calcSphereDistance(q, 0.5);
}

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))
    ));
}

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);

    vec3 cameraPosition = vec3(0.0, 0.0, 0.0);
    vec3 viewCenter = cameraPosition + vec3(cos(mouse.x * PI), sin(mouse.y * HALF_PI), sin(mouse.x * PI));
    vec3 viewDirection = normalize(viewCenter - cameraPosition);

    float fov = 60.0;
    float angle = fov / 2.0 * PI / 180.0;

    vec3 cameraTop = vec3(0.0, 1.0, 0.0);
    vec3 cameraSide = cross(viewDirection, cameraTop);
    vec3 rayDirection = normalize(cameraSide * sin(angle * st.x) + cameraTop * sin(angle * st.y) + viewDirection * cos(angle * st.x));


    vec3 rayPosition = cameraPosition;
    float radian = u_time * 0.0003;
    vec3 lightPosition = vec3(5.0, 5.0, 5.0);
    vec3 color = vec3(0.0);
    float d;
    for (int i = 0; i < 256; i++) {
        d = calcDistance(rayPosition);
        rayPosition += rayDirection * d;
    }

    if (d < 0.0001) {
        vec3 normal = calcNormal(rayPosition);
        float weight = dot(normal, normalize(lightPosition));
        color = vec3(1.0) * max(weight, 0.0) + vec3(0.1);
    }

    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>
            
          
!
            
                  body {
      margin: 0;
      overflow: hidden;
    }
            
          
!
            
              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
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console