misc_controls_orbit.html 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. <meta property="og:title" content="three.js webgl - orbit controls">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/misc_controls_orbit.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/misc_controls_orbit.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> - orbit 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 { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  37. let camera, controls, scene, renderer;
  38. init();
  39. //render(); // remove when using animation loop
  40. function init() {
  41. scene = new THREE.Scene();
  42. scene.background = new THREE.Color( 0xcccccc );
  43. scene.fog = new THREE.FogExp2( 0xcccccc, 0.002 );
  44. renderer = new THREE.WebGLRenderer( { antialias: true } );
  45. renderer.setPixelRatio( window.devicePixelRatio );
  46. renderer.setSize( window.innerWidth, window.innerHeight );
  47. renderer.setAnimationLoop( animate );
  48. document.body.appendChild( renderer.domElement );
  49. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 1000 );
  50. camera.position.set( 400, 200, 0 );
  51. // controls
  52. controls = new OrbitControls( camera, renderer.domElement );
  53. controls.listenToKeyEvents( window ); // optional
  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.cursorStyle = 'grab';
  61. controls.maxPolarAngle = Math.PI / 2;
  62. // world
  63. const geometry = new THREE.ConeGeometry( 10, 30, 4, 1 );
  64. const material = new THREE.MeshPhongMaterial( { color: 0xffffff, 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.updateMatrix();
  72. mesh.setMatrixAt( i, dummy.matrix );
  73. }
  74. scene.add( mesh );
  75. // lights
  76. const dirLight1 = new THREE.DirectionalLight( 0xffffff, 3 );
  77. dirLight1.position.set( 1, 1, 1 );
  78. scene.add( dirLight1 );
  79. const dirLight2 = new THREE.DirectionalLight( 0x002288, 3 );
  80. dirLight2.position.set( - 1, - 1, - 1 );
  81. scene.add( dirLight2 );
  82. const ambientLight = new THREE.AmbientLight( 0x555555 );
  83. scene.add( ambientLight );
  84. //
  85. window.addEventListener( 'resize', onWindowResize );
  86. }
  87. function onWindowResize() {
  88. camera.aspect = window.innerWidth / window.innerHeight;
  89. camera.updateProjectionMatrix();
  90. renderer.setSize( window.innerWidth, window.innerHeight );
  91. }
  92. function animate() {
  93. controls.update(); // only required if controls.enableDamping = true, or if controls.autoRotate = true
  94. render();
  95. }
  96. function render() {
  97. renderer.render( scene, camera );
  98. }
  99. </script>
  100. </body>
  101. </html>
粤ICP备19079148号