misc_camera_trackball.html 3.6 KB

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