一. 做三维只需要再迈出一小步
二维例子中的二维点 (x, y) 与 3x3 的矩阵相乘, 在三维中我们需要三维点 (x, y, z) 与 4x4 的矩阵相乘。
让我们将上个例子改成三维的,这里会继续使用 F ,但是这次是三维的 ‘F’ 。
首先需要修改顶点着色器以支持三维处理,这是原顶点着色器,
<script id="vertex-shader-2d" type="x-shader/x-vertex">attribute vec2 a_position;uniform mat3 u_matrix;void main() {// 将位置和矩阵相乘gl_Position = vec4((u_matrix * vec3(a_position, 1)).xy, 0, 1);}</script>// 对比前后<script id="vertex-shader-3d" type="x-shader/x-vertex">attribute vec4 a_position;uniform mat4 u_matrix;void main() {// 将位置和矩阵相乘gl_Position = u_matrix * a_position;}</script>
它甚至变简单了!在二维中我们提供x和y并设置z为1, 在三维中我们将提供x,y和z,然后将w设置为1, 而在属性中w的默认值就是1,我们可以利用这点不用再次设置。然后提供三维数据。
二. 二维中的点的操作
...// 告诉属性怎么从 positionBuffer (ARRAY_BUFFER) 中读取位置var size = 3; // 每次迭代使用 3 个单位的数据var type = gl.FLOAT; // 单位数据类型是32位的浮点型var normalize = false; // 不需要归一化数据var stride = 0; // 0 = 移动距离 * 单位距离长度sizeof(type) 每次迭代跳多少距离到下一个数据var offset = 0; // 从绑定缓冲的起始处开始gl.vertexAttribPointer(positionAttributeLocation, size, type, normalize, stride, offset);...// 填充当前 ARRAY_BUFFER 缓冲// 使用组成 'F' 的数据填充缓冲.function setGeometry(gl) {gl.bufferData(gl.ARRAY_BUFFER,new Float32Array([// 左竖0, 0, 0,30, 0, 0,0, 150, 0,0, 150, 0,30, 0, 0,30, 150, 0,// 上横30, 0, 0,100, 0, 0,30, 30, 0,30, 30, 0,100, 0, 0,100, 30, 0,// 下横30, 60, 0,67, 60, 0,30, 90, 0,30, 90, 0,67, 60, 0,67, 90, 0]),gl.STATIC_DRAW);}// 对应的二维矩阵var m3 = {translation: function translation(tx, ty) {return [1, 0, 0,0, 1, 0,tx, ty, 1];},rotation: function rotation(angleInRadians) {var c = Math.cos(angleInRadians);var s = Math.sin(angleInRadians);return [c,-s, 0,s, c, 0,0, 0, 1];},scaling: function scaling(sx, sy) {return [sx, 0, 0,0, sy, 0,0, 0, 1];},};
三. 三维中的点的操作
var m4 = {translation: function(tx, ty, tz) {return [1, 0, 0, 0,0, 1, 0, 0,0, 0, 1, 0,tx, ty, tz, 1,];},xRotation: function(angleInRadians) {var c = Math.cos(angleInRadians);var s = Math.sin(angleInRadians);return [1, 0, 0, 0,0, c, s, 0,0, -s, c, 0,0, 0, 0, 1,];},yRotation: function(angleInRadians) {var c = Math.cos(angleInRadians);var s = Math.sin(angleInRadians);return [c, 0, -s, 0,0, 1, 0, 0,s, 0, c, 0,0, 0, 0, 1,];},zRotation: function(angleInRadians) {var c = Math.cos(angleInRadians);var s = Math.sin(angleInRadians);return [c, s, 0, 0,-s, c, 0, 0,0, 0, 1, 0,0, 0, 0, 1,];},scaling: function(sx, sy, sz) {return [sx, 0, 0, 0,0, sy, 0, 0,0, 0, sz, 0,0, 0, 0, 1,];},};
注意到我们现在有三个旋转方法,在二维中只需要一个是因为我们只需要绕 Z 轴旋转,现在在三维中还可以绕 X 轴和 Y 轴旋转。它们看起来还是很简单, 如果使用它们后你会发现和之前一样
绕 Z 轴旋转
newX = x c + y s;
newY = x -s + y c;
绕 Y 轴旋转
newX = x c + z s;
newZ = x -s + z c;
绕 X 轴旋转
newY = y c + z s;
newZ = y -s + z c;
它们提供这些旋转方式。
四. 更新对应的点的矩阵运算
// 在一些方法上面做一些简单运算 ex:m4.translate(matrix做一些矩阵运算)translate: function(m, tx, ty, tz) {return m4.multiply(m, m4.translation(tx, ty, tz));},xRotate: function(m, angleInRadians) {return m4.multiply(m, m4.xRotation(angleInRadians));},yRotate: function(m, angleInRadians) {return m4.multiply(m, m4.yRotation(angleInRadians));},zRotate: function(m, angleInRadians) {return m4.multiply(m, m4.zRotation(angleInRadians));},scale: function(m, sx, sy, sz) {return m4.multiply(m, m4.scaling(sx, sy, sz));},
projection: function (width, height) {// 注意:这个矩阵翻转了 Y 轴,所以 0 在上方return [2 / width, 0, 0,0, -2 / height, 0,-1, 1, 1];},}// 它将像素坐标转换到裁剪空间,在初次尝试三维时我们将这样做projection: function(width, height, depth) {// 注意:这个矩阵翻转了 Y 轴,所以 0 在上方return [2 / width, 0, 0, 0,0, -2 / height, 0, 0,0, 0, 2 / depth, 0,-1, 1, 0, 1,];},
就像 X 和 Y 需要从像素空间转换到裁剪空间一样,Z 也需要。 在这个例子中我也将 Z 单位化了,我会传递一些和 width 相似的值给 depth ,所以我们的空间将会是 0 到 width 像素宽,0 到 height 像素高, 但是对于depth将会是 -depth / 2 到 +depth / 2 。
最后需要更新计算矩阵的代码
// 计算矩阵【透视图中这个又可以优化】var matrix = m4.projection(gl.canvas.clientWidth, gl.canvas.clientHeight, 400);matrix = m4.translate(matrix, translation[0], translation[1], translation[2]);matrix = m4.xRotate(matrix, rotation[0]);matrix = m4.yRotate(matrix, rotation[1]);matrix = m4.zRotate(matrix, rotation[2]);matrix = m4.scale(matrix, scale[0], scale[1], scale[2]);// 设置矩阵gl.uniformMatrix4fv(matrixLocation, false, matrix);
五. 三维中的点基本流程 【 正视投影 】
// javascript"use strict";function main() {// Get A WebGL context/** @type {HTMLCanvasElement} */var canvas = document.querySelector("#canvas");var gl = canvas.getContext("webgl");if (!gl) {return;}// setup GLSL programvar program = webglUtils.createProgramFromScripts(gl, ["vertex-shader-3d", "fragment-shader-3d"]);// look up where the vertex data needs to go.var positionLocation = gl.getAttribLocation(program, "a_position");// lookup uniformsvar colorLocation = gl.getUniformLocation(program, "u_color");var matrixLocation = gl.getUniformLocation(program, "u_matrix");// Create a buffer to put positions invar positionBuffer = gl.createBuffer();// Bind it to ARRAY_BUFFER (think of it as ARRAY_BUFFER = positionBuffer)gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);// Put geometry data into buffersetGeometry(gl);function radToDeg(r) {return r * 180 / Math.PI;}function degToRad(d) {return d * Math.PI / 180;}var translation = [45, 150, 0];var rotation = [degToRad(40), degToRad(25), degToRad(325)];var scale = [1, 1, 1];var color = [Math.random(), Math.random(), Math.random(), 1];drawScene();// Setup a ui.webglLessonsUI.setupSlider("#x", {value: translation[0], slide: updatePosition(0), max: gl.canvas.width });webglLessonsUI.setupSlider("#y", {value: translation[1], slide: updatePosition(1), max: gl.canvas.height});webglLessonsUI.setupSlider("#z", {value: translation[2], slide: updatePosition(2), max: gl.canvas.height});webglLessonsUI.setupSlider("#angleX", {value: radToDeg(rotation[0]), slide: updateRotation(0), max: 360});webglLessonsUI.setupSlider("#angleY", {value: radToDeg(rotation[1]), slide: updateRotation(1), max: 360});webglLessonsUI.setupSlider("#angleZ", {value: radToDeg(rotation[2]), slide: updateRotation(2), max: 360});webglLessonsUI.setupSlider("#scaleX", {value: scale[0], slide: updateScale(0), min: -5, max: 5, step: 0.01, precision: 2});webglLessonsUI.setupSlider("#scaleY", {value: scale[1], slide: updateScale(1), min: -5, max: 5, step: 0.01, precision: 2});webglLessonsUI.setupSlider("#scaleZ", {value: scale[2], slide: updateScale(2), min: -5, max: 5, step: 0.01, precision: 2});function updatePosition(index) {return function(event, ui) {translation[index] = ui.value;drawScene();};}function updateRotation(index) {return function(event, ui) {var angleInDegrees = ui.value;var angleInRadians = angleInDegrees * Math.PI / 180;rotation[index] = angleInRadians;drawScene();};}function updateScale(index) {return function(event, ui) {scale[index] = ui.value;drawScene();};}// Draw the scene.function drawScene() {webglUtils.resizeCanvasToDisplaySize(gl.canvas);// Tell WebGL how to convert from clip space to pixelsgl.viewport(0, 0, gl.canvas.width, gl.canvas.height);// Clear the canvas.gl.clear(gl.COLOR_BUFFER_BIT);// Tell it to use our program (pair of shaders)gl.useProgram(program);// Turn on the attributegl.enableVertexAttribArray(positionLocation);// Bind the position buffer.gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);// Tell the attribute how to get data out of positionBuffer (ARRAY_BUFFER)var size = 3; // 3 components per iterationvar type = gl.FLOAT; // the data is 32bit floatsvar normalize = false; // don't normalize the datavar stride = 0; // 0 = move forward size * sizeof(type) each iteration to get the next positionvar offset = 0; // start at the beginning of the buffergl.vertexAttribPointer(positionLocation, size, type, normalize, stride, offset);// set the colorgl.uniform4fv(colorLocation, color);// Compute the matricesvar matrix = m4.projection(gl.canvas.clientWidth, gl.canvas.clientHeight, 400);matrix = m4.translate(matrix, translation[0], translation[1], translation[2]);matrix = m4.xRotate(matrix, rotation[0]);matrix = m4.yRotate(matrix, rotation[1]);matrix = m4.zRotate(matrix, rotation[2]);matrix = m4.scale(matrix, scale[0], scale[1], scale[2]);// Set the matrix.gl.uniformMatrix4fv(matrixLocation, false, matrix);// Draw the geometry.var primitiveType = gl.TRIANGLES;var offset = 0;var count = 18; // 6 triangles in the 'F', 3 points per trianglegl.drawArrays(primitiveType, offset, count);}}var m4 = {projection: function(width, height, depth) {// Note: This matrix flips the Y axis so 0 is at the top.return [2 / width, 0, 0, 0,0, -2 / height, 0, 0,0, 0, 2 / depth, 0,-1, 1, 0, 1,];},multiply: function(a, b) {var a00 = a[0 * 4 + 0];var a01 = a[0 * 4 + 1];var a02 = a[0 * 4 + 2];var a03 = a[0 * 4 + 3];var a10 = a[1 * 4 + 0];var a11 = a[1 * 4 + 1];var a12 = a[1 * 4 + 2];var a13 = a[1 * 4 + 3];var a20 = a[2 * 4 + 0];var a21 = a[2 * 4 + 1];var a22 = a[2 * 4 + 2];var a23 = a[2 * 4 + 3];var a30 = a[3 * 4 + 0];var a31 = a[3 * 4 + 1];var a32 = a[3 * 4 + 2];var a33 = a[3 * 4 + 3];var b00 = b[0 * 4 + 0];var b01 = b[0 * 4 + 1];var b02 = b[0 * 4 + 2];var b03 = b[0 * 4 + 3];var b10 = b[1 * 4 + 0];var b11 = b[1 * 4 + 1];var b12 = b[1 * 4 + 2];var b13 = b[1 * 4 + 3];var b20 = b[2 * 4 + 0];var b21 = b[2 * 4 + 1];var b22 = b[2 * 4 + 2];var b23 = b[2 * 4 + 3];var b30 = b[3 * 4 + 0];var b31 = b[3 * 4 + 1];var b32 = b[3 * 4 + 2];var b33 = b[3 * 4 + 3];return [b00 * a00 + b01 * a10 + b02 * a20 + b03 * a30,b00 * a01 + b01 * a11 + b02 * a21 + b03 * a31,b00 * a02 + b01 * a12 + b02 * a22 + b03 * a32,b00 * a03 + b01 * a13 + b02 * a23 + b03 * a33,b10 * a00 + b11 * a10 + b12 * a20 + b13 * a30,b10 * a01 + b11 * a11 + b12 * a21 + b13 * a31,b10 * a02 + b11 * a12 + b12 * a22 + b13 * a32,b10 * a03 + b11 * a13 + b12 * a23 + b13 * a33,b20 * a00 + b21 * a10 + b22 * a20 + b23 * a30,b20 * a01 + b21 * a11 + b22 * a21 + b23 * a31,b20 * a02 + b21 * a12 + b22 * a22 + b23 * a32,b20 * a03 + b21 * a13 + b22 * a23 + b23 * a33,b30 * a00 + b31 * a10 + b32 * a20 + b33 * a30,b30 * a01 + b31 * a11 + b32 * a21 + b33 * a31,b30 * a02 + b31 * a12 + b32 * a22 + b33 * a32,b30 * a03 + b31 * a13 + b32 * a23 + b33 * a33,];},translation: function(tx, ty, tz) {return [1, 0, 0, 0,0, 1, 0, 0,0, 0, 1, 0,tx, ty, tz, 1,];},xRotation: function(angleInRadians) {var c = Math.cos(angleInRadians);var s = Math.sin(angleInRadians);return [1, 0, 0, 0,0, c, s, 0,0, -s, c, 0,0, 0, 0, 1,];},yRotation: function(angleInRadians) {var c = Math.cos(angleInRadians);var s = Math.sin(angleInRadians);return [c, 0, -s, 0,0, 1, 0, 0,s, 0, c, 0,0, 0, 0, 1,];},zRotation: function(angleInRadians) {var c = Math.cos(angleInRadians);var s = Math.sin(angleInRadians);return [c, s, 0, 0,-s, c, 0, 0,0, 0, 1, 0,0, 0, 0, 1,];},scaling: function(sx, sy, sz) {return [sx, 0, 0, 0,0, sy, 0, 0,0, 0, sz, 0,0, 0, 0, 1,];},translate: function(m, tx, ty, tz) {return m4.multiply(m, m4.translation(tx, ty, tz));},xRotate: function(m, angleInRadians) {return m4.multiply(m, m4.xRotation(angleInRadians));},yRotate: function(m, angleInRadians) {return m4.multiply(m, m4.yRotation(angleInRadians));},zRotate: function(m, angleInRadians) {return m4.multiply(m, m4.zRotation(angleInRadians));},scale: function(m, sx, sy, sz) {return m4.multiply(m, m4.scaling(sx, sy, sz));},};// Fill the buffer with the values that define a letter 'F'. 其实这个对应一个分存一个模型的所有点;function setGeometry(gl) {gl.bufferData(gl.ARRAY_BUFFER,new Float32Array([// left column0, 0, 0,30, 0, 0,0, 150, 0,0, 150, 0,30, 0, 0,30, 150, 0,// top rung30, 0, 0,100, 0, 0,30, 30, 0,30, 30, 0,100, 0, 0,100, 30, 0,// middle rung30, 60, 0,67, 60, 0,30, 90, 0,30, 90, 0,67, 60, 0,67, 90, 0]),gl.STATIC_DRAW);}main();// html 代码 已经简化好多了;几乎所有的运动都存在矩阵中去了;<canvas id="canvas"></canvas><div id="uiContainer"><div id="ui"><div id="x"></div><div id="y"></div><div id="z"></div><div id="angleX"></div><div id="angleY"></div><div id="angleZ"></div><div id="scaleX"></div><div id="scaleY"></div><div id="scaleZ"></div></div></div><!-- vertex shader --><script id="vertex-shader-3d" type="x-shader/x-vertex">attribute vec4 a_position;uniform mat4 u_matrix;void main() {// Multiply the position by the matrix.gl_Position = u_matrix * a_position;}</script><!-- fragment shader --><script id="fragment-shader-3d" type="x-shader/x-fragment">precision mediump float;uniform vec4 u_color;void main() {gl_FragColor = u_color;}</script><script src="https://webglfundamentals.org/webgl/resources/webgl-utils.js"></script><script src="https://webglfundamentals.org/webgl/resources/webgl-lessons-ui.js"></script>
最后公式 gl_Position = u_matrix * a_position;
六. 拉伸具有三维效果
我们遇到的第一个问题是 F 在三维中过于扁平, 所以很难看出三维效果。解决这个问题的方法是将它拉伸成三维几何体。 现在的 F 是由三个矩形组成,每个矩形两个三角形。让它变三维需要 16 个矩形。 三个矩形在正面,三个背面,一个左侧,四个右侧,两个上侧,三个底面。
需要列出的还有很多,16 个矩形每个有两个三角形,每个三角形有 3 个顶点, 所以一共有 96 个顶点。如果你想看这些可以去示例的源码里找。
其实也就是把对应保存有模型点的轨迹修改就可以
function setGeometry(gl) {gl.bufferData(gl.ARRAY_BUFFER,new Float32Array([// left column front0, 0, 0,30, 0, 0,0, 150, 0,0, 150, 0,30, 0, 0,30, 150, 0,// top rung front30, 0, 0,100, 0, 0,30, 30, 0,30, 30, 0,100, 0, 0,100, 30, 0,// middle rung front30, 60, 0,67, 60, 0,30, 90, 0,30, 90, 0,67, 60, 0,67, 90, 0,// left column back0, 0, 30,30, 0, 30,0, 150, 30,0, 150, 30,30, 0, 30,30, 150, 30,// top rung back30, 0, 30,100, 0, 30,30, 30, 30,30, 30, 30,100, 0, 30,100, 30, 30,// middle rung back30, 60, 30,67, 60, 30,30, 90, 30,30, 90, 30,67, 60, 30,67, 90, 30,// top0, 0, 0,100, 0, 0,100, 0, 30,0, 0, 0,100, 0, 30,0, 0, 30,// top rung right100, 0, 0,100, 30, 0,100, 30, 30,100, 0, 0,100, 30, 30,100, 0, 30,// under top rung30, 30, 0,30, 30, 30,100, 30, 30,30, 30, 0,100, 30, 30,100, 30, 0,// between top rung and middle30, 30, 0,30, 30, 30,30, 60, 30,30, 30, 0,30, 60, 30,30, 60, 0,// top of middle rung30, 60, 0,30, 60, 30,67, 60, 30,30, 60, 0,67, 60, 30,67, 60, 0,// right of middle rung67, 60, 0,67, 60, 30,67, 90, 30,67, 60, 0,67, 90, 30,67, 90, 0,// bottom of middle rung.30, 90, 0,30, 90, 30,67, 90, 30,30, 90, 0,67, 90, 30,67, 90, 0,// right of bottom30, 90, 0,30, 90, 30,30, 150, 30,30, 90, 0,30, 150, 30,30, 150, 0,// bottom0, 150, 0,0, 150, 30,30, 150, 30,0, 150, 0,30, 150, 30,30, 150, 0,// left side0, 0, 0,0, 0, 30,0, 150, 30,0, 0, 0,0, 150, 30,0, 150, 0]),gl.STATIC_DRAW);}
所以可以想象这还是一个比较简单模型;如果模型的复杂度有人物或者那种特别精致的物件;我们还能一个个点去操作这些吗?这个问题留到后面
七. 模型处理不同的颜色
在这之前我们需要修改一下对应的模型上有好几种颜色而不是对应就只有一种颜色
<script id="vertex-shader-3d" type="x-shader/x-vertex">attribute vec4 a_position;attribute vec4 a_color;uniform mat4 u_matrix;varying vec4 v_color;void main() {// 将位置和矩阵相乘.gl_Position = u_matrix * a_position;// 将颜色传递给片断着色器v_color = a_color;}</script>// 片段着色器 使用<script id="fragment-shader-3d" type="x-shader/x-fragment">precision mediump float;// 从顶点着色器中传入varying vec4 v_color;void main() {gl_FragColor = v_color;}</script>
我们需要找到属性的位置,然后在另一个缓冲中存入对应的颜色。
...var colorLocation = gl.getAttribLocation(program, "a_color");...// 给颜色创建一个缓冲var colorBuffer = gl.createBuffer();gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer);// 将颜色值传入缓冲setColors(gl);...// 向缓冲传入 'F' 的颜色值function setColors(gl) {gl.bufferData(gl.ARRAY_BUFFER,new Uint8Array([// 正面左竖200, 70, 120,200, 70, 120,200, 70, 120,200, 70, 120,200, 70, 120,200, 70, 120,// 正面上横200, 70, 120,200, 70, 120,......gl.STATIC_DRAW);}// 在渲染时告诉颜色属性如何从缓冲中获取颜色值// 启用颜色属性gl.enableVertexAttribArray(colorLocation);// 绑定颜色缓冲gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer);// 告诉颜色属性怎么从 colorBuffer (ARRAY_BUFFER) 中读取颜色值var size = 3; // 每次迭代使用3个单位的数据var type = gl.UNSIGNED_BYTE; // 单位数据类型是无符号 8 位整数var normalize = true; // 标准化数据 (从 0-255 转换到 0.0-1.0)var stride = 0; // 0 = 移动距离 * 单位距离长度sizeof(type) 每次迭代跳多少距离到下一个数据var offset = 0; // 从绑定缓冲的起始处开始gl.vertexAttribPointer(colorLocation, size, type, normalize, stride, offset)
八. 正反三角面


不过他好像和我们看到实际那种的模型都不太一样;比如前面有遮挡的时候我们会看不见后面;
你要知道电脑并没有三维的概念;在线性代数中两个点决定一条线;只不过是我们通过规则和数学的方式将图形变化成我们想看到那种形态;很显然现在规则有点问题;排在前面的三角面会比较先画出来然后后面的点就会把前面覆盖;很显然电脑并不知道哪些点是否会重合;而在真实的物理世界那些面是可以看到哪些又是看不到的;
聪明如大家
WebGL中的三角形有正反面的概念,正面三角形的顶点顺序是逆时针方向, 反面三角形是顺时针方向。
gl.enable(gl.CULL_FACE);
将它放在 drawScene 方法里,开启这个特性后WebGL默认“剔除”背面三角形, “剔除”在这里是“不用绘制”的花哨叫法。
对于WebGL而言,一个三角形是顺时针还是逆时针是根据裁剪空间中的顶点顺序判断的, 换句话说,WebGL是根据你在顶点着色器中运算后提供的结果来判定的, 这就意味着如果你把一个顺时针的三角形沿 X 轴缩放 -1 ,它将会变成逆时针, 或者将顺时针的三角形旋转180度后变成逆时针。由于我们没有开启 CULL_FACE, 所以可以同时看到顺时针(正面)和逆时针(反面)三角形。现在开启了, 任何时候正面三角形无论是缩放还是旋转的原因导致翻转了,WebGL就不会绘制它。 这件事很有用,因为通常情况下你只需要看到你正面对的面。
不得不说WEBGL在编写协议的时候就已经给我们准备好了【 当然后面又是一层精致的底层算法 ,他会在一定的时机给你算出对应的点谁在谁物理世界层面的顶部 】

结果对应的数据中显示出如下效果
大多数三角形朝向都是错的, 旋转的时候你会看到背面的三角形,幸好它很容易解决, 我们只需要看看哪些是三角形是反的,然后交换它们的两个顶点
1, 2, 3,40, 50, 60,700, 800, 900,1, 2, 3,700, 800, 900,40, 50, 60,
九. DEPTH BUFFER(深度缓冲)

不过好像对应的点的序列对应的模型部分还有有问题;
这很接近实际效果了但是还有一个问题,即使所有三角形的朝向是正确的, 然后背面的被剔除了,有些应该在背面的部分还是出现在了前面。
接触 DEPTH BUFFER(深度缓冲)
深度缓冲有时也叫 Z-Buffer,是一个存储像素深度的矩形, 一个深度像素对应一个着色像素,在绘制图像时组合使用。 当WebGL绘制每个着色像素时也会写入深度像素, 它的值基于顶点着色器返回的Z值,就像我们将 X 和 Y 转换到裁剪空间一样, Z 也在裁剪空间或者 (-1 到 +1) 。这个值会被转换到深度空间( 0 到 +1), WebGL绘制一个着色像素之前会检查对应的深度像素, 如果对应的深度像素中的深度值小于当前像素的深度值,WebGL就不会绘制新的颜色。 反之它会绘制片断着色器提供的新颜色并更新深度像素中的深度值。 这也意味着在其他像素后面的像素不会被绘制。
我们可以像这样开启这个特性
gl.enable(gl.DEPTH_TEST);// code// 绘制场景function drawScene() {...// 清空画布和深度缓冲gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);...
十. 终极代码
"use strict";function main() {// Get A WebGL context/** @type {HTMLCanvasElement} */var canvas = document.querySelector("#canvas");var gl = canvas.getContext("webgl");if (!gl) {return;}// setup GLSL programvar program = webglUtils.createProgramFromScripts(gl, ["vertex-shader-3d", "fragment-shader-3d"]);// look up where the vertex data needs to go.var positionLocation = gl.getAttribLocation(program, "a_position");var colorLocation = gl.getAttribLocation(program, "a_color");// lookup uniformsvar matrixLocation = gl.getUniformLocation(program, "u_matrix");// Create a buffer to put positions invar positionBuffer = gl.createBuffer();// Bind it to ARRAY_BUFFER (think of it as ARRAY_BUFFER = positionBuffer)gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);// Put geometry data into buffersetGeometry(gl);// Create a buffer to put colors invar colorBuffer = gl.createBuffer();// Bind it to ARRAY_BUFFER (think of it as ARRAY_BUFFER = colorBuffer)gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer);// Put geometry data into buffersetColors(gl);function radToDeg(r) {return r * 180 / Math.PI;}function degToRad(d) {return d * Math.PI / 180;}var translation = [45, 150, 0];var rotation = [degToRad(40), degToRad(25), degToRad(325)];var scale = [1, 1, 1];drawScene();// Setup a ui.webglLessonsUI.setupSlider("#x", {value: translation[0], slide: updatePosition(0), max: gl.canvas.width });webglLessonsUI.setupSlider("#y", {value: translation[1], slide: updatePosition(1), max: gl.canvas.height});webglLessonsUI.setupSlider("#z", {value: translation[2], slide: updatePosition(2), max: gl.canvas.height});webglLessonsUI.setupSlider("#angleX", {value: radToDeg(rotation[0]), slide: updateRotation(0), max: 360});webglLessonsUI.setupSlider("#angleY", {value: radToDeg(rotation[1]), slide: updateRotation(1), max: 360});webglLessonsUI.setupSlider("#angleZ", {value: radToDeg(rotation[2]), slide: updateRotation(2), max: 360});webglLessonsUI.setupSlider("#scaleX", {value: scale[0], slide: updateScale(0), min: -5, max: 5, step: 0.01, precision: 2});webglLessonsUI.setupSlider("#scaleY", {value: scale[1], slide: updateScale(1), min: -5, max: 5, step: 0.01, precision: 2});webglLessonsUI.setupSlider("#scaleZ", {value: scale[2], slide: updateScale(2), min: -5, max: 5, step: 0.01, precision: 2});function updatePosition(index) {return function(event, ui) {translation[index] = ui.value;drawScene();};}function updateRotation(index) {return function(event, ui) {var angleInDegrees = ui.value;var angleInRadians = angleInDegrees * Math.PI / 180;rotation[index] = angleInRadians;drawScene();};}function updateScale(index) {return function(event, ui) {scale[index] = ui.value;drawScene();};}// Draw the scene.function drawScene() {webglUtils.resizeCanvasToDisplaySize(gl.canvas);// Tell WebGL how to convert from clip space to pixelsgl.viewport(0, 0, gl.canvas.width, gl.canvas.height);// Clear the canvas.gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);// Turn on culling. By default backfacing triangles// will be culled.gl.enable(gl.CULL_FACE);// Enable the depth buffergl.enable(gl.DEPTH_TEST);// Tell it to use our program (pair of shaders)gl.useProgram(program);// Turn on the position attributegl.enableVertexAttribArray(positionLocation);// Bind the position buffer.gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);// Tell the position attribute how to get data out of positionBuffer (ARRAY_BUFFER)var size = 3; // 3 components per iterationvar type = gl.FLOAT; // the data is 32bit floatsvar normalize = false; // don't normalize the datavar stride = 0; // 0 = move forward size * sizeof(type) each iteration to get the next positionvar offset = 0; // start at the beginning of the buffergl.vertexAttribPointer(positionLocation, size, type, normalize, stride, offset);// Turn on the color attributegl.enableVertexAttribArray(colorLocation);// Bind the color buffer.gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer);// Tell the attribute how to get data out of colorBuffer (ARRAY_BUFFER)var size = 3; // 3 components per iterationvar type = gl.UNSIGNED_BYTE; // the data is 8bit unsigned valuesvar normalize = true; // normalize the data (convert from 0-255 to 0-1)var stride = 0; // 0 = move forward size * sizeof(type) each iteration to get the next positionvar offset = 0; // start at the beginning of the buffergl.vertexAttribPointer(colorLocation, size, type, normalize, stride, offset);// Compute the matricesvar matrix = m4.projection(gl.canvas.clientWidth, gl.canvas.clientHeight, 400);matrix = m4.translate(matrix, translation[0], translation[1], translation[2]);matrix = m4.xRotate(matrix, rotation[0]);matrix = m4.yRotate(matrix, rotation[1]);matrix = m4.zRotate(matrix, rotation[2]);matrix = m4.scale(matrix, scale[0], scale[1], scale[2]);// Set the matrix.gl.uniformMatrix4fv(matrixLocation, false, matrix);// Draw the geometry.var primitiveType = gl.TRIANGLES;var offset = 0;var count = 16 * 6;gl.drawArrays(primitiveType, offset, count);}}var m4 = {projection: function(width, height, depth) {// Note: This matrix flips the Y axis so 0 is at the top.return [2 / width, 0, 0, 0,0, -2 / height, 0, 0,0, 0, 2 / depth, 0,-1, 1, 0, 1,];},multiply: function(a, b) {var a00 = a[0 * 4 + 0];var a01 = a[0 * 4 + 1];var a02 = a[0 * 4 + 2];var a03 = a[0 * 4 + 3];var a10 = a[1 * 4 + 0];var a11 = a[1 * 4 + 1];var a12 = a[1 * 4 + 2];var a13 = a[1 * 4 + 3];var a20 = a[2 * 4 + 0];var a21 = a[2 * 4 + 1];var a22 = a[2 * 4 + 2];var a23 = a[2 * 4 + 3];var a30 = a[3 * 4 + 0];var a31 = a[3 * 4 + 1];var a32 = a[3 * 4 + 2];var a33 = a[3 * 4 + 3];var b00 = b[0 * 4 + 0];var b01 = b[0 * 4 + 1];var b02 = b[0 * 4 + 2];var b03 = b[0 * 4 + 3];var b10 = b[1 * 4 + 0];var b11 = b[1 * 4 + 1];var b12 = b[1 * 4 + 2];var b13 = b[1 * 4 + 3];var b20 = b[2 * 4 + 0];var b21 = b[2 * 4 + 1];var b22 = b[2 * 4 + 2];var b23 = b[2 * 4 + 3];var b30 = b[3 * 4 + 0];var b31 = b[3 * 4 + 1];var b32 = b[3 * 4 + 2];var b33 = b[3 * 4 + 3];return [b00 * a00 + b01 * a10 + b02 * a20 + b03 * a30,b00 * a01 + b01 * a11 + b02 * a21 + b03 * a31,b00 * a02 + b01 * a12 + b02 * a22 + b03 * a32,b00 * a03 + b01 * a13 + b02 * a23 + b03 * a33,b10 * a00 + b11 * a10 + b12 * a20 + b13 * a30,b10 * a01 + b11 * a11 + b12 * a21 + b13 * a31,b10 * a02 + b11 * a12 + b12 * a22 + b13 * a32,b10 * a03 + b11 * a13 + b12 * a23 + b13 * a33,b20 * a00 + b21 * a10 + b22 * a20 + b23 * a30,b20 * a01 + b21 * a11 + b22 * a21 + b23 * a31,b20 * a02 + b21 * a12 + b22 * a22 + b23 * a32,b20 * a03 + b21 * a13 + b22 * a23 + b23 * a33,b30 * a00 + b31 * a10 + b32 * a20 + b33 * a30,b30 * a01 + b31 * a11 + b32 * a21 + b33 * a31,b30 * a02 + b31 * a12 + b32 * a22 + b33 * a32,b30 * a03 + b31 * a13 + b32 * a23 + b33 * a33,];},translation: function(tx, ty, tz) {return [1, 0, 0, 0,0, 1, 0, 0,0, 0, 1, 0,tx, ty, tz, 1,];},xRotation: function(angleInRadians) {var c = Math.cos(angleInRadians);var s = Math.sin(angleInRadians);return [1, 0, 0, 0,0, c, s, 0,0, -s, c, 0,0, 0, 0, 1,];},yRotation: function(angleInRadians) {var c = Math.cos(angleInRadians);var s = Math.sin(angleInRadians);return [c, 0, -s, 0,0, 1, 0, 0,s, 0, c, 0,0, 0, 0, 1,];},zRotation: function(angleInRadians) {var c = Math.cos(angleInRadians);var s = Math.sin(angleInRadians);return [c, s, 0, 0,-s, c, 0, 0,0, 0, 1, 0,0, 0, 0, 1,];},scaling: function(sx, sy, sz) {return [sx, 0, 0, 0,0, sy, 0, 0,0, 0, sz, 0,0, 0, 0, 1,];},translate: function(m, tx, ty, tz) {return m4.multiply(m, m4.translation(tx, ty, tz));},xRotate: function(m, angleInRadians) {return m4.multiply(m, m4.xRotation(angleInRadians));},yRotate: function(m, angleInRadians) {return m4.multiply(m, m4.yRotation(angleInRadians));},zRotate: function(m, angleInRadians) {return m4.multiply(m, m4.zRotation(angleInRadians));},scale: function(m, sx, sy, sz) {return m4.multiply(m, m4.scaling(sx, sy, sz));},};// Fill the buffer with the values that define a letter 'F'.function setGeometry(gl) {gl.bufferData(gl.ARRAY_BUFFER,new Float32Array([// left column front0, 0, 0,0, 150, 0,30, 0, 0,0, 150, 0,30, 150, 0,30, 0, 0,// top rung front30, 0, 0,30, 30, 0,100, 0, 0,30, 30, 0,100, 30, 0,100, 0, 0,// middle rung front30, 60, 0,30, 90, 0,67, 60, 0,30, 90, 0,67, 90, 0,67, 60, 0,// left column back0, 0, 30,30, 0, 30,0, 150, 30,0, 150, 30,30, 0, 30,30, 150, 30,// top rung back30, 0, 30,100, 0, 30,30, 30, 30,30, 30, 30,100, 0, 30,100, 30, 30,// middle rung back30, 60, 30,67, 60, 30,30, 90, 30,30, 90, 30,67, 60, 30,67, 90, 30,// top0, 0, 0,100, 0, 0,100, 0, 30,0, 0, 0,100, 0, 30,0, 0, 30,// top rung right100, 0, 0,100, 30, 0,100, 30, 30,100, 0, 0,100, 30, 30,100, 0, 30,// under top rung30, 30, 0,30, 30, 30,100, 30, 30,30, 30, 0,100, 30, 30,100, 30, 0,// between top rung and middle30, 30, 0,30, 60, 30,30, 30, 30,30, 30, 0,30, 60, 0,30, 60, 30,// top of middle rung30, 60, 0,67, 60, 30,30, 60, 30,30, 60, 0,67, 60, 0,67, 60, 30,// right of middle rung67, 60, 0,67, 90, 30,67, 60, 30,67, 60, 0,67, 90, 0,67, 90, 30,// bottom of middle rung.30, 90, 0,30, 90, 30,67, 90, 30,30, 90, 0,67, 90, 30,67, 90, 0,// right of bottom30, 90, 0,30, 150, 30,30, 90, 30,30, 90, 0,30, 150, 0,30, 150, 30,// bottom0, 150, 0,0, 150, 30,30, 150, 30,0, 150, 0,30, 150, 30,30, 150, 0,// left side0, 0, 0,0, 0, 30,0, 150, 30,0, 0, 0,0, 150, 30,0, 150, 0]),gl.STATIC_DRAW);}// Fill the buffer with colors for the 'F'.function setColors(gl) {gl.bufferData(gl.ARRAY_BUFFER,new Uint8Array([// left column front200, 70, 120,200, 70, 120,200, 70, 120,200, 70, 120,200, 70, 120,200, 70, 120,// top rung front200, 70, 120,200, 70, 120,200, 70, 120,200, 70, 120,200, 70, 120,200, 70, 120,// middle rung front200, 70, 120,200, 70, 120,200, 70, 120,200, 70, 120,200, 70, 120,200, 70, 120,// left column back80, 70, 200,80, 70, 200,80, 70, 200,80, 70, 200,80, 70, 200,80, 70, 200,// top rung back80, 70, 200,80, 70, 200,80, 70, 200,80, 70, 200,80, 70, 200,80, 70, 200,// middle rung back80, 70, 200,80, 70, 200,80, 70, 200,80, 70, 200,80, 70, 200,80, 70, 200,// top70, 200, 210,70, 200, 210,70, 200, 210,70, 200, 210,70, 200, 210,70, 200, 210,// top rung right200, 200, 70,200, 200, 70,200, 200, 70,200, 200, 70,200, 200, 70,200, 200, 70,// under top rung210, 100, 70,210, 100, 70,210, 100, 70,210, 100, 70,210, 100, 70,210, 100, 70,// between top rung and middle210, 160, 70,210, 160, 70,210, 160, 70,210, 160, 70,210, 160, 70,210, 160, 70,// top of middle rung70, 180, 210,70, 180, 210,70, 180, 210,70, 180, 210,70, 180, 210,70, 180, 210,// right of middle rung100, 70, 210,100, 70, 210,100, 70, 210,100, 70, 210,100, 70, 210,100, 70, 210,// bottom of middle rung.76, 210, 100,76, 210, 100,76, 210, 100,76, 210, 100,76, 210, 100,76, 210, 100,// right of bottom140, 210, 80,140, 210, 80,140, 210, 80,140, 210, 80,140, 210, 80,140, 210, 80,// bottom90, 130, 110,90, 130, 110,90, 130, 110,90, 130, 110,90, 130, 110,90, 130, 110,// left side160, 160, 220,160, 160, 220,160, 160, 220,160, 160, 220,160, 160, 220,160, 160, 220]),gl.STATIC_DRAW);}main();
<canvas id="canvas"></canvas><div id="uiContainer"><div id="ui"><div id="x"></div><div id="y"></div><div id="z"></div><div id="angleX"></div><div id="angleY"></div><div id="angleZ"></div><div id="scaleX"></div><div id="scaleY"></div><div id="scaleZ"></div></div></div><!-- vertex shader --><script id="vertex-shader-3d" type="x-shader/x-vertex">attribute vec4 a_position;attribute vec4 a_color;uniform mat4 u_matrix;varying vec4 v_color;void main() {// Multiply the position by the matrix.gl_Position = u_matrix * a_position;// Pass the color to the fragment shader.v_color = a_color;}</script><!-- fragment shader --><script id="fragment-shader-3d" type="x-shader/x-fragment">precision mediump float;// Passed in from the vertex shader.varying vec4 v_color;void main() {gl_FragColor = v_color;}</script><script src="https://webglfundamentals.org/webgl/resources/webgl-utils.js"></script><script src="https://webglfundamentals.org/webgl/resources/webgl-lessons-ui.js"></script>

