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

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