misc_controls_trackball.html 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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/WebGL.js"></script>
  42. <script src="js/libs/stats.min.js"></script>
  43. <script>
  44. if ( WEBGL.isWebGLAvailable() === false ) {
  45. document.body.appendChild( WEBGL.getWebGLErrorMessage() );
  46. }
  47. var camera, controls, scene, renderer, stats;
  48. init();
  49. animate();
  50. function init() {
  51. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 1000 );
  52. camera.position.z = 500;
  53. controls = new THREE.TrackballControls( camera );
  54. controls.rotateSpeed = 1.0;
  55. controls.zoomSpeed = 1.2;
  56. controls.panSpeed = 0.8;
  57. controls.noZoom = false;
  58. controls.noPan = false;
  59. controls.staticMoving = true;
  60. controls.dynamicDampingFactor = 0.3;
  61. controls.keys = [ 65, 83, 68 ];
  62. controls.addEventListener( 'change', render );
  63. // world
  64. scene = new THREE.Scene();
  65. scene.background = new THREE.Color( 0xcccccc );
  66. scene.fog = new THREE.FogExp2( 0xcccccc, 0.002 );
  67. var geometry = new THREE.CylinderBufferGeometry( 0, 10, 30, 4, 1 );
  68. var material = new THREE.MeshPhongMaterial( { color: 0xffffff, flatShading: true } );
  69. for ( var i = 0; i < 500; i ++ ) {
  70. var mesh = new THREE.Mesh( geometry, material );
  71. mesh.position.x = ( Math.random() - 0.5 ) * 1000;
  72. mesh.position.y = ( Math.random() - 0.5 ) * 1000;
  73. mesh.position.z = ( Math.random() - 0.5 ) * 1000;
  74. mesh.updateMatrix();
  75. mesh.matrixAutoUpdate = false;
  76. scene.add( mesh );
  77. }
  78. // lights
  79. var light = new THREE.DirectionalLight( 0xffffff );
  80. light.position.set( 1, 1, 1 );
  81. scene.add( light );
  82. var light = new THREE.DirectionalLight( 0x002288 );
  83. light.position.set( - 1, - 1, - 1 );
  84. scene.add( light );
  85. var light = new THREE.AmbientLight( 0x222222 );
  86. scene.add( light );
  87. // renderer
  88. renderer = new THREE.WebGLRenderer( { antialias: true } );
  89. renderer.setPixelRatio( window.devicePixelRatio );
  90. renderer.setSize( window.innerWidth, window.innerHeight );
  91. document.body.appendChild( renderer.domElement );
  92. stats = new Stats();
  93. document.body.appendChild( stats.dom );
  94. //
  95. window.addEventListener( 'resize', onWindowResize, false );
  96. //
  97. render();
  98. }
  99. function onWindowResize() {
  100. camera.aspect = window.innerWidth / window.innerHeight;
  101. camera.updateProjectionMatrix();
  102. renderer.setSize( window.innerWidth, window.innerHeight );
  103. controls.handleResize();
  104. render();
  105. }
  106. function animate() {
  107. requestAnimationFrame( animate );
  108. controls.update();
  109. stats.update();
  110. }
  111. function render() {
  112. renderer.render( scene, camera );
  113. }
  114. </script>
  115. </body>
  116. </html>