Skip to content

Commit

Permalink
Get stars working
Browse files Browse the repository at this point in the history
  • Loading branch information
typpo committed Feb 23, 2019
1 parent f61f293 commit 283e603
Show file tree
Hide file tree
Showing 14 changed files with 18,375 additions and 1,686 deletions.
110 changes: 71 additions & 39 deletions build/spacekit.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var Spacekit = (function (exports) {
init() {
const containerWidth = this._context.container.width;
const containerHeight = this._context.container.height;
this._camera = new THREE.PerspectiveCamera(75, containerWidth / containerHeight, 0.001, 100000);
this._camera = new THREE.PerspectiveCamera(50, containerWidth / containerHeight, 0.001, 100000);
}

/**
Expand Down Expand Up @@ -699,7 +699,6 @@ var Spacekit = (function (exports) {
`;

const STAR_SHADER_VERTEX = `
attribute vec3 color;
attribute float size;
varying vec3 vColor;
Expand Down Expand Up @@ -740,14 +739,45 @@ var Spacekit = (function (exports) {
return val * 15.0;
}

function hmsToDecimalRa(raHour, raMin, raSec) {
// https://astronomy.stackexchange.com/questions/24518/convert-a-decimal-into-ra-or-dec
return raHour * 15.0 + raMin / 4.0 + raSec / 240.0;
}

function hmsToDecimalDec(decDeg, decMin, decSec, isObserverBelowEquator = false) {
const posneg = isObserverBelowEquator ? -1 : 1;
return decDeg + decMin / 60.0 + posneg * decSec / 3600.0;
}

function decimalToHmsRa(decimal) {
const val = parseFloat(decimal);
const raHour = Math.trunc(val / 15.0);
const raMin = Math.trunc((val - raHour * 15.0) * 4.0);
const raSec = (val - raHour * 15.0 - raMin / 4.0) * 240.0;
return [raHour, raMin, raSec];
}

function decimalToHmsDec(decimal, isObserverBelowEquator = false) {
const val = parseFloat(decimal);
const posneg = isObserverBelowEquator ? -1 : 1;

const decDeg = Math.trunc(val);
const decMin = Math.trunc((val - posneg * decDeg) * 60.0 * posneg);
const decSec = (val - posneg * decDeg - posneg * decMin / 60.0) * 3600.0 * posneg;
return [decDeg, decMin, decSec];
}

console.log(hmsToDecimalRa(17, 45, 40.04));
console.log(hmsToDecimalDec(-29, 0, 28.1));

const J2000 = 2451545.0;

function sphericalToCartesian(ra, dec, dist) {
// See http://www.stargazing.net/kepler/rectang.html
return [
dist * Math.cos(ra) * Math.cos(dec),
dist * Math.sin(ra) * Math.cos(dec),
dist * Math.sin(ra),
dist * Math.sin(dec),
];
}

Expand Down Expand Up @@ -796,27 +826,25 @@ var Spacekit = (function (exports) {

/**
* Maps spectral class to star color
* @param spectralClass {String} Star temperature classification
* @param temperature {Number} Star temperature in Kelvin
* @return {Number} Color for star of given spectral class
*/
function getColorForStar(spectralClass) {
switch (spectralClass) {
case 'O':
return 0xc8c8ff;
case 'B':
return 0xe3e3ff;
case 'A':
return 0xffffff;
case 'F':
return 0xffffe3;
case 'G':
return 0xffffc8;
case 'K':
return 0xffe3c8;
case 'M':
return 0xffc8c8;
}
return 0xffffff;
function getColorForStar(temp) {
if (temp >= 30000) return 0x92B5FF;
if (temp >= 10000) return 0xA2C0FF;
if (temp >= 7500) return 0xd5e0ff;
if (temp >= 6000) return 0xf9f5ff;
if (temp >= 5200) return 0xffede3;
if (temp >= 3700) return 0xffdab5;
if (temp >= 2400) return 0xffb56c;
return 0xffb56c;
}

function getSizeForStar(mag) {
if (mag < 2.0) return 4;
if (mag < 4.0) return 2;
if (mag < 6.0) return 1;
return 1;
}

/**
Expand Down Expand Up @@ -852,7 +880,7 @@ var Spacekit = (function (exports) {
* @private
*/
init() {
const geometry = new THREE.SphereBufferGeometry(99000, 32, 32);
const geometry = new THREE.SphereBufferGeometry(1e10, 32, 32);

const fullTextureUrl = getFullTextureUrl(this._options.textureUrl,
this._context.options.assetPath);
Expand Down Expand Up @@ -880,9 +908,7 @@ var Spacekit = (function (exports) {
}

loadStars() {
fetch('../../src/data/bsc_short.json').then(resp => resp.json()).then((result) => {
const library = result.BSC;

fetch('../../src/data/bsc_processed.json').then(resp => resp.json()).then(library => {
const n = library.length;

const geometry = new THREE.BufferGeometry();
Expand All @@ -896,31 +922,33 @@ var Spacekit = (function (exports) {
geometry.addAttribute('size', new THREE.BufferAttribute(sizes, 1));

library.forEach((star, idx) => {
const spectralClass = star.Sp.slice(0, 1);
const [ ra, dec, temp, mag ] = star;

const raRad = rad(hoursToDeg(star.RAh));
const decRad = rad(star.DEd);
const raRad = rad(hoursToDeg(ra));
const decRad = rad(dec);

const cartesianSpherical = sphericalToCartesian(raRad, decRad, 98000);
const cartesianSpherical = sphericalToCartesian(raRad, decRad, 1e9);
const pos = equatorialToEcliptic_Cartesian(cartesianSpherical[0], cartesianSpherical[1], cartesianSpherical[2]);

positions[idx] = pos[0];
positions[idx + 1] = pos[1];
positions[idx + 2] = pos[2];
positions.set(pos, idx * 3);

const color = new THREE.Color(getColorForStar(spectralClass));
colors[idx] = color.r;
colors[idx + 1] = color.g;
colors[idx + 2] = color.b;
const color = new THREE.Color(getColorForStar(temp));
colors.set(color.toArray(), idx * 3);

sizes[idx] = Math.random() * 5;
if (idx < 1) {
sizes[idx] = 50;
colors.set([1, 0, 0], idx * 3);
} else {
sizes[idx] = getSizeForStar(mag);
}
});

const material = new THREE.ShaderMaterial({
uniforms: {},
vertexShader: STAR_SHADER_VERTEX,
fragmentShader: STAR_SHADER_FRAGMENT,
transparent: true,
vertexColors: THREE.VertexColors,
});

this._stars = new THREE.Points(geometry, material);
Expand All @@ -936,7 +964,7 @@ var Spacekit = (function (exports) {
* @return {THREE.Object} Skybox mesh
*/
get3jsObjects() {
return [this._mesh, this._stars];
return [/*this._mesh,*/ this._stars];
}

/**
Expand Down Expand Up @@ -2401,6 +2429,10 @@ var Spacekit = (function (exports) {
exports.rad = rad;
exports.deg = deg;
exports.hoursToDeg = hoursToDeg;
exports.hmsToDecimalRa = hmsToDecimalRa;
exports.hmsToDecimalDec = hmsToDecimalDec;
exports.decimalToHmsRa = decimalToHmsRa;
exports.decimalToHmsDec = decimalToHmsDec;

return exports;

Expand Down
6 changes: 4 additions & 2 deletions examples/roadster/main.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// Create the visualization and put it in our div.
const viz = new Spacekit.Simulation(document.getElementById('main-container'), {
assetPath: '../../src/assets',
jd: 2458461.459,
//jd: 2458461.459,
startDate: new Date(2019, 5, 1),
startPaused: true,
debug: {
showAxesHelper: true,
showAxes: true,
showStats: true,
},
});
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
astropy==-2.0.11
2 changes: 1 addition & 1 deletion src/Camera.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class Camera {
init() {
const containerWidth = this._context.container.width;
const containerHeight = this._context.container.height;
this._camera = new THREE.PerspectiveCamera(75, containerWidth / containerHeight, 0.001, 100000);
this._camera = new THREE.PerspectiveCamera(50, containerWidth / containerHeight, 0.001, 100000);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Coordinates.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export function sphericalToCartesian(ra, dec, dist) {
return [
dist * Math.cos(ra) * Math.cos(dec),
dist * Math.sin(ra) * Math.cos(dec),
dist * Math.sin(ra),
dist * Math.sin(dec),
];
}

Expand Down
70 changes: 34 additions & 36 deletions src/Skybox.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,25 @@ import { sphericalToCartesian, equatorialToEcliptic_Cartesian } from './Coordina

/**
* Maps spectral class to star color
* @param spectralClass {String} Star temperature classification
* @param temperature {Number} Star temperature in Kelvin
* @return {Number} Color for star of given spectral class
*/
function getColorForStar(spectralClass) {
switch (spectralClass) {
case 'O':
return 0xc8c8ff;
case 'B':
return 0xe3e3ff;
case 'A':
return 0xffffff;
case 'F':
return 0xffffe3;
case 'G':
return 0xffffc8;
case 'K':
return 0xffe3c8;
case 'M':
return 0xffc8c8;
}
return 0xffffff;
function getColorForStar(temp) {
if (temp >= 30000) return 0x92B5FF;
if (temp >= 10000) return 0xA2C0FF;
if (temp >= 7500) return 0xd5e0ff;
if (temp >= 6000) return 0xf9f5ff;
if (temp >= 5200) return 0xffede3;
if (temp >= 3700) return 0xffdab5;
if (temp >= 2400) return 0xffb56c;
return 0xffb56c;
}

function getSizeForStar(mag) {
if (mag < 2.0) return 4;
if (mag < 4.0) return 2;
if (mag < 6.0) return 1;
return 1;
}

/**
Expand Down Expand Up @@ -65,7 +63,7 @@ export class Skybox {
* @private
*/
init() {
const geometry = new THREE.SphereBufferGeometry(99000, 32, 32);
const geometry = new THREE.SphereBufferGeometry(1e10, 32, 32);

const fullTextureUrl = getFullTextureUrl(this._options.textureUrl,
this._context.options.assetPath);
Expand Down Expand Up @@ -93,9 +91,7 @@ export class Skybox {
}

loadStars() {
fetch('../../src/data/bsc_short.json').then(resp => resp.json()).then((result) => {
const library = result.BSC;

fetch('../../src/data/bsc_processed.json').then(resp => resp.json()).then(library => {
const n = library.length;

const geometry = new THREE.BufferGeometry();
Expand All @@ -109,31 +105,33 @@ export class Skybox {
geometry.addAttribute('size', new THREE.BufferAttribute(sizes, 1));

library.forEach((star, idx) => {
const spectralClass = star.Sp.slice(0, 1);
const [ ra, dec, temp, mag ] = star;

const raRad = rad(hoursToDeg(star.RAh));
const decRad = rad(star.DEd);
const raRad = rad(hoursToDeg(ra));
const decRad = rad(dec);

const cartesianSpherical = sphericalToCartesian(raRad, decRad, 98000);
const cartesianSpherical = sphericalToCartesian(raRad, decRad, 1e9);
const pos = equatorialToEcliptic_Cartesian(cartesianSpherical[0], cartesianSpherical[1], cartesianSpherical[2]);

positions[idx] = pos[0];
positions[idx + 1] = pos[1];
positions[idx + 2] = pos[2];
positions.set(pos, idx * 3);

const color = new THREE.Color(getColorForStar(spectralClass));
colors[idx] = color.r;
colors[idx + 1] = color.g;
colors[idx + 2] = color.b;
const color = new THREE.Color(getColorForStar(temp));
colors.set(color.toArray(), idx * 3);

sizes[idx] = Math.random() * 5;
if (idx < 1) {
sizes[idx] = 50;
colors.set([1, 0, 0], idx * 3);
} else {
sizes[idx] = getSizeForStar(mag);
}
});

const material = new THREE.ShaderMaterial({
uniforms: {},
vertexShader: STAR_SHADER_VERTEX,
fragmentShader: STAR_SHADER_FRAGMENT,
transparent: true,
vertexColors: THREE.VertexColors,
});

this._stars = new THREE.Points(geometry, material);
Expand All @@ -149,7 +147,7 @@ export class Skybox {
* @return {THREE.Object} Skybox mesh
*/
get3jsObjects() {
return [this._mesh, this._stars];
return [/*this._mesh,*/ this._stars];
}

/**
Expand Down
31 changes: 31 additions & 0 deletions src/Units.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,34 @@ export function deg(val) {
export function hoursToDeg(val) {
return val * 15.0;
}

export function hmsToDecimalRa(raHour, raMin, raSec) {
// https://astronomy.stackexchange.com/questions/24518/convert-a-decimal-into-ra-or-dec
return raHour * 15.0 + raMin / 4.0 + raSec / 240.0;
}

export function hmsToDecimalDec(decDeg, decMin, decSec, isObserverBelowEquator = false) {
const posneg = isObserverBelowEquator ? -1 : 1;
return decDeg + decMin / 60.0 + posneg * decSec / 3600.0;
}

export function decimalToHmsRa(decimal) {
const val = parseFloat(decimal);
const raHour = Math.trunc(val / 15.0);
const raMin = Math.trunc((val - raHour * 15.0) * 4.0);
const raSec = (val - raHour * 15.0 - raMin / 4.0) * 240.0;
return [raHour, raMin, raSec];
}

export function decimalToHmsDec(decimal, isObserverBelowEquator = false) {
const val = parseFloat(decimal);
const posneg = isObserverBelowEquator ? -1 : 1;

const decDeg = Math.trunc(val);
const decMin = Math.trunc((val - posneg * decDeg) * 60.0 * posneg);
const decSec = (val - posneg * decDeg - posneg * decMin / 60.0) * 3600.0 * posneg;
return [decDeg, decMin, decSec];
}

console.log(hmsToDecimalRa(17, 45, 40.04))
console.log(hmsToDecimalDec(-29, 0, 28.1))
Binary file added src/assets/skybox/eso_lite.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 283e603

Please sign in to comment.