misc_controls_trackball.html 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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/CameraControls.js';
  28. var camera, 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. camera = perspectiveCamera;
  42. // world
  43. scene = new THREE.Scene();
  44. scene.background = new THREE.Color( 0xcccccc );
  45. scene.fog = new THREE.FogExp2( 0xcccccc, 0.002 );
  46. var geometry = new THREE.CylinderBufferGeometry( 0, 10, 30, 4, 1 );
  47. var material = new THREE.MeshPhongMaterial( { color: 0xffffff, flatShading: true } );
  48. for ( var i = 0; i < 500; i ++ ) {
  49. var mesh = new THREE.Mesh( geometry, material );
  50. mesh.position.x = ( Math.random() - 0.5 ) * 1000;
  51. mesh.position.y = ( Math.random() - 0.5 ) * 1000;
  52. mesh.position.z = ( Math.random() - 0.5 ) * 1000;
  53. mesh.updateMatrix();
  54. mesh.matrixAutoUpdate = false;
  55. scene.add( mesh );
  56. }
  57. // lights
  58. var light = new THREE.DirectionalLight( 0xffffff );
  59. light.position.set( 1, 1, 1 );
  60. scene.add( light );
  61. var light = new THREE.DirectionalLight( 0x002288 );
  62. light.position.set( - 1, - 1, - 1 );
  63. scene.add( light );
  64. var light = new THREE.AmbientLight( 0x222222 );
  65. scene.add( light );
  66. // renderer
  67. renderer = new THREE.WebGLRenderer( { antialias: true } );
  68. renderer.setPixelRatio( window.devicePixelRatio );
  69. renderer.setSize( window.innerWidth, window.innerHeight );
  70. document.body.appendChild( renderer.domElement );
  71. stats = new Stats();
  72. document.body.appendChild( stats.dom );
  73. //
  74. var gui = new GUI();
  75. gui.add( params, 'orthographicCamera' ).name( 'use orthographic' ).onChange( function ( value ) {
  76. controls.dispose();
  77. camera = ( value ) ? orthographicCamera : perspectiveCamera;
  78. createControls( camera );
  79. } );
  80. //
  81. window.addEventListener( 'resize', onWindowResize, false );
  82. createControls( perspectiveCamera );
  83. }
  84. function createControls( camera ) {
  85. controls = new TrackballControls( camera, renderer.domElement );
  86. controls.enableDamping = true;
  87. controls.minDistance = 100;
  88. controls.maxDistance = 1000;
  89. controls.minZoom = 0.5;
  90. controls.maxZoom = 2;
  91. }
  92. function onWindowResize() {
  93. var aspect = window.innerWidth / window.innerHeight;
  94. perspectiveCamera.aspect = aspect;
  95. perspectiveCamera.updateProjectionMatrix();
  96. orthographicCamera.left = - frustumSize * aspect / 2;
  97. orthographicCamera.right = frustumSize * aspect / 2;
  98. orthographicCamera.top = frustumSize / 2;
  99. orthographicCamera.bottom = - frustumSize / 2;
  100. orthographicCamera.updateProjectionMatrix();
  101. renderer.setSize( window.innerWidth, window.innerHeight );
  102. controls.handleResize();
  103. }
  104. function animate() {
  105. requestAnimationFrame( animate );
  106. controls.update(); // required because damping is enabled
  107. stats.update();
  108. render();
  109. }
  110. function render() {
  111. renderer.render( scene, camera );
  112. }
  113. </script>
  114. </body>
  115. </html>