Advertisement
kolyaventuri

Render.js

Sep 15th, 2015
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.     3D Countdown Using THREE.js
  3.     Latest Rev: 9/15/2015
  4. */
  5.  
  6. //Globals
  7. var container, scene, camera, renderer, controls, width, height, minutes, tens, seconds, cDGroup, boxWidth = -1, boxHeight;
  8.  
  9. //Font size
  10. var SIZE = 100;
  11.  
  12. //Total countdown in seconds
  13. /*
  14.     LIMITATION: 599s as no code is currently in place to handle more than 9:59 of countdown.
  15.     600 (10:00) will display as [?]:00 (Actual output unknown, I just know it wont work)
  16.  */
  17. var time = 300, mts;
  18.  
  19. var isrunning = true;
  20. var numberToStart = 9;
  21. //var currentNum = istesting ? numberToStart : numberToStart;
  22.  
  23. var getMTS = function() {
  24.     var t = time;
  25.     //Get minutes
  26.     var ms = Math.floor(time / 60);
  27.     t -= ms*60;
  28.     //Get TENS of seconds
  29.     var ts = Math.floor(t / 10);
  30.     t -= ts*10;
  31.     //Whatever is left is the ONES of seconds
  32.     var s = t;
  33.    
  34.     return [ms, ts, s]
  35. };
  36.  
  37. function init() {
  38.     // SCENE
  39.     scene = new THREE.Scene();
  40.  
  41.     // CAMERA
  42.     var SCREEN_WIDTH = window.innerWidth, SCREEN_HEIGHT = window.innerHeight;
  43.     var VIEW_ANGLE = 45, ASPECT = SCREEN_WIDTH / SCREEN_HEIGHT, NEAR = 1, FAR = 20000;
  44.     camera = new THREE.OrthographicCamera(SCREEN_WIDTH / -2, SCREEN_WIDTH / 2, SCREEN_HEIGHT  / 2, SCREEN_HEIGHT  / -2, NEAR, FAR);
  45.     scene.add(camera);
  46.     camera.position.set(0,0,1000);
  47.     camera.lookAt(scene.position);
  48.  
  49.     //Setup renderer
  50.     renderer = new THREE.WebGLRenderer( {antialias:true, alpha: false} );
  51.     renderer.setClearColor( 0x19181A); //Background color hex
  52.     renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
  53.     container = document.getElementById( 'canvas' );
  54.     container.appendChild( renderer.domElement );
  55.  
  56.     //CONTROLS
  57.     controls = new THREE.OrbitControls( camera );
  58.     controls.damping = 0.2;
  59.     controls.addEventListener( 'change', render );
  60.    
  61.     // LIGHT
  62.     var z = 150;
  63.     //scene.add(createLight([0,0,z]));
  64.     scene.add(createLight([0,0,z]));
  65.     //Initial render
  66.     renderTime();
  67. }
  68.  
  69. var renderTime = function() {
  70.     //Don't remove null group, otherwise clear for next OBJ render
  71.     if(cDGroup != null && typeof cDGroup != 'undefined') {
  72.         cDGroup.remove(seconds);
  73.         cDGroup.remove(tens);
  74.         cDGroup.remove(minutes);
  75.     }
  76.     /*
  77.         Go from time, to value of
  78.         [
  79.             minutes,
  80.             TENS of seconds,
  81.             ONES of seconds
  82.         ]
  83.     */
  84.     mts = getMTS();
  85.  
  86.     //Render the objects
  87.     minutes = renderText(mts[0]);
  88.     tens = renderText(mts[1], true);
  89.     seconds = renderText(mts[2]);
  90.     colon = renderText(":");
  91.    
  92.     //Position the objects, relative to the text size
  93.     minutes.position.x += SIZE * (-22 / 12);
  94.     colon.position.x -= SIZE * (1/12);
  95.     tens.position.x += SIZE * (2/3);
  96.     seconds.position.x += SIZE * 2.5;
  97.    
  98.     //Group objects
  99.     cDGroup = new THREE.Object3D();
  100.    
  101.     cDGroup.add(seconds);
  102.     cDGroup.add(tens);
  103.     cDGroup.add(minutes);
  104.     cDGroup.add(colon);
  105.  
  106.     //Calculate center of screen, only first time
  107.     if(boxWidth == -1) {
  108.         var bbox = new THREE.Box3().setFromObject(cDGroup);
  109.         boxWidth = bbox.max.x - bbox.min.x;
  110.         boxHeight = bbox.max.y - bbox.min.y;
  111.     }
  112.     //Position and add group to scene
  113.     cDGroup.position.x = window.innerWidth / 4 - boxWidth;
  114.     cDGroup.position.y = window.innerHeight / 4 - boxHeight;
  115.    
  116.     scene.add(cDGroup);
  117. };
  118.  
  119. var renderText = function(currentNum, isTens) {
  120.     var number;
  121.     //Special case
  122.     if(currentNum == ":") {
  123.         var textObject = createTextObject(":");
  124.         return textObject;
  125.     }
  126.     //Set key to current number (X), and next (Y) such that key is "XY"
  127.     if(!isTens) {
  128.         number = currentNum + "";
  129.         currentNum--;
  130.         if(currentNum < 0) currentNum = 9;
  131.         number += currentNum;
  132.     } else {
  133.         //If the number is the tens place, AND the tens are 0, prepare to move from 0 to 5 (for 59 seconds)
  134.         if(currentNum == 0) {
  135.             number = "50";
  136.         } else {
  137.             //Otherwise normal
  138.             number = currentNum + "";
  139.             currentNum--;
  140.             if(currentNum < 0) currentNum = 9;
  141.             number += currentNum;
  142.         }
  143.     }
  144.     //Create and return the text object
  145.     var textObject = createTextObject(number);
  146.     return textObject;
  147. };
  148.  
  149. var createLight = function(position) {
  150.     var light = new THREE.DirectionalLight( 0xBBBBBB, 1, 0 );
  151.     light.position.set(position[0], position[1], position[2]);
  152.     return light;
  153. };
  154.  
  155. var width = SIZE;
  156. var height = width;
  157. var ratio = height / width;
  158. var lineWidth = width / 3;
  159.  
  160. var createTextObject = function(letter, params) {
  161.     //Central cube
  162.     var cubeSize = 0; //No size, invisible
  163.     var centralCubeGeometry = new THREE.BoxGeometry( cubeSize,cubeSize,cubeSize );
  164.     var centralCubeMaterial = new THREE.MeshBasicMaterial( { color: 0x00ff00, alpha: 0 } );
  165.     var centralCube = new THREE.Mesh( centralCubeGeometry, centralCubeMaterial );
  166.    
  167.     //Placement cube (from TL corner)
  168.     cubeSize = 0; //No size, invisible
  169.     var growthCubeGeometry = new THREE.BoxGeometry( cubeSize,cubeSize,cubeSize );
  170.     var growthCubeMaterial = new THREE.MeshBasicMaterial( { color: 0x0000ff, alpha: 0 } );
  171.     var growthCube = new THREE.Mesh( growthCubeGeometry, growthCubeMaterial );
  172.    
  173.     growthCube.position.x = -(width / 2) + 2.5;
  174.     growthCube.position.y = (height / 2) - 2.5;
  175.     growthCube.position.z = (width / 2) - 2.5;
  176.    
  177.     centralCube.add(growthCube);
  178.    
  179.     //Temporary array of geometry
  180.     //Geometry for each object stored in js/LettersBlock.js
  181.     var loadedLetters = JSON.parse(JSON.stringify(letters));
  182.     var letter = loadedLetters[letter];
  183.    
  184.     for(var i = 0; i < letter.length; i++) {
  185.         var line = letter[i];
  186.         var dimensions = line[1];
  187.         var location = line[0];
  188.        
  189.         //Scale dimensions
  190.         dimensions[0] = (dimensions[0] == 0) ? lineWidth : width * dimensions[0];
  191.         dimensions[1] = (dimensions[1] == 0) ? lineWidth : height * dimensions[1];
  192.         dimensions[2] = (dimensions[2] == 0) ? lineWidth : width * dimensions[2];
  193.        
  194.         //Scale locations
  195.         location[0] += (location[0] == 0) ? dimensions[0] / 2 : (dimensions[0] / 2) + (width * location[0]);
  196.         location[0] -= 2.5;
  197.         location[1] -= (location[1] == 0) ? dimensions[1] / 2 : (dimensions[1] / 2) + (height * location[1]);
  198.         location[1] += 2.5;
  199.         location[2] = (location[2] == 0) ? dimensions[2] / 2 : (dimensions[2] / 2) + (width * location[2]);
  200.        
  201.         //MOD locations
  202.         location[0] += (location[3] == 0) ? 0 : dimensions[0] * location[3];
  203.         location[1] += (location[4] == 0) ? 0 : dimensions[1] * location[4];
  204.         location[1] -= location[4];
  205.         location[2] -= (location[5] == 0 || typeof location[5] == 'undefined') ? 0 : dimensions[2] * location[2];
  206.        
  207.         //Create and position geometry
  208.         var lineGeom = new THREE.BoxGeometry( dimensions[0],dimensions[1],dimensions[2] );
  209.         lineGeom.computeFaceNormals();
  210.         var lineMaterial = new THREE.MeshPhongMaterial( { color: 0xffffff, shading: THREE.FlatShading } ); //Color of text
  211.         var newLine = new THREE.Mesh( lineGeom, lineMaterial );
  212.        
  213.         newLine.position.x = location[0];
  214.         newLine.position.y = location[1];
  215.         newLine.position.z = location[2];
  216.  
  217.         growthCube.add(newLine);
  218.     }
  219.    
  220.     return centralCube;
  221. };
  222.  
  223. //Rotation and positioning stuff
  224. var i = 0;
  225. var delay = 0;
  226.  
  227. function render() {
  228.     if(isrunning) {
  229.         //5 frame window when stopped
  230.         //All rotation in increments of PI/110
  231.         if(i < 55) {
  232.             //Rotate seconds
  233.             seconds.rotateY(Math.PI / 110);
  234.             //If no ones of seconds left, rotate tens
  235.             if(mts[2] < 1) {
  236.                 tens.rotateY(Math.PI / 110);
  237.             }
  238.             //If tens of seconds is 0, maybe rotate
  239.             if(mts[1] < 1) {
  240.                 var n = (mts[1] * 10) + mts[2];
  241.                 //if no seconds left, turn to next minute down
  242.                 if(n == 0) {
  243.                     minutes.rotateY(Math.PI / 110);
  244.                 }
  245.             }
  246.         } else if(i == 60) {
  247.             //Reset, count down time, and stop if needed
  248.             i = 0;
  249.             time--;
  250.             renderTime();
  251.             if(time == 0) {
  252.                 isrunning = false;
  253.             }
  254.         }
  255.     }
  256.     i++;
  257.     //Render changes
  258.     renderer.render( scene, camera );
  259. }
  260.  
  261. function animate() {
  262.     //Update controls, render everything anew, and start the animation method again
  263.     window.requestAnimationFrame( animate );
  264.     controls.update();
  265.     render();
  266. }
  267. //Setup and start animation loop (every 1/60s)
  268. init();
  269. animate();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement