lots-of-objects-slow.html 5.8 KB

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