misc_controls_trackball.html 4.6 KB

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