Improved the cube sphere textures

This commit is contained in:
Nick J. Connors 2021-12-18 03:05:37 -05:00
parent 52584c278d
commit 4e01de845a
7 changed files with 67 additions and 11 deletions

View File

@ -25,6 +25,9 @@ namespace NewHorizons.Body
CreateVertices(mesh, resolution, heightMap, minHeight, maxHeight);
CreateTriangles(mesh, resolution);
mesh.RecalculateNormals();
mesh.RecalculateTangents();
return mesh;
}
@ -45,6 +48,10 @@ namespace NewHorizons.Body
for (int x = 0; x <= resolution; x++)
{
SetVertex(vertices, normals, uvs, v++, x, y, 0, resolution, heightMap, minHeight, maxHeight);
if (x == resolution / 2 && y < resolution / 2)
{
Logger.Log($"{uvs[v - 1]}");
}
}
for (int z = 1; z <= resolution; z++)
{
@ -53,6 +60,10 @@ namespace NewHorizons.Body
for (int x = resolution - 1; x >= 0; x--)
{
SetVertex(vertices, normals, uvs, v++, x, y, resolution, resolution, heightMap, minHeight, maxHeight);
if (x == resolution / 2 && y < resolution / 2)
{
Logger.Log($"{uvs[v - 1]}");
}
}
for (int z = resolution - 1; z > 0; z--)
{
@ -72,6 +83,10 @@ namespace NewHorizons.Body
for (int x = 1; x < resolution; x++)
{
SetVertex(vertices, normals, uvs, v++, x, 0, z, resolution, heightMap, minHeight, maxHeight);
if (x == resolution / 2)
{
Logger.Log($"{uvs[v - 1]}");
}
}
}
@ -92,8 +107,10 @@ namespace NewHorizons.Body
v.y = v2.y * Mathf.Sqrt(1f - x2 / 2f - z2 / 2f + x2 * z2 / 3f);
v.z = v2.z * Mathf.Sqrt(1f - x2 / 2f - y2 / 2f + x2 * y2 / 3f);
float latitude = (Mathf.Rad2Deg * Mathf.Acos(v.z / Mathf.Sqrt(v.x * v.x + v.y * v.y + v.z * v.z))) % 180f;
float longitude = (Mathf.Rad2Deg * (v.x > 0 ? Mathf.Atan(v.y / v.x) : Mathf.Atan(v.y / v.x) + Mathf.PI) + 90f) % 360f;
float latitude = (Mathf.Rad2Deg * Mathf.Acos(v.z / Mathf.Sqrt(v.x * v.x + v.y * v.y + v.z * v.z)));
float longitude = 180f;
if(v.x > 0) longitude = Mathf.Rad2Deg * Mathf.Atan(v.y / v.x) + 90f;
if(v.x < 0) longitude = Mathf.Rad2Deg * (Mathf.Atan(v.y / v.x) + Mathf.PI) + 90f;
float sampleX = heightMap.width * longitude / 360f;
float sampleY = heightMap.height * latitude / 180f;
@ -102,7 +119,10 @@ namespace NewHorizons.Body
normals[i] = v.normalized;
vertices[i] = normals[i] * (relativeHeight * (maxHeight - minHeight) + minHeight);
uvs[i] = new Vector2(sampleX / (float)heightMap.width, sampleY / (float)heightMap.height);
var uvX = sampleX / (float)heightMap.width;
var uvY = sampleY / (float)heightMap.height;
uvs[i] = new Vector2(uvX, uvY);
}
private static void CreateTriangles(Mesh mesh, int resolution)

View File

@ -72,12 +72,14 @@ namespace NewHorizons.Body.Geometry
Vector3[] normals = new Vector3[verticesToCopy.Length];
Vector2[] uvs = new Vector2[verticesToCopy.Length];
Dictionary<float, int> seamVertices = new Dictionary<float, int>();
for(int i = 0; i < verticesToCopy.Length; i++)
{
var v = verticesToCopy[i];
float latitude = (Mathf.Rad2Deg * Mathf.Acos(v.z / Mathf.Sqrt(v.x * v.x + v.y * v.y + v.z * v.z))) % 180f;
float longitude = (Mathf.Rad2Deg * (v.x > 0 ? Mathf.Atan(v.y / v.x) : Mathf.Atan(v.y / v.x) + Mathf.PI) + 90f) % 360f;
float latitude = Mathf.Repeat(Mathf.Rad2Deg * Mathf.Acos(v.z / Mathf.Sqrt(v.x * v.x + v.y * v.y + v.z * v.z)), 180f);
float longitude = Mathf.Repeat(Mathf.Rad2Deg * (v.x > 0 ? Mathf.Atan(v.y / v.x) : Mathf.Atan(v.y / v.x) + Mathf.PI) + 90f, 360f);
float sampleX = heightMap.width * longitude / 360f;
float sampleY = heightMap.height * latitude / 180f;
@ -86,7 +88,14 @@ namespace NewHorizons.Body.Geometry
newVertices[i] = verticesToCopy[i] * height;
normals[i] = v.normalized;
uvs[i] = new Vector2(sampleX / (float)heightMap.width, sampleY / (float)heightMap.height);
var x = longitude / 360f;
var y = latitude / 180f;
if (x == 0) seamVertices.Add(y, i);
uvs[i] = new Vector2(x, y);
}
mesh.vertices = newVertices;

View File

@ -12,6 +12,9 @@ namespace NewHorizons.Body
{
static class HeightMapBuilder
{
public static AssetBundle ShaderBundle;
public static Shader PlanetShader;
public static void Make(GameObject go, HeightMapModule module)
{
Texture2D heightMap, textureMap;
@ -26,24 +29,28 @@ namespace NewHorizons.Body
return;
}
/*
GameObject cubeSphere = new GameObject("CubeSphere");
cubeSphere.transform.parent = go.transform;
cubeSphere.transform.rotation = Quaternion.Euler(90, 0, 0);
Mesh mesh = CubeSphere.Build(100, heightMap, module.MinHeight, module.MaxHeight);
Mesh mesh = CubeSphere.Build(51, heightMap, module.MinHeight, module.MaxHeight);
cubeSphere.AddComponent<MeshFilter>();
cubeSphere.GetComponent<MeshFilter>().mesh = mesh;
if(ShaderBundle == null) ShaderBundle = Main.Instance.ModHelper.Assets.LoadBundle("assets/shader");
if(PlanetShader == null) PlanetShader = ShaderBundle.LoadAsset<Shader>("Assets/SphereTextureWrapper.shader");
var cubeSphereMR = cubeSphere.AddComponent<MeshRenderer>();
cubeSphereMR.material = new Material(Shader.Find("Standard"));
cubeSphereMR.material = new Material(PlanetShader);
cubeSphereMR.material.mainTexture = textureMap;
var cubeSphereMC = cubeSphere.AddComponent<MeshCollider>();
cubeSphereMC.sharedMesh = mesh;
*/
/*
GameObject icosphere = new GameObject("Icosphere");
icosphere.transform.parent = go.transform;
icosphere.transform.rotation = Quaternion.Euler(90, 0, 0);
@ -59,6 +66,7 @@ namespace NewHorizons.Body
var cubeSphereMC = icosphere.AddComponent<MeshCollider>();
cubeSphereMC.sharedMesh = mesh;
*/
}
}
}

View File

@ -25,7 +25,7 @@ namespace NewHorizons.General
GravityVolume GV = gravityGO.AddComponent<GravityVolume>();
GV.SetValue("_cutoffAcceleration", 0.1f);
GV.SetValue("_falloffType", GV.GetType().GetNestedType("FalloffType", BindingFlags.NonPublic).GetField("inverseSquared").GetValue(GV));
GV.SetValue("_falloffType", GV.GetType().GetNestedType("FalloffType", BindingFlags.NonPublic).GetField("linear").GetValue(GV));
GV.SetValue("_alignmentRadius", 0.75f * upperSurface);
GV.SetValue("_upperSurfaceRadius", lowerSurface);
GV.SetValue("_lowerSurfaceRadius", 0);

View File

@ -20,6 +20,9 @@ namespace NewHorizons.General
spawnGO.transform.localPosition = module.PlayerSpawnPoint;
// Move it up a bit more
spawnGO.transform.position = spawnGO.transform.position + spawnGO.transform.TransformDirection(Vector3.up) * 1f;
playerSpawn = spawnGO.AddComponent<SpawnPoint>();
GameObject.FindObjectOfType<PlayerSpawner>().SetInitialSpawnPoint(playerSpawn);
}

BIN
NewHorizons/assets/shader Normal file

Binary file not shown.

View File

@ -0,0 +1,16 @@
ManifestFileVersion: 0
CRC: 3655445632
Hashes:
AssetFileHash:
serializedVersion: 2
Hash: 160438a2f12bb2f4bd67ddfa0e6f2f3f
TypeTreeHash:
serializedVersion: 2
Hash: 6370d3f9de9eca57f523bce048404a46
HashAppended: 0
ClassTypes:
- Class: 48
Script: {instanceID: 0}
Assets:
- Assets/SphereTextureWrapper.shader
Dependencies: []