solar-system
Version:
Solar System with Threejs
9 lines • 15 kB
JavaScript
/*
* solar-system
* @Description Solar System with Threejs
* @version v0.1.45 - 2016-04-20
* @link https://github.com/kdaimiel/solar-system#readme
* @author Enrique Daimiel Ruiz <k.daimiel@gmail.com>
* @license MIT License, http://www.opensource.org/licenses/MIT
*/
THREE.SolarBody=function(a){THREE.Object3D.call(this),this.properties=_.extend({name:arguments[0]||null,type:arguments[1]||null,map:arguments[2]||null,bumpMap:arguments[3]||null,specularMap:arguments[4]||null,orbitProperties:arguments[5]||null,cloudsProperties:arguments[6]||null,ringsProperties:arguments[7]||null},a),this.name=this.properties.name,this.type=this.properties.type,this.geometry=new THREE.SphereGeometry(5,32,32),this.material=new THREE.MeshBasicMaterial,this.properties.map&&this.texloader.load(this.properties.map,this.loadTexture.bind(this)),this.properties.bumpMap&&this.texloader.load(this.properties.bumpMap,this.loadbumpMap.bind(this)),this.properties.specularMap&&this.texloader.load(this.properties.specularMap,this.loadspecularMap.bind(this)),this.properties.orbitProperties&&(this.orbitProperties=this.properties.orbitProperties),this.properties.cloudsProperties&&this.addClouds(this.properties.cloudsProperties),this.properties.ringsProperties&&this.addRings(this.properties.ringsProperties),this.drawMode=THREE.TrianglesDrawMode,this.updateMorphTargets()},THREE.SolarBody.prototype=Object.create(THREE.Mesh.prototype),THREE.SolarBody.prototype.constructor=THREE.SolarBody,THREE.SolarBody.prototype.addClouds=function(a){this.add(new THREE.CloudsMesh(a))},THREE.SolarBody.prototype.addRings=function(a){this.add(new THREE.RingsMesh(a))},THREE.SolarBody.prototype.addSatellite=function(a,b){var c=new THREE.OrbitMesh(b);a.orbit=c,this.orbit?(this.orbit.add(c),c.position.z=this.position.z||0):this.parent&&this.parent.add(c),c.add(a),a.position.z=this.radius+c.distance+a.radius||0},THREE.SolarBody.prototype.loadTexture=function(a){this.material=new THREE.MeshPhongMaterial({map:a,side:THREE.DoubleSide})},THREE.SolarBody.prototype.loadbumpMap=function(a){this.material.bumpMap=a},THREE.SolarBody.prototype.loadspecularMap=function(a){this.material.specularMap=a},THREE.SolarBody.prototype.update=function(a){this.rotation.y-=this.vRotation*Math.PI/180;for(var b in this.children)this.children[b].update(a,this)},THREE.SolarBody.prototype.texloader=new THREE.TextureLoader,THREE.PlanetMesh=function(a){this.properties=_.extend({name:arguments[0]||null,type:arguments[1]||"PlanetMesh",radius:arguments[2]||50,tilt:arguments[3]||0,vRotation:arguments[4]||0},a),THREE.SolarBody.call(this,this.properties),this.radius=this.properties.radius,this.rotation.x=this.properties.tilt,this.vRotation=this.properties.vRotation,this.geometry=new THREE.SphereGeometry(this.radius||50,100,100)},THREE.PlanetMesh.prototype=Object.create(THREE.SolarBody.prototype),THREE.PlanetMesh.prototype.constructor=THREE.PlanetMesh,THREE.RingsGeometry=function(a,b,c,d,e,f){THREE.Geometry.call(this),this.type="RingsGeometry",this.innerRadius=a||0,this.outerRadius=b||50,this.thetaStart=void 0!==c?c:0,this.thetaLength=void 0!==d?d:2*Math.PI,this.thetaSegments=void 0!==e?Math.max(3,e):50,this.phiSegments=void 0!==f?Math.max(1,f):50;var g,h,i,j=[],k=this.innerRadius,l=(b-a)/this.phiSegments;for(g=0;g<this.phiSegments+1;g++){for(h=0;h<this.thetaSegments+1;h++){var m=new THREE.Vector3;i=this.thetaStart+h/this.thetaSegments*this.thetaLength,m.x=k*Math.cos(i),m.y=k*Math.sin(i),this.vertices.push(m),j.push(new THREE.Vector2(g/(this.thetaSegments-1),h/(this.phiSegments-1)))}k+=l}var n=new THREE.Vector3(0,0,1);for(g=0;g<this.phiSegments;g++){var o=g*(this.thetaSegments+1);for(h=0;h<this.thetaSegments;h++){i=h+o;var p=i,q=i+this.thetaSegments+1,r=i+this.thetaSegments+2;this.faces.push(new THREE.Face3(p,q,r,[n.clone(),n.clone(),n.clone()])),this.faceVertexUvs[0].push([j[p].clone(),j[q].clone(),j[r].clone()]),p=i,q=i+this.thetaSegments+2,r=i+1,this.faces.push(new THREE.Face3(p,q,r,[n.clone(),n.clone(),n.clone()])),this.faceVertexUvs[0].push([j[p].clone(),j[q].clone(),j[r].clone()])}}this.computeFaceNormals(),this.boundingSphere=new THREE.Sphere(new THREE.Vector3,k)},THREE.RingsGeometry.prototype=Object.create(THREE.RingGeometry.prototype),THREE.RingsGeometry.prototype.constructor=THREE.RingsGeometry,THREE.CloudsMesh=function(a){this.properties=_.extend({name:arguments[0]||null,type:arguments[1]||"CloudsMesh",radius:arguments[2]||20,opacity:arguments[3]||.5,transparent:arguments[4]||!0,depthWrite:arguments[5]||!1,speed:arguments[6]||.2,map:arguments[7]||null,bumpMap:arguments[8]||null,specularMap:arguments[9]||null},a),THREE.SolarBody.call(this,this.properties),this.radius=this.properties.radius,this.opacity=this.properties.opacity,this.transparent=this.properties.transparent,this.depthWrite=this.properties.depthWrite,this.speed=this.properties.speed,this.geometry=new THREE.SphereGeometry(this.radius,100,100)},THREE.CloudsMesh.prototype=Object.create(THREE.SolarBody.prototype),THREE.CloudsMesh.prototype.constructor=THREE.CloudsMesh,THREE.CloudsMesh.prototype.loadTexture=function(a){this.material=new THREE.MeshPhongMaterial({map:a,side:THREE.DoubleSide,opacity:this.opacity,transparent:this.transparent,depthWrite:this.depthWrite})},THREE.CloudsMesh.prototype.update=function(){this.rotation.x-=THREE.Math.randFloat(0,this.speed)*Math.PI/180,this.rotation.y-=THREE.Math.randFloat(0,this.speed)*Math.PI/180},THREE.MoonMesh=function(a){this.properties=_.extend({name:arguments[0]||null,type:arguments[1]||"MoonMesh",radius:arguments[2]||50,tilt:arguments[3]||0,vRotation:arguments[4]||0,map:arguments[5]||null,bumpMap:arguments[6]||null,specularMap:arguments[7]||null,orbitProperties:arguments[8]||null,cloudsProperties:arguments[9]||null,ringsProperties:arguments[10]||null},a),THREE.PlanetMesh.call(this,this.properties)},THREE.MoonMesh.prototype=Object.create(THREE.PlanetMesh.prototype),THREE.MoonMesh.prototype.constructor=THREE.MoonMesh,THREE.OrbitMesh=function(a){this.properties=_.extend({name:arguments[0]||null,type:arguments[1]||"OrbitMesh",distance:arguments[2]||50,speed:arguments[3]||0,tilt:arguments[4]||0},a),THREE.Object3D.call(this),this.name=this.properties.name,this.type=this.properties.type,this.distance=this.properties.distance,this.speed=this.properties.speed,this.tilt=this.properties.tilt},THREE.OrbitMesh.prototype=Object.create(THREE.Object3D.prototype),THREE.OrbitMesh.prototype.constructor=THREE.OrbitMesh,THREE.OrbitMesh.prototype.update=function(){this.rotation.y-=this.speed*Math.PI/180;for(var a in this.children)this.children[a].update()},THREE.RingsMesh=function(a){this.properties=_.extend({name:arguments[0]||null,type:arguments[1]||"RingsMesh",tilt:arguments[2]||0,vRotation:arguments[3]||0,map:arguments[4]||null,bumpMap:arguments[5]||null,specularMap:arguments[6]||null,orbitProperties:arguments[7]||null,cloudsProperties:arguments[8]||null,ringsProperties:arguments[9]||null},a),THREE.SolarBody.call(this,this.properties),this.tilt=this.properties.tilt,this.rotation.x=(90-this.properties.tilt)*Math.PI/180,this.vRotation=this.properties.vRotation,this.geometry=new THREE.RingsGeometry(this.properties.innerRadius,this.properties.outerRadius,this.properties.thetaStart,this.properties.thetaLength,this.properties.thetaSegments,this.properties.phiSegments)},THREE.RingsMesh.prototype=Object.create(THREE.SolarBody.prototype),THREE.RingsMesh.prototype.constructor=THREE.RingsMesh,THREE.RingsMesh.prototype.update=function(){this.rotation.y-=this.vRotation*Math.PI/180},THREE.RingsMesh.prototype.loadTexture=function(a){this.material=new THREE.MeshPhongMaterial({map:a,side:THREE.DoubleSide})},THREE.StarMesh=function(a){this.properties=_.extend({name:arguments[0]||null,type:arguments[1]||"StarMesh",radius:arguments[2]||50,tilt:arguments[3]||0,vRotation:arguments[4]||0,intesity:arguments[5]||.8,map:arguments[6]||null,bumpMap:arguments[7]||null,specularMap:arguments[8]||null,orbitProperties:arguments[9]||null,cloudsProperties:arguments[10]||null,ringsProperties:arguments[11]||null},a),THREE.SolarBody.call(this,this.properties),this.radius=this.properties.radius,this.rotation.x=this.properties.tilt,this.vRotation=this.properties.vRotation,this.intesity=this.properties.intensity,this.geometry=new THREE.SphereGeometry(this.radius||50,100,100);var b=new THREE.PointLight(16777215,1.5,4500);b.update=function(a){for(var c in b.children)b.children[c].update&&b.children[c].update(a)},this.add(b),this.createLensFlare()},THREE.StarMesh.prototype=Object.create(THREE.SolarBody.prototype),THREE.StarMesh.prototype.constructor=THREE.StarMesh,THREE.StarMesh.prototype.createLensFlare=function(){var a=2*this.radius*this.intesity,b=new THREE.Color(16777215),c=new THREE.LensFlare(b),d=new THREE.TextureLoader;d.load("img/sun/lensflare1.png",function(b){c.add(b,16*a,0,THREE.AdditiveBlending)}),d.load("img/sun/lensflare2.png",function(b){c.add(b,16*a,0,THREE.AdditiveBlending)}),d.load("img/sun/lensflare3.png",function(b){c.add(b,16*a,0,THREE.AdditiveBlending)}),d.load("img/sun/lensflare4.png",function(b){c.add(b,64*a,0,THREE.AdditiveBlending)}),c.position=this.position,c.update=function(a,b){if(a){var c=a.position.distanceTo(b.position);for(var d in this.lensFlares)this.lensFlares[d].position=b.position,this.lensFlares[d].scale=this.lensFlares[d].size/c}this.updateLensFlares()},this.add(c),this.hasLensFlare=!0},THREE.StarMesh.prototype.loadTexture=function(a){this.material=new THREE.MeshBasicMaterial({map:a,side:THREE.BackSide})},define("scene-builder",function(){"use strict";function a(a){i=a}function b(a){h.add(a),i.aspect=(m||window.innerWidth)/(n||window.innerHeight),i.updateProjectionMatrix()}function c(a){k=a,k&&k.addEventListener&&k.addEventListener("change",d)}function d(){j.render(h,i)}function e(a){h=new THREE.Scene,a&&(m=a.width||null,n=a.height||null),k&&k.addEventListener("change",d),j=new THREE.WebGLRenderer({alpha:!0}),j.setSize(m||window.innerWidth,n||window.innerHeight),document.body.appendChild(j.domElement),l=new Stats,l.domElement.style.position="absolute",l.domElement.style.top="0px",document.body.appendChild(l.domElement),window.addEventListener("resize",g,!1)}function f(){if(!h)throw new TypeError("Scene has to be initiated before animate");requestAnimationFrame(f);for(var a in h.children)h.children[a].update&&h.children[a].update(i);k&&k.update(),d(),l.update()}function g(){i&&(i.aspect=(m||window.innerWidth)/(n||window.innerHeight),i.updateProjectionMatrix()),j.setSize(m||window.innerWidth,n||window.innerHeight),k&&k.handleResize()}var h,i,j,k,l,m,n,o={addObject:b,animate:f,setCamera:a,setControls:c,init:e};return o}),define("scene-factory",function(){"use strict";function a(a){var b;switch(a.aspect=a.aspect||window.innerWidth/window.innerHeight,a.type){case"PerspectiveCamera":b=new THREE.PerspectiveCamera(a.fov,a.aspect,a.near,a.far);break;case"CubeCamera":b=new THREE.CubeCamera(a.near,a.far,a.cubeResolution);break;case"OrthographicCamera":b=new THREE.OrthographicCamera(a.left,a.right,a.top,a.bottom,a.near,a.far);break;default:throw new TypeError(a.type+" is not a kind of valid camera")}return a.position&&(b.position.x=a.position.x||0,b.position.y=a.position.y||0,b.position.z=a.position.z||0),b.zoom=a.zoom||1,b}function b(a,b){var c;switch(b){case"TrackballControls":c=new THREE.TrackballControls(a);break;case"DeviceOrientationControls":c=new THREE.DeviceOrientationControls(a);break;case"FlyControls":c=new THREE.FlyControls(a);break;case"OrbitControls":c=new THREE.OrbitControls(a);break;case"PointerLockControls":c=new THREE.PointerLockControls(a);break;case"TransformControls":c=new THREE.TransformControls(a);break;default:throw new TypeError(b+" is not a kind of valid camera controls")}return c}function c(a){var b,c=new THREE.Color(a.hexColor)||16777215;switch(a.type){case"AmbientLight":b=new THREE.AmbientLight(c);break;case"DirectionalLight":b=new THREE.DirectionalLight(c,a.intensity||.5);break;case"HemisphereLight":b=new THREE.HemisphereLight(a.skyColor||16777147,a.groundColor||526368,a.intensity||1);break;case"PointLight":b=new THREE.PointLight(c,a.intensity||1,a.distance||0,a.decay||1);break;case"SpotLight":b=new THREE.SpotLight(c,a.intensity||1,a.distance||0,a.angle||Math.PI/3,a.exponent||10,a.decay||1),b.castShadow=a.castShadow||!1,b.shadow.camera.near=a.shadowCameraNear||50,b.shadow.camera.far=a.shadowCameraFar||5e3,b.shadow.camera.left=a.shadowCameraLeft||-500,b.shadow.camera.right=a.shadowCameraRight||500,b.shadow.camera.top=a.shadowCameraTop||500,b.shadow.camera.bottom=a.shadowCameraBottom||-500,a.shadowCameraVisible&&THREE.CameraHelper(b.shadow.camera),b.shadow.bias=a.shadowBias||0,b.shadow.mapSize.width=a.shadowMapWidth||512,b.shadow.mapSize.height=a.shadowMapHeight||512,b.shadowMapSize=a.shadowMapSize,b.shadowCamera=a.shadowCamera,b.shadowMatrix=a.shadowMatrix,b.shadowMap=a.shadowMap;break;default:throw new TypeError(a.type+" is not a kind of valid light")}return a.position&&b.position.set(a.position.x||0,a.position.y||0,a.position.z||0),b}var d={createCamera:a,createControls:b,createLight:c};return d}),define("solar-factory",function(){"use strict";function a(a){var b=new THREE.MoonMesh(a);return b}function b(a){var b=new THREE.PlanetMesh(a);return b}function c(a){var b=new THREE.StarMesh(a);return b}var d={createMoon:a,createPlanet:b,createStar:c};return d}),define("solar-properties",function(){"use strict";function a(a){a&&(g.width=a.width||b,g.height=a.height||c,g.bodiesSrc=a.bodiesSrc||d,g.cameraSrc=a.cameraSrc||e,g.lightsSrc=a.lightsSrc||f)}var b=null,c=null,d="data/bodies.properties.json",e="data/camera.properties.json",f="data/lights.properties.json",g={width:b,height:c,bodiesSrc:d,cameraSrc:e,lightsSrc:f,setProperties:a};return g}),define("solar-service",function(){"use strict";function a(a,b){d(a,b)}function b(a,b){d(a,b)}function c(a,b){d(a,b)}function d(a,b){$.ajax(a,{success:function(a){return b(a)},error:function(b){console.error("Load "+a+" error :"+b)}})}var e={getCamera:a,getBodies:b,getLights:c};return e}),define("solar-system",["scene-builder","scene-factory","solar-factory","solar-service","solar-properties"],function(a,b,c,d,e){"use strict";function f(b){e.setProperties(b),a.init(e),k()}function g(b){o[b.name]=b,b.orbitProperties?o[b.orbitProperties.round].addSatellite(b,b.orbitProperties):a.addObject(b)}function h(a){var b=c.createMoon(a);g(b)}function i(a){var b=c.createPlanet(a);g(b)}function j(a){var b=c.createStar(a);g(b)}function k(){d.getCamera(e.cameraSrc,l),d.getBodies(e.bodiesSrc,m),d.getLights(e.lightsSrc,n)}function l(c){var d=b.createCamera(c);a.setCamera(d);var e=b.createControls(d,c.controls);a.setControls(e),a.animate()}function m(a){a.forEach(function(a){switch(a.type){case"Star":j(a);break;case"Planet":i(a);break;case"Dwarf Planet":i(a);break;case"Moon":h(a);break;default:return void console.error(a.type+" is not considered a kind of solar body")}})}function n(c){c.forEach(function(c){var d=b.createLight(c);a.addObject(d)})}var o={},p={addMoon:h,addPlanet:i,addStar:j,init:f};return p});