lots-of-objects-slow.html 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. <!-- Import maps polyfill -->
  24. <!-- Remove this when import maps will be widely supported -->
  25. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  26. <script type="importmap">
  27. {
  28. "imports": {
  29. "three": "../../build/three.module.js"
  30. }
  31. }
  32. </script>
  33. <script type="module">
  34. import * as THREE from 'three';
  35. import {OrbitControls} from '../../examples/jsm/controls/OrbitControls.js';
  36. function main() {
  37. const canvas = document.querySelector('#c');
  38. const renderer = new THREE.WebGLRenderer({canvas});
  39. const fov = 60;
  40. const aspect = 2; // the canvas default
  41. const near = 0.1;
  42. const far = 10;
  43. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  44. camera.position.z = 2.5;
  45. const controls = new OrbitControls(camera, canvas);
  46. controls.enableDamping = true;
  47. controls.enablePan = false;
  48. controls.minDistance = 1.2;
  49. controls.maxDistance = 4;
  50. controls.update();
  51. const scene = new THREE.Scene();
  52. scene.background = new THREE.Color('black');
  53. {
  54. const loader = new THREE.TextureLoader();
  55. const texture = loader.load('resources/images/world.jpg', render);
  56. const geometry = new THREE.SphereGeometry(1, 64, 32);
  57. const material = new THREE.MeshBasicMaterial({map: texture});
  58. scene.add(new THREE.Mesh(geometry, material));
  59. }
  60. async function loadFile(url) {
  61. const req = await fetch(url);
  62. return req.text();
  63. }
  64. function parseData(text) {
  65. const data = [];
  66. const settings = {data};
  67. let max;
  68. let min;
  69. // split into lines
  70. text.split('\n').forEach((line) => {
  71. // split the line by whitespace
  72. const parts = line.trim().split(/\s+/);
  73. if (parts.length === 2) {
  74. // only 2 parts, must be a key/value pair
  75. settings[parts[0]] = parseFloat(parts[1]);
  76. } else if (parts.length > 2) {
  77. // more than 2 parts, must be data
  78. const values = parts.map((v) => {
  79. const value = parseFloat(v);
  80. if (value === settings.NODATA_value) {
  81. return undefined;
  82. }
  83. max = Math.max(max === undefined ? value : max, value);
  84. min = Math.min(min === undefined ? value : min, value);
  85. return value;
  86. });
  87. data.push(values);
  88. }
  89. });
  90. return Object.assign(settings, {min, max});
  91. }
  92. function addBoxes(file) {
  93. const {min, max, data} = file;
  94. const range = max - min;
  95. // make one box geometry
  96. const boxWidth = 1;
  97. const boxHeight = 1;
  98. const boxDepth = 1;
  99. const geometry = new THREE.BoxGeometry(boxWidth, boxHeight, boxDepth);
  100. // make it so it scales away from the positive Z axis
  101. geometry.applyMatrix4(new THREE.Matrix4().makeTranslation(0, 0, 0.5));
  102. // these helpers will make it easy to position the boxes
  103. // We can rotate the lon helper on its Y axis to the longitude
  104. const lonHelper = new THREE.Object3D();
  105. scene.add(lonHelper);
  106. // We rotate the latHelper on its X axis to the latitude
  107. const latHelper = new THREE.Object3D();
  108. lonHelper.add(latHelper);
  109. // The position helper moves the object to the edge of the sphere
  110. const positionHelper = new THREE.Object3D();
  111. positionHelper.position.z = 1;
  112. latHelper.add(positionHelper);
  113. const lonFudge = Math.PI * .5;
  114. const latFudge = Math.PI * -0.135;
  115. data.forEach((row, latNdx) => {
  116. row.forEach((value, lonNdx) => {
  117. if (value === undefined) {
  118. return;
  119. }
  120. const amount = (value - min) / range;
  121. const material = new THREE.MeshBasicMaterial();
  122. const hue = THREE.MathUtils.lerp(0.7, 0.3, amount);
  123. const saturation = 1;
  124. const lightness = THREE.MathUtils.lerp(0.4, 1.0, amount);
  125. material.color.setHSL(hue, saturation, lightness);
  126. const mesh = new THREE.Mesh(geometry, material);
  127. scene.add(mesh);
  128. // adjust the helpers to point to the latitude and longitude
  129. lonHelper.rotation.y = THREE.MathUtils.degToRad(lonNdx + file.xllcorner) + lonFudge;
  130. latHelper.rotation.x = THREE.MathUtils.degToRad(latNdx + file.yllcorner) + latFudge;
  131. // use the world matrix of the position helper to
  132. // position this mesh.
  133. positionHelper.updateWorldMatrix(true, false);
  134. mesh.applyMatrix4(positionHelper.matrixWorld);
  135. mesh.scale.set(0.005, 0.005, THREE.MathUtils.lerp(0.01, 0.5, amount));
  136. });
  137. });
  138. }
  139. loadFile('resources/data/gpw/gpw_v4_basic_demographic_characteristics_rev10_a000_014mt_2010_cntm_1_deg.asc')
  140. .then(parseData)
  141. .then(addBoxes)
  142. .then(render);
  143. function resizeRendererToDisplaySize(renderer) {
  144. const canvas = renderer.domElement;
  145. const width = canvas.clientWidth;
  146. const height = canvas.clientHeight;
  147. const needResize = canvas.width !== width || canvas.height !== height;
  148. if (needResize) {
  149. renderer.setSize(width, height, false);
  150. }
  151. return needResize;
  152. }
  153. let renderRequested = false;
  154. function render() {
  155. renderRequested = undefined;
  156. if (resizeRendererToDisplaySize(renderer)) {
  157. const canvas = renderer.domElement;
  158. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  159. camera.updateProjectionMatrix();
  160. }
  161. controls.update();
  162. renderer.render(scene, camera);
  163. }
  164. render();
  165. function requestRenderIfNotRequested() {
  166. if (!renderRequested) {
  167. renderRequested = true;
  168. requestAnimationFrame(render);
  169. }
  170. }
  171. controls.addEventListener('change', requestRenderIfNotRequested);
  172. window.addEventListener('resize', requestRenderIfNotRequested);
  173. }
  174. main();
  175. </script>
  176. </html>