misc_camera_trackball.html 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - trackball camera</title>
  5. <meta charset="utf-8">
  6. <style type="text/css">
  7. body {
  8. color: #000;
  9. font-family:Monospace;
  10. font-size:13px;
  11. text-align:center;
  12. font-weight: bold;
  13. background-color: #fff;
  14. margin: 0px;
  15. overflow: hidden;
  16. }
  17. #info {
  18. color:#000;
  19. position: absolute;
  20. top: 0px; width: 100%;
  21. padding: 5px;
  22. }
  23. a { color: red; }
  24. </style>
  25. </head>
  26. <body>
  27. <div id="container"></div>
  28. <div id="info">
  29. <a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> - trackball camera example</br>
  30. MOVE mouse & press LEFT/A: rotate, MIDDLE/S: zoom, RIGHT/D: pan
  31. </div>
  32. <script type="text/javascript" src="../build/Three.js"></script>
  33. <script type="text/javascript" src="js/Stats.js"></script>
  34. <script type="text/javascript">
  35. var statsEnabled = true;
  36. var container, stats;
  37. var camera, scene, renderer;
  38. var cross;
  39. init();
  40. function init() {
  41. // scene and camera
  42. scene = new THREE.Scene();
  43. scene.fog = new THREE.FogExp2( 0xffffff, 0.002 );
  44. camera = new THREE.TrackballCamera({
  45. fov: 60,
  46. aspect: window.innerWidth / window.innerHeight,
  47. near: 1,
  48. far: 1e3,
  49. rotateSpeed: 1.0,
  50. zoomSpeed: 1.2,
  51. panSpeed: 0.8,
  52. noZoom: false,
  53. noPan: false,
  54. staticMoving: true,
  55. dynamicDampingFactor: 0.3,
  56. keys: [ 65, 83, 68 ]
  57. });
  58. camera.position.z = 500;
  59. // world
  60. var cube = new THREE.CubeGeometry( 20, 60, 20 );
  61. cube.vertices[ 0 ].position.multiplyScalar( 0.01 );
  62. cube.vertices[ 1 ].position.multiplyScalar( 0.01 );
  63. cube.vertices[ 4 ].position.multiplyScalar( 0.01 );
  64. cube.vertices[ 5 ].position.multiplyScalar( 0.01 );
  65. var material = new THREE.MeshLambertMaterial( { color:0xffffff } );
  66. for( var i = 0; i < 500; i++ ) {
  67. var mesh = new THREE.Mesh( cube, material );
  68. mesh.position.set(( Math.random() - 0.5 ) * 1000,
  69. ( Math.random() - 0.5 ) * 1000,
  70. ( Math.random() - 0.5 ) * 1000 );
  71. mesh.updateMatrix();
  72. mesh.matrixAutoUpdate = false;
  73. scene.addChild( mesh );
  74. }
  75. // create cross
  76. /*
  77. cross = new THREE.Object3D();
  78. cross.scale.set( 0.5, 0.5, 0.5 );
  79. cross.matrixAutoUpdate = false;
  80. var material = new THREE.MeshPhongMaterial( { color:0xff0000 } );
  81. var mesh = new THREE.Mesh( new THREE.Cube( 40, 5, 5 ), material );
  82. mesh.position.x = 20;
  83. cross.addChild( mesh );
  84. var material = new THREE.MeshPhongMaterial( { color:0x00ff00 } );
  85. var mesh = new THREE.Mesh( new THREE.Cube( 5, 40, 5 ), material );
  86. mesh.position.y = 20;
  87. cross.addChild( mesh );
  88. var material = new THREE.MeshPhongMaterial( { color:0x0000ff } );
  89. var mesh = new THREE.Mesh( new THREE.Cube( 5, 5, 40 ), material );
  90. mesh.position.z = 20;
  91. cross.addChild( mesh );
  92. camera.addChild( cross );
  93. */
  94. // lights
  95. light = new THREE.DirectionalLight( 0xffffff );
  96. light.position.set( 1, 1, 1 );
  97. scene.addChild( light );
  98. light = new THREE.DirectionalLight( 0x002288 );
  99. light.position.set( -1, -1, -1 );
  100. scene.addChild( light );
  101. light = new THREE.AmbientLight( 0x222222 );
  102. scene.addChild( light );
  103. // renderer
  104. renderer = new THREE.WebGLRenderer( { antialias: false } );
  105. renderer.setClearColorHex( 0xffffff, 1 );
  106. renderer.setSize( window.innerWidth, window.innerHeight );
  107. container = document.getElementById( 'container' );
  108. container.appendChild( renderer.domElement );
  109. if ( statsEnabled ) {
  110. stats = new Stats();
  111. stats.domElement.style.position = 'absolute';
  112. stats.domElement.style.top = '0px';
  113. stats.domElement.style.zIndex = 100;
  114. container.appendChild( stats.domElement );
  115. }
  116. setInterval( loop, 1000 / 60 );
  117. }
  118. function loop() {
  119. /*
  120. cross.matrix.copy( camera.matrix );
  121. cross.matrix.n14 = 0;
  122. cross.matrix.n24 = 0;
  123. cross.matrix.n34 = -200;
  124. cross.matrixWorldNeedsUpdate = true;
  125. */
  126. renderer.render( scene, camera );
  127. if ( statsEnabled ) stats.update();
  128. }
  129. </script>
  130. </body>
  131. </html>