misc_controls_trackball.html 4.2 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">
  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="https://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. } );
  78. //
  79. window.addEventListener( 'resize', onWindowResize, false );
  80. createControls( perspectiveCamera );
  81. }
  82. function createControls( camera ) {
  83. controls = new TrackballControls( camera, renderer.domElement );
  84. controls.rotateSpeed = 1.0;
  85. controls.zoomSpeed = 1.2;
  86. controls.panSpeed = 0.8;
  87. controls.keys = [ 65, 83, 68 ];
  88. }
  89. function onWindowResize() {
  90. var aspect = window.innerWidth / window.innerHeight;
  91. perspectiveCamera.aspect = aspect;
  92. perspectiveCamera.updateProjectionMatrix();
  93. orthographicCamera.left = - frustumSize * aspect / 2;
  94. orthographicCamera.right = frustumSize * aspect / 2;
  95. orthographicCamera.top = frustumSize / 2;
  96. orthographicCamera.bottom = - frustumSize / 2;
  97. orthographicCamera.updateProjectionMatrix();
  98. renderer.setSize( window.innerWidth, window.innerHeight );
  99. controls.handleResize();
  100. }
  101. function animate() {
  102. requestAnimationFrame( animate );
  103. controls.update();
  104. stats.update();
  105. render();
  106. }
  107. function render() {
  108. var camera = ( params.orthographicCamera ) ? orthographicCamera : perspectiveCamera;
  109. renderer.render( scene, camera );
  110. }
  111. </script>
  112. </body>
  113. </html>