1
0

misc_controls_map.html 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - map controls</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. <style>
  9. body {
  10. background-color: #ccc;
  11. color: #000;
  12. }
  13. a {
  14. color: #f00;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div id="info">
  20. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - map controls
  21. </div>
  22. <script type="importmap">
  23. {
  24. "imports": {
  25. "three": "../build/three.module.js",
  26. "three/addons/": "./jsm/"
  27. }
  28. }
  29. </script>
  30. <script type="module">
  31. import * as THREE from 'three';
  32. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  33. import { MapControls } from 'three/addons/controls/MapControls.js';
  34. let camera, controls, scene, renderer;
  35. init();
  36. //render(); // remove when using animation loop
  37. function init() {
  38. scene = new THREE.Scene();
  39. scene.background = new THREE.Color( 0xcccccc );
  40. scene.fog = new THREE.FogExp2( 0xcccccc, 0.002 );
  41. renderer = new THREE.WebGLRenderer( { antialias: true } );
  42. renderer.setPixelRatio( window.devicePixelRatio );
  43. renderer.setSize( window.innerWidth, window.innerHeight );
  44. renderer.setAnimationLoop( animate );
  45. document.body.appendChild( renderer.domElement );
  46. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 1000 );
  47. camera.position.set( 0, 200, - 400 );
  48. // controls
  49. controls = new MapControls( camera, renderer.domElement );
  50. //controls.addEventListener( 'change', render ); // call this only in static scenes (i.e., if there is no animation loop)
  51. controls.enableDamping = true; // an animation loop is required when either damping or auto-rotation are enabled
  52. controls.dampingFactor = 0.05;
  53. controls.screenSpacePanning = false;
  54. controls.minDistance = 100;
  55. controls.maxDistance = 500;
  56. controls.maxPolarAngle = Math.PI / 2;
  57. // world
  58. const geometry = new THREE.BoxGeometry();
  59. geometry.translate( 0, 0.5, 0 );
  60. const material = new THREE.MeshPhongMaterial( { color: 0xeeeeee, flatShading: true } );
  61. for ( let i = 0; i < 500; i ++ ) {
  62. const mesh = new THREE.Mesh( geometry, material );
  63. mesh.position.x = Math.random() * 1600 - 800;
  64. mesh.position.y = 0;
  65. mesh.position.z = Math.random() * 1600 - 800;
  66. mesh.scale.x = 20;
  67. mesh.scale.y = Math.random() * 80 + 10;
  68. mesh.scale.z = 20;
  69. mesh.updateMatrix();
  70. mesh.matrixAutoUpdate = false;
  71. scene.add( mesh );
  72. }
  73. // lights
  74. const dirLight1 = new THREE.DirectionalLight( 0xffffff, 3 );
  75. dirLight1.position.set( 1, 1, 1 );
  76. scene.add( dirLight1 );
  77. const dirLight2 = new THREE.DirectionalLight( 0x002288, 3 );
  78. dirLight2.position.set( - 1, - 1, - 1 );
  79. scene.add( dirLight2 );
  80. const ambientLight = new THREE.AmbientLight( 0x555555 );
  81. scene.add( ambientLight );
  82. //
  83. window.addEventListener( 'resize', onWindowResize );
  84. const gui = new GUI();
  85. gui.add( controls, 'zoomToCursor' );
  86. gui.add( controls, 'screenSpacePanning' );
  87. }
  88. function onWindowResize() {
  89. camera.aspect = window.innerWidth / window.innerHeight;
  90. camera.updateProjectionMatrix();
  91. renderer.setSize( window.innerWidth, window.innerHeight );
  92. }
  93. function animate() {
  94. controls.update(); // only required if controls.enableDamping = true, or if controls.autoRotate = true
  95. render();
  96. }
  97. function render() {
  98. renderer.render( scene, camera );
  99. }
  100. </script>
  101. </body>
  102. </html>