misc_controls_trackball.html 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. <meta property="og:title" content="three.js webgl - trackball controls">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/misc_controls_trackball.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/misc_controls_trackball.jpg">
  11. <link type="text/css" rel="stylesheet" href="main.css">
  12. <style>
  13. body {
  14. background-color: #ccc;
  15. color: #000;
  16. }
  17. a {
  18. color: #f00;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <div id="info">
  24. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - trackball controls<br />
  25. MOVE mouse &amp; press LEFT/A: rotate, MIDDLE/S: zoom, RIGHT/D: pan
  26. </div>
  27. <script type="importmap">
  28. {
  29. "imports": {
  30. "three": "../build/three.module.js",
  31. "three/addons/": "./jsm/"
  32. }
  33. }
  34. </script>
  35. <script type="module">
  36. import * as THREE from 'three';
  37. import Stats from 'three/addons/libs/stats.module.js';
  38. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  39. import { TrackballControls } from 'three/addons/controls/TrackballControls.js';
  40. let perspectiveCamera, orthographicCamera, controls, scene, renderer, stats;
  41. const params = {
  42. orthographicCamera: false
  43. };
  44. const frustumSize = 400;
  45. init();
  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.ConeGeometry( 10, 30, 4, 1 );
  57. const material = new THREE.MeshPhongMaterial( { color: 0xffffff, flatShading: true } );
  58. const mesh = new THREE.InstancedMesh( geometry, material, 500 );
  59. const dummy = new THREE.Object3D();
  60. for ( let i = 0; i < 500; i ++ ) {
  61. dummy.position.x = ( Math.random() - 0.5 ) * 1000;
  62. dummy.position.y = ( Math.random() - 0.5 ) * 1000;
  63. dummy.position.z = ( Math.random() - 0.5 ) * 1000;
  64. dummy.updateMatrix();
  65. mesh.setMatrixAt( i, dummy.matrix );
  66. }
  67. scene.add( mesh );
  68. // lights
  69. const dirLight1 = new THREE.DirectionalLight( 0xffffff, 3 );
  70. dirLight1.position.set( 1, 1, 1 );
  71. scene.add( dirLight1 );
  72. const dirLight2 = new THREE.DirectionalLight( 0x002288, 3 );
  73. dirLight2.position.set( - 1, - 1, - 1 );
  74. scene.add( dirLight2 );
  75. const ambientLight = new THREE.AmbientLight( 0x555555 );
  76. scene.add( ambientLight );
  77. // renderer
  78. renderer = new THREE.WebGLRenderer( { antialias: true } );
  79. renderer.setPixelRatio( window.devicePixelRatio );
  80. renderer.setSize( window.innerWidth, window.innerHeight );
  81. renderer.setAnimationLoop( animate );
  82. document.body.appendChild( renderer.domElement );
  83. stats = new Stats();
  84. document.body.appendChild( stats.dom );
  85. //
  86. const gui = new GUI();
  87. gui.add( params, 'orthographicCamera' ).name( 'use orthographic' ).onChange( function ( value ) {
  88. controls.dispose();
  89. createControls( value ? orthographicCamera : perspectiveCamera );
  90. } );
  91. //
  92. window.addEventListener( 'resize', onWindowResize );
  93. createControls( perspectiveCamera );
  94. }
  95. function createControls( camera ) {
  96. controls = new TrackballControls( camera, renderer.domElement );
  97. controls.rotateSpeed = 1.0;
  98. controls.zoomSpeed = 1.2;
  99. controls.panSpeed = 0.8;
  100. controls.keys = [ 'KeyA', 'KeyS', 'KeyD' ];
  101. }
  102. function onWindowResize() {
  103. const aspect = window.innerWidth / window.innerHeight;
  104. perspectiveCamera.aspect = aspect;
  105. perspectiveCamera.updateProjectionMatrix();
  106. orthographicCamera.left = - frustumSize * aspect / 2;
  107. orthographicCamera.right = frustumSize * aspect / 2;
  108. orthographicCamera.top = frustumSize / 2;
  109. orthographicCamera.bottom = - frustumSize / 2;
  110. orthographicCamera.updateProjectionMatrix();
  111. renderer.setSize( window.innerWidth, window.innerHeight );
  112. controls.handleResize();
  113. }
  114. function animate() {
  115. controls.update();
  116. render();
  117. stats.update();
  118. }
  119. function render() {
  120. const camera = ( params.orthographicCamera ) ? orthographicCamera : perspectiveCamera;
  121. renderer.render( scene, camera );
  122. }
  123. </script>
  124. </body>
  125. </html>
粤ICP备19079148号