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.

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