CoordinateUtilities.cs: match shader jank

This commit is contained in:
JohnCorby 2023-04-21 19:43:44 -07:00
parent 468126b9b0
commit cfd1fda28e
4 changed files with 16 additions and 24 deletions

View File

@ -125,7 +125,7 @@ namespace NewHorizons.Builder.Body.Geometry
v.z = v2.z * Mathf.Sqrt(1f - x2 / 2f - y2 / 2f + x2 * y2 / 3f); v.z = v2.z * Mathf.Sqrt(1f - x2 / 2f - y2 / 2f + x2 * y2 / 3f);
// The shader uses real coords // The shader uses real coords
var sphericals = CoordinateUtilities.CartesianToSpherical(v, false); var sphericals = CoordinateUtilities.CartesianToSpherical(v, true);
float longitude = sphericals.x; float longitude = sphericals.x;
float latitude = sphericals.y; float latitude = sphericals.y;

View File

@ -161,7 +161,7 @@ namespace NewHorizons.Builder.Body
var superGroup = planetGO.GetComponent<ProxyShadowCasterSuperGroup>(); var superGroup = planetGO.GetComponent<ProxyShadowCasterSuperGroup>();
if (superGroup != null) cubeSphere.AddComponent<ProxyShadowCaster>()._superGroup = superGroup; if (superGroup != null) cubeSphere.AddComponent<ProxyShadowCaster>()._superGroup = superGroup;
// rotate for back compat :P // rotate for backwards compat :P
cubeSphere.transform.rotation = planetGO.transform.TransformRotation(Quaternion.Euler(0, -90, 0)); cubeSphere.transform.rotation = planetGO.transform.TransformRotation(Quaternion.Euler(0, -90, 0));
cubeSphere.transform.position = planetGO.transform.position; cubeSphere.transform.position = planetGO.transform.position;

View File

@ -98,7 +98,7 @@ namespace NewHorizons.Builder.Props
var height = radius; var height = radius;
if (heightMapTexture != null) if (heightMapTexture != null)
{ {
var sphericals = CoordinateUtilities.CartesianToSpherical(point, false); var sphericals = CoordinateUtilities.CartesianToSpherical(point, true);
float longitude = sphericals.x; float longitude = sphericals.x;
float latitude = sphericals.y; float latitude = sphericals.y;

View File

@ -5,22 +5,23 @@ namespace NewHorizons.Utility.Geometry
{ {
// Longitude and latitude are in degrees // Longitude and latitude are in degrees
// Using the phi and theta convention used on https://mathworld.wolfram.com/SphericalCoordinates.html (Mathematics not physics convention) // Using the phi and theta convention used on https://mathworld.wolfram.com/SphericalCoordinates.html (Mathematics not physics convention)
public static Vector3 CartesianToSpherical(Vector3 v, bool useUnityCoords = true) public static Vector3 CartesianToSpherical(Vector3 v, bool useShaderCoords = false)
{ {
float x, y, z; float x, y, z;
if (useUnityCoords) if (useShaderCoords)
{
// shader does some jank stuff to match standard shader
x = -v.z;
y = v.x;
z = -v.y;
}
else
{ {
// Y is up in unity // Y is up in unity
x = v.x; x = v.x;
y = v.z; y = v.z;
z = v.y; z = v.y;
} }
else
{
x = v.x;
y = v.y;
z = v.z;
}
float dist = Mathf.Sqrt(x * x + y * y + z * z); float dist = Mathf.Sqrt(x * x + y * y + z * z);
@ -33,7 +34,7 @@ namespace NewHorizons.Utility.Geometry
return new Vector3(longitude, latitude, dist); return new Vector3(longitude, latitude, dist);
} }
public static Vector3 SphericalToCartesian(Vector3 v, bool useUnityCoords = true) public static Vector3 SphericalToCartesian(Vector3 v)
{ {
var longitude = v.x; var longitude = v.x;
var latitude = v.y; var latitude = v.y;
@ -44,18 +45,9 @@ namespace NewHorizons.Utility.Geometry
float x, y, z; float x, y, z;
if (useUnityCoords) x = r * Mathf.Cos(theta) * Mathf.Sin(phi);
{ z = r * Mathf.Sin(theta) * Mathf.Sin(phi);
x = r * Mathf.Cos(theta) * Mathf.Sin(phi); y = r * Mathf.Cos(phi);
z = r * Mathf.Sin(theta) * Mathf.Sin(phi);
y = r * Mathf.Cos(phi);
}
else
{
x = r * Mathf.Cos(theta) * Mathf.Sin(phi);
y = r * Mathf.Sin(theta) * Mathf.Sin(phi);
z = r * Mathf.Cos(phi);
}
return new Vector3(x, y, z); return new Vector3(x, y, z);
} }