misc_controls_trackball.html 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - trackball 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="http://threejs.org" target="_blank" rel="noopener">three.js</a> - trackball controls<br />
  21. MOVE mouse &amp; press LEFT/A: rotate, MIDDLE/S: zoom, RIGHT/D: pan
  22. </div>
  23. <script type="module">
  24. import * as THREE from '../build/three.module.js';
  25. import Stats from './jsm/libs/stats.module.js';
  26. import { TrackballControls } from './jsm/controls/TrackballControls.js';
  27. var camera, controls, scene, renderer, stats;
  28. init();
  29. animate();
  30. function init() {
  31. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 1000 );
  32. camera.position.z = 500;
  33. // world
  34. scene = new THREE.Scene();
  35. scene.background = new THREE.Color( 0xcccccc );
  36. scene.fog = new THREE.FogExp2( 0xcccccc, 0.002 );
  37. var geometry = new THREE.CylinderBufferGeometry( 0, 10, 30, 4, 1 );
  38. var material = new THREE.MeshPhongMaterial( { color: 0xffffff, flatShading: true } );
  39. for ( var i = 0; i < 500; i ++ ) {
  40. var mesh = new THREE.Mesh( geometry, material );
  41. mesh.position.x = ( Math.random() - 0.5 ) * 1000;
  42. mesh.position.y = ( Math.random() - 0.5 ) * 1000;
  43. mesh.position.z = ( Math.random() - 0.5 ) * 1000;
  44. mesh.updateMatrix();
  45. mesh.matrixAutoUpdate = false;
  46. scene.add( mesh );
  47. }
  48. // lights
  49. var light = new THREE.DirectionalLight( 0xffffff );
  50. light.position.set( 1, 1, 1 );
  51. scene.add( light );
  52. var light = new THREE.DirectionalLight( 0x002288 );
  53. light.position.set( - 1, - 1, - 1 );
  54. scene.add( light );
  55. var light = new THREE.AmbientLight( 0x222222 );
  56. scene.add( light );
  57. // renderer
  58. renderer = new THREE.WebGLRenderer( { antialias: true } );
  59. renderer.setPixelRatio( window.devicePixelRatio );
  60. renderer.setSize( window.innerWidth, window.innerHeight );
  61. document.body.appendChild( renderer.domElement );
  62. controls = new TrackballControls( camera, renderer.domElement );
  63. controls.rotateSpeed = 1.0;
  64. controls.zoomSpeed = 1.2;
  65. controls.panSpeed = 0.8;
  66. controls.noZoom = false;
  67. controls.noPan = false;
  68. controls.staticMoving = true;
  69. controls.dynamicDampingFactor = 0.3;
  70. controls.keys = [ 65, 83, 68 ];
  71. controls.addEventListener( 'change', render );
  72. stats = new Stats();
  73. document.body.appendChild( stats.dom );
  74. //
  75. window.addEventListener( 'resize', onWindowResize, false );
  76. //
  77. render();
  78. }
  79. function onWindowResize() {
  80. camera.aspect = window.innerWidth / window.innerHeight;
  81. camera.updateProjectionMatrix();
  82. renderer.setSize( window.innerWidth, window.innerHeight );
  83. controls.handleResize();
  84. render();
  85. }
  86. function animate() {
  87. requestAnimationFrame( animate );
  88. controls.update();
  89. stats.update();
  90. }
  91. function render() {
  92. renderer.render( scene, camera );
  93. }
  94. </script>
  95. </body>
  96. </html>