misc_controls_orbit.html 3.2 KB

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