1
0

misc_controls_orbit.html 3.4 KB

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