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

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