misc_controls_trackball.html 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 type="module">
  24. import * as THREE from '../build/three.module.js';
  25. import Stats from './jsm/libs/stats.module.js';
  26. import { GUI } from './jsm/libs/dat.gui.module.js';
  27. import { TrackballControls } from './jsm/controls/TrackballControls.js';
  28. var perspectiveCamera, orthographicCamera, controls, scene, renderer, stats;
  29. var params = {
  30. orthographicCamera: false
  31. };
  32. var frustumSize = 400;
  33. init();
  34. animate();
  35. function init() {
  36. var aspect = window.innerWidth / window.innerHeight;
  37. perspectiveCamera = new THREE.PerspectiveCamera( 60, aspect, 1, 1000 );
  38. perspectiveCamera.position.z = 500;
  39. orthographicCamera = new THREE.OrthographicCamera( frustumSize * aspect / - 2, frustumSize * aspect / 2, frustumSize / 2, frustumSize / - 2, 1, 1000 );
  40. orthographicCamera.position.z = 500;
  41. // world
  42. scene = new THREE.Scene();
  43. scene.background = new THREE.Color( 0xcccccc );
  44. scene.fog = new THREE.FogExp2( 0xcccccc, 0.002 );
  45. var geometry = new THREE.CylinderBufferGeometry( 0, 10, 30, 4, 1 );
  46. var material = new THREE.MeshPhongMaterial( { color: 0xffffff, flatShading: true } );
  47. for ( var i = 0; i < 500; i ++ ) {
  48. var mesh = new THREE.Mesh( geometry, material );
  49. mesh.position.x = ( Math.random() - 0.5 ) * 1000;
  50. mesh.position.y = ( Math.random() - 0.5 ) * 1000;
  51. mesh.position.z = ( Math.random() - 0.5 ) * 1000;
  52. mesh.updateMatrix();
  53. mesh.matrixAutoUpdate = false;
  54. scene.add( mesh );
  55. }
  56. // lights
  57. var light = new THREE.DirectionalLight( 0xffffff );
  58. light.position.set( 1, 1, 1 );
  59. scene.add( light );
  60. var light = new THREE.DirectionalLight( 0x002288 );
  61. light.position.set( - 1, - 1, - 1 );
  62. scene.add( light );
  63. var light = new THREE.AmbientLight( 0x222222 );
  64. scene.add( light );
  65. // renderer
  66. renderer = new THREE.WebGLRenderer( { antialias: true } );
  67. renderer.setPixelRatio( window.devicePixelRatio );
  68. renderer.setSize( window.innerWidth, window.innerHeight );
  69. document.body.appendChild( renderer.domElement );
  70. stats = new Stats();
  71. document.body.appendChild( stats.dom );
  72. //
  73. var gui = new GUI();
  74. gui.add( params, 'orthographicCamera' ).name( 'use orthographic' ).onChange( function ( value ) {
  75. controls.dispose();
  76. createControls( value ? orthographicCamera : perspectiveCamera );
  77. render();
  78. } );
  79. //
  80. window.addEventListener( 'resize', onWindowResize, false );
  81. createControls( perspectiveCamera );
  82. render();
  83. }
  84. function createControls( camera ) {
  85. controls = new TrackballControls( camera, renderer.domElement );
  86. controls.rotateSpeed = 1.0;
  87. controls.zoomSpeed = 1.2;
  88. controls.panSpeed = 0.8;
  89. controls.staticMoving = true;
  90. controls.dynamicDampingFactor = 0.3;
  91. controls.keys = [ 65, 83, 68 ];
  92. controls.addEventListener( 'change', render );
  93. }
  94. function onWindowResize() {
  95. var aspect = window.innerWidth / window.innerHeight;
  96. perspectiveCamera.aspect = aspect;
  97. perspectiveCamera.updateProjectionMatrix();
  98. orthographicCamera.left = - frustumSize * aspect / 2;
  99. orthographicCamera.right = frustumSize * aspect / 2;
  100. orthographicCamera.top = frustumSize / 2;
  101. orthographicCamera.bottom = - frustumSize / 2;
  102. orthographicCamera.updateProjectionMatrix();
  103. renderer.setSize( window.innerWidth, window.innerHeight );
  104. controls.handleResize();
  105. render();
  106. }
  107. function animate() {
  108. requestAnimationFrame( animate );
  109. controls.update();
  110. stats.update();
  111. }
  112. function render() {
  113. var camera = ( params.orthographicCamera ) ? orthographicCamera : perspectiveCamera;
  114. renderer.render( scene, camera );
  115. }
  116. </script>
  117. </body>
  118. </html>