misc_controls_map.html 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. <meta property="og:title" content="three.js webgl - map controls">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/misc_controls_map.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/misc_controls_map.jpg">
  11. <link type="text/css" rel="stylesheet" href="main.css">
  12. <style>
  13. body {
  14. background-color: #ccc;
  15. color: #000;
  16. }
  17. a {
  18. color: #f00;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <div id="info">
  24. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - map controls
  25. </div>
  26. <script type="importmap">
  27. {
  28. "imports": {
  29. "three": "../build/three.module.js",
  30. "three/addons/": "./jsm/"
  31. }
  32. }
  33. </script>
  34. <script type="module">
  35. import * as THREE from 'three';
  36. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  37. import { MapControls } from 'three/addons/controls/MapControls.js';
  38. let camera, controls, scene, renderer;
  39. init();
  40. //render(); // remove when using animation loop
  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. renderer.setAnimationLoop( animate );
  49. document.body.appendChild( renderer.domElement );
  50. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 1000 );
  51. camera.position.set( 0, 200, - 200 );
  52. // controls
  53. controls = new MapControls( camera, renderer.domElement );
  54. //controls.addEventListener( 'change', render ); // call this only in static scenes (i.e., if there is no animation loop)
  55. controls.enableDamping = true; // an animation loop is required when either damping or auto-rotation are enabled
  56. controls.dampingFactor = 0.05;
  57. controls.screenSpacePanning = false;
  58. controls.minDistance = 100;
  59. controls.maxDistance = 500;
  60. controls.maxPolarAngle = Math.PI / 2;
  61. // world
  62. const geometry = new THREE.BoxGeometry();
  63. geometry.translate( 0, 0.5, 0 );
  64. const material = new THREE.MeshPhongMaterial( { color: 0xeeeeee, flatShading: true } );
  65. const mesh = new THREE.InstancedMesh( geometry, material, 500 );
  66. const dummy = new THREE.Object3D();
  67. for ( let i = 0; i < 500; i ++ ) {
  68. dummy.position.x = Math.random() * 1600 - 800;
  69. dummy.position.y = 0;
  70. dummy.position.z = Math.random() * 1600 - 800;
  71. dummy.scale.x = 20;
  72. dummy.scale.y = Math.random() * 80 + 10;
  73. dummy.scale.z = 20;
  74. dummy.updateMatrix();
  75. mesh.setMatrixAt( i, dummy.matrix );
  76. }
  77. scene.add( mesh );
  78. // lights
  79. const dirLight1 = new THREE.DirectionalLight( 0xffffff, 3 );
  80. dirLight1.position.set( 1, 1, 1 );
  81. scene.add( dirLight1 );
  82. const dirLight2 = new THREE.DirectionalLight( 0x002288, 3 );
  83. dirLight2.position.set( - 1, - 1, - 1 );
  84. scene.add( dirLight2 );
  85. const ambientLight = new THREE.AmbientLight( 0x555555 );
  86. scene.add( ambientLight );
  87. //
  88. window.addEventListener( 'resize', onWindowResize );
  89. const gui = new GUI();
  90. gui.add( controls, 'zoomToCursor' );
  91. gui.add( controls, 'screenSpacePanning' );
  92. }
  93. function onWindowResize() {
  94. camera.aspect = window.innerWidth / window.innerHeight;
  95. camera.updateProjectionMatrix();
  96. renderer.setSize( window.innerWidth, window.innerHeight );
  97. }
  98. function animate() {
  99. controls.update(); // only required if controls.enableDamping = true, or if controls.autoRotate = true
  100. render();
  101. }
  102. function render() {
  103. renderer.render( scene, camera );
  104. }
  105. </script>
  106. </body>
  107. </html>
粤ICP备19079148号