2
0

misc_controls_orbit.html 3.6 KB

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