Guest User

Untitled

a guest
Jan 9th, 2016
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     <!DOCTYPE html>
  2.     <html>
  3.     <head>
  4.         <title></title>
  5.         <script src="../libs/three.js"></script>
  6.         <script src="../libs/OrbitControls.js"></script>
  7.         <script src="../libs/dat.gui.min.js"></script>
  8.         <script src="../libs/stats.min.js"></script>
  9.         <style>
  10.             body {
  11.                 /* set margin to 0 and overflow to hidden, to go fullscreen */
  12.                 margin: 0;
  13.                 overflow: hidden;
  14.             }
  15.         </style>
  16.     </head>
  17.     <script>
  18.    
  19.         // global variables
  20.         var renderer;
  21.         var scene;
  22.         var camera;
  23.         var control;
  24.         var stats;
  25.         var cameraControl;
  26.    
  27.         /**
  28.          * Initializes the scene, camera and objects. Called when the window is
  29.          * loaded by using window.onload (see below)
  30.          */
  31.         function init() {
  32.    
  33.             // create a scene, that will hold all our elements such as objects, cameras and lights.
  34.             scene = new THREE.Scene();
  35.    
  36.             // create a camera, which defines where we're looking at.
  37.             camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);
  38.    
  39.             // create a render, sets the background color and the size
  40.             renderer = new THREE.WebGLRenderer();
  41.             renderer.setClearColor(0x000000, 1.0);
  42.             renderer.setSize(window.innerWidth, window.innerHeight);
  43.             renderer.shadowMapEnabled = true;
  44.    
  45.             // create a sphere
  46.             var sphereGeometry = new THREE.SphereGeometry(15, 30, 30);
  47.             var sphereMaterial = new THREE.MeshNormalMaterial();
  48.             var earthMesh = new THREE.Mesh(sphereGeometry, sphereMaterial);
  49.             earthMesh.name = 'earth';
  50.             scene.add(earthMesh);
  51.    
  52.             // position and point the camera to the center of the scene
  53.             camera.position.x = 35;
  54.             camera.position.y = 36;
  55.             camera.position.z = 33;
  56.             camera.lookAt(scene.position);
  57.    
  58.             // add controls
  59.             cameraControl = new THREE.OrbitControls(camera);
  60.    
  61.    
  62.             // setup the control object for the control gui
  63.             control = new function () {
  64.                 this.rotationSpeed = 0.005;
  65.                 this.opacity = 0.6;
  66.             };
  67.    
  68.             // add extras
  69.             addControlGui(control);
  70.             addStatsObject();
  71.    
  72.    
  73.             // add the output of the renderer to the html element
  74.             document.body.appendChild(renderer.domElement);
  75.    
  76.             // call the render function, after the first render, interval is determined
  77.             // by requestAnimationFrame
  78.             render();
  79.         }
  80.    
  81.    
  82.         function addControlGui(controlObject) {
  83.             var gui = new dat.GUI();
  84.             gui.add(controlObject, 'rotationSpeed', -0.01, 0.01);
  85.         }
  86.    
  87.         function addStatsObject() {
  88.             stats = new Stats();
  89.             stats.setMode(0);
  90.    
  91.             stats.domElement.style.position = 'absolute';
  92.             stats.domElement.style.left = '0px';
  93.             stats.domElement.style.top = '0px';
  94.    
  95.             document.body.appendChild(stats.domElement);
  96.         }
  97.    
  98.    
  99.         /**
  100.          * Called when the scene needs to be rendered. Delegates to requestAnimationFrame
  101.          * for future renders
  102.          */
  103.         function render() {
  104.    
  105.             // update stats
  106.             stats.update();
  107.    
  108.             // update the camera
  109.             cameraControl.update();
  110.    
  111.             scene.getObjectByName('earth').rotation.y+=control.rotationSpeed;
  112.    
  113.             // and render the scene
  114.             renderer.render(scene, camera);
  115.    
  116.             // render using requestAnimationFrame
  117.             requestAnimationFrame(render);
  118.         }
  119.    
  120.    
  121.         /**
  122.          * Function handles the resize event. This make sure the camera and the renderer
  123.          * are updated at the correct moment.
  124.          */
  125.         function handleResize() {
  126.             camera.aspect = window.innerWidth / window.innerHeight;
  127.             camera.updateProjectionMatrix();
  128.             renderer.setSize(window.innerWidth, window.innerHeight);
  129.         }
  130.    
  131.         // calls the init function when the window is done loading.
  132.         window.onload = init;
  133.         // calls the handleResize function when the window is resized
  134.         window.addEventListener('resize', handleResize, false);
  135.    
  136.     </script>
  137.     <body>
  138.     </body>
  139.     </html>
Advertisement
Add Comment
Please, Sign In to add comment