Advertisement
Guest User

Untitled

a guest
Apr 17th, 2015
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3.  
  4.     <head>
  5.         <title>Robot Scene</title>
  6.         <script type="text/javascript" src="../libs/three.js"></script>
  7.         <script type="text/javascript" src="../libs/jquery-1.9.0.js"></script>
  8.         <script type="text/javascript" src="../libs/stats.js"></script>
  9.         <script type="text/javascript" src="../libs/dat.gui.js"></script>
  10.         <script type="text/javascript" src="../libs/keypress.js"></script>
  11.         <style>
  12.             body{
  13.                 /* set margin to 0 and overflow to hidden, to go fullscreen */
  14.                 margin: 0;
  15.                 overflow: hidden;
  16.             }
  17.            
  18.             #robotFacing-output
  19.             {
  20.                 font-size: 18px;
  21.                color: rgb(57, 252, 253);
  22.                line-height: 48px;
  23.                background-color: rgb(5,2,26);
  24.                display: block;
  25.                vertical-align: middle;
  26.                width: 135px;
  27.                font-family: helvetica;
  28.                left: 80px;
  29.                position: fixed;
  30.             }
  31.            
  32.            
  33.         </style>
  34.     </head>
  35.     <body>
  36.  
  37.         <div id="Stats-output">
  38.         </div>
  39.         <div id="robotFacing-output">
  40.             Robot facing: N
  41.         </div>
  42.         <!-- WebGL Output goes here -->
  43.         <div id="WebGL-output">
  44.         </div>
  45.  
  46.         <!-- 3D Computer Graphics Robot Scene -->
  47.         <script type="text/javascript">
  48.             // Variable to store the 3D robot
  49.             var robotGroup;
  50.            
  51.             // Array of and current direction robot can face in
  52.             var directions = ['N','E','S','W'];
  53.             var currentDirection = 0;
  54.            
  55.             //Variables to control strobe speed
  56.             var counter = 0;
  57.             var strobeSpeed = 5;
  58.            
  59.             //Variable to make camera accesible
  60.             var camera;
  61.             var focalLength = 35;
  62.  
  63.             $(function() {
  64.                
  65.                 // Introduction message
  66.                 alert('Welcome to Rave Robot! \n - W,A,S,D to move around \n - F to focus camera on robot \n - Up/Down to zoom camera \n Have fun!');
  67.  
  68.                 var stats = initStats();
  69.  
  70.                 // create a scene, that will hold all our elements such as objects, cameras and lights.
  71.                 var scene = new THREE.Scene();
  72.  
  73.                 // create a camera, which defines where we're looking at.
  74.                 camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1500);
  75.  
  76.                 // create a render and set the size
  77.                 var renderer = new THREE.WebGLRenderer();
  78.                 renderer.setClearColor(0xEEEEEE, 1.0);
  79.                 renderer.setSize(window.innerWidth, window.innerHeight);
  80.                 renderer.shadowMapEnabled = true;
  81.                
  82.                 //Creating the location texture cube for realistic surroundings      
  83.                 var textureCube = createCubeMap("space");
  84.                 var shader = THREE.ShaderLib[ "cube" ];
  85.                 shader.uniforms[ "tCube" ].value = textureCube;
  86.                 var material = new THREE.ShaderMaterial({
  87.                     fragmentShader: shader.fragmentShader,
  88.                     vertexShader: shader.vertexShader,
  89.                     uniforms: shader.uniforms,
  90.                     depthWrite: false,
  91.                     side: THREE.BackSide
  92.                 });
  93.                
  94.                 material.reflectivity = 5;
  95.                 material.recieveShadow = true;
  96.  
  97.                 var cubeMesh = new THREE.Mesh(new THREE.CubeGeometry(500, 500, 500), material);
  98.                 scene.add(cubeMesh);
  99.                
  100.                
  101.                 //Creating material that shines
  102.                 var shinyMaterial = new THREE.MeshPhongMaterial({});
  103.                 shinyMaterial.envMap = textureCube;
  104.  
  105.                 // create a cube
  106.                 var cubeGeometry = new THREE.CubeGeometry(4, 4, 4);
  107.                 var cube = new THREE.Mesh(cubeGeometry, shinyMaterial);
  108.                 cube.castShadow = true;
  109.  
  110.                 // position the cube
  111.                 cube.position.x = -8;
  112.                 cube.position.y = 3;
  113.  
  114.                 // add the cube to the scene
  115.                 scene.add(cube);          
  116.                 //Creating shiny sphere and adding to the scene
  117.                 var shinySphere = new THREE.Mesh(new THREE.SphereGeometry(10, 15, 15), shinyMaterial);
  118.                 shinySphere.rotation.y = -0.5;
  119.                 shinySphere.position.x = 50;
  120.                 shinySphere.position.y = 12;
  121.                 shinySphere.castShadow = true;
  122.                 scene.add(shinySphere);
  123.                
  124.                 //Creating group to hold all the robots componenets
  125.                 robotGroup = new THREE.Object3D();
  126.                 robotGroup.position.x = 10;
  127.                 scene.add(robotGroup);
  128.                
  129.                 //Creating material to be used for robot
  130.                 var robotBlackMaterial = new THREE.MeshLambertMaterial({color: 0x000000});
  131.                 var robotYellowMaterial = new THREE.MeshLambertMaterial({color: 0xFAF139});
  132.                
  133.                 //Creating geometry to be used for robot
  134.                 var robotGeometry = new THREE.CubeGeometry(1,1,1);              
  135.                 var robotLegGeometry = new THREE.CylinderGeometry(1,1,10);
  136.                 var robotKneckGeometry = new THREE.CylinderGeometry(1,1,3);
  137.                 var robotArmGeometry = new THREE.CylinderGeometry(1,1,4);
  138.                 var robotForearmGeometry = new THREE.CylinderGeometry(1,1,6);
  139.                 var robotElbowGeometry = new THREE.SphereGeometry(1.3);
  140.                 var robotHandGeometry = new THREE.SphereGeometry(2);
  141.                 var robotGreyMaterial = new THREE.MeshPhongMaterial({});
  142.                     robotGreyMaterial.envMap = textureCube;
  143.  
  144.                  //Creating robot feet
  145.                  var robotFeet = [];
  146.                  
  147.                  for(var i = 0; i < 2; i++)
  148.                  {
  149.                      robotFeet[i] = new THREE.Mesh(robotGeometry, robotBlackMaterial);
  150.                      robotFeet[i].castShadow = true;
  151.                      robotFeet[i].scale.set(4,1,8);
  152.                      
  153.                     if(i === 0)
  154.                     {
  155.                         robotFeet[i].position.x = 5;
  156.                         robotFeet[i].position.y = 1;
  157.                     }
  158.                     if(i === 1)
  159.                     {
  160.                         robotFeet[i].position.x = 15;
  161.                         robotFeet[i].position.y = 1;                      
  162.                     }
  163.                     robotGroup.add(robotFeet[i]);
  164.                  }
  165.                  
  166.                    //Creating robot legs
  167.                  var robotLegs = [];
  168.                  
  169.                  for(var i = 0; i < 2; i++)
  170.                  {
  171.                      robotLegs[i] = new THREE.Mesh(robotLegGeometry, robotGreyMaterial);
  172.                      robotLegs[i].castShadow = true;
  173.                      robotLegs[i].position.z = 2;
  174.                      robotLegs[i].position.y = 6;
  175.                     if(i === 0)
  176.                     {
  177.                         robotLegs[i].position.x = 5;                      
  178.                     }
  179.                     if(i === 1)
  180.                     {
  181.                         robotLegs[i].position.x = 15;                      
  182.                     }
  183.                    
  184.                     robotGroup.add(robotLegs[i]);
  185.                  }
  186.                  
  187.                  //Creating the robot body and adding to robot group
  188.                  var robotBody = new THREE.Mesh(robotGeometry, robotBlackMaterial);                
  189.                  robotBody.scale.set(13,15,3);
  190.                  robotBody.castShadow = true;
  191.                  robotBody.position.x = 10;
  192.                  robotBody.position.y = 18;
  193.                  robotBody.position.z = 2;
  194.                  
  195.                  robotGroup.add(robotBody);
  196.                  
  197.                  //Creating the robots kneck and adding to robot group
  198.                  var robotKneck = new THREE.Mesh(robotKneckGeometry, robotGreyMaterial);        
  199.                  robotKneck.castShadow = true;
  200.                  robotKneck.position.x = 10;
  201.                  robotKneck.position.z = 2;
  202.                  robotKneck.position.y = 26;
  203.                  
  204.                  robotGroup.add(robotKneck);
  205.                  
  206.                  //Creating the robots arms
  207.                  var robotArms = [];
  208.                  var robotForearms = [];
  209.                  var robotElbows = [];
  210.                  var robotHands = [];
  211.                  
  212.                  // Creating orbit groups for the rave animation
  213.                  var leftArmRotationGroup = new THREE.Object3D();
  214.                  leftArmRotationGroup.position.y = 23;
  215.                  leftArmRotationGroup.position.z = 2;
  216.                  leftArmRotationGroup.position.x = -0.5;
  217.                  robotGroup.add(leftArmRotationGroup);
  218.                  
  219.                  var rightArmRotationGroup = new THREE.Object3D();
  220.                  rightArmRotationGroup.position.y = 23;
  221.                  rightArmRotationGroup.position.z = 2;
  222.                  rightArmRotationGroup.position.x = 20.5;
  223.                  robotGroup.add(rightArmRotationGroup);
  224.                  
  225.                  // Creating the robots arms
  226.                  for(var i = 0; i < 2; i++)
  227.                  {
  228.                      robotArms[i] = new THREE.Mesh(robotArmGeometry, robotGreyMaterial);
  229.                      robotForearms[i] = new THREE.Mesh(robotForearmGeometry, robotGreyMaterial);
  230.                      robotElbows[i] = new THREE.Mesh(robotElbowGeometry, robotGreyMaterial);
  231.                      robotHands[i] = new THREE.Mesh(robotHandGeometry, robotBlackMaterial);
  232.                      robotArms[i].rotation.z =  -0.5 * Math.PI;
  233.                      robotArms[i].position.y = 23;
  234.                      robotForearms[i].position.y = -3.5;
  235.                      robotElbows[i].position.y = 23;
  236.                      robotHands[i].position.y = -8;
  237.                      robotArms[i].position.z = 2;
  238.                      robotElbows[i].position.z = 2;
  239.                      
  240.                      if(i === 0)
  241.                      {
  242.                          robotArms[i].position.x = 2;
  243.                          robotElbows[i].position.x = -0.5;
  244.                          leftArmRotationGroup.add(robotForearms[i]);
  245.                          leftArmRotationGroup.add(robotHands[i]);
  246.                      }
  247.                      if(i === 1)
  248.                      {
  249.                         robotArms[i].position.x = 18;
  250.                         robotElbows[i].position.x = 20.5;
  251.                         rightArmRotationGroup.add(robotForearms[i]);
  252.                         rightArmRotationGroup.add(robotHands[i]);
  253.                      }
  254.                      
  255.                      robotGroup.add(robotArms[i]);
  256.                      robotGroup.add(robotElbows[i]);
  257.                  }
  258.                  
  259.                  //Creating rotate group for the head for rave animation
  260.                  var robotHeadRotateGroup = new THREE.Object3D();
  261.                  robotHeadRotateGroup.position.x = 10;
  262.                  robotHeadRotateGroup.position.y = 31;
  263.                  robotHeadRotateGroup.position.z = 2;
  264.                  robotGroup.add(robotHeadRotateGroup);
  265.                  
  266.                  var robotHead = new THREE.Mesh(robotGeometry,robotBlackMaterial);
  267.                  
  268.                  robotHead.castShadow = true;
  269.                  robotHead.scale.set(8,8,3);
  270.                  
  271.                  robotHeadRotateGroup.add(robotHead);
  272.                  
  273.                  // Creating a group to hold facial features
  274.                  var robotFace = new THREE.Object3D();
  275.                  
  276.                  var robotFaceBase = new THREE.Mesh(robotGeometry, robotYellowMaterial);
  277.                  robotFaceBase.scale.set(7,7,1);
  278.                  robotFaceBase.position.z = -1.5;
  279.                  robotFace.add(robotFaceBase);
  280.                  
  281.                  var robotMouth = new THREE.Mesh(robotGeometry, robotBlackMaterial);
  282.                  robotMouth.scale.set(5,1,1);
  283.                  robotMouth.position.y = -2;
  284.                  robotMouth.position.z = -1.9;
  285.                  robotFace.add(robotMouth);
  286.                  
  287.                  var robotEyes = [];
  288.                  
  289.                  for(var i=0; i < 2; i++)
  290.                  {
  291.                      robotEyes[i] = new THREE.Mesh(robotGeometry, robotBlackMaterial);
  292.                      robotEyes[i].scale.set(2,2,1);
  293.                      robotEyes[i].position.y = 1;
  294.                      robotEyes[i].position.z = -1.9;
  295.                  
  296.                      if(i === 0)
  297.                      {
  298.                         robotEyes[i].position.x = -1.5;
  299.                      }
  300.                      if(i === 1)
  301.                      {
  302.                          robotEyes[i].position.x = 1.5;
  303.                      }
  304.                      
  305.                      robotFace.add(robotEyes[i]);
  306.                  }
  307.                  
  308.                  robotHeadRotateGroup.add(robotFace);
  309.                  
  310.                  //Creating the robots chest panel
  311.                  var robotChest = new THREE.Object3D();
  312.                  
  313.                  var robotChestPanel = new THREE.Mesh(robotGeometry, robotYellowMaterial);
  314.                  robotChestPanel.scale.set(12,14,1);
  315.                  robotChestPanel.position.x = 10;
  316.                  robotChestPanel.position.y = 18;
  317.                  robotChestPanel.position.z = 0.5;                
  318.                  robotChest.add(robotChestPanel);
  319.          
  320.                  robotGroup.add(robotChest);
  321.                  
  322.                  
  323.                 // position and point the camera to the center of the scene
  324.                 camera.position.x = 40;
  325.                 camera.position.y = 80;
  326.                 camera.position.z = -60;
  327.                 camera.lookAt(robotHead.position);
  328.                
  329.                 // *** LIGHTING ***
  330.  
  331.                 // add subtle ambient lighting
  332.                 var ambientLight = new THREE.AmbientLight(0x0c0c0c);
  333.                 scene.add(ambientLight);
  334.                
  335.                 // Light for the rave animation
  336.                 var robotLight = new THREE.SpotLight(0xffffff);
  337.                 robotLight.position.set(20,50,-20);
  338.                 robotLight.castShadow = true;
  339.                 robotLight.intensity = 0;
  340.                
  341.                 robotGroup.add(robotLight);
  342.  
  343.                 // add spotlight for the shadows
  344.                 var spotLight = new THREE.SpotLight(0xffffff);
  345.                 spotLight.position.set(-40, 60, -10);
  346.                 spotLight.castShadow = true;
  347.                 scene.add(spotLight);
  348.  
  349.                 // add spotlight to light robot face
  350.                 var spotLightFace = new THREE.SpotLight(0xffffff);
  351.                 spotLightFace.position.set(80,50,-60);
  352.                 spotLightFace.castShadow = true;
  353.                 spotLightFace.intensity = 1;
  354.                 scene.add(spotLightFace);
  355.                
  356.                 // Light orbiting the ball and sphere
  357.                 var lightColor = "#e80d5a";
  358.                 var light = new THREE.SpotLight(lightColor);
  359.                 light.position.x = 120;
  360.                 light.position.y = 30;
  361.                 light.position.z = 20;
  362.                 light.castShadow = true;
  363.                 light.shadowCameraNear = 2;
  364.                 light.shadowCameraFar = 200;
  365.                 light.shadowCameraFov = 130;
  366.                 light.distance = 100;
  367.                 light.intensity = 3;
  368.                 scene.add(light);
  369.                
  370.                 //Rotation group for light
  371.                 var lightRotationGroup = new THREE.Object3D();
  372.                 lightRotationGroup.position.y = 30;
  373.                 lightRotationGroup.position.x = 10;
  374.                 lightRotationGroup.add(light);
  375.                 scene.add(lightRotationGroup);
  376.                
  377.  
  378.                 // add a small sphere simulating the light
  379.                 var sphereLight = new THREE.SphereGeometry(0.2);
  380.                 var sphereLightMaterial = new THREE.MeshBasicMaterial({color: 0xac6c25});
  381.                 var sphereLightMesh = new THREE.Mesh(sphereLight, sphereLightMaterial);
  382.                 sphereLightMesh.position = light.position;
  383.                 scene.add(sphereLightMesh);
  384.  
  385.                 // add the output of the renderer to the html element
  386.                 $("#WebGL-output").append(renderer.domElement);
  387.  
  388.                 // Setting the variables to be controlled using the GUI
  389.                 var controls = new function() {
  390.                     this.rotationSpeed = 0.02;
  391.                     this.lightColor = lightColor;
  392.                     this.intensity = light.intensity;
  393.                     this.robotSpeed = 1,
  394.                     this.rave = false;
  395.                     this.spotlight = true;
  396.                     this.robotLight = false;
  397.                 };
  398.  
  399.                 // Creating a GUI controls panel, and settings controls
  400.                 var gui = new dat.GUI();
  401.                 gui.add(controls, 'rotationSpeed', 0, 0.5);
  402.                
  403.                 gui.addColor(controls, 'lightColor').onChange(function(e) {
  404.                     light.color = new THREE.Color(e);
  405.                 });
  406.                
  407.                 gui.add(controls, 'intensity', 0, 5).onChange(function(e) {
  408.                     light.intensity = e;
  409.                 });
  410.                
  411.                 gui.add(controls, 'robotSpeed', 1, 5);
  412.                
  413.                 gui.add(controls, "spotlight").listen();
  414.                
  415.                 gui.add(controls, 'robotLight').listen();
  416.                
  417.                 gui.add(controls, "rave");
  418.  
  419.                 // call the render function
  420.                 var clock = new THREE.Clock();
  421.                 var angle = 0;
  422.                 var lightAngle = 0;
  423.                
  424.  
  425.             render();
  426.  
  427.                 // Gets called multiple times a second to provide scene animation
  428.                 function render() {
  429.                    
  430.                     stats.update();
  431.                     // rotate the cube around its axes
  432.                     cube.rotation.x += controls.rotationSpeed;
  433.                     cube.rotation.y += controls.rotationSpeed;
  434.                     cube.rotation.z += controls.rotationSpeed;
  435.                    
  436.                     // rotate the cube around its axes
  437.                     shinySphere.rotation.x += controls.rotationSpeed;
  438.                     shinySphere.rotation.y += controls.rotationSpeed;
  439.                     shinySphere.rotation.z += controls.rotationSpeed;
  440.                                        
  441.                     if(controls.rave)
  442.                     {
  443.                         controls.spotlight = false;
  444.                         controls.robotLight = true;
  445.                
  446.                         counter++;
  447.                         leftArmRotationGroup.rotation.x += controls.rotationSpeed;
  448.                         rightArmRotationGroup.rotation.x -= controls.rotationSpeed;
  449.                         robotHeadRotateGroup.rotation.y += controls.rotationSpeed;
  450.                        
  451.                         if(counter >= strobeSpeed)
  452.                         {
  453.                            light.color = new THREE.Color(randomColour());
  454.                            robotLight.color = new THREE.Color(randomColour());
  455.                            counter = 0;
  456.                         }
  457.                        
  458.                          
  459.                          
  460.                          light.intensity = 5;
  461.                          controls.rotationSpeed = 0.1;
  462.                     }
  463.                    
  464.                     if(controls.spotlight)
  465.                     {
  466.                         spotLightFace.intensity = 1;
  467.                     }
  468.                     else
  469.                     {
  470.                          spotLightFace.intensity = 0;
  471.                     }
  472.                    
  473.                     if(controls.robotLight)
  474.                     {
  475.                         robotLight.intensity = 3;
  476.                     }
  477.                     else
  478.                     {
  479.                          robotLight.intensity = 0;
  480.                     }
  481.                    
  482.                      // move the light
  483.                     lightAngle += 0.01;
  484.                     light.position.x = 20 +(10 * (Math.sin(lightAngle)));
  485.                     light.position.z = (10 * (Math.cos(lightAngle)));
  486.                     lightRotationGroup.position = light.position;
  487.                    
  488.                     var delta = clock.getDelta();
  489.                    
  490.                     // render using requestAnimationFrame
  491.                     requestAnimationFrame(render);
  492.                     renderer.render(scene, camera);
  493.                 }
  494.  
  495.                 function initStats() {
  496.  
  497.                     var stats = new Stats();
  498.  
  499.                     stats.setMode(0); // 0: fps, 1: ms
  500.  
  501.                     // Align top-left
  502.                     stats.domElement.style.position = 'absolute';
  503.                     stats.domElement.style.left = '0px';
  504.                     stats.domElement.style.top = '0px';
  505.  
  506.                     $("#Stats-output").append(stats.domElement);
  507.  
  508.                     return stats;
  509.                    
  510.        
  511.                 }
  512.                
  513.                 // Keyboard interactions
  514.                 //
  515.                 //
  516.                 // Moves the robot backward in whatever direction it facing
  517.                 var keypress = new window.keypress.Listener();
  518.  
  519.                 keypress.simple_combo("s", function() {
  520.                                        
  521.                     var facing = directions[currentDirection];
  522.                    
  523.                     // Switch to determine how to animate robot based on direction facing
  524.                     switch(facing)
  525.                     {
  526.                        
  527.                         case 'N':
  528.                              robotGroup.position.z += controls.robotSpeed;
  529.                         break;
  530.                        
  531.                         case 'E':
  532.                               robotGroup.position.x += controls.robotSpeed;  
  533.                             break;
  534.  
  535.                         case 'S':
  536.                               robotGroup.position.z -= controls.robotSpeed;  
  537.                             break
  538.  
  539.                         case 'W':
  540.                               robotGroup.position.x -= controls.robotSpeed;
  541.                             break;
  542.  
  543.                         default:
  544.                             robotGroup.position.z += controls.robotSpeed;
  545.                             break;
  546.                        
  547.                     }
  548.                    
  549.                 });
  550.                
  551.                 // Moves the robot forward in whatever direction it facing
  552.                 keypress.simple_combo("w", function() {
  553.                                      
  554.                     var facing = directions[currentDirection];
  555.                    
  556.                     // Switch to determine how to animate robot based on direction facing
  557.                     switch(facing)
  558.                     {
  559.                        
  560.                         case 'N':
  561.                              robotGroup.position.z -= controls.robotSpeed;
  562.                         break;
  563.                        
  564.                         case 'E':
  565.                               robotGroup.position.x -= controls.robotSpeed;  
  566.                             break;
  567.  
  568.                         case 'S':
  569.                               robotGroup.position.z += controls.robotSpeed;  
  570.                             break
  571.  
  572.                         case 'W':
  573.                               robotGroup.position.x += controls.robotSpeed;
  574.                             break;
  575.  
  576.                         default:
  577.                             robotGroup.position.z -= controls.robotSpeed;
  578.                             break;
  579.                        
  580.                     }
  581.                    
  582.                    
  583.                 });
  584.                  // A press fires rotation to the left of 1/4 radian to ensure robot always remains on axis z, x
  585.                 keypress.simple_combo("a", function() {
  586.                          
  587.                     robotGroup.rotation.y += 1.57;
  588.                     // Incremenet the current direction variable, if higher than array length, loop
  589.                     currentDirection++;
  590.                                        
  591.                     if(currentDirection > directions.length-1)
  592.                     {
  593.                         currentDirection = directions.length - directions.length;
  594.                     }
  595.                    
  596.                      var facing = directions[currentDirection];
  597.                      $('#robotFacing-output').text('Robot facing: ' + facing);
  598.                    
  599.                 });
  600.                  // D press fires rotation to the right of 1/4 radian to ensure robot always remains on axis z, x
  601.                 keypress.simple_combo("d", function() {
  602.                    
  603.                     robotGroup.rotation.y -= 1.57;
  604.  
  605.                     // Incremenet the current direction variable, if higher than array length, loop
  606.                     currentDirection = currentDirection-1;
  607.                                        
  608.                     if(currentDirection < directions.length-directions.length)
  609.                     {
  610.                         currentDirection = directions.length - 1;
  611.                     }
  612.                    
  613.                      var facing = directions[currentDirection];
  614.                      $('#robotFacing-output').text('Robot facing: ' + facing);
  615.                    
  616.                 });
  617.                  // Q press fires rotation to the left of 1/4 radian to ensure robot always remains on axis z, x
  618.                 keypress.simple_combo("q", function() {
  619.                    
  620.                     robotGroup.rotation.y += 1.57;
  621.                     // Incremenet the current direction variable, if higher than array length, loop
  622.                     currentDirection++;
  623.                                        
  624.                     if(currentDirection > directions.length-1)
  625.                     {
  626.                         currentDirection = directions.length - directions.length;
  627.                     }
  628.                    
  629.                      var facing = directions[currentDirection];
  630.                      $('#robotFacing-output').text('Robot facing: ' + facing);
  631.                    
  632.                 });
  633.                
  634.                  // E press fires rotation to the right of 1/4 radian to ensure robot always remains on axis z, x
  635.                 keypress.simple_combo("e", function() {
  636.                    
  637.                     robotGroup.rotation.y -= 1.57;
  638.  
  639.                     // Incremenet the current direction variable, if higher than array length, loop
  640.                     currentDirection = currentDirection-1;
  641.                                        
  642.                     if(currentDirection < directions.length-directions.length)
  643.                     {
  644.                         currentDirection = directions.length - 1;
  645.                     }
  646.                    
  647.                      var facing = directions[currentDirection];
  648.                      $('#robotFacing-output').text('Robot facing: ' + facing);
  649.                    
  650.                 });
  651.                
  652.                
  653.                   // zoom the camera up
  654.                 keypress.simple_combo("up", function() {
  655.                    
  656.                     focalLength++;
  657.                    
  658.                     camera.setLens(focalLength);
  659.                     camera.updateProjectionMatrix();
  660.                    
  661.                 });
  662.                
  663.                    // zoom the camera out
  664.                 keypress.simple_combo("down", function() {
  665.                    
  666.                     focalLength = focalLength - 1;
  667.                    
  668.                     camera.setLens(focalLength);
  669.                     camera.updateProjectionMatrix();
  670.                    
  671.                 });
  672.                
  673.                    // zoom the camera out
  674.                 keypress.simple_combo("f", function() {
  675.                    
  676.                     var vector = robotGroup.position;
  677.                    
  678.                     camera.lookAt(vector);
  679.                    
  680.                 });
  681.                
  682.                 // creats 3D environement cube map
  683.                  function createCubeMap(name) {
  684.  
  685.                     var path = "../assets/textures/cubemap/" + name + "/";
  686.                     var format = '.jpg';
  687.                     var urls = [
  688.                         path + 'posx' + format, path + 'negx' + format,
  689.                         path + 'posy' + format, path + 'negy' + format,
  690.                         path + 'posz' + format, path + 'negz' + format
  691.                     ];
  692.  
  693.                     var textureCube = THREE.ImageUtils.loadTextureCube(urls, new THREE.CubeReflectionMapping());
  694.                     return textureCube;
  695.                 }
  696.                
  697.                 // Gets a random hexidecimal colour
  698.                 function randomColour()
  699.                 {
  700.                     return "#" + Math.random().toString(16).slice(2, 8);
  701.                    
  702.                    
  703.                 }
  704.                
  705.                
  706.             });
  707.  
  708.         </script>
  709.     </body>
  710. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement