lots-of-objects-merged.html 5.8 KB

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