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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <html lang="en">
  <head>
    <meta charset="utf-8" />
    <title>Hello Triangle</title>
  </head>

  <body onload="main()">
    <canvas id="webgl" width="400" height="400"></canvas>
  </body>
</html>

            
          
!
            
              
// 顶点着色器源码
var VSHADER_SOURCE =
  'attribute vec4 a_Position;\n' +
  'attribute vec4 a_Color;\n' +
  'varying vec4 v_Color;\n' +
  'uniform mat4 m_Matrix;\n' +
  'uniform mat4 v_Matrix;\n' +
  'uniform mat4 p_Matrix;\n' +
  'void main() {\n' +
  '  gl_Position = p_Matrix * v_Matrix * m_Matrix * a_Position;\n' +
  '  v_Color = a_Color;\n' +
  '}\n';

// 片元着色器源码
var FSHADER_SOURCE =
  'precision mediump float;\n'+
  'varying vec4 v_Color;\n' +
  'void main() {\n' +
  '  gl_FragColor = v_Color;\n' +
  '}\n';


function main() {

  // Part 1
  var canvas = document.getElementById('webgl');
  var gl = canvas.getContext('experimental-webgl');
  if (!gl) {
    console.warn('获取WebGL上下文失败');
    return;
  }
  // 至此获取了webgl上下文gl

  // Part2
  var vshader = gl.createShader(gl.VERTEX_SHADER),
    fshader = gl.createShader(gl.FRAGMENT_SHADER);
  gl.shaderSource(vshader, VSHADER_SOURCE);
  gl.shaderSource(fshader, FSHADER_SOURCE);
  gl.compileShader(vshader);
  gl.compileShader(fshader);
  var program = gl.createProgram();
  gl.attachShader(program, vshader);
  gl.attachShader(program, fshader);
  gl.linkProgram(program);
  // 至此将着色器源码编译为了着色器程序program

  gl.useProgram(program);

  // Par3
  var varray = new Float32Array([-1, -1, 0, 1, -1, 0, 0, 1, 0])
  var vbuffer = gl.createBuffer();
  gl.bindBuffer(gl.ARRAY_BUFFER, vbuffer);
  gl.bufferData(gl.ARRAY_BUFFER, varray, gl.STATIC_DRAW);
  // 至此将三个顶点数据填入gl.ARRAY_BUFFER中
  var aloc = gl.getAttribLocation(program, 'a_Position');
  gl.vertexAttribPointer(aloc, 3, gl.FLOAT, false, 0, 0);
  gl.enableVertexAttribArray(aloc);
  // 至此将gl.ARRAY_BUFFER中的数据分配给了着色器中的attribute变量a_Position
  
  var carray = new Float32Array([1,0,0,0,1,0,0,0,1]);
  var cbuffer = gl.createBuffer();
  gl.bindBuffer(gl.ARRAY_BUFFER, cbuffer);
  gl.bufferData(gl.ARRAY_BUFFER, carray, gl.STATIC_DRAW);
  // 至此将三个顶点的颜色填入gl.ARRAY_BUFFER中
  var cloc = gl.getAttribLocation(program, 'a_Color');
  gl.vertexAttribPointer(cloc, 3, gl.FLOAT, false, 0, 0);
  gl.enableVertexAttribArray(cloc);
  // 至此将gl.ARRAY_BUFFER中的数据分配给了着色器中的attribute变量a_Color
  
  var mloc = gl.getUniformLocation(program, 'm_Matrix');
  var vloc = gl.getUniformLocation(program, 'v_Matrix');
  var ploc = gl.getUniformLocation(program, 'p_Matrix');
  var mmatrix = new Float32Array([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, -1, 1]);
  var vmatrix = new Float32Array([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, -1, 1]);
  var pmatrix = new Float32Array([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, -2, -1, 0, 0, -3, 0]);
  gl.uniformMatrix4fv(mloc, false, mmatrix);
  gl.uniformMatrix4fv(vloc, false, vmatrix);
  gl.uniformMatrix4fv(ploc, false, pmatrix);
  // 至此设置好着色器中三个uniform变量的值
  
  // Part4
  gl.clearColor(0, 0, 0, 1);
  gl.clear(gl.COLOR_BUFFER_BIT);
  gl.drawArrays(gl.TRIANGLES, 0, 3);
  // 至此画出了三角形
}
            
          
!
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.

Console