misc_controls_map.html 3.4 KB

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