mirror of
https://github.com/Outer-Wilds-New-Horizons/new-horizons.git
synced 2025-12-11 20:15:44 +01:00
OrbitLine stuff
This commit is contained in:
parent
15071fdb62
commit
6a5d048ba9
@ -10,7 +10,7 @@ namespace NewHorizons.Builder.Orbital
|
|||||||
{
|
{
|
||||||
static class OrbitlineBuilder
|
static class OrbitlineBuilder
|
||||||
{
|
{
|
||||||
public static void Make(GameObject body, AstroObject astroobject, bool isMoon, IPlanetConfig config)
|
public static void Make(GameObject body, NHAstroObject astroObject, bool isMoon, IPlanetConfig config)
|
||||||
{
|
{
|
||||||
GameObject orbitGO = new GameObject("Orbit");
|
GameObject orbitGO = new GameObject("Orbit");
|
||||||
orbitGO.transform.parent = body.transform;
|
orbitGO.transform.parent = body.transform;
|
||||||
@ -25,19 +25,32 @@ namespace NewHorizons.Builder.Orbital
|
|||||||
|
|
||||||
var ecc = config.Orbit.Eccentricity;
|
var ecc = config.Orbit.Eccentricity;
|
||||||
|
|
||||||
var parentGravity = astroobject.GetPrimaryBody()?.GetGravityVolume();
|
var parentGravity = astroObject.GetPrimaryBody()?.GetGravityVolume();
|
||||||
|
|
||||||
OrbitLine orbitLine;
|
OrbitLine orbitLine;
|
||||||
|
if(ecc == 0)
|
||||||
if(config.Orbit.Eccentricity == 0)
|
|
||||||
{
|
{
|
||||||
orbitLine = orbitGO.AddComponent<OrbitLine>();
|
orbitLine = new OrbitLine();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
orbitLine = orbitGO.AddComponent<TrackingOrbitLine>();
|
orbitLine = new TrackingOrbitLine();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
NHOrbitLine orbitLine = orbitGO.AddComponent<NHOrbitLine>();
|
||||||
|
|
||||||
|
var a = astroObject.SemiMajorAxis;
|
||||||
|
var e = astroObject.Eccentricity;
|
||||||
|
var b = a * Mathf.Sqrt(1f - (e * e));
|
||||||
|
var l = astroObject.LongitudeOfAscendingNode;
|
||||||
|
var p = astroObject.ArgumentOfPeriapsis;
|
||||||
|
var i = astroObject.Inclination;
|
||||||
|
|
||||||
|
orbitLine.SemiMajorAxis = a * OrbitalParameters.Rotate(Vector3.forward, l, i, p);
|
||||||
|
orbitLine.SemiMinorAxis = b * OrbitalParameters.Rotate(Vector3.left, l, i, p);
|
||||||
|
*/
|
||||||
|
|
||||||
var color = Color.white;
|
var color = Color.white;
|
||||||
if (config.Orbit.Tint != null) color = config.Orbit.Tint.ToColor32();
|
if (config.Orbit.Tint != null) color = config.Orbit.Tint.ToColor32();
|
||||||
else if (config.Star != null) color = config.Star.Tint.ToColor32();
|
else if (config.Star != null) color = config.Star.Tint.ToColor32();
|
||||||
@ -58,7 +71,7 @@ namespace NewHorizons.Builder.Orbital
|
|||||||
|
|
||||||
orbitLine._color = color;
|
orbitLine._color = color;
|
||||||
|
|
||||||
orbitLine._astroObject = astroobject;
|
orbitLine._astroObject = astroObject;
|
||||||
orbitLine._fade = fade;
|
orbitLine._fade = fade;
|
||||||
orbitLine._lineWidth = 2f;
|
orbitLine._lineWidth = 2f;
|
||||||
|
|
||||||
|
|||||||
103
NewHorizons/Components/Orbital/NHOrbitLine.cs
Normal file
103
NewHorizons/Components/Orbital/NHOrbitLine.cs
Normal file
@ -0,0 +1,103 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
namespace NewHorizons.Components.Orbital
|
||||||
|
{
|
||||||
|
public class NHOrbitLine : OrbitLine
|
||||||
|
{
|
||||||
|
public Vector3 SemiMajorAxis { get; set; }
|
||||||
|
public Vector3 SemiMinorAxis { get; set; }
|
||||||
|
|
||||||
|
private Vector3 _upAxis;
|
||||||
|
private float _fociDistance;
|
||||||
|
private Vector3[] _verts;
|
||||||
|
|
||||||
|
public override void InitializeLineRenderer()
|
||||||
|
{
|
||||||
|
base.GetComponent<LineRenderer>().positionCount = this._numVerts;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void OnValidate()
|
||||||
|
{
|
||||||
|
if (_numVerts < 0 || _numVerts > 4096)
|
||||||
|
{
|
||||||
|
_numVerts = Mathf.Clamp(_numVerts, 0, 4096);
|
||||||
|
}
|
||||||
|
if (base.GetComponent<LineRenderer>().positionCount != this._numVerts)
|
||||||
|
{
|
||||||
|
InitializeLineRenderer();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Start()
|
||||||
|
{
|
||||||
|
base.Start();
|
||||||
|
|
||||||
|
var a = SemiMajorAxis.magnitude;
|
||||||
|
var b = SemiMinorAxis.magnitude;
|
||||||
|
|
||||||
|
_upAxis = Vector3.Cross(SemiMajorAxis.normalized, SemiMinorAxis.normalized);
|
||||||
|
|
||||||
|
_fociDistance = Mathf.Sqrt(a * a - b * b);
|
||||||
|
if (float.IsNaN(_fociDistance)) _fociDistance = 0f;
|
||||||
|
|
||||||
|
_verts = new Vector3[this._numVerts];
|
||||||
|
|
||||||
|
transform.localRotation = Quaternion.Euler(270, 90, 0);
|
||||||
|
|
||||||
|
base.enabled = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Update()
|
||||||
|
{
|
||||||
|
AstroObject primary = _astroObject?.GetPrimaryBody();
|
||||||
|
|
||||||
|
// If it has nothing to orbit then why is this here
|
||||||
|
if (primary == null)
|
||||||
|
{
|
||||||
|
base.enabled = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector3 origin = primary.transform.position + SemiMajorAxis.normalized * _fociDistance;
|
||||||
|
|
||||||
|
float num = CalcProjectedAngleToCenter(origin, SemiMajorAxis, SemiMinorAxis, _astroObject.transform.position);
|
||||||
|
|
||||||
|
for (int i = 0; i < _numVerts; i++)
|
||||||
|
{
|
||||||
|
float f = (float)i / (float)(_numVerts - 1) * 3.1415927f * 2f - (num + 3.1415927f);
|
||||||
|
_verts[i] = SemiMajorAxis * Mathf.Cos(f) + SemiMinorAxis * Mathf.Sin(f);
|
||||||
|
}
|
||||||
|
_lineRenderer.SetPositions(_verts);
|
||||||
|
|
||||||
|
transform.position = origin;
|
||||||
|
transform.rotation = Quaternion.LookRotation(SemiMinorAxis, _upAxis);
|
||||||
|
|
||||||
|
float num2 = DistanceToEllipticalOrbitLine(origin, SemiMajorAxis, SemiMinorAxis, _upAxis, Locator.GetActiveCamera().transform.position);
|
||||||
|
float widthMultiplier = Mathf.Min(num2 * (_lineWidth / 1000f), _maxLineWidth);
|
||||||
|
float num3 = _fade ? (1f - Mathf.Clamp01((num2 - _fadeStartDist) / (_fadeEndDist - _fadeStartDist))) : 1f;
|
||||||
|
|
||||||
|
_lineRenderer.widthMultiplier = widthMultiplier;
|
||||||
|
_lineRenderer.startColor = new Color(_color.r, _color.g, _color.b, num3 * num3);
|
||||||
|
}
|
||||||
|
|
||||||
|
private float CalcProjectedAngleToCenter(Vector3 foci, Vector3 semiMajorAxis, Vector3 semiMinorAxis, Vector3 point)
|
||||||
|
{
|
||||||
|
Vector3 lhs = point - foci;
|
||||||
|
Vector3 vector = new Vector3(Vector3.Dot(lhs, semiMajorAxis.normalized), 0f, Vector3.Dot(lhs, semiMinorAxis.normalized));
|
||||||
|
vector.x *= semiMinorAxis.magnitude / semiMajorAxis.magnitude;
|
||||||
|
return Mathf.Atan2(vector.z, vector.x);
|
||||||
|
}
|
||||||
|
|
||||||
|
private float DistanceToEllipticalOrbitLine(Vector3 foci, Vector3 semiMajorAxis, Vector3 semiMinorAxis, Vector3 upAxis, Vector3 point)
|
||||||
|
{
|
||||||
|
float f = CalcProjectedAngleToCenter(foci, semiMajorAxis, semiMinorAxis, point);
|
||||||
|
Vector3 b = foci + SemiMajorAxis * Mathf.Cos(f) + SemiMinorAxis * Mathf.Sin(f);
|
||||||
|
return Vector3.Distance(point, b);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -90,14 +90,10 @@ namespace NewHorizons.Components.Orbital
|
|||||||
var x = semiMajorAxis * Mathf.Cos(eccentricAnomaly) - focusDistance;
|
var x = semiMajorAxis * Mathf.Cos(eccentricAnomaly) - focusDistance;
|
||||||
var y = semiMinorAxis * Mathf.Sin(eccentricAnomaly);
|
var y = semiMinorAxis * Mathf.Sin(eccentricAnomaly);
|
||||||
|
|
||||||
var R1 = Quaternion.AngleAxis(longitudeOfAscendingNode, Vector3.up);
|
|
||||||
var R2 = Quaternion.AngleAxis(inclination, Vector3.forward);
|
|
||||||
var R3 = Quaternion.AngleAxis(argumentOfPeriapsis, Vector3.up);
|
|
||||||
|
|
||||||
var n_p = new Vector2(x, y).normalized;
|
var n_p = new Vector2(x, y).normalized;
|
||||||
|
|
||||||
var dir = R1 * R2 * R3 * new Vector3(n_p.x, 0f, n_p.y).normalized;
|
var dir = Rotate(new Vector3(n_p.x, 0f, n_p.y).normalized, longitudeOfAscendingNode, inclination, argumentOfPeriapsis);
|
||||||
var up = R1 * R2 * R3 * Vector3.up;
|
var up = Rotate(Vector3.up, longitudeOfAscendingNode, inclination, argumentOfPeriapsis);
|
||||||
|
|
||||||
var pos = r * dir;
|
var pos = r * dir;
|
||||||
var vel = v * Vector3.Cross(dir, up).normalized;
|
var vel = v * Vector3.Cross(dir, up).normalized;
|
||||||
@ -107,5 +103,14 @@ namespace NewHorizons.Components.Orbital
|
|||||||
|
|
||||||
return orbitalParameters;
|
return orbitalParameters;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Vector3 Rotate(Vector3 vector, float longitudeOfAscendingNode, float inclination, float argumentOfPeriapsis)
|
||||||
|
{
|
||||||
|
var R1 = Quaternion.AngleAxis(longitudeOfAscendingNode, Vector3.up);
|
||||||
|
var R2 = Quaternion.AngleAxis(inclination, Vector3.forward);
|
||||||
|
var R3 = Quaternion.AngleAxis(argumentOfPeriapsis, Vector3.up);
|
||||||
|
|
||||||
|
return R1 * R2 * R3 * vector;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -214,29 +214,42 @@ namespace NewHorizons.Handlers
|
|||||||
var go = new GameObject(body.Config.Name.Replace(" ", "").Replace("'", "") + "_Body");
|
var go = new GameObject(body.Config.Name.Replace(" ", "").Replace("'", "") + "_Body");
|
||||||
go.SetActive(false);
|
go.SetActive(false);
|
||||||
|
|
||||||
if (body.Config.Base.GroundSize != 0) GeometryBuilder.Make(go, body.Config.Base.GroundSize);
|
if (body.Config.Base.GroundSize != 0)
|
||||||
|
{
|
||||||
|
GeometryBuilder.Make(go, body.Config.Base.GroundSize);
|
||||||
|
}
|
||||||
|
|
||||||
var atmoSize = body.Config.Atmosphere != null ? body.Config.Atmosphere.Size : 0f;
|
var atmoSize = body.Config.Atmosphere != null ? body.Config.Atmosphere.Size : 0f;
|
||||||
float sphereOfInfluence = Mathf.Max(Mathf.Max(atmoSize, 50), body.Config.Base.SurfaceSize * 2f);
|
float sphereOfInfluence = Mathf.Max(Mathf.Max(atmoSize, 50), body.Config.Base.SurfaceSize * 2f);
|
||||||
var overrideSOI = body.Config.Base.SphereOfInfluence;
|
var overrideSOI = body.Config.Base.SphereOfInfluence;
|
||||||
if (overrideSOI != 0) sphereOfInfluence = overrideSOI;
|
if (overrideSOI != 0)
|
||||||
|
{
|
||||||
|
sphereOfInfluence = overrideSOI;
|
||||||
|
}
|
||||||
|
|
||||||
var outputTuple = BaseBuilder.Make(go, primaryBody, body.Config);
|
var outputTuple = BaseBuilder.Make(go, primaryBody, body.Config);
|
||||||
var ao = (AstroObject)outputTuple.Item1;
|
var ao = outputTuple.Item1;
|
||||||
var owRigidBody = (OWRigidbody)outputTuple.Item2;
|
var owRigidBody = outputTuple.Item2;
|
||||||
|
|
||||||
GravityVolume gv = null;
|
|
||||||
if (body.Config.Base.SurfaceGravity != 0)
|
if (body.Config.Base.SurfaceGravity != 0)
|
||||||
gv = GravityBuilder.Make(go, ao, body.Config);
|
{
|
||||||
|
GravityBuilder.Make(go, ao, body.Config);
|
||||||
|
}
|
||||||
|
|
||||||
if (body.Config.Base.HasReferenceFrame)
|
if (body.Config.Base.HasReferenceFrame)
|
||||||
|
{
|
||||||
RFVolumeBuilder.Make(go, owRigidBody, sphereOfInfluence);
|
RFVolumeBuilder.Make(go, owRigidBody, sphereOfInfluence);
|
||||||
|
}
|
||||||
|
|
||||||
if (body.Config.Base.HasMapMarker)
|
if (body.Config.Base.HasMapMarker)
|
||||||
|
{
|
||||||
MarkerBuilder.Make(go, body.Config.Name, body.Config);
|
MarkerBuilder.Make(go, body.Config.Name, body.Config);
|
||||||
|
}
|
||||||
|
|
||||||
if (body.Config.Base.HasAmbientLight)
|
if (body.Config.Base.HasAmbientLight)
|
||||||
|
{
|
||||||
AmbientLightBuilder.Make(go, sphereOfInfluence);
|
AmbientLightBuilder.Make(go, sphereOfInfluence);
|
||||||
|
}
|
||||||
|
|
||||||
var sector = MakeSector.Make(go, owRigidBody, sphereOfInfluence * 2f);
|
var sector = MakeSector.Make(go, owRigidBody, sphereOfInfluence * 2f);
|
||||||
ao._rootSector = sector;
|
ao._rootSector = sector;
|
||||||
@ -244,15 +257,24 @@ namespace NewHorizons.Handlers
|
|||||||
VolumesBuilder.Make(go, body.Config.Base.SurfaceSize, sphereOfInfluence, body.Config);
|
VolumesBuilder.Make(go, body.Config.Base.SurfaceSize, sphereOfInfluence, body.Config);
|
||||||
|
|
||||||
if (body.Config.HeightMap != null)
|
if (body.Config.HeightMap != null)
|
||||||
|
{
|
||||||
HeightMapBuilder.Make(go, body.Config.HeightMap, body.Mod);
|
HeightMapBuilder.Make(go, body.Config.HeightMap, body.Mod);
|
||||||
|
}
|
||||||
|
|
||||||
if (body.Config.ProcGen != null)
|
if (body.Config.ProcGen != null)
|
||||||
|
{
|
||||||
ProcGenBuilder.Make(go, body.Config.ProcGen);
|
ProcGenBuilder.Make(go, body.Config.ProcGen);
|
||||||
|
}
|
||||||
|
|
||||||
if (body.Config.Star != null) StarLightController.AddStar(StarBuilder.Make(go, sector, body.Config.Star));
|
if (body.Config.Star != null)
|
||||||
|
{
|
||||||
|
StarLightController.AddStar(StarBuilder.Make(go, sector, body.Config.Star));
|
||||||
|
}
|
||||||
|
|
||||||
if (body.Config.FocalPoint != null)
|
if (body.Config.FocalPoint != null)
|
||||||
|
{
|
||||||
FocalPointBuilder.Make(go, ao, body.Config, body.Mod);
|
FocalPointBuilder.Make(go, ao, body.Config, body.Mod);
|
||||||
|
}
|
||||||
|
|
||||||
// Do stuff that's shared between generating new planets and updating old ones
|
// Do stuff that's shared between generating new planets and updating old ones
|
||||||
go = SharedGenerateBody(body, go, sector, owRigidBody);
|
go = SharedGenerateBody(body, go, sector, owRigidBody);
|
||||||
@ -272,11 +294,20 @@ namespace NewHorizons.Handlers
|
|||||||
Main.SystemDict[body.Config.StarSystem].SpawnPoint = SpawnPointBuilder.Make(go, body.Config.Spawn, owRigidBody);
|
Main.SystemDict[body.Config.StarSystem].SpawnPoint = SpawnPointBuilder.Make(go, body.Config.Spawn, owRigidBody);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (body.Config.Orbit.ShowOrbitLine && !body.Config.Orbit.IsStatic) OrbitlineBuilder.Make(body.Object, ao, body.Config.Orbit.IsMoon, body.Config);
|
if (body.Config.Orbit.ShowOrbitLine && !body.Config.Orbit.IsStatic)
|
||||||
|
{
|
||||||
|
OrbitlineBuilder.Make(body.Object, ao as NHAstroObject, body.Config.Orbit.IsMoon, body.Config);
|
||||||
|
}
|
||||||
|
|
||||||
if (!body.Config.Orbit.IsStatic) DetectorBuilder.Make(go, owRigidBody, primaryBody, ao, body.Config);
|
if (!body.Config.Orbit.IsStatic)
|
||||||
|
{
|
||||||
|
DetectorBuilder.Make(go, owRigidBody, primaryBody, ao, body.Config);
|
||||||
|
}
|
||||||
|
|
||||||
if (ao.GetAstroObjectName() == AstroObject.Name.CustomString) AstroObjectLocator.RegisterCustomAstroObject(ao);
|
if (ao.GetAstroObjectName() == AstroObject.Name.CustomString)
|
||||||
|
{
|
||||||
|
AstroObjectLocator.RegisterCustomAstroObject(ao);
|
||||||
|
}
|
||||||
|
|
||||||
return go;
|
return go;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user