misc_controls_map.html 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. <!-- Import maps polyfill -->
  23. <!-- Remove this when import maps will be widely supported -->
  24. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  25. <script type="importmap">
  26. {
  27. "imports": {
  28. "three": "../build/three.module.js",
  29. "three/addons/": "./jsm/"
  30. }
  31. }
  32. </script>
  33. <script type="module">
  34. import * as THREE from 'three';
  35. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  36. import { MapControls } from 'three/addons/controls/OrbitControls.js';
  37. let camera, controls, scene, renderer;
  38. init();
  39. //render(); // remove when using next line for animation loop (requestAnimationFrame)
  40. animate();
  41. function init() {
  42. scene = new THREE.Scene();
  43. scene.background = new THREE.Color( 0xcccccc );
  44. scene.fog = new THREE.FogExp2( 0xcccccc, 0.002 );
  45. renderer = new THREE.WebGLRenderer( { antialias: true } );
  46. renderer.setPixelRatio( window.devicePixelRatio );
  47. renderer.setSize( window.innerWidth, window.innerHeight );
  48. document.body.appendChild( renderer.domElement );
  49. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 1000 );
  50. camera.position.set( 400, 200, 0 );
  51. // controls
  52. controls = new MapControls( camera, renderer.domElement );
  53. //controls.addEventListener( 'change', render ); // call this only in static scenes (i.e., if there is no animation loop)
  54. controls.enableDamping = true; // an animation loop is required when either damping or auto-rotation are enabled
  55. controls.dampingFactor = 0.05;
  56. controls.screenSpacePanning = false;
  57. controls.minDistance = 100;
  58. controls.maxDistance = 500;
  59. controls.maxPolarAngle = Math.PI / 2;
  60. // world
  61. const geometry = new THREE.BoxGeometry( 1, 1, 1 );
  62. geometry.translate( 0, 0.5, 0 );
  63. const material = new THREE.MeshPhongMaterial( { color: 0xffffff, flatShading: true } );
  64. for ( let i = 0; i < 500; i ++ ) {
  65. const mesh = new THREE.Mesh( geometry, material );
  66. mesh.position.x = Math.random() * 1600 - 800;
  67. mesh.position.y = 0;
  68. mesh.position.z = Math.random() * 1600 - 800;
  69. mesh.scale.x = 20;
  70. mesh.scale.y = Math.random() * 80 + 10;
  71. mesh.scale.z = 20;
  72. mesh.updateMatrix();
  73. mesh.matrixAutoUpdate = false;
  74. scene.add( mesh );
  75. }
  76. // lights
  77. const dirLight1 = new THREE.DirectionalLight( 0xffffff );
  78. dirLight1.position.set( 1, 1, 1 );
  79. scene.add( dirLight1 );
  80. const dirLight2 = new THREE.DirectionalLight( 0x002288 );
  81. dirLight2.position.set( - 1, - 1, - 1 );
  82. scene.add( dirLight2 );
  83. const ambientLight = new THREE.AmbientLight( 0x222222 );
  84. scene.add( ambientLight );
  85. //
  86. window.addEventListener( 'resize', onWindowResize );
  87. const gui = new GUI();
  88. gui.add( controls, 'screenSpacePanning' );
  89. }
  90. function onWindowResize() {
  91. camera.aspect = window.innerWidth / window.innerHeight;
  92. camera.updateProjectionMatrix();
  93. renderer.setSize( window.innerWidth, window.innerHeight );
  94. }
  95. function animate() {
  96. requestAnimationFrame( animate );
  97. controls.update(); // only required if controls.enableDamping = true, or if controls.autoRotate = true
  98. render();
  99. }
  100. function render() {
  101. renderer.render( scene, camera );
  102. }
  103. </script>
  104. </body>
  105. </html>