polygon-drawing-order.gif

一. 做三维只需要再迈出一小步

二维例子中的二维点 (x, y) 与 3x3 的矩阵相乘, 在三维中我们需要三维点 (x, y, z) 与 4x4 的矩阵相乘。
让我们将上个例子改成三维的,这里会继续使用 F ,但是这次是三维的 ‘F’ 。
首先需要修改顶点着色器以支持三维处理,这是原顶点着色器,

  1. <script id="vertex-shader-2d" type="x-shader/x-vertex">
  2. attribute vec2 a_position;
  3. uniform mat3 u_matrix;
  4. void main() {
  5. // 将位置和矩阵相乘
  6. gl_Position = vec4((u_matrix * vec3(a_position, 1)).xy, 0, 1);
  7. }
  8. </script>
  9. // 对比前后
  10. <script id="vertex-shader-3d" type="x-shader/x-vertex">
  11. attribute vec4 a_position;
  12. uniform mat4 u_matrix;
  13. void main() {
  14. // 将位置和矩阵相乘
  15. gl_Position = u_matrix * a_position;
  16. }
  17. </script>

它甚至变简单了!在二维中我们提供x和y并设置z为1, 在三维中我们将提供x,y和z,然后将w设置为1, 而在属性中w的默认值就是1,我们可以利用这点不用再次设置。然后提供三维数据。

二. 二维中的点的操作

  1. ...
  2. // 告诉属性怎么从 positionBuffer (ARRAY_BUFFER) 中读取位置
  3. var size = 3; // 每次迭代使用 3 个单位的数据
  4. var type = gl.FLOAT; // 单位数据类型是32位的浮点型
  5. var normalize = false; // 不需要归一化数据
  6. var stride = 0; // 0 = 移动距离 * 单位距离长度sizeof(type) 每次迭代跳多少距离到下一个数据
  7. var offset = 0; // 从绑定缓冲的起始处开始
  8. gl.vertexAttribPointer(
  9. positionAttributeLocation, size, type, normalize, stride, offset);
  10. ...
  11. // 填充当前 ARRAY_BUFFER 缓冲
  12. // 使用组成 'F' 的数据填充缓冲.
  13. function setGeometry(gl) {
  14. gl.bufferData(
  15. gl.ARRAY_BUFFER,
  16. new Float32Array([
  17. // 左竖
  18. 0, 0, 0,
  19. 30, 0, 0,
  20. 0, 150, 0,
  21. 0, 150, 0,
  22. 30, 0, 0,
  23. 30, 150, 0,
  24. // 上横
  25. 30, 0, 0,
  26. 100, 0, 0,
  27. 30, 30, 0,
  28. 30, 30, 0,
  29. 100, 0, 0,
  30. 100, 30, 0,
  31. // 下横
  32. 30, 60, 0,
  33. 67, 60, 0,
  34. 30, 90, 0,
  35. 30, 90, 0,
  36. 67, 60, 0,
  37. 67, 90, 0]),
  38. gl.STATIC_DRAW);
  39. }
  40. // 对应的二维矩阵
  41. var m3 = {
  42. translation: function translation(tx, ty) {
  43. return [
  44. 1, 0, 0,
  45. 0, 1, 0,
  46. tx, ty, 1
  47. ];
  48. },
  49. rotation: function rotation(angleInRadians) {
  50. var c = Math.cos(angleInRadians);
  51. var s = Math.sin(angleInRadians);
  52. return [
  53. c,-s, 0,
  54. s, c, 0,
  55. 0, 0, 1
  56. ];
  57. },
  58. scaling: function scaling(sx, sy) {
  59. return [
  60. sx, 0, 0,
  61. 0, sy, 0,
  62. 0, 0, 1
  63. ];
  64. },
  65. };

三. 三维中的点的操作

  1. var m4 = {
  2. translation: function(tx, ty, tz) {
  3. return [
  4. 1, 0, 0, 0,
  5. 0, 1, 0, 0,
  6. 0, 0, 1, 0,
  7. tx, ty, tz, 1,
  8. ];
  9. },
  10. xRotation: function(angleInRadians) {
  11. var c = Math.cos(angleInRadians);
  12. var s = Math.sin(angleInRadians);
  13. return [
  14. 1, 0, 0, 0,
  15. 0, c, s, 0,
  16. 0, -s, c, 0,
  17. 0, 0, 0, 1,
  18. ];
  19. },
  20. yRotation: function(angleInRadians) {
  21. var c = Math.cos(angleInRadians);
  22. var s = Math.sin(angleInRadians);
  23. return [
  24. c, 0, -s, 0,
  25. 0, 1, 0, 0,
  26. s, 0, c, 0,
  27. 0, 0, 0, 1,
  28. ];
  29. },
  30. zRotation: function(angleInRadians) {
  31. var c = Math.cos(angleInRadians);
  32. var s = Math.sin(angleInRadians);
  33. return [
  34. c, s, 0, 0,
  35. -s, c, 0, 0,
  36. 0, 0, 1, 0,
  37. 0, 0, 0, 1,
  38. ];
  39. },
  40. scaling: function(sx, sy, sz) {
  41. return [
  42. sx, 0, 0, 0,
  43. 0, sy, 0, 0,
  44. 0, 0, sz, 0,
  45. 0, 0, 0, 1,
  46. ];
  47. },
  48. };

注意到我们现在有三个旋转方法,在二维中只需要一个是因为我们只需要绕 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;
它们提供这些旋转方式。
image.png

四. 更新对应的点的矩阵运算

  1. // 在一些方法上面做一些简单运算 ex:m4.translate(matrix做一些矩阵运算)
  2. translate: function(m, tx, ty, tz) {
  3. return m4.multiply(m, m4.translation(tx, ty, tz));
  4. },
  5. xRotate: function(m, angleInRadians) {
  6. return m4.multiply(m, m4.xRotation(angleInRadians));
  7. },
  8. yRotate: function(m, angleInRadians) {
  9. return m4.multiply(m, m4.yRotation(angleInRadians));
  10. },
  11. zRotate: function(m, angleInRadians) {
  12. return m4.multiply(m, m4.zRotation(angleInRadians));
  13. },
  14. scale: function(m, sx, sy, sz) {
  15. return m4.multiply(m, m4.scaling(sx, sy, sz));
  16. },
  1. projection: function (width, height) {
  2. // 注意:这个矩阵翻转了 Y 轴,所以 0 在上方
  3. return [
  4. 2 / width, 0, 0,
  5. 0, -2 / height, 0,
  6. -1, 1, 1
  7. ];
  8. },
  9. }
  10. // 它将像素坐标转换到裁剪空间,在初次尝试三维时我们将这样做
  11. projection: function(width, height, depth) {
  12. // 注意:这个矩阵翻转了 Y 轴,所以 0 在上方
  13. return [
  14. 2 / width, 0, 0, 0,
  15. 0, -2 / height, 0, 0,
  16. 0, 0, 2 / depth, 0,
  17. -1, 1, 0, 1,
  18. ];
  19. },

就像 X 和 Y 需要从像素空间转换到裁剪空间一样,Z 也需要。 在这个例子中我也将 Z 单位化了,我会传递一些和 width 相似的值给 depth ,所以我们的空间将会是 0 到 width 像素宽,0 到 height 像素高, 但是对于depth将会是 -depth / 2 到 +depth / 2 。
最后需要更新计算矩阵的代码

  1. // 计算矩阵【透视图中这个又可以优化】
  2. var matrix = m4.projection(gl.canvas.clientWidth, gl.canvas.clientHeight, 400);
  3. matrix = m4.translate(matrix, translation[0], translation[1], translation[2]);
  4. matrix = m4.xRotate(matrix, rotation[0]);
  5. matrix = m4.yRotate(matrix, rotation[1]);
  6. matrix = m4.zRotate(matrix, rotation[2]);
  7. matrix = m4.scale(matrix, scale[0], scale[1], scale[2]);
  8. // 设置矩阵
  9. gl.uniformMatrix4fv(matrixLocation, false, matrix);

五. 三维中的点基本流程 【 正视投影 】

  1. // javascript
  2. "use strict";
  3. function main() {
  4. // Get A WebGL context
  5. /** @type {HTMLCanvasElement} */
  6. var canvas = document.querySelector("#canvas");
  7. var gl = canvas.getContext("webgl");
  8. if (!gl) {
  9. return;
  10. }
  11. // setup GLSL program
  12. var program = webglUtils.createProgramFromScripts(gl, ["vertex-shader-3d", "fragment-shader-3d"]);
  13. // look up where the vertex data needs to go.
  14. var positionLocation = gl.getAttribLocation(program, "a_position");
  15. // lookup uniforms
  16. var colorLocation = gl.getUniformLocation(program, "u_color");
  17. var matrixLocation = gl.getUniformLocation(program, "u_matrix");
  18. // Create a buffer to put positions in
  19. var positionBuffer = gl.createBuffer();
  20. // Bind it to ARRAY_BUFFER (think of it as ARRAY_BUFFER = positionBuffer)
  21. gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
  22. // Put geometry data into buffer
  23. setGeometry(gl);
  24. function radToDeg(r) {
  25. return r * 180 / Math.PI;
  26. }
  27. function degToRad(d) {
  28. return d * Math.PI / 180;
  29. }
  30. var translation = [45, 150, 0];
  31. var rotation = [degToRad(40), degToRad(25), degToRad(325)];
  32. var scale = [1, 1, 1];
  33. var color = [Math.random(), Math.random(), Math.random(), 1];
  34. drawScene();
  35. // Setup a ui.
  36. webglLessonsUI.setupSlider("#x", {value: translation[0], slide: updatePosition(0), max: gl.canvas.width });
  37. webglLessonsUI.setupSlider("#y", {value: translation[1], slide: updatePosition(1), max: gl.canvas.height});
  38. webglLessonsUI.setupSlider("#z", {value: translation[2], slide: updatePosition(2), max: gl.canvas.height});
  39. webglLessonsUI.setupSlider("#angleX", {value: radToDeg(rotation[0]), slide: updateRotation(0), max: 360});
  40. webglLessonsUI.setupSlider("#angleY", {value: radToDeg(rotation[1]), slide: updateRotation(1), max: 360});
  41. webglLessonsUI.setupSlider("#angleZ", {value: radToDeg(rotation[2]), slide: updateRotation(2), max: 360});
  42. webglLessonsUI.setupSlider("#scaleX", {value: scale[0], slide: updateScale(0), min: -5, max: 5, step: 0.01, precision: 2});
  43. webglLessonsUI.setupSlider("#scaleY", {value: scale[1], slide: updateScale(1), min: -5, max: 5, step: 0.01, precision: 2});
  44. webglLessonsUI.setupSlider("#scaleZ", {value: scale[2], slide: updateScale(2), min: -5, max: 5, step: 0.01, precision: 2});
  45. function updatePosition(index) {
  46. return function(event, ui) {
  47. translation[index] = ui.value;
  48. drawScene();
  49. };
  50. }
  51. function updateRotation(index) {
  52. return function(event, ui) {
  53. var angleInDegrees = ui.value;
  54. var angleInRadians = angleInDegrees * Math.PI / 180;
  55. rotation[index] = angleInRadians;
  56. drawScene();
  57. };
  58. }
  59. function updateScale(index) {
  60. return function(event, ui) {
  61. scale[index] = ui.value;
  62. drawScene();
  63. };
  64. }
  65. // Draw the scene.
  66. function drawScene() {
  67. webglUtils.resizeCanvasToDisplaySize(gl.canvas);
  68. // Tell WebGL how to convert from clip space to pixels
  69. gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);
  70. // Clear the canvas.
  71. gl.clear(gl.COLOR_BUFFER_BIT);
  72. // Tell it to use our program (pair of shaders)
  73. gl.useProgram(program);
  74. // Turn on the attribute
  75. gl.enableVertexAttribArray(positionLocation);
  76. // Bind the position buffer.
  77. gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
  78. // Tell the attribute how to get data out of positionBuffer (ARRAY_BUFFER)
  79. var size = 3; // 3 components per iteration
  80. var type = gl.FLOAT; // the data is 32bit floats
  81. var normalize = false; // don't normalize the data
  82. var stride = 0; // 0 = move forward size * sizeof(type) each iteration to get the next position
  83. var offset = 0; // start at the beginning of the buffer
  84. gl.vertexAttribPointer(
  85. positionLocation, size, type, normalize, stride, offset);
  86. // set the color
  87. gl.uniform4fv(colorLocation, color);
  88. // Compute the matrices
  89. var matrix = m4.projection(gl.canvas.clientWidth, gl.canvas.clientHeight, 400);
  90. matrix = m4.translate(matrix, translation[0], translation[1], translation[2]);
  91. matrix = m4.xRotate(matrix, rotation[0]);
  92. matrix = m4.yRotate(matrix, rotation[1]);
  93. matrix = m4.zRotate(matrix, rotation[2]);
  94. matrix = m4.scale(matrix, scale[0], scale[1], scale[2]);
  95. // Set the matrix.
  96. gl.uniformMatrix4fv(matrixLocation, false, matrix);
  97. // Draw the geometry.
  98. var primitiveType = gl.TRIANGLES;
  99. var offset = 0;
  100. var count = 18; // 6 triangles in the 'F', 3 points per triangle
  101. gl.drawArrays(primitiveType, offset, count);
  102. }
  103. }
  104. var m4 = {
  105. projection: function(width, height, depth) {
  106. // Note: This matrix flips the Y axis so 0 is at the top.
  107. return [
  108. 2 / width, 0, 0, 0,
  109. 0, -2 / height, 0, 0,
  110. 0, 0, 2 / depth, 0,
  111. -1, 1, 0, 1,
  112. ];
  113. },
  114. multiply: function(a, b) {
  115. var a00 = a[0 * 4 + 0];
  116. var a01 = a[0 * 4 + 1];
  117. var a02 = a[0 * 4 + 2];
  118. var a03 = a[0 * 4 + 3];
  119. var a10 = a[1 * 4 + 0];
  120. var a11 = a[1 * 4 + 1];
  121. var a12 = a[1 * 4 + 2];
  122. var a13 = a[1 * 4 + 3];
  123. var a20 = a[2 * 4 + 0];
  124. var a21 = a[2 * 4 + 1];
  125. var a22 = a[2 * 4 + 2];
  126. var a23 = a[2 * 4 + 3];
  127. var a30 = a[3 * 4 + 0];
  128. var a31 = a[3 * 4 + 1];
  129. var a32 = a[3 * 4 + 2];
  130. var a33 = a[3 * 4 + 3];
  131. var b00 = b[0 * 4 + 0];
  132. var b01 = b[0 * 4 + 1];
  133. var b02 = b[0 * 4 + 2];
  134. var b03 = b[0 * 4 + 3];
  135. var b10 = b[1 * 4 + 0];
  136. var b11 = b[1 * 4 + 1];
  137. var b12 = b[1 * 4 + 2];
  138. var b13 = b[1 * 4 + 3];
  139. var b20 = b[2 * 4 + 0];
  140. var b21 = b[2 * 4 + 1];
  141. var b22 = b[2 * 4 + 2];
  142. var b23 = b[2 * 4 + 3];
  143. var b30 = b[3 * 4 + 0];
  144. var b31 = b[3 * 4 + 1];
  145. var b32 = b[3 * 4 + 2];
  146. var b33 = b[3 * 4 + 3];
  147. return [
  148. b00 * a00 + b01 * a10 + b02 * a20 + b03 * a30,
  149. b00 * a01 + b01 * a11 + b02 * a21 + b03 * a31,
  150. b00 * a02 + b01 * a12 + b02 * a22 + b03 * a32,
  151. b00 * a03 + b01 * a13 + b02 * a23 + b03 * a33,
  152. b10 * a00 + b11 * a10 + b12 * a20 + b13 * a30,
  153. b10 * a01 + b11 * a11 + b12 * a21 + b13 * a31,
  154. b10 * a02 + b11 * a12 + b12 * a22 + b13 * a32,
  155. b10 * a03 + b11 * a13 + b12 * a23 + b13 * a33,
  156. b20 * a00 + b21 * a10 + b22 * a20 + b23 * a30,
  157. b20 * a01 + b21 * a11 + b22 * a21 + b23 * a31,
  158. b20 * a02 + b21 * a12 + b22 * a22 + b23 * a32,
  159. b20 * a03 + b21 * a13 + b22 * a23 + b23 * a33,
  160. b30 * a00 + b31 * a10 + b32 * a20 + b33 * a30,
  161. b30 * a01 + b31 * a11 + b32 * a21 + b33 * a31,
  162. b30 * a02 + b31 * a12 + b32 * a22 + b33 * a32,
  163. b30 * a03 + b31 * a13 + b32 * a23 + b33 * a33,
  164. ];
  165. },
  166. translation: function(tx, ty, tz) {
  167. return [
  168. 1, 0, 0, 0,
  169. 0, 1, 0, 0,
  170. 0, 0, 1, 0,
  171. tx, ty, tz, 1,
  172. ];
  173. },
  174. xRotation: function(angleInRadians) {
  175. var c = Math.cos(angleInRadians);
  176. var s = Math.sin(angleInRadians);
  177. return [
  178. 1, 0, 0, 0,
  179. 0, c, s, 0,
  180. 0, -s, c, 0,
  181. 0, 0, 0, 1,
  182. ];
  183. },
  184. yRotation: function(angleInRadians) {
  185. var c = Math.cos(angleInRadians);
  186. var s = Math.sin(angleInRadians);
  187. return [
  188. c, 0, -s, 0,
  189. 0, 1, 0, 0,
  190. s, 0, c, 0,
  191. 0, 0, 0, 1,
  192. ];
  193. },
  194. zRotation: function(angleInRadians) {
  195. var c = Math.cos(angleInRadians);
  196. var s = Math.sin(angleInRadians);
  197. return [
  198. c, s, 0, 0,
  199. -s, c, 0, 0,
  200. 0, 0, 1, 0,
  201. 0, 0, 0, 1,
  202. ];
  203. },
  204. scaling: function(sx, sy, sz) {
  205. return [
  206. sx, 0, 0, 0,
  207. 0, sy, 0, 0,
  208. 0, 0, sz, 0,
  209. 0, 0, 0, 1,
  210. ];
  211. },
  212. translate: function(m, tx, ty, tz) {
  213. return m4.multiply(m, m4.translation(tx, ty, tz));
  214. },
  215. xRotate: function(m, angleInRadians) {
  216. return m4.multiply(m, m4.xRotation(angleInRadians));
  217. },
  218. yRotate: function(m, angleInRadians) {
  219. return m4.multiply(m, m4.yRotation(angleInRadians));
  220. },
  221. zRotate: function(m, angleInRadians) {
  222. return m4.multiply(m, m4.zRotation(angleInRadians));
  223. },
  224. scale: function(m, sx, sy, sz) {
  225. return m4.multiply(m, m4.scaling(sx, sy, sz));
  226. },
  227. };
  228. // Fill the buffer with the values that define a letter 'F'. 其实这个对应一个分存一个模型的所有点;
  229. function setGeometry(gl) {
  230. gl.bufferData(
  231. gl.ARRAY_BUFFER,
  232. new Float32Array([
  233. // left column
  234. 0, 0, 0,
  235. 30, 0, 0,
  236. 0, 150, 0,
  237. 0, 150, 0,
  238. 30, 0, 0,
  239. 30, 150, 0,
  240. // top rung
  241. 30, 0, 0,
  242. 100, 0, 0,
  243. 30, 30, 0,
  244. 30, 30, 0,
  245. 100, 0, 0,
  246. 100, 30, 0,
  247. // middle rung
  248. 30, 60, 0,
  249. 67, 60, 0,
  250. 30, 90, 0,
  251. 30, 90, 0,
  252. 67, 60, 0,
  253. 67, 90, 0]),
  254. gl.STATIC_DRAW);
  255. }
  256. main();
  257. // html 代码 已经简化好多了;几乎所有的运动都存在矩阵中去了;
  258. <canvas id="canvas"></canvas>
  259. <div id="uiContainer">
  260. <div id="ui">
  261. <div id="x"></div>
  262. <div id="y"></div>
  263. <div id="z"></div>
  264. <div id="angleX"></div>
  265. <div id="angleY"></div>
  266. <div id="angleZ"></div>
  267. <div id="scaleX"></div>
  268. <div id="scaleY"></div>
  269. <div id="scaleZ"></div>
  270. </div>
  271. </div>
  272. <!-- vertex shader -->
  273. <script id="vertex-shader-3d" type="x-shader/x-vertex">
  274. attribute vec4 a_position;
  275. uniform mat4 u_matrix;
  276. void main() {
  277. // Multiply the position by the matrix.
  278. gl_Position = u_matrix * a_position;
  279. }
  280. </script>
  281. <!-- fragment shader -->
  282. <script id="fragment-shader-3d" type="x-shader/x-fragment">
  283. precision mediump float;
  284. uniform vec4 u_color;
  285. void main() {
  286. gl_FragColor = u_color;
  287. }
  288. </script>
  289. <script src="https://webglfundamentals.org/webgl/resources/webgl-utils.js"></script>
  290. <script src="https://webglfundamentals.org/webgl/resources/webgl-lessons-ui.js"></script>

最后公式 gl_Position = u_matrix * a_position;

六. 拉伸具有三维效果

我们遇到的第一个问题是 F 在三维中过于扁平, 所以很难看出三维效果。解决这个问题的方法是将它拉伸成三维几何体。 现在的 F 是由三个矩形组成,每个矩形两个三角形。让它变三维需要 16 个矩形。 三个矩形在正面,三个背面,一个左侧,四个右侧,两个上侧,三个底面。
image.png
需要列出的还有很多,16 个矩形每个有两个三角形,每个三角形有 3 个顶点, 所以一共有 96 个顶点。如果你想看这些可以去示例的源码里找。
其实也就是把对应保存有模型点的轨迹修改就可以

  1. function setGeometry(gl) {
  2. gl.bufferData(
  3. gl.ARRAY_BUFFER,
  4. new Float32Array([
  5. // left column front
  6. 0, 0, 0,
  7. 30, 0, 0,
  8. 0, 150, 0,
  9. 0, 150, 0,
  10. 30, 0, 0,
  11. 30, 150, 0,
  12. // top rung front
  13. 30, 0, 0,
  14. 100, 0, 0,
  15. 30, 30, 0,
  16. 30, 30, 0,
  17. 100, 0, 0,
  18. 100, 30, 0,
  19. // middle rung front
  20. 30, 60, 0,
  21. 67, 60, 0,
  22. 30, 90, 0,
  23. 30, 90, 0,
  24. 67, 60, 0,
  25. 67, 90, 0,
  26. // left column back
  27. 0, 0, 30,
  28. 30, 0, 30,
  29. 0, 150, 30,
  30. 0, 150, 30,
  31. 30, 0, 30,
  32. 30, 150, 30,
  33. // top rung back
  34. 30, 0, 30,
  35. 100, 0, 30,
  36. 30, 30, 30,
  37. 30, 30, 30,
  38. 100, 0, 30,
  39. 100, 30, 30,
  40. // middle rung back
  41. 30, 60, 30,
  42. 67, 60, 30,
  43. 30, 90, 30,
  44. 30, 90, 30,
  45. 67, 60, 30,
  46. 67, 90, 30,
  47. // top
  48. 0, 0, 0,
  49. 100, 0, 0,
  50. 100, 0, 30,
  51. 0, 0, 0,
  52. 100, 0, 30,
  53. 0, 0, 30,
  54. // top rung right
  55. 100, 0, 0,
  56. 100, 30, 0,
  57. 100, 30, 30,
  58. 100, 0, 0,
  59. 100, 30, 30,
  60. 100, 0, 30,
  61. // under top rung
  62. 30, 30, 0,
  63. 30, 30, 30,
  64. 100, 30, 30,
  65. 30, 30, 0,
  66. 100, 30, 30,
  67. 100, 30, 0,
  68. // between top rung and middle
  69. 30, 30, 0,
  70. 30, 30, 30,
  71. 30, 60, 30,
  72. 30, 30, 0,
  73. 30, 60, 30,
  74. 30, 60, 0,
  75. // top of middle rung
  76. 30, 60, 0,
  77. 30, 60, 30,
  78. 67, 60, 30,
  79. 30, 60, 0,
  80. 67, 60, 30,
  81. 67, 60, 0,
  82. // right of middle rung
  83. 67, 60, 0,
  84. 67, 60, 30,
  85. 67, 90, 30,
  86. 67, 60, 0,
  87. 67, 90, 30,
  88. 67, 90, 0,
  89. // bottom of middle rung.
  90. 30, 90, 0,
  91. 30, 90, 30,
  92. 67, 90, 30,
  93. 30, 90, 0,
  94. 67, 90, 30,
  95. 67, 90, 0,
  96. // right of bottom
  97. 30, 90, 0,
  98. 30, 90, 30,
  99. 30, 150, 30,
  100. 30, 90, 0,
  101. 30, 150, 30,
  102. 30, 150, 0,
  103. // bottom
  104. 0, 150, 0,
  105. 0, 150, 30,
  106. 30, 150, 30,
  107. 0, 150, 0,
  108. 30, 150, 30,
  109. 30, 150, 0,
  110. // left side
  111. 0, 0, 0,
  112. 0, 0, 30,
  113. 0, 150, 30,
  114. 0, 0, 0,
  115. 0, 150, 30,
  116. 0, 150, 0]),
  117. gl.STATIC_DRAW);
  118. }

所以可以想象这还是一个比较简单模型;如果模型的复杂度有人物或者那种特别精致的物件;我们还能一个个点去操作这些吗?这个问题留到后面

七. 模型处理不同的颜色

在这之前我们需要修改一下对应的模型上有好几种颜色而不是对应就只有一种颜色

  1. <script id="vertex-shader-3d" type="x-shader/x-vertex">
  2. attribute vec4 a_position;
  3. attribute vec4 a_color;
  4. uniform mat4 u_matrix;
  5. varying vec4 v_color;
  6. void main() {
  7. // 将位置和矩阵相乘.
  8. gl_Position = u_matrix * a_position;
  9. // 将颜色传递给片断着色器
  10. v_color = a_color;
  11. }
  12. </script>
  13. // 片段着色器 使用
  14. <script id="fragment-shader-3d" type="x-shader/x-fragment">
  15. precision mediump float;
  16. // 从顶点着色器中传入
  17. varying vec4 v_color;
  18. void main() {
  19. gl_FragColor = v_color;
  20. }
  21. </script>

我们需要找到属性的位置,然后在另一个缓冲中存入对应的颜色。

  1. ...
  2. var colorLocation = gl.getAttribLocation(program, "a_color");
  3. ...
  4. // 给颜色创建一个缓冲
  5. var colorBuffer = gl.createBuffer();
  6. gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer);
  7. // 将颜色值传入缓冲
  8. setColors(gl);
  9. ...
  10. // 向缓冲传入 'F' 的颜色值
  11. function setColors(gl) {
  12. gl.bufferData(
  13. gl.ARRAY_BUFFER,
  14. new Uint8Array([
  15. // 正面左竖
  16. 200, 70, 120,
  17. 200, 70, 120,
  18. 200, 70, 120,
  19. 200, 70, 120,
  20. 200, 70, 120,
  21. 200, 70, 120,
  22. // 正面上横
  23. 200, 70, 120,
  24. 200, 70, 120,
  25. ...
  26. ...
  27. gl.STATIC_DRAW);
  28. }
  29. // 在渲染时告诉颜色属性如何从缓冲中获取颜色值
  30. // 启用颜色属性
  31. gl.enableVertexAttribArray(colorLocation);
  32. // 绑定颜色缓冲
  33. gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer);
  34. // 告诉颜色属性怎么从 colorBuffer (ARRAY_BUFFER) 中读取颜色值
  35. var size = 3; // 每次迭代使用3个单位的数据
  36. var type = gl.UNSIGNED_BYTE; // 单位数据类型是无符号 8 位整数
  37. var normalize = true; // 标准化数据 (从 0-255 转换到 0.0-1.0)
  38. var stride = 0; // 0 = 移动距离 * 单位距离长度sizeof(type) 每次迭代跳多少距离到下一个数据
  39. var offset = 0; // 从绑定缓冲的起始处开始
  40. gl.vertexAttribPointer(
  41. colorLocation, size, type, normalize, stride, offset)

八. 正反三角面

image.png2323.gif
不过他好像和我们看到实际那种的模型都不太一样;比如前面有遮挡的时候我们会看不见后面;
你要知道电脑并没有三维的概念;在线性代数中两个点决定一条线;只不过是我们通过规则和数学的方式将图形变化成我们想看到那种形态;很显然现在规则有点问题;排在前面的三角面会比较先画出来然后后面的点就会把前面覆盖;很显然电脑并不知道哪些点是否会重合;而在真实的物理世界那些面是可以看到哪些又是看不到的;
聪明如大家
image.png
WebGL中的三角形有正反面的概念,正面三角形的顶点顺序是逆时针方向, 反面三角形是顺时针方向。

  1. gl.enable(gl.CULL_FACE);

将它放在 drawScene 方法里,开启这个特性后WebGL默认“剔除”背面三角形, “剔除”在这里是“不用绘制”的花哨叫法。
对于WebGL而言,一个三角形是顺时针还是逆时针是根据裁剪空间中的顶点顺序判断的, 换句话说,WebGL是根据你在顶点着色器中运算后提供的结果来判定的, 这就意味着如果你把一个顺时针的三角形沿 X 轴缩放 -1 ,它将会变成逆时针, 或者将顺时针的三角形旋转180度后变成逆时针。由于我们没有开启 CULL_FACE, 所以可以同时看到顺时针(正面)和逆时针(反面)三角形。现在开启了, 任何时候正面三角形无论是缩放还是旋转的原因导致翻转了,WebGL就不会绘制它。 这件事很有用,因为通常情况下你只需要看到你正面对的面。

不得不说WEBGL在编写协议的时候就已经给我们准备好了【 当然后面又是一层精致的底层算法 ,他会在一定的时机给你算出对应的点谁在谁物理世界层面的顶部 】

image.png
结果对应的数据中显示出如下效果
大多数三角形朝向都是错的, 旋转的时候你会看到背面的三角形,幸好它很容易解决, 我们只需要看看哪些是三角形是反的,然后交换它们的两个顶点

  1. 1, 2, 3,
  2. 40, 50, 60,
  3. 700, 800, 900,
  4. 1, 2, 3,
  5. 700, 800, 900,
  6. 40, 50, 60,

通过修正朝向错误后得到

九. DEPTH BUFFER(深度缓冲)

image.png
不过好像对应的点的序列对应的模型部分还有有问题;
这很接近实际效果了但是还有一个问题,即使所有三角形的朝向是正确的, 然后背面的被剔除了,有些应该在背面的部分还是出现在了前面。
接触 DEPTH BUFFER(深度缓冲)
深度缓冲有时也叫 Z-Buffer,是一个存储像素深度的矩形, 一个深度像素对应一个着色像素,在绘制图像时组合使用。 当WebGL绘制每个着色像素时也会写入深度像素, 它的值基于顶点着色器返回的Z值,就像我们将 X 和 Y 转换到裁剪空间一样, Z 也在裁剪空间或者 (-1 到 +1) 。这个值会被转换到深度空间( 0 到 +1), WebGL绘制一个着色像素之前会检查对应的深度像素, 如果对应的深度像素中的深度值小于当前像素的深度值,WebGL就不会绘制新的颜色。 反之它会绘制片断着色器提供的新颜色并更新深度像素中的深度值。 这也意味着在其他像素后面的像素不会被绘制。
我们可以像这样开启这个特性

  1. gl.enable(gl.DEPTH_TEST);
  2. // code
  3. // 绘制场景
  4. function drawScene() {
  5. ...
  6. // 清空画布和深度缓冲
  7. gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
  8. ...

image.png
这才是三维!

十. 终极代码

  1. "use strict";
  2. function main() {
  3. // Get A WebGL context
  4. /** @type {HTMLCanvasElement} */
  5. var canvas = document.querySelector("#canvas");
  6. var gl = canvas.getContext("webgl");
  7. if (!gl) {
  8. return;
  9. }
  10. // setup GLSL program
  11. var program = webglUtils.createProgramFromScripts(gl, ["vertex-shader-3d", "fragment-shader-3d"]);
  12. // look up where the vertex data needs to go.
  13. var positionLocation = gl.getAttribLocation(program, "a_position");
  14. var colorLocation = gl.getAttribLocation(program, "a_color");
  15. // lookup uniforms
  16. var matrixLocation = gl.getUniformLocation(program, "u_matrix");
  17. // Create a buffer to put positions in
  18. var positionBuffer = gl.createBuffer();
  19. // Bind it to ARRAY_BUFFER (think of it as ARRAY_BUFFER = positionBuffer)
  20. gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
  21. // Put geometry data into buffer
  22. setGeometry(gl);
  23. // Create a buffer to put colors in
  24. var colorBuffer = gl.createBuffer();
  25. // Bind it to ARRAY_BUFFER (think of it as ARRAY_BUFFER = colorBuffer)
  26. gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer);
  27. // Put geometry data into buffer
  28. setColors(gl);
  29. function radToDeg(r) {
  30. return r * 180 / Math.PI;
  31. }
  32. function degToRad(d) {
  33. return d * Math.PI / 180;
  34. }
  35. var translation = [45, 150, 0];
  36. var rotation = [degToRad(40), degToRad(25), degToRad(325)];
  37. var scale = [1, 1, 1];
  38. drawScene();
  39. // Setup a ui.
  40. webglLessonsUI.setupSlider("#x", {value: translation[0], slide: updatePosition(0), max: gl.canvas.width });
  41. webglLessonsUI.setupSlider("#y", {value: translation[1], slide: updatePosition(1), max: gl.canvas.height});
  42. webglLessonsUI.setupSlider("#z", {value: translation[2], slide: updatePosition(2), max: gl.canvas.height});
  43. webglLessonsUI.setupSlider("#angleX", {value: radToDeg(rotation[0]), slide: updateRotation(0), max: 360});
  44. webglLessonsUI.setupSlider("#angleY", {value: radToDeg(rotation[1]), slide: updateRotation(1), max: 360});
  45. webglLessonsUI.setupSlider("#angleZ", {value: radToDeg(rotation[2]), slide: updateRotation(2), max: 360});
  46. webglLessonsUI.setupSlider("#scaleX", {value: scale[0], slide: updateScale(0), min: -5, max: 5, step: 0.01, precision: 2});
  47. webglLessonsUI.setupSlider("#scaleY", {value: scale[1], slide: updateScale(1), min: -5, max: 5, step: 0.01, precision: 2});
  48. webglLessonsUI.setupSlider("#scaleZ", {value: scale[2], slide: updateScale(2), min: -5, max: 5, step: 0.01, precision: 2});
  49. function updatePosition(index) {
  50. return function(event, ui) {
  51. translation[index] = ui.value;
  52. drawScene();
  53. };
  54. }
  55. function updateRotation(index) {
  56. return function(event, ui) {
  57. var angleInDegrees = ui.value;
  58. var angleInRadians = angleInDegrees * Math.PI / 180;
  59. rotation[index] = angleInRadians;
  60. drawScene();
  61. };
  62. }
  63. function updateScale(index) {
  64. return function(event, ui) {
  65. scale[index] = ui.value;
  66. drawScene();
  67. };
  68. }
  69. // Draw the scene.
  70. function drawScene() {
  71. webglUtils.resizeCanvasToDisplaySize(gl.canvas);
  72. // Tell WebGL how to convert from clip space to pixels
  73. gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);
  74. // Clear the canvas.
  75. gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
  76. // Turn on culling. By default backfacing triangles
  77. // will be culled.
  78. gl.enable(gl.CULL_FACE);
  79. // Enable the depth buffer
  80. gl.enable(gl.DEPTH_TEST);
  81. // Tell it to use our program (pair of shaders)
  82. gl.useProgram(program);
  83. // Turn on the position attribute
  84. gl.enableVertexAttribArray(positionLocation);
  85. // Bind the position buffer.
  86. gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
  87. // Tell the position attribute how to get data out of positionBuffer (ARRAY_BUFFER)
  88. var size = 3; // 3 components per iteration
  89. var type = gl.FLOAT; // the data is 32bit floats
  90. var normalize = false; // don't normalize the data
  91. var stride = 0; // 0 = move forward size * sizeof(type) each iteration to get the next position
  92. var offset = 0; // start at the beginning of the buffer
  93. gl.vertexAttribPointer(
  94. positionLocation, size, type, normalize, stride, offset);
  95. // Turn on the color attribute
  96. gl.enableVertexAttribArray(colorLocation);
  97. // Bind the color buffer.
  98. gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer);
  99. // Tell the attribute how to get data out of colorBuffer (ARRAY_BUFFER)
  100. var size = 3; // 3 components per iteration
  101. var type = gl.UNSIGNED_BYTE; // the data is 8bit unsigned values
  102. var normalize = true; // normalize the data (convert from 0-255 to 0-1)
  103. var stride = 0; // 0 = move forward size * sizeof(type) each iteration to get the next position
  104. var offset = 0; // start at the beginning of the buffer
  105. gl.vertexAttribPointer(
  106. colorLocation, size, type, normalize, stride, offset);
  107. // Compute the matrices
  108. var matrix = m4.projection(gl.canvas.clientWidth, gl.canvas.clientHeight, 400);
  109. matrix = m4.translate(matrix, translation[0], translation[1], translation[2]);
  110. matrix = m4.xRotate(matrix, rotation[0]);
  111. matrix = m4.yRotate(matrix, rotation[1]);
  112. matrix = m4.zRotate(matrix, rotation[2]);
  113. matrix = m4.scale(matrix, scale[0], scale[1], scale[2]);
  114. // Set the matrix.
  115. gl.uniformMatrix4fv(matrixLocation, false, matrix);
  116. // Draw the geometry.
  117. var primitiveType = gl.TRIANGLES;
  118. var offset = 0;
  119. var count = 16 * 6;
  120. gl.drawArrays(primitiveType, offset, count);
  121. }
  122. }
  123. var m4 = {
  124. projection: function(width, height, depth) {
  125. // Note: This matrix flips the Y axis so 0 is at the top.
  126. return [
  127. 2 / width, 0, 0, 0,
  128. 0, -2 / height, 0, 0,
  129. 0, 0, 2 / depth, 0,
  130. -1, 1, 0, 1,
  131. ];
  132. },
  133. multiply: function(a, b) {
  134. var a00 = a[0 * 4 + 0];
  135. var a01 = a[0 * 4 + 1];
  136. var a02 = a[0 * 4 + 2];
  137. var a03 = a[0 * 4 + 3];
  138. var a10 = a[1 * 4 + 0];
  139. var a11 = a[1 * 4 + 1];
  140. var a12 = a[1 * 4 + 2];
  141. var a13 = a[1 * 4 + 3];
  142. var a20 = a[2 * 4 + 0];
  143. var a21 = a[2 * 4 + 1];
  144. var a22 = a[2 * 4 + 2];
  145. var a23 = a[2 * 4 + 3];
  146. var a30 = a[3 * 4 + 0];
  147. var a31 = a[3 * 4 + 1];
  148. var a32 = a[3 * 4 + 2];
  149. var a33 = a[3 * 4 + 3];
  150. var b00 = b[0 * 4 + 0];
  151. var b01 = b[0 * 4 + 1];
  152. var b02 = b[0 * 4 + 2];
  153. var b03 = b[0 * 4 + 3];
  154. var b10 = b[1 * 4 + 0];
  155. var b11 = b[1 * 4 + 1];
  156. var b12 = b[1 * 4 + 2];
  157. var b13 = b[1 * 4 + 3];
  158. var b20 = b[2 * 4 + 0];
  159. var b21 = b[2 * 4 + 1];
  160. var b22 = b[2 * 4 + 2];
  161. var b23 = b[2 * 4 + 3];
  162. var b30 = b[3 * 4 + 0];
  163. var b31 = b[3 * 4 + 1];
  164. var b32 = b[3 * 4 + 2];
  165. var b33 = b[3 * 4 + 3];
  166. return [
  167. b00 * a00 + b01 * a10 + b02 * a20 + b03 * a30,
  168. b00 * a01 + b01 * a11 + b02 * a21 + b03 * a31,
  169. b00 * a02 + b01 * a12 + b02 * a22 + b03 * a32,
  170. b00 * a03 + b01 * a13 + b02 * a23 + b03 * a33,
  171. b10 * a00 + b11 * a10 + b12 * a20 + b13 * a30,
  172. b10 * a01 + b11 * a11 + b12 * a21 + b13 * a31,
  173. b10 * a02 + b11 * a12 + b12 * a22 + b13 * a32,
  174. b10 * a03 + b11 * a13 + b12 * a23 + b13 * a33,
  175. b20 * a00 + b21 * a10 + b22 * a20 + b23 * a30,
  176. b20 * a01 + b21 * a11 + b22 * a21 + b23 * a31,
  177. b20 * a02 + b21 * a12 + b22 * a22 + b23 * a32,
  178. b20 * a03 + b21 * a13 + b22 * a23 + b23 * a33,
  179. b30 * a00 + b31 * a10 + b32 * a20 + b33 * a30,
  180. b30 * a01 + b31 * a11 + b32 * a21 + b33 * a31,
  181. b30 * a02 + b31 * a12 + b32 * a22 + b33 * a32,
  182. b30 * a03 + b31 * a13 + b32 * a23 + b33 * a33,
  183. ];
  184. },
  185. translation: function(tx, ty, tz) {
  186. return [
  187. 1, 0, 0, 0,
  188. 0, 1, 0, 0,
  189. 0, 0, 1, 0,
  190. tx, ty, tz, 1,
  191. ];
  192. },
  193. xRotation: function(angleInRadians) {
  194. var c = Math.cos(angleInRadians);
  195. var s = Math.sin(angleInRadians);
  196. return [
  197. 1, 0, 0, 0,
  198. 0, c, s, 0,
  199. 0, -s, c, 0,
  200. 0, 0, 0, 1,
  201. ];
  202. },
  203. yRotation: function(angleInRadians) {
  204. var c = Math.cos(angleInRadians);
  205. var s = Math.sin(angleInRadians);
  206. return [
  207. c, 0, -s, 0,
  208. 0, 1, 0, 0,
  209. s, 0, c, 0,
  210. 0, 0, 0, 1,
  211. ];
  212. },
  213. zRotation: function(angleInRadians) {
  214. var c = Math.cos(angleInRadians);
  215. var s = Math.sin(angleInRadians);
  216. return [
  217. c, s, 0, 0,
  218. -s, c, 0, 0,
  219. 0, 0, 1, 0,
  220. 0, 0, 0, 1,
  221. ];
  222. },
  223. scaling: function(sx, sy, sz) {
  224. return [
  225. sx, 0, 0, 0,
  226. 0, sy, 0, 0,
  227. 0, 0, sz, 0,
  228. 0, 0, 0, 1,
  229. ];
  230. },
  231. translate: function(m, tx, ty, tz) {
  232. return m4.multiply(m, m4.translation(tx, ty, tz));
  233. },
  234. xRotate: function(m, angleInRadians) {
  235. return m4.multiply(m, m4.xRotation(angleInRadians));
  236. },
  237. yRotate: function(m, angleInRadians) {
  238. return m4.multiply(m, m4.yRotation(angleInRadians));
  239. },
  240. zRotate: function(m, angleInRadians) {
  241. return m4.multiply(m, m4.zRotation(angleInRadians));
  242. },
  243. scale: function(m, sx, sy, sz) {
  244. return m4.multiply(m, m4.scaling(sx, sy, sz));
  245. },
  246. };
  247. // Fill the buffer with the values that define a letter 'F'.
  248. function setGeometry(gl) {
  249. gl.bufferData(
  250. gl.ARRAY_BUFFER,
  251. new Float32Array([
  252. // left column front
  253. 0, 0, 0,
  254. 0, 150, 0,
  255. 30, 0, 0,
  256. 0, 150, 0,
  257. 30, 150, 0,
  258. 30, 0, 0,
  259. // top rung front
  260. 30, 0, 0,
  261. 30, 30, 0,
  262. 100, 0, 0,
  263. 30, 30, 0,
  264. 100, 30, 0,
  265. 100, 0, 0,
  266. // middle rung front
  267. 30, 60, 0,
  268. 30, 90, 0,
  269. 67, 60, 0,
  270. 30, 90, 0,
  271. 67, 90, 0,
  272. 67, 60, 0,
  273. // left column back
  274. 0, 0, 30,
  275. 30, 0, 30,
  276. 0, 150, 30,
  277. 0, 150, 30,
  278. 30, 0, 30,
  279. 30, 150, 30,
  280. // top rung back
  281. 30, 0, 30,
  282. 100, 0, 30,
  283. 30, 30, 30,
  284. 30, 30, 30,
  285. 100, 0, 30,
  286. 100, 30, 30,
  287. // middle rung back
  288. 30, 60, 30,
  289. 67, 60, 30,
  290. 30, 90, 30,
  291. 30, 90, 30,
  292. 67, 60, 30,
  293. 67, 90, 30,
  294. // top
  295. 0, 0, 0,
  296. 100, 0, 0,
  297. 100, 0, 30,
  298. 0, 0, 0,
  299. 100, 0, 30,
  300. 0, 0, 30,
  301. // top rung right
  302. 100, 0, 0,
  303. 100, 30, 0,
  304. 100, 30, 30,
  305. 100, 0, 0,
  306. 100, 30, 30,
  307. 100, 0, 30,
  308. // under top rung
  309. 30, 30, 0,
  310. 30, 30, 30,
  311. 100, 30, 30,
  312. 30, 30, 0,
  313. 100, 30, 30,
  314. 100, 30, 0,
  315. // between top rung and middle
  316. 30, 30, 0,
  317. 30, 60, 30,
  318. 30, 30, 30,
  319. 30, 30, 0,
  320. 30, 60, 0,
  321. 30, 60, 30,
  322. // top of middle rung
  323. 30, 60, 0,
  324. 67, 60, 30,
  325. 30, 60, 30,
  326. 30, 60, 0,
  327. 67, 60, 0,
  328. 67, 60, 30,
  329. // right of middle rung
  330. 67, 60, 0,
  331. 67, 90, 30,
  332. 67, 60, 30,
  333. 67, 60, 0,
  334. 67, 90, 0,
  335. 67, 90, 30,
  336. // bottom of middle rung.
  337. 30, 90, 0,
  338. 30, 90, 30,
  339. 67, 90, 30,
  340. 30, 90, 0,
  341. 67, 90, 30,
  342. 67, 90, 0,
  343. // right of bottom
  344. 30, 90, 0,
  345. 30, 150, 30,
  346. 30, 90, 30,
  347. 30, 90, 0,
  348. 30, 150, 0,
  349. 30, 150, 30,
  350. // bottom
  351. 0, 150, 0,
  352. 0, 150, 30,
  353. 30, 150, 30,
  354. 0, 150, 0,
  355. 30, 150, 30,
  356. 30, 150, 0,
  357. // left side
  358. 0, 0, 0,
  359. 0, 0, 30,
  360. 0, 150, 30,
  361. 0, 0, 0,
  362. 0, 150, 30,
  363. 0, 150, 0]),
  364. gl.STATIC_DRAW);
  365. }
  366. // Fill the buffer with colors for the 'F'.
  367. function setColors(gl) {
  368. gl.bufferData(
  369. gl.ARRAY_BUFFER,
  370. new Uint8Array([
  371. // left column front
  372. 200, 70, 120,
  373. 200, 70, 120,
  374. 200, 70, 120,
  375. 200, 70, 120,
  376. 200, 70, 120,
  377. 200, 70, 120,
  378. // top rung front
  379. 200, 70, 120,
  380. 200, 70, 120,
  381. 200, 70, 120,
  382. 200, 70, 120,
  383. 200, 70, 120,
  384. 200, 70, 120,
  385. // middle rung front
  386. 200, 70, 120,
  387. 200, 70, 120,
  388. 200, 70, 120,
  389. 200, 70, 120,
  390. 200, 70, 120,
  391. 200, 70, 120,
  392. // left column back
  393. 80, 70, 200,
  394. 80, 70, 200,
  395. 80, 70, 200,
  396. 80, 70, 200,
  397. 80, 70, 200,
  398. 80, 70, 200,
  399. // top rung back
  400. 80, 70, 200,
  401. 80, 70, 200,
  402. 80, 70, 200,
  403. 80, 70, 200,
  404. 80, 70, 200,
  405. 80, 70, 200,
  406. // middle rung back
  407. 80, 70, 200,
  408. 80, 70, 200,
  409. 80, 70, 200,
  410. 80, 70, 200,
  411. 80, 70, 200,
  412. 80, 70, 200,
  413. // top
  414. 70, 200, 210,
  415. 70, 200, 210,
  416. 70, 200, 210,
  417. 70, 200, 210,
  418. 70, 200, 210,
  419. 70, 200, 210,
  420. // top rung right
  421. 200, 200, 70,
  422. 200, 200, 70,
  423. 200, 200, 70,
  424. 200, 200, 70,
  425. 200, 200, 70,
  426. 200, 200, 70,
  427. // under top rung
  428. 210, 100, 70,
  429. 210, 100, 70,
  430. 210, 100, 70,
  431. 210, 100, 70,
  432. 210, 100, 70,
  433. 210, 100, 70,
  434. // between top rung and middle
  435. 210, 160, 70,
  436. 210, 160, 70,
  437. 210, 160, 70,
  438. 210, 160, 70,
  439. 210, 160, 70,
  440. 210, 160, 70,
  441. // top of middle rung
  442. 70, 180, 210,
  443. 70, 180, 210,
  444. 70, 180, 210,
  445. 70, 180, 210,
  446. 70, 180, 210,
  447. 70, 180, 210,
  448. // right of middle rung
  449. 100, 70, 210,
  450. 100, 70, 210,
  451. 100, 70, 210,
  452. 100, 70, 210,
  453. 100, 70, 210,
  454. 100, 70, 210,
  455. // bottom of middle rung.
  456. 76, 210, 100,
  457. 76, 210, 100,
  458. 76, 210, 100,
  459. 76, 210, 100,
  460. 76, 210, 100,
  461. 76, 210, 100,
  462. // right of bottom
  463. 140, 210, 80,
  464. 140, 210, 80,
  465. 140, 210, 80,
  466. 140, 210, 80,
  467. 140, 210, 80,
  468. 140, 210, 80,
  469. // bottom
  470. 90, 130, 110,
  471. 90, 130, 110,
  472. 90, 130, 110,
  473. 90, 130, 110,
  474. 90, 130, 110,
  475. 90, 130, 110,
  476. // left side
  477. 160, 160, 220,
  478. 160, 160, 220,
  479. 160, 160, 220,
  480. 160, 160, 220,
  481. 160, 160, 220,
  482. 160, 160, 220]),
  483. gl.STATIC_DRAW);
  484. }
  485. main();
  1. <canvas id="canvas"></canvas>
  2. <div id="uiContainer">
  3. <div id="ui">
  4. <div id="x"></div>
  5. <div id="y"></div>
  6. <div id="z"></div>
  7. <div id="angleX"></div>
  8. <div id="angleY"></div>
  9. <div id="angleZ"></div>
  10. <div id="scaleX"></div>
  11. <div id="scaleY"></div>
  12. <div id="scaleZ"></div>
  13. </div>
  14. </div>
  15. <!-- vertex shader -->
  16. <script id="vertex-shader-3d" type="x-shader/x-vertex">
  17. attribute vec4 a_position;
  18. attribute vec4 a_color;
  19. uniform mat4 u_matrix;
  20. varying vec4 v_color;
  21. void main() {
  22. // Multiply the position by the matrix.
  23. gl_Position = u_matrix * a_position;
  24. // Pass the color to the fragment shader.
  25. v_color = a_color;
  26. }
  27. </script>
  28. <!-- fragment shader -->
  29. <script id="fragment-shader-3d" type="x-shader/x-fragment">
  30. precision mediump float;
  31. // Passed in from the vertex shader.
  32. varying vec4 v_color;
  33. void main() {
  34. gl_FragColor = v_color;
  35. }
  36. </script>
  37. <script src="https://webglfundamentals.org/webgl/resources/webgl-utils.js"></script>
  38. <script src="https://webglfundamentals.org/webgl/resources/webgl-lessons-ui.js"></script>