misc_controls_trackball.html 3.6 KB

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