misc_controls_trackball.html 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. <style>
  8. html, body {
  9. width: 100%;
  10. height: 100%;
  11. overflow: hidden;
  12. }
  13. body {
  14. color: #000;
  15. font-family:Monospace;
  16. font-size:13px;
  17. text-align:center;
  18. font-weight: bold;
  19. background-color: #fff;
  20. margin: 0px;
  21. }
  22. #info {
  23. color:#000;
  24. position: absolute;
  25. top: 0px; width: 100%;
  26. padding: 5px;
  27. box-sizing: border-box;
  28. }
  29. a {
  30. color: red;
  31. }
  32. </style>
  33. </head>
  34. <body>
  35. <div id="info">
  36. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - trackball controls example<br />
  37. MOVE mouse &amp; press LEFT/A: rotate, MIDDLE/S: zoom, RIGHT/D: pan
  38. </div>
  39. <script src="../build/three.js"></script>
  40. <script src="js/controls/TrackballControls.js"></script>
  41. <script src="js/Detector.js"></script>
  42. <script src="js/libs/stats.min.js"></script>
  43. <script>
  44. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  45. var camera, controls, scene, renderer, stats;
  46. init();
  47. animate();
  48. function init() {
  49. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 1000 );
  50. camera.position.z = 500;
  51. controls = new THREE.TrackballControls( camera );
  52. controls.rotateSpeed = 1.0;
  53. controls.zoomSpeed = 1.2;
  54. controls.panSpeed = 0.8;
  55. controls.noZoom = false;
  56. controls.noPan = false;
  57. controls.staticMoving = true;
  58. controls.dynamicDampingFactor = 0.3;
  59. controls.keys = [ 65, 83, 68 ];
  60. controls.addEventListener( 'change', render );
  61. // world
  62. scene = new THREE.Scene();
  63. scene.background = new THREE.Color( 0xcccccc );
  64. scene.fog = new THREE.FogExp2( 0xcccccc, 0.002 );
  65. var geometry = new THREE.CylinderBufferGeometry( 0, 10, 30, 4, 1 );
  66. var material = new THREE.MeshPhongMaterial( { color: 0xffffff, flatShading: true } );
  67. for ( var i = 0; i < 500; i ++ ) {
  68. var mesh = new THREE.Mesh( geometry, material );
  69. mesh.position.x = ( Math.random() - 0.5 ) * 1000;
  70. mesh.position.y = ( Math.random() - 0.5 ) * 1000;
  71. mesh.position.z = ( Math.random() - 0.5 ) * 1000;
  72. mesh.updateMatrix();
  73. mesh.matrixAutoUpdate = false;
  74. scene.add( mesh );
  75. }
  76. // lights
  77. var light = new THREE.DirectionalLight( 0xffffff );
  78. light.position.set( 1, 1, 1 );
  79. scene.add( light );
  80. var light = new THREE.DirectionalLight( 0x002288 );
  81. light.position.set( -1, -1, -1 );
  82. scene.add( light );
  83. var light = new THREE.AmbientLight( 0x222222 );
  84. scene.add( light );
  85. // renderer
  86. renderer = new THREE.WebGLRenderer( { antialias: true } );
  87. renderer.setPixelRatio( window.devicePixelRatio );
  88. renderer.setSize( window.innerWidth, window.innerHeight );
  89. document.body.appendChild( renderer.domElement );
  90. stats = new Stats();
  91. document.body.appendChild( stats.dom );
  92. //
  93. window.addEventListener( 'resize', onWindowResize, false );
  94. //
  95. render();
  96. }
  97. function onWindowResize() {
  98. camera.aspect = window.innerWidth / window.innerHeight;
  99. camera.updateProjectionMatrix();
  100. renderer.setSize( window.innerWidth, window.innerHeight );
  101. controls.handleResize();
  102. render();
  103. }
  104. function animate() {
  105. requestAnimationFrame( animate );
  106. controls.update();
  107. stats.update();
  108. }
  109. function render() {
  110. renderer.render( scene, camera );
  111. }
  112. </script>
  113. </body>
  114. </html>