misc_controls_orbit.html 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - orbit 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="http://threejs.org" target="_blank" rel="noopener">three.js</a> - orbit controls
  21. </div>
  22. <script src="../build/three.js"></script>
  23. <script src="js/controls/OrbitControls.js"></script>
  24. <script src="js/WebGL.js"></script>
  25. <script>
  26. if ( WEBGL.isWebGLAvailable() === false ) {
  27. document.body.appendChild( WEBGL.getWebGLErrorMessage() );
  28. }
  29. var camera, controls, scene, renderer;
  30. init();
  31. //render(); // remove when using next line for animation loop (requestAnimationFrame)
  32. animate();
  33. function init() {
  34. scene = new THREE.Scene();
  35. scene.background = new THREE.Color( 0xcccccc );
  36. scene.fog = new THREE.FogExp2( 0xcccccc, 0.002 );
  37. renderer = new THREE.WebGLRenderer( { antialias: true } );
  38. renderer.setPixelRatio( window.devicePixelRatio );
  39. renderer.setSize( window.innerWidth, window.innerHeight );
  40. document.body.appendChild( renderer.domElement );
  41. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 1000 );
  42. camera.position.set( 400, 200, 0 );
  43. // controls
  44. controls = new THREE.OrbitControls( camera, renderer.domElement );
  45. //controls.addEventListener( 'change', render ); // call this only in static scenes (i.e., if there is no animation loop)
  46. controls.enableDamping = true; // an animation loop is required when either damping or auto-rotation are enabled
  47. controls.dampingFactor = 0.25;
  48. controls.screenSpacePanning = false;
  49. controls.minDistance = 100;
  50. controls.maxDistance = 500;
  51. controls.maxPolarAngle = Math.PI / 2;
  52. // world
  53. var geometry = new THREE.CylinderBufferGeometry( 0, 10, 30, 4, 1 );
  54. var material = new THREE.MeshPhongMaterial( { color: 0xffffff, flatShading: true } );
  55. for ( var i = 0; i < 500; i ++ ) {
  56. var mesh = new THREE.Mesh( geometry, material );
  57. mesh.position.x = Math.random() * 1600 - 800;
  58. mesh.position.y = 0;
  59. mesh.position.z = Math.random() * 1600 - 800;
  60. mesh.updateMatrix();
  61. mesh.matrixAutoUpdate = false;
  62. scene.add( mesh );
  63. }
  64. // lights
  65. var light = new THREE.DirectionalLight( 0xffffff );
  66. light.position.set( 1, 1, 1 );
  67. scene.add( light );
  68. var light = new THREE.DirectionalLight( 0x002288 );
  69. light.position.set( - 1, - 1, - 1 );
  70. scene.add( light );
  71. var light = new THREE.AmbientLight( 0x222222 );
  72. scene.add( light );
  73. //
  74. window.addEventListener( 'resize', onWindowResize, false );
  75. }
  76. function onWindowResize() {
  77. camera.aspect = window.innerWidth / window.innerHeight;
  78. camera.updateProjectionMatrix();
  79. renderer.setSize( window.innerWidth, window.innerHeight );
  80. }
  81. function animate() {
  82. requestAnimationFrame( animate );
  83. controls.update(); // only required if controls.enableDamping = true, or if controls.autoRotate = true
  84. render();
  85. }
  86. function render() {
  87. renderer.render( scene, camera );
  88. }
  89. </script>
  90. </body>
  91. </html>