misc_controls_trackball.html 4.4 KB

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