Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.             var scene, camera, renderer, loader;
  3.             var spritescene;
  4.             var clouds = new Array();
  5.             var stars = new Array();
  6.             var timer = Date.now();
  7.             var smoke, ambientclouds, movingclouds;
  8.            
  9.             function SmokeParticles(count)
  10.             {
  11.                 var obj = new THREE.Object3D;
  12.                 var particles = new Array;
  13.                 var num = count;
  14.                
  15.                 var radius = 1.35;
  16.                
  17.                 function CreateParticle(idx, lag)
  18.                 {
  19.                     if (!particles[idx])
  20.                     {
  21.                         var material = new THREE.MeshBasicMaterial({
  22.                             map: THREE.ImageUtils.loadTexture('img/cloudsmall.png'),
  23.                             transparent: true,
  24.                         });
  25.                         var geometry = new THREE.PlaneGeometry(1, 1);
  26.                         var particle = new THREE.Mesh( geometry, material );
  27.                         particle.scale.x = particle.scale.y = 1.5;
  28.                         obj.add(particle);
  29.                        
  30.                         particles[idx] = {
  31.                             p: particle,
  32.                             lifetime: 0,
  33.                             angle: 0,
  34.                             phase: 0
  35.                         };
  36.                        
  37.                         lag = true;
  38.                     }
  39.                    
  40.                     if (lag)
  41.                         particles[idx].phase = Math.random()*Math.PI*2;
  42.                     else
  43.                         particles[idx].phase = 0;
  44.                    
  45.                     var angle = Math.random()*Math.PI*2;
  46.                     particles[idx].p.rotation.z = angle+THREE.Math.randFloatSpread(0.2);
  47.                     particles[idx].p.material.opacity = 0;
  48.                     particles[idx].angle = angle;
  49.                     particles[idx].lifetime = 4000+Math.random()*6000;
  50.                    
  51.                 }
  52.                
  53.                 for (var i = 0; i < num; ++i)
  54.                 {
  55.                     CreateParticle(i);
  56.                 }
  57.                
  58.                 this.Update = function(diff)
  59.                 {
  60.                     var lag = false;
  61.                     if (diff > 1000)
  62.                         lag = true;
  63.                     for (var i = 0; i < num; ++i)
  64.                     {
  65.                         particles[i].phase += diff/particles[i].lifetime*Math.PI;
  66.                         if (particles[i].phase >= Math.PI*2)
  67.                         {
  68.                             CreateParticle(i, lag);
  69.                         }
  70.                         else
  71.                         {
  72.                             particles[i].p.material.opacity = Math.sin(particles[i].phase)*0.1;
  73.                             var angle = particles[i].angle;
  74.                             var crad = radius + Math.sin(particles[i].phase*0.5)*0.3;
  75.                             particles[i].p.position.x = crad*Math.cos(angle);
  76.                             particles[i].p.position.y = crad*Math.sin(angle);
  77.                         }
  78.                     }
  79.                 }
  80.                
  81.                 this.AddToScene = function(scene)
  82.                 {
  83.                     obj.position.z = 1.1;
  84.                     scene.add(obj);
  85.                 }
  86.             }
  87.  
  88.             function AmbientClouds(count)
  89.             {
  90.                 var obj = new THREE.Object3D;
  91.                 var particles = new Array;
  92.                 var num = count;
  93.                
  94.                 var speed = 0.00008;
  95.                
  96.                 var material = new THREE.MeshBasicMaterial({
  97.                     map: THREE.ImageUtils.loadTexture('img/ambientclouds.png'),
  98.                     transparent: true,
  99.                     opacity: 0.2
  100.                 });
  101.                
  102.                 function CreateParticle(idx)
  103.                 {
  104.                     var geometry = new THREE.PlaneGeometry(1, 1);
  105.                     var particle = new THREE.Mesh( geometry, material );
  106.                     particle.position.x = THREE.Math.randFloatSpread(20);
  107.                     particle.position.y = THREE.Math.randFloatSpread(10);
  108.                     particle.scale.x = particle.scale.y = 10;
  109.                     obj.add(particle);
  110.                    
  111.                     particles[idx] = particle;
  112.                 }
  113.                
  114.                 for (var i = 0; i < num; ++i)
  115.                 {
  116.                     CreateParticle(i);
  117.                 }
  118.                
  119.                 this.Update = function(diff)
  120.                 {
  121.                     for (var i = 0; i < num; ++i)
  122.                     {
  123.                         particles[i].rotation.z += diff*speed;
  124.                     }
  125.                 }
  126.                
  127.                 this.AddToScene = function(scene)
  128.                 {
  129.                     obj.position.z = 0.5;
  130.                     scene.add(obj);
  131.                 }
  132.             }
  133.            
  134.             function MovingClouds(count)
  135.             {
  136.                 var obj = new THREE.Object3D;
  137.                 var particles = new Array;
  138.                 var num = count;
  139.                
  140.                 var xlimit = 12;
  141.                
  142.                 var material = new THREE.MeshBasicMaterial({
  143.                     map: THREE.ImageUtils.loadTexture('img/clouds.png'),
  144.                     transparent: true,
  145.                     opacity: 0.2
  146.                 });
  147.                
  148.                 function CreateParticle(idx, lag)
  149.                 {
  150.                     if (!particles[idx])
  151.                     {
  152.                         var geometry = new THREE.PlaneGeometry(1, 1);
  153.                         var particle = new THREE.Mesh(geometry, material);
  154.                         particle.scale.x = particle.scale.y = 10;
  155.                         obj.add(particle);
  156.                        
  157.                         particles[idx] = {
  158.                             p: particle,
  159.                             speed: 0
  160.                         };
  161.                        
  162.                         lag = true;
  163.                     }
  164.                    
  165.                     if (lag)
  166.                         particles[idx].p.position.x = THREE.Math.randFloatSpread(xlimit*2);
  167.                     else
  168.                         particles[idx].p.position.x = -xlimit;
  169.                    
  170.                     particles[idx].p.position.y = THREE.Math.randFloatSpread(3)-4;
  171.                     particles[idx].speed = (Math.random()+0.2)*0.0003;
  172.                    
  173.                 }
  174.                
  175.                 for (var i = 0; i < num; ++i)
  176.                 {
  177.                     CreateParticle(i);
  178.                 }
  179.                
  180.                 this.Update = function(diff)
  181.                 {
  182.                     var lag = false;
  183.                     if (diff > 1000)
  184.                         lag = true;
  185.                     for (var i = 0; i < num; ++i)
  186.                     {
  187.                         particles[i].p.position.x += diff*particles[i].speed;
  188.                        
  189.                         if (particles[i].p.position.x > xlimit)
  190.                             CreateParticle(i, lag);
  191.                     }
  192.                 }
  193.                
  194.                 this.AddToScene = function(scene)
  195.                 {
  196.                     obj.position.z = 2;
  197.                     scene.add(obj);
  198.                 }
  199.             }
  200.            
  201.             function CreatePlanet()
  202.             {
  203.                 var material = new THREE.MeshBasicMaterial({
  204.                     map: THREE.ImageUtils.loadTexture('img/planet.png'),
  205.                     transparent: true
  206.                 });
  207.                 var geometry = new THREE.PlaneGeometry(1, 1);
  208.                 var planet = new THREE.Mesh( geometry, material );
  209.                 planet.position.z = 1;
  210.                 planet.scale.x = planet.scale.y = 3;
  211.                 return planet;
  212.             }
  213.            
  214.             function CreatePlanet2()
  215.             {
  216.                 var material = new THREE.MeshBasicMaterial({
  217.                     map: THREE.ImageUtils.loadTexture('img/planet2.png'),
  218.                     transparent: true
  219.                 });
  220.                 var geometry = new THREE.PlaneGeometry(1, 1);
  221.                 var planet = new THREE.Mesh( geometry, material );
  222.                 planet.position.z = 0.6;
  223.                 planet.position.x = -5;
  224.                 planet.position.y = 2;
  225.                 planet.scale.x = planet.scale.y = 1;
  226.                 scene.add( planet );
  227.             }
  228.            
  229.             function CreateStars()
  230.             {
  231.                 var group = new THREE.Object3D;
  232.                 var material = new THREE.MeshBasicMaterial({
  233.                     map: THREE.ImageUtils.loadTexture('img/bluestar.png'),
  234.                     transparent: true
  235.                 });
  236.                 var sgeometry = new THREE.PlaneGeometry(1, 1);
  237.            
  238.                 for(var i=0; i < 1000; ++i) {
  239.                     if (i == 500)
  240.                     {
  241.                         material = new THREE.MeshLambertMaterial({
  242.                             map: THREE.ImageUtils.loadTexture('img/whitestar.png'),
  243.                             transparent: true
  244.                         });
  245.                     }
  246.                    
  247.                     var celObj = new THREE.Mesh(sgeometry, material);
  248.                     celObj.position.x = THREE.Math.randFloatSpread(20);
  249.                     celObj.position.y = THREE.Math.randFloatSpread(10);
  250.                     celObj.scale.x = celObj.scale.y = 0.02 + Math.random() * 0.08;
  251.                     stars[i] = {c: celObj, s: celObj.scale.x, r: Math.random(), rs: (1.1+Math.random())};
  252.            
  253.                     group.add(celObj);
  254.                 }
  255.                
  256.                 scene.add(group);
  257.             }
  258.            
  259.             function init() {
  260.                 loader = new THREE.JSONLoader();
  261.                 scene = new THREE.Scene();
  262.                
  263.                 //scene.fog = new THREE.Fog( 0xcce0ff, 500, 10000 );
  264.                
  265.                 // Lights
  266.                 scene.add( new THREE.AmbientLight( 0xffffff ) );
  267.                
  268.                 /*var geometry = new THREE.PlaneGeometry( 24, 24);
  269.                 var material = new THREE.MeshLambertMaterial({
  270.                     map: THREE.ImageUtils.loadTexture('img/background.png'),
  271.                 });
  272.                 var plane = new THREE.Mesh( geometry, material );
  273.                 plane.position.z = -1;
  274.                 scene.add( plane );*/
  275.                
  276.                 var planetcontainer = new THREE.Object3D;
  277.                 planetcontainer.add(CreatePlanet());
  278.                 scene.add(planetcontainer);
  279.                
  280.                 planetcontainer.position.x = 4;
  281.                 planetcontainer.position.y = 0.2;
  282.                
  283.                 CreatePlanet2();
  284.                
  285.                
  286.                
  287.                 //camera = new THREE.OrthographicCamera( window.innerWidth / - 2, window.innerWidth / 2, window.innerHeight / 2, window.innerHeight / - 2, 1, 1000 );
  288.                 //scene.add( camera );
  289.                 camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 100);
  290.                 camera.position.set(0, 0, 10);
  291.                 camera.lookAt(scene.position);
  292.                 scene.add(camera);
  293.  
  294.                 CreateStars();
  295.                
  296.                 smoke = new SmokeParticles(150);
  297.                 smoke.AddToScene(planetcontainer);
  298.                
  299.                 ambientclouds = new AmbientClouds(10);
  300.                 ambientclouds.AddToScene(scene);
  301.                
  302.                 movingclouds = new MovingClouds(40);
  303.                 movingclouds.AddToScene(scene);
  304.            
  305.                 if (window.WebGLRenderingContext)
  306.                     renderer = new THREE.WebGLRenderer();
  307.                 else
  308.                     renderer = new THREE.CanvasRenderer();
  309.                 renderer.setSize( window.innerWidth, window.innerHeight );
  310.                 renderer.setClearColor(0x010010, 0);
  311.                 document.body.appendChild( renderer.domElement );
  312.             }
  313.            
  314.             function animate() {
  315.                 var diff = Date.now() - timer;
  316.                 timer = Date.now();
  317.                
  318.                 smoke.Update(diff);
  319.                 ambientclouds.Update(diff);
  320.                 movingclouds.Update(diff);
  321.                
  322.                
  323.                 for(var i=0; i < 1000; ++i)
  324.                     stars[i].c.scale.x = stars[i].c.scale.y = stars[i].s + Math.sin((timer*0.002+6.28*stars[i].r)*stars[i].rs)*0.02;
  325.                
  326.                 // note: three.js includes requestAnimationFrame shim
  327.                 requestAnimationFrame( animate );
  328.            
  329.                 renderer.render( scene, camera );
  330.             }
  331.            
  332.             window.addEventListener( 'resize', onWindowResize, false );
  333.  
  334.             function onWindowResize(){
  335.            
  336.                 camera.aspect = window.innerWidth / window.innerHeight;
  337.                 camera.updateProjectionMatrix();
  338.            
  339.                 renderer.setSize( window.innerWidth, window.innerHeight );
  340.            
  341.             }
  342.            
  343.             init();
  344.             animate();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement