Skip to content

Commit

Permalink
- Planetary orbits
Browse files Browse the repository at this point in the history
- Changed Saturn Ring Material
- Fixed Saturn and Uranus Rings not rendering after scaling their size
  • Loading branch information
fatihbalsoy committed Apr 24, 2023
1 parent ab1599d commit 4a90434
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 1,017 deletions.
907 changes: 1 addition & 906 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,5 @@
"glslify": "^7.1.1",
"glslify-import-loader": "^0.1.2",
"glslify-loader": "^2.0.0"
},
"devDependencies": {}
}
}
}
91 changes: 42 additions & 49 deletions src/objects/planet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import THREE = require("three")
import GUIMovableObject from "../gui/movable_3d_object"
import * as dat from 'dat.gui'
import * as objectsJson from '../data/objects.json';
import { AstronomyClass } from "../services/astronomy";
import { AstronomyClass, CartesianCoordinates } from "../services/astronomy";
import { Astronomy } from "../services/astronomy_static";
import { distanceScale, sizeScale } from "../settings";

class Planet extends GUIMovableObject {
// ID
Expand Down Expand Up @@ -49,9 +50,6 @@ class Planet extends GUIMovableObject {
*/
realMesh: Mesh

// Distance Scale
distanceScale: number = 10000

constructor(id: string, material: Material[], geometry: SphereGeometry) {

super()
Expand Down Expand Up @@ -94,7 +92,7 @@ class Planet extends GUIMovableObject {
this.astro = bodies[this.id]

// GEOMETRY //
const radiusScale = this.radius / Planet.getJSONValue('meanRadius', 'earth')
const radiusScale = this.radius / sizeScale
this.geometry = geometry
var enlarge = 1;
// if (this.id != "moon" && this.id != "sun") {
Expand Down Expand Up @@ -159,54 +157,37 @@ class Planet extends GUIMovableObject {
// this.realMesh.rotation.y = finalSpeed
}

/**
* Places the planet in its current real-time position
*/
setPosition() {
let astroDate = new Date()
let day = Astronomy.s.DayValue(astroDate);
let helioCoords = this.astro.EclipticCartesianCoordinates(day)
let AUtoKM = 1.496e+8
this.mesh.position.z = -helioCoords.x * AUtoKM / this.distanceScale
this.mesh.position.x = -helioCoords.y * AUtoKM / this.distanceScale
this.mesh.position.y = helioCoords.z * AUtoKM / this.distanceScale
let coordinates = this.getPositionForDate(astroDate)
this.mesh.position.x = coordinates.x
this.mesh.position.y = coordinates.y
this.mesh.position.z = coordinates.z
}
/**
* Traces out the planet's orbit
*/
displayOrbit(parent: Object3D, scene: THREE.Scene) {
const curve = new THREE.CatmullRomCurve3()
console.log(this.name)
for (let i = 0; i < 365 * (this.orbitalPeriod / 365); i++) {
let currDate = new Date()
let currYear = new Date(currDate.getFullYear(), 0)
let date = new Date(currYear.setDate(i))
let pos = this.getPositionForDate(date)

// /**
// * Places the planet in an orbiting position around its parent according to its orbital period.
// * @param parent - parent object to orbit around
// * @param time - the time elapsed since start / current time in seconds
// */
// orbit(parent: Object3D, time: number) {
// // TODO: Not real-time
// let yearInSeconds = 365 * 24 * 60 * 60
// let today = Date.now()
// let fullPeriod = 2 * Math.PI
// let orbitPercent = (today / yearInSeconds) / this.orbitalPeriod
// let mult = 1 //0.000001
// let finalSpeed = fullPeriod * -orbitPercent / mult
// this.mesh.position.z = parent.position.z + Math.sin(finalSpeed) * (this.distance / this.distanceScale)
// this.mesh.position.x = parent.position.x + Math.cos(finalSpeed) * (this.distance / this.distanceScale)
// // this.mesh.position.y = parent.position.y + Math.sin(this.orbitalInclination) * Math.sin(finalSpeed) * (this.distance / this.distanceScale)
// }
curve.points[i] = new Vector3(pos.x, pos.y, pos.z)
}
const points = curve.getPoints(500)
const geometry = new THREE.BufferGeometry().setFromPoints(points)
const material = new THREE.LineBasicMaterial({ color: 0xff0000 })
const line = new THREE.Line(geometry, material)
line.renderOrder = -1

displayOrbit(parent: Object3D) {
// const curve = new THREE.EllipseCurve(
// // ax, aY
// parent.position.x, parent.position.y,
// // xRadius, yRadius
// this.distance / this.distanceScale, this.distance / this.distanceScale,
// // aStartAngle, aEndAngle
// 0, 2 * Math.PI,
// // aClockwise
// false,
// // aRotation
// 0
// );
// const points = curve.getPoints(5000);
// const ellGeometry = new THREE.BufferGeometry().setFromPoints(points);
// const ellMaterial = new THREE.LineBasicMaterial({ color: 0xff0000 });
// const ellipse = new THREE.Line(ellGeometry, ellMaterial);
// ellipse.rotateX(Math.PI / 2)
// ellipse.rotateY(this.orbitalInclination * Math.PI / 180)
// parent.add(ellipse)
scene.add(line);
}

getRadius(): number {
Expand All @@ -218,7 +199,7 @@ class Planet extends GUIMovableObject {
}

getDistanceScale(): number {
return this.distanceScale
return distanceScale
}

getMesh(): Mesh {
Expand All @@ -233,6 +214,18 @@ class Planet extends GUIMovableObject {
return this.mesh.position.x.toFixed(0) + "," + this.mesh.position.y.toFixed(0) + "," + this.mesh.position.z.toFixed(0)
}

getPositionForDate(date: Date): CartesianCoordinates {
let day = Astronomy.s.DayValue(date);
let helioCoords = this.astro.EclipticCartesianCoordinates(day)
let AUtoKM = 1.496e+8
// z,x,y
return new CartesianCoordinates(
-helioCoords.y * AUtoKM / distanceScale, // x
helioCoords.z * AUtoKM / distanceScale, // y
- helioCoords.x * AUtoKM / distanceScale, // z
)
}

static getJSONValue(key: String, planetId: String) {
return objectsJson[planetId.toLowerCase()][key]
}
Expand Down
23 changes: 12 additions & 11 deletions src/objects/saturn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { Vector3 } from "three";
import THREE = require("three");
import Earth from "./earth";
import Planet from "./planet";
import { Quality, quality } from "../settings";
import { Quality, quality, sizeScale } from "../settings";

class Saturn extends Planet {

Expand All @@ -29,37 +29,38 @@ class Saturn extends Planet {
})

//* RING MATERIAL *//
const ringMaterial = new THREE.MeshPhongMaterial({
const ringMaterial = new THREE.MeshToonMaterial({
color: new THREE.Color(0xffffff),
side: THREE.DoubleSide,
map: ringTexture,
transparent: true,
shininess: 100,
reflectivity: 100,
refractionRatio: 0.5
// opacity: 1,
// shininess: 100,
// reflectivity: 100,
// refractionRatio: 0.5
})

//? -- GEOMETRY -- ?//
const geometry = new THREE.SphereBufferGeometry(1, 64, 64)
geometry.clearGroups()
geometry.addGroup(0, Infinity, 0)


//* RING GEOMETRY + MESH *//
const eScale = Planet.getJSONValue('meanRadius', 'earth')
const ringGeometry = new THREE.RingBufferGeometry(66900 / eScale, 180000 / eScale, 96, 1)
const ringGeometry = new THREE.RingBufferGeometry(66900, 180000, 96, 1)
ringGeometry.scale(1 / sizeScale, 1 / sizeScale, 1 / sizeScale)

var ringPos = ringGeometry.attributes.position;
var ringV3 = new THREE.Vector3();
for (let i = 0; i < ringPos.count; i++) {
ringV3.fromBufferAttribute(ringPos, i);
// console.log(ringV3.length())
// 10.999999
// 28.55555
ringGeometry.attributes.uv.setXY(i, ringV3.length() < 28 ? 0 : 1, 1);

ringGeometry.attributes.uv.setXY(i, i < ringPos.count / 2 ? 0 : 1, 1);
}

const ringMesh = new THREE.Mesh(ringGeometry, ringMaterial)
ringMesh.rotation.x = 90 * Math.PI / 180
// ringMesh.renderOrder = 1000000
ringMesh.castShadow = true
ringMesh.receiveShadow = true

Expand Down
8 changes: 7 additions & 1 deletion src/objects/sun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { PointLight } from "three";
import THREE = require("three");
import Planet from "./planet";
import * as dat from 'dat.gui'
import { sizeScale } from "../settings";

class Sun extends Planet {
// Light
Expand All @@ -29,10 +30,15 @@ class Sun extends Planet {

const light = new THREE.PointLight(0xffffff, 1.35) // prev intesity: 3
light.position.set(0, 0, 0)
// light.distance = 0
light.castShadow = true
// light.shadow.radius = 1
light.shadow.camera.visible = true
light.shadow.camera.near = 0.00001
light.shadow.camera.near = 0.00000001
light.shadow.camera.far = 10000000000
light.shadow.mapSize.width = 512;
light.shadow.mapSize.height = 512;
// light.scale.set(sizeScale * 2, sizeScale * 2, sizeScale * 2)

this.light = light
this.mesh.add(this.light)
Expand Down
5 changes: 3 additions & 2 deletions src/objects/uranus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import THREE = require("three");
import Earth from "./earth";
import Planet from "./planet";
import { sizeScale } from "../settings";

class Uranus extends Planet {

Expand Down Expand Up @@ -38,8 +39,8 @@ class Uranus extends Planet {
geometry.addGroup(0, Infinity, 0)

//* RING GEOMETRY + MESH *//
const eScale = Planet.getJSONValue('meanRadius', 'earth')
const ringGeometry = new THREE.RingBufferGeometry(51149 / eScale, (51149 + 90) / eScale, 96, 1)
const ringGeometry = new THREE.RingBufferGeometry(51149, (51149 + 90), 96, 1)
ringGeometry.scale(1 / sizeScale, 1 / sizeScale, 1 / sizeScale)
const ringMesh = new THREE.Mesh(ringGeometry, ringMaterial)
ringMesh.rotation.x = 15 * Math.PI / 180

Expand Down
Loading

0 comments on commit 4a90434

Please sign in to comment.