threex.planets.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. var THREEx = THREEx || {}
  2. THREEx.Planets = {}
  3. THREEx.Planets.baseURL = '../'
  4. // from http://planetpixelemporium.com/
  5. THREEx.Planets.createSun = function(){
  6. var geometry = new THREE.SphereGeometry(0.5, 32, 32)
  7. var texture = THREE.ImageUtils.loadTexture(THREEx.Planets.baseURL+'images/sunmap.jpg')
  8. var material = new THREE.MeshPhongMaterial({
  9. map : texture,
  10. bumpMap : texture,
  11. bumpScale: 0.05,
  12. })
  13. var mesh = new THREE.Mesh(geometry, material)
  14. return mesh
  15. }
  16. THREEx.Planets.createMercury = function(){
  17. var geometry = new THREE.SphereGeometry(0.5, 32, 32)
  18. var material = new THREE.MeshPhongMaterial({
  19. map : THREE.ImageUtils.loadTexture(THREEx.Planets.baseURL+'images/mercurymap.jpg'),
  20. bumpMap : THREE.ImageUtils.loadTexture(THREEx.Planets.baseURL+'images/mercurybump.jpg'),
  21. bumpScale: 0.005,
  22. })
  23. var mesh = new THREE.Mesh(geometry, material)
  24. return mesh
  25. }
  26. THREEx.Planets.createVenus = function(){
  27. var geometry = new THREE.SphereGeometry(0.5, 32, 32)
  28. var material = new THREE.MeshPhongMaterial({
  29. map : THREE.ImageUtils.loadTexture(THREEx.Planets.baseURL+'images/venusmap.jpg'),
  30. bumpMap : THREE.ImageUtils.loadTexture(THREEx.Planets.baseURL+'images/venusbump.jpg'),
  31. bumpScale: 0.005,
  32. })
  33. var mesh = new THREE.Mesh(geometry, material)
  34. return mesh
  35. }
  36. THREEx.Planets.createEarth = function(){
  37. var geometry = new THREE.SphereGeometry(0.5, 32, 32)
  38. var material = new THREE.MeshPhongMaterial({
  39. map : THREE.ImageUtils.loadTexture(THREEx.Planets.baseURL+'images/earthmap1k.jpg'),
  40. bumpMap : THREE.ImageUtils.loadTexture(THREEx.Planets.baseURL+'images/earthbump1k.jpg'),
  41. bumpScale : 0.05,
  42. specularMap : THREE.ImageUtils.loadTexture(THREEx.Planets.baseURL+'images/earthspec1k.jpg'),
  43. specular : new THREE.Color('grey'),
  44. })
  45. var mesh = new THREE.Mesh(geometry, material)
  46. return mesh
  47. }
  48. THREEx.Planets.createEarthCloud = function(){
  49. // create destination canvas
  50. var canvasResult = document.createElement('canvas')
  51. canvasResult.width = 1024
  52. canvasResult.height = 512
  53. var contextResult = canvasResult.getContext('2d')
  54. // load earthcloudmap
  55. var imageMap = new Image();
  56. imageMap.addEventListener("load", function() {
  57. // create dataMap ImageData for earthcloudmap
  58. var canvasMap = document.createElement('canvas')
  59. canvasMap.width = imageMap.width
  60. canvasMap.height= imageMap.height
  61. var contextMap = canvasMap.getContext('2d')
  62. contextMap.drawImage(imageMap, 0, 0)
  63. var dataMap = contextMap.getImageData(0, 0, canvasMap.width, canvasMap.height)
  64. // load earthcloudmaptrans
  65. var imageTrans = new Image();
  66. imageTrans.addEventListener("load", function(){
  67. // create dataTrans ImageData for earthcloudmaptrans
  68. var canvasTrans = document.createElement('canvas')
  69. canvasTrans.width = imageTrans.width
  70. canvasTrans.height = imageTrans.height
  71. var contextTrans = canvasTrans.getContext('2d')
  72. contextTrans.drawImage(imageTrans, 0, 0)
  73. var dataTrans = contextTrans.getImageData(0, 0, canvasTrans.width, canvasTrans.height)
  74. // merge dataMap + dataTrans into dataResult
  75. var dataResult = contextMap.createImageData(canvasMap.width, canvasMap.height)
  76. for(var y = 0, offset = 0; y < imageMap.height; y++){
  77. for(var x = 0; x < imageMap.width; x++, offset += 4){
  78. dataResult.data[offset+0] = dataMap.data[offset+0]
  79. dataResult.data[offset+1] = dataMap.data[offset+1]
  80. dataResult.data[offset+2] = dataMap.data[offset+2]
  81. dataResult.data[offset+3] = 255 - dataTrans.data[offset+0]
  82. }
  83. }
  84. // update texture with result
  85. contextResult.putImageData(dataResult,0,0)
  86. material.map.needsUpdate = true;
  87. })
  88. imageTrans.src = THREEx.Planets.baseURL+'images/earthcloudmaptrans.jpg';
  89. }, false);
  90. imageMap.src = THREEx.Planets.baseURL+'images/earthcloudmap.jpg';
  91. var geometry = new THREE.SphereGeometry(0.51, 32, 32)
  92. var material = new THREE.MeshPhongMaterial({
  93. map : new THREE.Texture(canvasResult),
  94. side : THREE.DoubleSide,
  95. transparent : true,
  96. opacity : 0.8,
  97. })
  98. var mesh = new THREE.Mesh(geometry, material)
  99. return mesh
  100. }
  101. THREEx.Planets.createMoon = function(){
  102. var geometry = new THREE.SphereGeometry(0.5, 32, 32)
  103. var material = new THREE.MeshPhongMaterial({
  104. map : THREE.ImageUtils.loadTexture(THREEx.Planets.baseURL+'images/moonmap1k.jpg'),
  105. bumpMap : THREE.ImageUtils.loadTexture(THREEx.Planets.baseURL+'images/moonbump1k.jpg'),
  106. bumpScale: 0.002,
  107. })
  108. var mesh = new THREE.Mesh(geometry, material)
  109. return mesh
  110. }
  111. THREEx.Planets.createMars = function(){
  112. var geometry = new THREE.SphereGeometry(0.5, 32, 32)
  113. var material = new THREE.MeshPhongMaterial({
  114. map : THREE.ImageUtils.loadTexture(THREEx.Planets.baseURL+'images/marsmap1k.jpg'),
  115. bumpMap : THREE.ImageUtils.loadTexture(THREEx.Planets.baseURL+'images/marsbump1k.jpg'),
  116. bumpScale: 0.05,
  117. })
  118. var mesh = new THREE.Mesh(geometry, material)
  119. return mesh
  120. }
  121. THREEx.Planets.createJupiter = function(){
  122. var geometry = new THREE.SphereGeometry(0.5, 32, 32)
  123. var texture = THREE.ImageUtils.loadTexture(THREEx.Planets.baseURL+'images/jupitermap.jpg')
  124. var material = new THREE.MeshPhongMaterial({
  125. map : texture,
  126. bumpMap : texture,
  127. bumpScale: 0.02,
  128. })
  129. var mesh = new THREE.Mesh(geometry, material)
  130. return mesh
  131. }
  132. THREEx.Planets.createSaturn = function(){
  133. var geometry = new THREE.SphereGeometry(0.5, 32, 32)
  134. var texture = THREE.ImageUtils.loadTexture(THREEx.Planets.baseURL+'images/saturnmap.jpg')
  135. var material = new THREE.MeshPhongMaterial({
  136. map : texture,
  137. bumpMap : texture,
  138. bumpScale: 0.05,
  139. })
  140. var mesh = new THREE.Mesh(geometry, material)
  141. return mesh
  142. }
  143. THREEx.Planets.createSaturnRing = function(){
  144. // create destination canvas
  145. var canvasResult = document.createElement('canvas')
  146. canvasResult.width = 915
  147. canvasResult.height = 64
  148. var contextResult = canvasResult.getContext('2d')
  149. // load earthcloudmap
  150. var imageMap = new Image();
  151. imageMap.addEventListener("load", function() {
  152. // create dataMap ImageData for earthcloudmap
  153. var canvasMap = document.createElement('canvas')
  154. canvasMap.width = imageMap.width
  155. canvasMap.height= imageMap.height
  156. var contextMap = canvasMap.getContext('2d')
  157. contextMap.drawImage(imageMap, 0, 0)
  158. var dataMap = contextMap.getImageData(0, 0, canvasMap.width, canvasMap.height)
  159. // load earthcloudmaptrans
  160. var imageTrans = new Image();
  161. imageTrans.addEventListener("load", function(){
  162. // create dataTrans ImageData for earthcloudmaptrans
  163. var canvasTrans = document.createElement('canvas')
  164. canvasTrans.width = imageTrans.width
  165. canvasTrans.height = imageTrans.height
  166. var contextTrans = canvasTrans.getContext('2d')
  167. contextTrans.drawImage(imageTrans, 0, 0)
  168. var dataTrans = contextTrans.getImageData(0, 0, canvasTrans.width, canvasTrans.height)
  169. // merge dataMap + dataTrans into dataResult
  170. var dataResult = contextMap.createImageData(canvasResult.width, canvasResult.height)
  171. for(var y = 0, offset = 0; y < imageMap.height; y++){
  172. for(var x = 0; x < imageMap.width; x++, offset += 4){
  173. dataResult.data[offset+0] = dataMap.data[offset+0]
  174. dataResult.data[offset+1] = dataMap.data[offset+1]
  175. dataResult.data[offset+2] = dataMap.data[offset+2]
  176. dataResult.data[offset+3] = 255 - dataTrans.data[offset+0]/4
  177. }
  178. }
  179. // update texture with result
  180. contextResult.putImageData(dataResult,0,0)
  181. material.map.needsUpdate = true;
  182. })
  183. imageTrans.src = THREEx.Planets.baseURL+'images/saturnringpattern.gif';
  184. }, false);
  185. imageMap.src = THREEx.Planets.baseURL+'images/saturnringcolor.jpg';
  186. var geometry = new THREEx.Planets._RingGeometry(0.55, 0.75, 64);
  187. var material = new THREE.MeshPhongMaterial({
  188. map : new THREE.Texture(canvasResult),
  189. // map : THREE.ImageUtils.loadTexture(THREEx.Planets.baseURL+'images/ash_uvgrid01.jpg'),
  190. side : THREE.DoubleSide,
  191. transparent : true,
  192. opacity : 0.8,
  193. })
  194. var mesh = new THREE.Mesh(geometry, material)
  195. mesh.lookAt(new THREE.Vector3(0.5,-4,1))
  196. return mesh
  197. }
  198. THREEx.Planets.createUranus = function(){
  199. var geometry = new THREE.SphereGeometry(0.5, 32, 32)
  200. var texture = THREE.ImageUtils.loadTexture(THREEx.Planets.baseURL+'images/uranusmap.jpg')
  201. var material = new THREE.MeshPhongMaterial({
  202. map : texture,
  203. bumpMap : texture,
  204. bumpScale: 0.05,
  205. })
  206. var mesh = new THREE.Mesh(geometry, material)
  207. return mesh
  208. }
  209. THREEx.Planets.createUranusRing = function(){
  210. // create destination canvas
  211. var canvasResult = document.createElement('canvas')
  212. canvasResult.width = 1024
  213. canvasResult.height = 72
  214. var contextResult = canvasResult.getContext('2d')
  215. // load earthcloudmap
  216. var imageMap = new Image();
  217. imageMap.addEventListener("load", function() {
  218. // create dataMap ImageData for earthcloudmap
  219. var canvasMap = document.createElement('canvas')
  220. canvasMap.width = imageMap.width
  221. canvasMap.height= imageMap.height
  222. var contextMap = canvasMap.getContext('2d')
  223. contextMap.drawImage(imageMap, 0, 0)
  224. var dataMap = contextMap.getImageData(0, 0, canvasMap.width, canvasMap.height)
  225. // load earthcloudmaptrans
  226. var imageTrans = new Image();
  227. imageTrans.addEventListener("load", function(){
  228. // create dataTrans ImageData for earthcloudmaptrans
  229. var canvasTrans = document.createElement('canvas')
  230. canvasTrans.width = imageTrans.width
  231. canvasTrans.height = imageTrans.height
  232. var contextTrans = canvasTrans.getContext('2d')
  233. contextTrans.drawImage(imageTrans, 0, 0)
  234. var dataTrans = contextTrans.getImageData(0, 0, canvasTrans.width, canvasTrans.height)
  235. // merge dataMap + dataTrans into dataResult
  236. var dataResult = contextMap.createImageData(canvasResult.width, canvasResult.height)
  237. for(var y = 0, offset = 0; y < imageMap.height; y++){
  238. for(var x = 0; x < imageMap.width; x++, offset += 4){
  239. dataResult.data[offset+0] = dataMap.data[offset+0]
  240. dataResult.data[offset+1] = dataMap.data[offset+1]
  241. dataResult.data[offset+2] = dataMap.data[offset+2]
  242. dataResult.data[offset+3] = 255 - dataTrans.data[offset+0]/2
  243. }
  244. }
  245. // update texture with result
  246. contextResult.putImageData(dataResult,0,0)
  247. material.map.needsUpdate = true;
  248. })
  249. imageTrans.src = THREEx.Planets.baseURL+'images/uranusringtrans.gif';
  250. }, false);
  251. imageMap.src = THREEx.Planets.baseURL+'images/uranusringcolour.jpg';
  252. var geometry = new THREEx.Planets._RingGeometry(0.55, 0.75, 64);
  253. var material = new THREE.MeshPhongMaterial({
  254. map : new THREE.Texture(canvasResult),
  255. // map : THREE.ImageUtils.loadTexture(THREEx.Planets.baseURL+'images/ash_uvgrid01.jpg'),
  256. side : THREE.DoubleSide,
  257. transparent : true,
  258. opacity : 0.8,
  259. })
  260. var mesh = new THREE.Mesh(geometry, material)
  261. mesh.lookAt(new THREE.Vector3(0.5,-4,1))
  262. return mesh
  263. }
  264. THREEx.Planets.createNeptune = function(){
  265. var geometry = new THREE.SphereGeometry(0.5, 32, 32)
  266. var texture = THREE.ImageUtils.loadTexture(THREEx.Planets.baseURL+'images/neptunemap.jpg')
  267. var material = new THREE.MeshPhongMaterial({
  268. map : texture,
  269. bumpMap : texture,
  270. bumpScale: 0.05,
  271. })
  272. var mesh = new THREE.Mesh(geometry, material)
  273. return mesh
  274. }
  275. THREEx.Planets.createPluto = function(){
  276. var geometry = new THREE.SphereGeometry(0.5, 32, 32)
  277. var material = new THREE.MeshPhongMaterial({
  278. map : THREE.ImageUtils.loadTexture(THREEx.Planets.baseURL+'images/plutomap1k.jpg'),
  279. bumpMap : THREE.ImageUtils.loadTexture(THREEx.Planets.baseURL+'images/plutobump1k.jpg'),
  280. bumpScale: 0.005,
  281. })
  282. var mesh = new THREE.Mesh(geometry, material)
  283. return mesh
  284. }
  285. THREEx.Planets.createStarfield = function(){
  286. var texture = THREE.ImageUtils.loadTexture(THREEx.Planets.baseURL+'images/galaxy_starfield.png')
  287. var material = new THREE.MeshBasicMaterial({
  288. map : texture,
  289. side : THREE.BackSide
  290. })
  291. var geometry = new THREE.SphereGeometry(100, 32, 32)
  292. var mesh = new THREE.Mesh(geometry, material)
  293. return mesh
  294. }
  295. //////////////////////////////////////////////////////////////////////////////////
  296. // comment //
  297. //////////////////////////////////////////////////////////////////////////////////
  298. /**
  299. * change the original from three.js because i needed different UV
  300. *
  301. * @author Kaleb Murphy
  302. * @author jerome etienne
  303. */
  304. THREEx.Planets._RingGeometry = function ( innerRadius, outerRadius, thetaSegments ) {
  305. THREE.Geometry.call( this )
  306. innerRadius = innerRadius || 0
  307. outerRadius = outerRadius || 50
  308. thetaSegments = thetaSegments || 8
  309. var normal = new THREE.Vector3( 0, 0, 1 )
  310. for(var i = 0; i < thetaSegments; i++ ){
  311. var angleLo = (i / thetaSegments) *Math.PI*2
  312. var angleHi = ((i+1) / thetaSegments) *Math.PI*2
  313. var vertex1 = new THREE.Vector3(innerRadius * Math.cos(angleLo), innerRadius * Math.sin(angleLo), 0);
  314. var vertex2 = new THREE.Vector3(outerRadius * Math.cos(angleLo), outerRadius * Math.sin(angleLo), 0);
  315. var vertex3 = new THREE.Vector3(innerRadius * Math.cos(angleHi), innerRadius * Math.sin(angleHi), 0);
  316. var vertex4 = new THREE.Vector3(outerRadius * Math.cos(angleHi), outerRadius * Math.sin(angleHi), 0);
  317. this.vertices.push( vertex1 );
  318. this.vertices.push( vertex2 );
  319. this.vertices.push( vertex3 );
  320. this.vertices.push( vertex4 );
  321. var vertexIdx = i * 4;
  322. // Create the first triangle
  323. var face = new THREE.Face3(vertexIdx + 0, vertexIdx + 1, vertexIdx + 2, normal);
  324. var uvs = []
  325. var uv = new THREE.Vector2(0, 0)
  326. uvs.push(uv)
  327. var uv = new THREE.Vector2(1, 0)
  328. uvs.push(uv)
  329. var uv = new THREE.Vector2(0, 1)
  330. uvs.push(uv)
  331. this.faces.push(face);
  332. this.faceVertexUvs[0].push(uvs);
  333. // Create the second triangle
  334. var face = new THREE.Face3(vertexIdx + 2, vertexIdx + 1, vertexIdx + 3, normal);
  335. var uvs = []
  336. var uv = new THREE.Vector2(0, 1)
  337. uvs.push(uv)
  338. var uv = new THREE.Vector2(1, 0)
  339. uvs.push(uv)
  340. var uv = new THREE.Vector2(1, 1)
  341. uvs.push(uv)
  342. this.faces.push(face);
  343. this.faceVertexUvs[0].push(uvs);
  344. }
  345. this.computeCentroids();
  346. this.computeFaceNormals();
  347. this.boundingSphere = new THREE.Sphere( new THREE.Vector3(), outerRadius );
  348. };
  349. THREEx.Planets._RingGeometry.prototype = Object.create( THREE.Geometry.prototype );