lots-of-objects-slow.html 6.0 KB

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