当前位置:   article > 正文

HTML3D立体城市特效代码_html3d代码

html3d代码

HTML3D立体城市特效代码

鼠标可以控制“行走”方向,立体性很强

index.html代码如下

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>3D城市</title>
  6. <style>
  7. html {
  8. overflow: hidden;
  9. touch-action: none;
  10. content-zooming: none;
  11. }
  12. body {
  13. position: absolute;
  14. margin: 0;
  15. padding: 0;
  16. width: 100%;
  17. height: 100%;
  18. background: #000;
  19. }
  20. #canvas {
  21. position: absolute;
  22. width: 100%;
  23. height: 100%;
  24. background: #000;
  25. cursor: crosshair;
  26. }</style>
  27. </head>
  28. <body>
  29. <canvas id="canvas"></canvas>
  30. <!-- GLSL shaders in the header -->
  31. <script id="shader-vs" type="x-shader/x-vertex">
  32. attribute vec3 aVertexPosition;
  33. attribute vec3 aVertexNormal;
  34. uniform mat4 uMVMatrix;
  35. uniform mat4 uPMatrix;
  36. uniform mat3 uNMatrix;
  37. varying vec3 vNormal;
  38. varying vec4 vPosition;
  39. varying vec3 vPos;
  40. void main(void) {
  41. vPosition = uMVMatrix * vec4(aVertexPosition, 1.0);
  42. gl_Position = uPMatrix * vPosition;
  43. vNormal = uNMatrix * aVertexNormal;
  44. vPos = aVertexPosition;
  45. }
  46. </script>
  47. <script id="shader-fs" type="x-shader/x-fragment">
  48. precision mediump float;
  49. varying vec3 vNormal;
  50. varying vec4 vPosition;
  51. uniform vec3 uAmbientColor;
  52. uniform vec3 uPointLightingLocation;
  53. uniform vec3 uPointLightingSpecularColor;
  54. uniform vec3 uPointLightingDiffuseColor;
  55. uniform vec2 uResolution;
  56. varying vec3 vPos;
  57. void main(void) {
  58. vec3 col;
  59. if (vPos.y < 0.0) {
  60. col = vec3(0.3, 0.2, 0.1);
  61. } else {
  62. col = vec3(1.0, 1.0, 1.0);
  63. }
  64. vec3 lightDirection = normalize(uPointLightingLocation - vPosition.xyz);
  65. vec3 normal = normalize(vNormal);
  66. vec3 eyeDirection = normalize(-vPosition.xyz);
  67. vec3 reflectionDirection = reflect(-lightDirection, normal);
  68. float specularLightWeighting = pow(max(dot(reflectionDirection, eyeDirection), 0.0), 6.0);
  69. float diffuseLightWeighting = max(dot(normal, lightDirection), 0.0);
  70. col *= uAmbientColor
  71. + uPointLightingSpecularColor * specularLightWeighting
  72. + uPointLightingDiffuseColor * diffuseLightWeighting;
  73. vec2 uv = gl_FragCoord.xy / uResolution;
  74. float d = length(uv - vec2(0.5,0.5));
  75. col *= 2.5 - d * 3.5;
  76. gl_FragColor = vec4(col.x, col.y, col.z, 1.0);
  77. }
  78. </script>
  79. <script>
  80. // WebGL micro-library
  81. // Gerard Ferrandez
  82. // last modified: 29/Mar/2016
  83. var webgl = {};
  84. (function() {
  85. // webgl matrix 3x3
  86. function mat3 () {
  87. var mat = new Float32Array([1,0,0,0,1,0,0,0,1]);
  88. mat.identity = function() {
  89. this[0] = 1;
  90. this[1] = 0;
  91. this[2] = 0;
  92. this[3] = 0;
  93. this[4] = 1;
  94. this[5] = 0;
  95. this[6] = 0;
  96. this[7] = 0;
  97. this[8] = 1;
  98. return this;
  99. }
  100. mat.normalFromMat4 = function (a) {
  101. var
  102. a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3],
  103. a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7],
  104. a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11],
  105. a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15],
  106. b00 = a00 * a11 - a01 * a10,
  107. b01 = a00 * a12 - a02 * a10,
  108. b02 = a00 * a13 - a03 * a10,
  109. b03 = a01 * a12 - a02 * a11,
  110. b04 = a01 * a13 - a03 * a11,
  111. b05 = a02 * a13 - a03 * a12,
  112. b06 = a20 * a31 - a21 * a30,
  113. b07 = a20 * a32 - a22 * a30,
  114. b08 = a20 * a33 - a23 * a30,
  115. b09 = a21 * a32 - a22 * a31,
  116. b10 = a21 * a33 - a23 * a31,
  117. b11 = a22 * a33 - a23 * a32,
  118. det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;
  119. if (!det) return null;
  120. det = 1.0 / det;
  121. this[0] = (a11 * b11 - a12 * b10 + a13 * b09) * det;
  122. this[1] = (a12 * b08 - a10 * b11 - a13 * b07) * det;
  123. this[2] = (a10 * b10 - a11 * b08 + a13 * b06) * det;
  124. this[3] = (a02 * b10 - a01 * b11 - a03 * b09) * det;
  125. this[4] = (a00 * b11 - a02 * b08 + a03 * b07) * det;
  126. this[5] = (a01 * b08 - a00 * b10 - a03 * b06) * det;
  127. this[6] = (a31 * b05 - a32 * b04 + a33 * b03) * det;
  128. this[7] = (a32 * b02 - a30 * b05 - a33 * b01) * det;
  129. this[8] = (a30 * b04 - a31 * b02 + a33 * b00) * det;
  130. return this;
  131. }
  132. return mat;
  133. }
  134. // webgl matrix 4x4
  135. function mat4 () {
  136. var mat = new Float32Array([1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1]);
  137. mat.identity = function() {
  138. this[0] = 1;
  139. this[1] = 0;
  140. this[2] = 0;
  141. this[3] = 0;
  142. this[4] = 0;
  143. this[5] = 1;
  144. this[6] = 0;
  145. this[7] = 0;
  146. this[8] = 0;
  147. this[9] = 0;
  148. this[10] = 1;
  149. this[11] = 0;
  150. this[12] = 0;
  151. this[13] = 0;
  152. this[14] = 0;
  153. this[15] = 1;
  154. return this;
  155. };
  156. mat.translate = function(v) {
  157. var d = v[0],
  158. e = v[1],
  159. b = v[2];
  160. this[12] = this[0] * d + this[4] * e + this[8] * b + this[12];
  161. this[13] = this[1] * d + this[5] * e + this[9] * b + this[13];
  162. this[14] = this[2] * d + this[6] * e + this[10] * b + this[14];
  163. this[15] = this[3] * d + this[7] * e + this[11] * b + this[15];
  164. return this;
  165. };
  166. mat.fromTranslation = function(v) {
  167. this[0] = 1;
  168. this[1] = 0;
  169. this[2] = 0;
  170. this[3] = 0;
  171. this[4] = 0;
  172. this[5] = 1;
  173. this[6] = 0;
  174. this[7] = 0;
  175. this[8] = 0;
  176. this[9] = 0;
  177. this[10] = 1;
  178. this[11] = 0;
  179. this[12] = v[0];
  180. this[13] = v[1];
  181. this[14] = v[2];
  182. this[15] = 1;
  183. return this;
  184. };
  185. mat.rotateX = function (angle) {
  186. var s = Math.sin(angle),
  187. c = Math.cos(angle),
  188. a10 = this[4],
  189. a11 = this[5],
  190. a12 = this[6],
  191. a13 = this[7],
  192. a20 = this[8],
  193. a21 = this[9],
  194. a22 = this[10],
  195. a23 = this[11];
  196. this[4] = a10 * c + a20 * s;
  197. this[5] = a11 * c + a21 * s;
  198. this[6] = a12 * c + a22 * s;
  199. this[7] = a13 * c + a23 * s;
  200. this[8] = a20 * c - a10 * s;
  201. this[9] = a21 * c - a11 * s;
  202. this[10] = a22 * c - a12 * s;
  203. this[11] = a23 * c - a13 * s;
  204. return this;
  205. };
  206. mat.rotateY = function (angle) {
  207. var s = Math.sin(angle),
  208. c = Math.cos(angle),
  209. a00 = this[0],
  210. a01 = this[1],
  211. a02 = this[2],
  212. a03 = this[3],
  213. a20 = this[8],
  214. a21 = this[9],
  215. a22 = this[10],
  216. a23 = this[11];
  217. this[0] = a00 * c - a20 * s;
  218. this[1] = a01 * c - a21 * s;
  219. this[2] = a02 * c - a22 * s;
  220. this[3] = a03 * c - a23 * s;
  221. this[8] = a00 * s + a20 * c;
  222. this[9] = a01 * s + a21 * c;
  223. this[10] = a02 * s + a22 * c;
  224. this[11] = a03 * s + a23 * c;
  225. return this;
  226. };
  227. mat.rotateZ = function (angle) {
  228. var s = Math.sin(angle),
  229. c = Math.cos(angle),
  230. a00 = this[0],
  231. a01 = this[1],
  232. a02 = this[2],
  233. a03 = this[3],
  234. a10 = this[4],
  235. a11 = this[5],
  236. a12 = this[6],
  237. a13 = this[7];
  238. this[0] = a00 * c + a10 * s;
  239. this[1] = a01 * c + a11 * s;
  240. this[2] = a02 * c + a12 * s;
  241. this[3] = a03 * c + a13 * s;
  242. this[4] = a10 * c - a00 * s;
  243. this[5] = a11 * c - a01 * s;
  244. this[6] = a12 * c - a02 * s;
  245. this[7] = a13 * c - a03 * s;
  246. return this;
  247. };
  248. mat.multiply = function (a, b) {
  249. var a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3],
  250. a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7],
  251. a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11],
  252. a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15];
  253. var b0 = b[0], b1 = b[1], b2 = b[2], b3 = b[3];
  254. this[0] = b0*a00 + b1*a10 + b2*a20 + b3*a30;
  255. this[1] = b0*a01 + b1*a11 + b2*a21 + b3*a31;
  256. this[2] = b0*a02 + b1*a12 + b2*a22 + b3*a32;
  257. this[3] = b0*a03 + b1*a13 + b2*a23 + b3*a33;
  258. b0 = b[4]; b1 = b[5]; b2 = b[6]; b3 = b[7];
  259. this[4] = b0*a00 + b1*a10 + b2*a20 + b3*a30;
  260. this[5] = b0*a01 + b1*a11 + b2*a21 + b3*a31;
  261. this[6] = b0*a02 + b1*a12 + b2*a22 + b3*a32;
  262. this[7] = b0*a03 + b1*a13 + b2*a23 + b3*a33;
  263. b0 = b[8]; b1 = b[9]; b2 = b[10]; b3 = b[11];
  264. this[8] = b0*a00 + b1*a10 + b2*a20 + b3*a30;
  265. this[9] = b0*a01 + b1*a11 + b2*a21 + b3*a31;
  266. this[10] = b0*a02 + b1*a12 + b2*a22 + b3*a32;
  267. this[11] = b0*a03 + b1*a13 + b2*a23 + b3*a33;
  268. b0 = b[12]; b1 = b[13]; b2 = b[14]; b3 = b[15];
  269. this[12] = b0*a00 + b1*a10 + b2*a20 + b3*a30;
  270. this[13] = b0*a01 + b1*a11 + b2*a21 + b3*a31;
  271. this[14] = b0*a02 + b1*a12 + b2*a22 + b3*a32;
  272. this[15] = b0*a03 + b1*a13 + b2*a23 + b3*a33;
  273. return this;
  274. };
  275. mat.frustum = function(a, b, c, d, e, g) {
  276. var h = b - a,
  277. i = d - c,
  278. j = g - e;
  279. this[0] = e * 2 / h;
  280. this[1] = 0;
  281. this[2] = 0;
  282. this[3] = 0;
  283. this[4] = 0;
  284. this[5] = e * 2 / i;
  285. this[6] = 0;
  286. this[7] = 0;
  287. this[8] = (b + a) / h;
  288. this[9] = (d + c) / i;
  289. this[10] = -(g + e) / j;
  290. this[11] = -1;
  291. this[12] = 0;
  292. this[13] = 0;
  293. this[14] = -(g * e * 2) / j;
  294. this[15] = 0;
  295. return this;
  296. };
  297. mat.perspective = function(a, b, c, d) {
  298. a = c * Math.tan(a * Math.PI / 360);
  299. b = a * b;
  300. this.frustum(-b, b, -a, a, c, d);
  301. return this;
  302. };
  303. return mat;
  304. }
  305. // set uniforms
  306. function setUniforms (gl, program) {
  307. var uniformSetters = {
  308. 0x8B50: function(v1,v2) {gl.uniform2f(this.index,v1,v2);},
  309. 0x8B51: function(v1,v2,v3) {gl.uniform3f(this.index,v1,v2,v3);},
  310. 0x8B52: function(v1,v2,v3,v4) {gl.uniform4f(this.index,v1,v2,v3,v4);},
  311. 0x8B53: function(v1,v2) {gl.uniform2i(this.index,v1,v2);},
  312. 0x8B54: function(v1,v2,v3) {gl.uniform3i(this.index,v1,v2,v3);},
  313. 0x8B55: function(v1,v2,v3,v4) {gl.uniform4i(this.index,v1,v2,v3,v4);},
  314. 0x8B56: function(v) {gl.uniform1i(this.index,v);},
  315. 0x8B5A: function(v) {gl.uniformMatrix2fv(this.index,false,v);},
  316. 0x8B5B: function(v) {gl.uniformMatrix3fv(this.index,false,v);},
  317. 0x8B5C: function(v) {gl.uniformMatrix4fv(this.index,false,v);},
  318. 0x8B5E: function(v) {gl.uniform1i(this.index,v);},
  319. 0x1404: function(v) {gl.uniform1i(this.index,v);},
  320. 0x1406: function(v) {gl.uniform1f(this.index,v);}
  321. },
  322. num = gl.getProgramParameter(program, gl.ACTIVE_UNIFORMS),
  323. uniforms = {};
  324. for (var i = 0; i < num; i++) {
  325. var u = gl.getActiveUniform(program, i);
  326. uniforms[u.name] = {
  327. index: gl.getUniformLocation(program, u.name),
  328. set: uniformSetters[u.type],
  329. get: function () {
  330. return gl.getUniform(program, this.index);
  331. }
  332. };
  333. }
  334. return uniforms;
  335. }
  336. // set attributes
  337. function setAttributes (gl, program) {
  338. var num = gl.getProgramParameter(program, gl.ACTIVE_ATTRIBUTES), attributes = {};
  339. for (var i = 0; i < num; i++) {
  340. var a = gl.getActiveAttrib(program, i)
  341. var index = gl.getAttribLocation(program, a.name);
  342. gl.enableVertexAttribArray(index);
  343. attributes[a.name] = {
  344. buffer: gl.createBuffer(),
  345. index: index,
  346. gl: gl,
  347. numElements: 0,
  348. load: function (data, size, normalize) {
  349. var gl = this.gl;
  350. this.data = data instanceof Array ? new Float32Array(data) : data;
  351. this.itemSize = size;
  352. gl.bindBuffer(gl.ARRAY_BUFFER, this.buffer);
  353. gl.bufferData(gl.ARRAY_BUFFER, this.data, gl.STATIC_DRAW);
  354. this.numElements = this.data.length / this.itemSize;
  355. gl.vertexAttribPointer(
  356. this.index,
  357. this.itemSize,
  358. gl.FLOAT,
  359. normalize || false, 0, 0
  360. );
  361. }
  362. };
  363. }
  364. return attributes;
  365. };
  366. // indexed geometry buffer
  367. function setIndices (gl) {
  368. var indices = {
  369. buffer: null,
  370. gl: gl,
  371. numElements: 0,
  372. load: function (data) {
  373. this.data = data instanceof Array ? new Uint16Array(data) : data;
  374. if (!this.buffer) this.buffer = this.gl.createBuffer();
  375. this.gl.bindBuffer(this.gl.ELEMENT_ARRAY_BUFFER, this.buffer);
  376. this.gl.bufferData(this.gl.ELEMENT_ARRAY_BUFFER, this.data, this.gl.STATIC_DRAW);
  377. this.numElements = data.length;
  378. }
  379. };
  380. return indices;
  381. }
  382. // compile shaders
  383. function compile (gl, vs, fs) {
  384. // compile vertex shader
  385. var vertexShader = gl.createShader(gl.VERTEX_SHADER);
  386. gl.shaderSource(vertexShader, document.getElementById(vs).text);
  387. gl.compileShader(vertexShader);
  388. // compile fragment shader
  389. var fragmentShader = gl.createShader(gl.FRAGMENT_SHADER);
  390. gl.shaderSource(fragmentShader, document.getElementById(fs).text);
  391. gl.compileShader(fragmentShader);
  392. // attach shader programs
  393. var program = gl.createProgram();
  394. gl.attachShader(program, vertexShader);
  395. gl.attachShader(program, fragmentShader);
  396. gl.linkProgram(program);
  397. if (!gl.getProgramParameter(program, gl.LINK_STATUS)) throw new Error("Unable to compile shaders!");
  398. gl.useProgram(program);
  399. return program;
  400. };
  401. // initialize context
  402. this.context = function (id, options) {
  403. // canvas container
  404. this.canvas = document.getElementById(id);
  405. canvas.width = this.width = this.canvas.offsetWidth;
  406. canvas.height = this.height = this.canvas.offsetHeight;
  407. // webgl context
  408. var gl = this.canvas.getContext("webgl", options);
  409. if (!gl) gl = this.canvas.getContext("experimental-webgl");
  410. if (!gl) throw new Error('This browser does not support WebGL');
  411. this.gl = gl;
  412. // compile shaders
  413. var program = compile(gl, "shader-vs", "shader-fs");
  414. // set uniforms and attributes
  415. this.uniforms = setUniforms(gl, program);
  416. this.attributes = setAttributes(gl, program);
  417. this.indices = setIndices(gl);
  418. this.camera = setCamera();
  419. return gl;
  420. }
  421. // clear screen
  422. this.clear = function (r, g, b) {
  423. this.gl.viewport(0, 0, this.gl.drawingBufferWidth, this.gl.drawingBufferHeight);
  424. this.gl.clearColor(r || 0, g || 0, b || 0, 1);
  425. this.gl.clear(this.gl.COLOR_BUFFER_BIT | this.gl.DEPTH_BUFFER_BIT);
  426. }
  427. // camera
  428. function setCamera () {
  429. var camera = {};
  430. camera.webgl = webgl;
  431. camera.fov = 90;
  432. camera.mvMatrix = mat4();
  433. camera.currentRotationMatrix = mat4();
  434. camera.newRotationMatrix = mat4();
  435. camera.pMatrix = mat4();
  436. camera.normalMatrix = mat3();
  437. camera.init = function (fov) {
  438. this.fov = fov || 90;
  439. var width = this.webgl.width,
  440. height = this.webgl.height;
  441. this.pMatrix.perspective(this.fov, width / height, 0.01, 100.0);
  442. this.webgl.uniforms.uPMatrix.set(this.pMatrix);
  443. if (webgl.uniforms.uResolution) webgl.uniforms.uResolution.set(width, height);
  444. return this;
  445. };
  446. return camera;
  447. }
  448. }).apply(webgl);
  449. </script>
  450. <script>
  451. ~ function() {
  452. 'use strict';
  453. function run() {
  454. requestAnimationFrame(run);
  455. webgl.clear(0, 0.05, 0.1);
  456. if (pointer.isDown) {
  457. cx += (pointer.x - pointer.xb) / 2000;
  458. cy += (pointer.y - pointer.yb) / 2000;
  459. cz = 0.015;
  460. }
  461. cx *= 0.98;
  462. cy *= 0.98;
  463. cz *= 0.98;
  464. camera.move(cx, cy, cz+0.01);
  465. gl.drawArrays(gl.TRIANGLES, 0, 30 * nCubes);
  466. pointer.xb = pointer.x;
  467. pointer.yb = pointer.y;
  468. }
  469. // init
  470. var size = 64,
  471. vertices = new Float32Array(90 + 90 * size * size),
  472. normals = new Float32Array(90 + 90 * size * size),
  473. nCubes = 0, cx = 0, cy = 0, cz = 0;
  474. // webgl
  475. var gl = webgl.context('canvas');
  476. gl.enable(gl.DEPTH_TEST);
  477. gl.enable(gl.CULL_FACE);
  478. // create cubes
  479. function createCube (x, y, z, l, h, w) {
  480. l /= 2;
  481. w /= 2;
  482. h /= 2;
  483. // vertices
  484. vertices.set([
  485. x-l,y-h,z+w,x+l,y-h,z+w,x+l,y+h,z+w,x-l,y-h,z+w,x+l,y+h,z+w,x-l,y+h,z+w,
  486. x-l,y-h,z-w,x-l,y+h,z-w,x+l,y+h,z-w,x-l,y-h,z-w,x+l,y+h,z-w,x+l,y-h,z-w,
  487. x-l,y+h,z-w,x-l,y+h,z+w,x+l,y+h,z+w,x-l,y+h,z-w,x+l,y+h,z+w,x+l,y+h,z-w,
  488. x+l,y-h,z-w,x+l,y+h,z-w,x+l,y+h,z+w,x+l,y-h,z-w,x+l,y+h,z+w,x+l,y-h,z+w,
  489. x-l,y-h,z-w,x-l,y-h,z+w,x-l,y+h,z+w,x-l,y-h,z-w,x-l,y+h,z+w,x-l,y+h,z-w
  490. ], nCubes * 5 * 18);
  491. // normals
  492. normals.set([
  493. 0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,
  494. 0,0,-1,0,0,-1,0,0,-1,0,0,-1,0,0,-1,0,0,-1,
  495. 0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,
  496. 1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,
  497. -1,0,0,-1,0,0,-1,0,0,-1,0,0,-1,0,0,-1,0,0
  498. ], nCubes * 5 * 18);
  499. nCubes++;
  500. }
  501. // create pointer
  502. var pointer = {
  503. x: 0,
  504. y: 0,
  505. xb: 0,
  506. yb: 0,
  507. isDown: false,
  508. add: function(elem, events, fn) {
  509. for (var i = 0, e = events.split(','); i < e.length; i++) {
  510. elem.addEventListener(e[i], fn.bind(pointer), false);
  511. }
  512. },
  513. pointer: function(e) {
  514. var touchMode = e.targetTouches, pointer;
  515. if (touchMode) {
  516. e.preventDefault();
  517. pointer = touchMode[0];
  518. } else pointer = e;
  519. this.x = pointer.clientX;
  520. this.y = pointer.clientY;
  521. }
  522. };
  523. // pointer events
  524. pointer.add(window, 'mousemove,touchmove', function(e) {this.pointer(e);});
  525. pointer.add(webgl.canvas, 'mousedown,touchstart', function(e) {
  526. this.pointer(e);
  527. this.xb = this.x;
  528. this.yb = this.y;
  529. this.isDown = true;
  530. });
  531. pointer.add(window, 'mouseup,touchend,touchcancel', function(e) {this.isDown = false;});
  532. // camera
  533. var camera = webgl.camera.init(60);
  534. camera.x = 0;
  535. camera.y = -2;
  536. camera.z = size / 3;
  537. camera.ax = 0;
  538. camera.ay = 0;
  539. camera.move = function (deltaX, deltaY, deltaZ) {
  540. this.ax -= deltaX / 10;
  541. this.ay -= deltaY / 10;
  542. this.x -= Math.sin(this.ax) * deltaZ;
  543. this.z -= Math.cos(this.ax) * deltaZ;
  544. this.y -= Math.sin(this.ay) * deltaZ;
  545. if (this.y > -0.5) this.y = -0.5;
  546. if (this.x > size * 0.5) this.x = size * 0.5; else if (this.x < -size * 0.5) this.x = -size * 0.5;
  547. if (this.z > size * 0.5) this.z = size * 0.5; else if (this.z < -size * 0.5) this.z = -size * 0.5;
  548. this.mvMatrix.identity().rotateX(-this.ay).rotateY(-this.ax).translate([-this.x, this.y, -this.z]);
  549. this.webgl.uniforms.uMVMatrix.set(this.mvMatrix);
  550. this.normalMatrix.normalFromMat4(this.mvMatrix);
  551. this.webgl.uniforms.uNMatrix.set(this.normalMatrix);
  552. };
  553. // create world
  554. createCube(0, -0.1, 0, size * 20, 0.1, size * 20);
  555. for (var x = 0; x < size; x++) {
  556. for (var y = 0; y < size; y++) {
  557. var xf = x - size * 0.5,
  558. yf = y - size * 0.5,
  559. d = Math.sqrt(xf * xf + yf * yf),
  560. h = 0.25 + 30 * Math.random() / d,
  561. w = 0.7;
  562. if (d < size * 0.5) createCube(xf, h * 0.5, yf, w, h, w);
  563. }
  564. }
  565. webgl.attributes.aVertexPosition.load(vertices, 3);
  566. webgl.attributes.aVertexNormal.load(normals, 3);
  567. // set lightning
  568. webgl.uniforms.uAmbientColor.set(0,0,0);
  569. webgl.uniforms.uPointLightingLocation.set(0, -1, -10);
  570. webgl.uniforms.uPointLightingSpecularColor.set(1,0.55,0.15);
  571. webgl.uniforms.uPointLightingDiffuseColor.set(0,0.25,0.5);
  572. run();
  573. } ();
  574. </script>
  575. </body>
  576. </html>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/834655
推荐阅读
相关标签
  

闽ICP备14008679号