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

              
                <script type="text/fragment" id="vertShader">
  attribute vec3 position;
  attribute vec3 normal;
  attribute vec2 uv;
  uniform mat4 u_viewMatrix;
  uniform mat4 u_modelMatrix;
  uniform mat4 u_modelViewMatrix;
  uniform mat4 u_projectionMatrix;
  uniform mat3 u_normalMatrix;
  varying vec3 vNormal;
  varying vec3 vPosition;
  varying vec3 vWorldPosition;
  varying vec2 vUV;
  
  void main() {
    vNormal = normalize(u_normalMatrix * normal);
    vUV = uv;
    vec4 camPos = u_projectionMatrix * u_modelViewMatrix * vec4(position, 1.0);
    
    gl_Position = camPos;
    
    vWorldPosition = (u_modelMatrix * vec4(0,0,0, 1.0)).xyz;
    vPosition = (u_modelMatrix * vec4(position, 1.0)).xyz;
  }
</script>
<script type="text/fragment" id="fragShader">
  precision highp float;
  varying vec3 vNormal;
  varying vec2 vUV;
  varying vec4 camPos;
  varying vec3 vPosition;
  varying vec3 vWorldPosition;
  uniform vec3 u_cameraPosition;
  
  uniform mat4 u_matrix;
  
  /* Raymarching constants */
  /* --------------------- */
  const float MAX_TRACE_DISTANCE = 10.;             // max trace distance
  const float INTERSECTION_PRECISION = 0.001;       // precision of the intersection
  const int NUM_OF_TRACE_STEPS = 128;               // max number of trace steps
  const float STEP_MULTIPLIER = .05;
  
  /* Structures */
  /* ---------- */
  struct Camera {
    vec3 ro;
    vec3 rd;
    float FOV;
  };
  struct Surface {
    float len;
    vec3 position;
    vec3 colour;
    float id;
    float steps;
    float AO;
  };
  struct Model {
    float dist;
    vec3 colour;
    float id;
  };
  
  Camera getCamera(in vec3 ro, in vec3 rd, in float FOV) {
    return Camera(
      ro,
      rd,
      FOV
    );
  }
  float sdBoxFrame( vec3 p, vec3 b, float e ) {
    p = abs(p  )-b;
    vec3 q = abs(p+e)-e;
    return min(min(
        length(max(vec3(p.x,q.y,q.z),0.0))+min(max(p.x,max(q.y,q.z)),0.0),
        length(max(vec3(q.x,p.y,q.z),0.0))+min(max(q.x,max(p.y,q.z)),0.0)),
        length(max(vec3(q.x,q.y,p.z),0.0))+min(max(q.x,max(q.y,p.z)),0.0));
  }
  float sdRoundBox( vec3 p, vec3 b, float r ) {
    vec3 q = abs(p) - b;
    return length(max(q,0.0)) + min(max(q.x,max(q.y,q.z)),0.0) - r;
  }
  
  Model map( vec3 p ){
    vec3 pos = (vec4(p-vWorldPosition, 1.) * u_matrix).xyz; // Transforming the cube position with the supplied transformation array
    pos = mod(pos - .5, 1.) - .5;
    return Model(
      min(
        sdBoxFrame(pos, vec3(.35), .001) - .01,
        sdRoundBox(pos, vec3(.1), .01)
        ), vec3(.5), 0.);
    // return Model(sdRoundBox(pos, vec3(.4), .05), vec3(.5), 0.);
    // return Model(distance(p,vWorldPosition)-.4, vec3(.5), 0.);
  }
  
  Surface calcIntersection( in Camera cam ){
    float h =  INTERSECTION_PRECISION*2.0;
    float rayDepth = 0.0;
    float hitDepth = -1.0;
    float id = -1.;
    float steps = 0.;
    float ao = 0.;
    vec3 position;
    vec3 colour;
    
    for( int i=0; i< NUM_OF_TRACE_STEPS ; i++ ) {
      if( abs(h) < INTERSECTION_PRECISION || rayDepth > MAX_TRACE_DISTANCE ) break;
      position = cam.ro+cam.rd*rayDepth;
      Model m = map( position );
      h = m.dist;
      rayDepth += h * STEP_MULTIPLIER;
      id = m.id;
      steps += 1.;
      ao += max(h, 0.);
      colour = m.colour;
    }

    if( rayDepth < MAX_TRACE_DISTANCE ) hitDepth = rayDepth;

    return Surface( hitDepth, position, colour, id, steps, ao );
  }
  
  void main() {
    
    Camera cam = getCamera((vec4(vPosition, 1.)).xyz, normalize(vPosition - u_cameraPosition), 1.4);
    
    vec3 normal = normalize(vNormal);
    float lighting = dot(normal, normalize(vec3(-0.3, 0.8, 0.6)));
    vec3 colour = vec3(.2, 0.8, 1.) + normal * .2 + lighting * lighting * 0.3;
    gl_FragColor = vec4(vec3(0), 1.);
    
    Surface s = calcIntersection(cam);
    gl_FragColor += vec4(.5,.5,.9,1.0) * (1. - s.AO * .25);
    
    
    gl_FragColor.rgb = mix(gl_FragColor.rgb, colour, length(sign(colour)*.5));
  }
</script>
              
            
!

CSS

              
                body {
  background: #333;
  color: #fff;
  font-family: sans-serif;
}
body,
html {
  margin: 0;
  overflow: hidden;
  padding: 0;
}
canvas {
  width:100%;
  height:100%;
  }

              
            
!

JS

              
                import { Renderer, Camera, DollyCamera, Obj, Program, Mesh, Box, Uniform } from 'https://cdn.skypack.dev/wtc-gl@1.0.0-beta.15';
import { Vec2, Vec3, Mat4 } from "https://cdn.skypack.dev/wtc-math";

console.clear();

const shaderV = document.querySelector('#vertShader').innerText;
const shaderF = document.querySelector('#fragShader').innerText;

const renderer = new Renderer({
  autoClear: true,
  dpr: 2
});
const gl = renderer.gl;
document.body.appendChild(gl.canvas);
gl.clearColor(0,.05,.1, 1);

const camera = new DollyCamera();
camera.position.reset(0, .5, 1.85);
camera.fov = 85;
camera.lookAt(new Vec3(0, 0, 0));

function resize() {
  renderer.dimensions = new Vec2(window.innerWidth, window.innerHeight);
  camera.perspective({ aspect: gl.canvas.width / gl.canvas.height });
}
window.addEventListener("resize", resize, false);
resize();

const scene = new Obj();

// const planeGeometry = new Plane(gl);
const cubeGeometry = new Box(gl);

const program = new Program(gl, {
  vertex: shaderV,
  fragment: shaderF,
  cullFace: null
});

const cube = new Mesh(gl, { geometry: cubeGeometry, program });
cube.position.reset(0, 0, 0);
cube.setParent(scene);

requestAnimationFrame(update);
function update(t) {
  requestAnimationFrame(update);
  
  // camera.update();
  
  const modifier = .5;
  const rot = new Vec3(0.01, 0.04, 0.02);
  rot.scale(modifier);

  cube.rotation.add(rot);
  // cube.scale.length = (Math.cos(t * 0.001 * modifier) * 0.3 + 0.8) * 3;
  cube.updateRotation();

  // cube.position.x = Math.sin(t * 0.001 * modifier)*2.;
  // cube.position.z = Math.cos(t * 0.001 * modifier)*2.;
  
  // camera.lookAt(cube.position)
  
  program.uniforms.u_matrix = new Uniform({name: 'mat', value: cube.matrix.array, type: 'mat4'});
  
  cube.updateMatrixWorld();
  
  renderer.render({ scene, camera });
}

              
            
!
999px

Console