lots-of-objects-slow.html 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. html, body {
  10. height: 100%;
  11. margin: 0;
  12. }
  13. #c {
  14. width: 100%;
  15. height: 100%;
  16. display: block;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <canvas id="c"></canvas>
  22. </body>
  23. <script type="module">
  24. import * as THREE from '../../build/three.module.js';
  25. import {OrbitControls} from '../../examples/jsm/controls/OrbitControls.js';
  26. function main() {
  27. const canvas = document.querySelector('#c');
  28. const renderer = new THREE.WebGLRenderer({canvas});
  29. const fov = 60;
  30. const aspect = 2; // the canvas default
  31. const near = 0.1;
  32. const far = 10;
  33. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  34. camera.position.z = 2.5;
  35. const controls = new OrbitControls(camera, canvas);
  36. controls.enableDamping = true;
  37. controls.enablePan = false;
  38. controls.minDistance = 1.2;
  39. controls.maxDistance = 4;
  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.SphereGeometry(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.BoxGeometry(boxWidth, boxHeight, boxDepth);
  90. // make it so it scales away from the positive Z axis
  91. geometry.applyMatrix4(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.MathUtils.lerp(0.7, 0.3, amount);
  113. const saturation = 1;
  114. const lightness = THREE.MathUtils.lerp(0.4, 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.MathUtils.degToRad(lonNdx + file.xllcorner) + lonFudge;
  120. latHelper.rotation.x = THREE.MathUtils.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.applyMatrix4(positionHelper.matrixWorld);
  125. mesh.scale.set(0.005, 0.005, THREE.MathUtils.lerp(0.01, 0.5, amount));
  126. });
  127. });
  128. }
  129. loadFile('resources/data/gpw/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 renderRequested = false;
  144. function render() {
  145. renderRequested = 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. function requestRenderIfNotRequested() {
  156. if (!renderRequested) {
  157. renderRequested = true;
  158. requestAnimationFrame(render);
  159. }
  160. }
  161. controls.addEventListener('change', requestRenderIfNotRequested);
  162. window.addEventListener('resize', requestRenderIfNotRequested);
  163. }
  164. main();
  165. </script>
  166. </html>