2
0

lots-of-objects-slow.html 5.9 KB

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