misc_controls_trackball.html 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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
  21. </div>
  22. <script type="module">
  23. import * as THREE from '../build/three.module.js';
  24. import Stats from './jsm/libs/stats.module.js';
  25. import { GUI } from './jsm/libs/dat.gui.module.js';
  26. import { TrackballControls } from './jsm/controls/CameraControls.js';
  27. var camera, perspectiveCamera, orthographicCamera, controls, scene, renderer, stats;
  28. var params = {
  29. orthographicCamera: false
  30. };
  31. var frustumSize = 400;
  32. init();
  33. animate();
  34. function init() {
  35. var aspect = window.innerWidth / window.innerHeight;
  36. perspectiveCamera = new THREE.PerspectiveCamera( 60, aspect, 1, 1000 );
  37. perspectiveCamera.position.z = 500;
  38. orthographicCamera = new THREE.OrthographicCamera( frustumSize * aspect / - 2, frustumSize * aspect / 2, frustumSize / 2, frustumSize / - 2, 1, 1000 );
  39. orthographicCamera.position.z = 500;
  40. camera = perspectiveCamera;
  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. camera = ( value ) ? orthographicCamera : perspectiveCamera;
  77. createControls( camera );
  78. } );
  79. //
  80. window.addEventListener( 'resize', onWindowResize, false );
  81. createControls( perspectiveCamera );
  82. }
  83. function createControls( camera ) {
  84. controls = new TrackballControls( camera, renderer.domElement );
  85. controls.enableDamping = true;
  86. controls.minDistance = 100;
  87. controls.maxDistance = 1000;
  88. controls.minZoom = 0.5;
  89. controls.maxZoom = 2;
  90. }
  91. function onWindowResize() {
  92. var aspect = window.innerWidth / window.innerHeight;
  93. perspectiveCamera.aspect = aspect;
  94. perspectiveCamera.updateProjectionMatrix();
  95. orthographicCamera.left = - frustumSize * aspect / 2;
  96. orthographicCamera.right = frustumSize * aspect / 2;
  97. orthographicCamera.top = frustumSize / 2;
  98. orthographicCamera.bottom = - frustumSize / 2;
  99. orthographicCamera.updateProjectionMatrix();
  100. renderer.setSize( window.innerWidth, window.innerHeight );
  101. controls.handleResize();
  102. }
  103. function animate() {
  104. requestAnimationFrame( animate );
  105. controls.update(); // required because damping is enabled
  106. stats.update();
  107. render();
  108. }
  109. function render() {
  110. renderer.render( scene, camera );
  111. }
  112. </script>
  113. </body>
  114. </html>