misc_controls_trackball.html 3.4 KB

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