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 CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

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.

            
              <canvas></canvas>
<div id="controls"></div>
<script id="vs" type="notjs">
uniform mat4 u_worldViewProjection;

attribute vec4 a_position;
attribute vec2 a_texcoord;

varying vec4 v_position;
varying vec2 v_texCoord;

void main() {
  v_texCoord = a_texcoord;
  gl_Position = u_worldViewProjection * a_position;
}
</script>
<script id="fs" type="notjs">
precision mediump float;
uniform sampler2D u_texture;
uniform vec4 u_frontColor;
uniform vec4 u_backColor;
varying vec2 v_texCoord;

void main() {
  gl_FragColor = texture2D(u_texture, v_texCoord) * (gl_FrontFacing ? u_frontColor : u_backColor);
}
</script>
<script src="//twgljs.org/dist/twgl-full.min.js"></script>

            
          
!
            
              body, html {
  height: 100%;
  margin: 0;
  font-family: monospace;
}
canvas {
  width: 100%;
  height: 100%;
}
#controls {
  position: absolute;
  left: 10px;
  top: 10px;
  z-index: 2;
  background-color: rgba(0,0,0,0.5);
  width: 30%;
  padding: 5px;
}
input[type=range] {
  width: 100%;
  display: block;
}

            
          
!
            
              "use strict";
var m4 = twgl.m4;
var $ = document.getElementById.bind(document);
twgl.setAttributePrefix("a_");
var gl = twgl.getWebGLContext(document.querySelector("canvas"));
                                                 var programInfo = twgl.createProgramInfo(gl, ["vs", "fs"], ["a_position", "a_texcoord"]);

var bufferInfo = twgl.primitives.createSphereBufferInfo(gl, 1, 24, 12);
var bufferInfo2 = twgl.primitives.createCubeBufferInfo(gl, 2);

var textures = twgl.createTextures(gl, {
  // A 2x2 pixel texture from a JavaScript array
  checker: {
    mag: gl.NEAREST,
    min: gl.LINEAR,
    src: [
      255, 255, 255, 255,
      192, 192, 192, 255,
      192, 192, 192, 255,
      255, 255, 255, 255,
    ],
  },
});

var g = {
  zNear: 3,
  zFar: 7,
  fovy: 45,
};

function setupRange(id, value, min, max) {
  var range = document.createElement("input");
  range.type = "range";
  range.value = value;
  range.min = min;
  range.max = max;
  range.step = 0.01;
  var label = document.createElement("label");
  label.appendChild(document.createTextNode(id + ": "));
  var span = document.createTextNode("");
  range.addEventListener('input', function(e) {
    span.nodeValue = e.target.value;
    g[id] = parseFloat(e.target.value);
  });
  span.nodeValue = range.value;
  label.appendChild(span);
  $("controls").appendChild(label);
  $("controls").appendChild(range);
}

setupRange("zNear", 3, 3, 7);
setupRange("zFar", 7, 3, 7);
//setupRange("fovy", 45, 20, 70);

var uniforms = {
  u_worldViewProjection: m4.identity(),
  u_frontColor: [1, 0, 1, 1],
  u_backColor: [0, 1, 1, 1],
  u_texture: textures.checker,
};  

var projection;

function draw(bufferInfo, translation, time) {
  var world =  m4.identity();
  m4.translate(world, translation, world);
  m4.rotateY(world, time, world);
  m4.rotateZ(world, time, world);
  m4.multiply(world, projection, uniforms.u_worldViewProjection);

  twgl.setBuffersAndAttributes(gl, programInfo, bufferInfo);
  twgl.setUniforms(programInfo, uniforms);
  twgl.drawBufferInfo(gl, gl.TRIANGLES, bufferInfo);
}

function render(time) {
  time *= 0.001;
  twgl.resizeCanvasToDisplaySize(gl.canvas);
  gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);
  gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
  gl.enable(gl.DEPTH_TEST);

  var aspect = gl.canvas.clientWidth / gl.canvas.clientHeight;
  projection = m4.perspective(g.fovy * Math.PI / 180, aspect, g.zNear, g.zFar);
  
  gl.useProgram(programInfo.program);
  
  draw(bufferInfo , [ 2, 0, -5], time);
  draw(bufferInfo2, [-2, 0, -5], time);
  
  requestAnimationFrame(render);
}
requestAnimationFrame(render);

            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console