threejs-lots-of-objects-slow.html 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <!-- Licensed under a BSD license. See license.html for license -->
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
  7. <title>Three.js - Lots of Objects - Slow</title>
  8. <style>
  9. body {
  10. margin: 0;
  11. }
  12. #c {
  13. width: 100vw;
  14. height: 100vh;
  15. display: block;
  16. }
  17. </style>
  18. </head>
  19. <body>
  20. <canvas id="c"></canvas>
  21. </body>
  22. <script src="resources/threejs/r98/three.js"></script>
  23. <script src="resources/threejs/r98/js/controls/OrbitControls.js"></script>
  24. <script>
  25. 'use strict';
  26. /* global THREE */
  27. function main() {
  28. const canvas = document.querySelector('#c');
  29. const renderer = new THREE.WebGLRenderer({canvas: canvas});
  30. const fov = 60;
  31. const aspect = 2; // the canvas default
  32. const near = 0.1;
  33. const far = 10;
  34. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  35. camera.position.z = 2.5;
  36. const controls = new THREE.OrbitControls(camera, canvas);
  37. controls.enableDamping = true;
  38. controls.dampingFactor = 0.05;
  39. controls.rotateSpeed = 0.1;
  40. controls.update();
  41. const scene = new THREE.Scene();
  42. scene.background = new THREE.Color('black');
  43. {
  44. const loader = new THREE.TextureLoader();
  45. const texture = loader.load('resources/images/world.jpg', render);
  46. const geometry = new THREE.SphereBufferGeometry(1, 64, 32);
  47. const material = new THREE.MeshBasicMaterial({map: texture});
  48. scene.add(new THREE.Mesh(geometry, material));
  49. }
  50. async function loadFile(url) {
  51. const req = await fetch(url);
  52. return req.text();
  53. }
  54. function parseData(text) {
  55. const data = [];
  56. const settings = {data};
  57. let max;
  58. let min;
  59. // split into lines
  60. text.split('\n').forEach((line) => {
  61. // split the line by whitespace
  62. const parts = line.trim().split(/\s+/);
  63. if (parts.length === 2) {
  64. // only 2 parts, must be a key/value pair
  65. settings[parts[0]] = parseFloat(parts[1]);
  66. } else if (parts.length > 2) {
  67. // more than 2 parts, must be data
  68. const values = parts.map((v) => {
  69. const value = parseFloat(v);
  70. if (value === settings.NODATA_value) {
  71. return undefined;
  72. }
  73. max = Math.max(max === undefined ? value : max, value);
  74. min = Math.min(min === undefined ? value : min, value);
  75. return value;
  76. });
  77. data.push(values);
  78. }
  79. });
  80. return Object.assign(settings, {min, max});
  81. }
  82. function addBoxes(file) {
  83. const {min, max, data} = file;
  84. const range = max - min;
  85. // make one box geometry
  86. const boxWidth = 1;
  87. const boxHeight = 1;
  88. const boxDepth = 1;
  89. const geometry = new THREE.BoxBufferGeometry(boxWidth, boxHeight, boxDepth);
  90. // make it so it scales away from the positive Z axis
  91. geometry.applyMatrix(new THREE.Matrix4().makeTranslation(0, 0, 0.5));
  92. // these helpers will make it easy to position the boxes
  93. // We can rotate the lon helper on its Y axis to the longitude
  94. const lonHelper = new THREE.Object3D();
  95. scene.add(lonHelper);
  96. // We rotate the latHelper on its X axis to the latitude
  97. const latHelper = new THREE.Object3D();
  98. lonHelper.add(latHelper);
  99. // The position helper moves the object to the edge of the sphere
  100. const positionHelper = new THREE.Object3D();
  101. positionHelper.position.z = 1;
  102. latHelper.add(positionHelper);
  103. const lonFudge = Math.PI * .5;
  104. const latFudge = Math.PI * -0.135;
  105. data.forEach((row, latNdx) => {
  106. row.forEach((value, lonNdx) => {
  107. if (value === undefined) {
  108. return;
  109. }
  110. const amount = (value - min) / range;
  111. const material = new THREE.MeshBasicMaterial();
  112. const hue = THREE.Math.lerp(0.7, 0.3, amount);
  113. const saturation = 1;
  114. const lightness = THREE.Math.lerp(0.1, 1.0, amount);
  115. material.color.setHSL(hue, saturation, lightness);
  116. const mesh = new THREE.Mesh(geometry, material);
  117. scene.add(mesh);
  118. // adjust the helpers to point to the latitude and longitude
  119. lonHelper.rotation.y = THREE.Math.degToRad(lonNdx + file.xllcorner) + lonFudge;
  120. latHelper.rotation.x = THREE.Math.degToRad(latNdx + file.yllcorner) + latFudge;
  121. // use the world matrix of the position helper to
  122. // position this mesh.
  123. positionHelper.updateWorldMatrix(true, false);
  124. mesh.applyMatrix(positionHelper.matrixWorld);
  125. mesh.scale.set(0.005, 0.005, THREE.Math.lerp(0.01, 0.5, amount));
  126. });
  127. });
  128. }
  129. loadFile('resources/data/gpw/gpw-v4-basic-demographic-characteristics-rev10_a000_014_2010_1_deg_asc/gpw_v4_basic_demographic_characteristics_rev10_a000_014mt_2010_cntm_1_deg.asc')
  130. .then(parseData)
  131. .then(addBoxes)
  132. .then(render);
  133. function resizeRendererToDisplaySize(renderer) {
  134. const canvas = renderer.domElement;
  135. const width = canvas.clientWidth;
  136. const height = canvas.clientHeight;
  137. const needResize = canvas.width !== width || canvas.height !== height;
  138. if (needResize) {
  139. renderer.setSize(width, height, false);
  140. }
  141. return needResize;
  142. }
  143. let requestId;
  144. function render() {
  145. requestId = undefined;
  146. if (resizeRendererToDisplaySize(renderer)) {
  147. const canvas = renderer.domElement;
  148. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  149. camera.updateProjectionMatrix();
  150. }
  151. controls.update();
  152. renderer.render(scene, camera);
  153. }
  154. render();
  155. controls.addEventListener('change', () => {
  156. if (!requestId) {
  157. requestId = requestAnimationFrame(render);
  158. }
  159. });
  160. window.addEventListener('resize', render);
  161. // note: this is a workaround for an OrbitControls issue
  162. // in an iframe. Will remove once the issue is fixed in
  163. // three.js
  164. window.addEventListener('mousedown', (e) => {
  165. e.preventDefault();
  166. window.focus();
  167. });
  168. window.addEventListener('keydown', (e) => {
  169. e.preventDefault();
  170. });
  171. }
  172. main();
  173. </script>
  174. </html>