From 38ac50652b798b60c81b86cc7c541ab10e9374c8 Mon Sep 17 00:00:00 2001 From: Nick Date: Thu, 14 Jul 2022 17:42:03 -0400 Subject: [PATCH 1/8] Use Atan2 in CoordinateUtilities --- NewHorizons/Utility/CoordinateUtilities.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/NewHorizons/Utility/CoordinateUtilities.cs b/NewHorizons/Utility/CoordinateUtilities.cs index 6d5af6ca..530e9a83 100644 --- a/NewHorizons/Utility/CoordinateUtilities.cs +++ b/NewHorizons/Utility/CoordinateUtilities.cs @@ -25,9 +25,7 @@ namespace NewHorizons.Utility float dist = Mathf.Sqrt(x * x + y * y + z * z); // theta - float longitude = 180f; - if (x > 0) longitude = Mathf.Rad2Deg * Mathf.Atan(y / x); - if (x < 0) longitude = Mathf.Rad2Deg * (Mathf.Atan(y / x) + Mathf.PI); + var longitude = Mathf.Rad2Deg * Mathf.Atan2(y, x); // phi float latitude = (Mathf.Rad2Deg * Mathf.Acos(z / dist)); From b7bb5b72179da5e2d78d5b280586c31a28165f63 Mon Sep 17 00:00:00 2001 From: Nick Date: Thu, 14 Jul 2022 17:42:10 -0400 Subject: [PATCH 2/8] Uncomment resolution --- NewHorizons/External/Modules/HeightMapModule.cs | 2 -- NewHorizons/Handlers/PlanetCreationHandler.cs | 3 --- 2 files changed, 5 deletions(-) diff --git a/NewHorizons/External/Modules/HeightMapModule.cs b/NewHorizons/External/Modules/HeightMapModule.cs index d2ef4371..945f65ed 100644 --- a/NewHorizons/External/Modules/HeightMapModule.cs +++ b/NewHorizons/External/Modules/HeightMapModule.cs @@ -33,7 +33,6 @@ namespace NewHorizons.External.Modules /// public string textureMap; - /* /// /// Resolution of the heightmap. /// Higher values means more detail but also more memory/cpu/gpu usage. @@ -42,6 +41,5 @@ namespace NewHorizons.External.Modules [Range(1 * 4, 500 * 4)] [DefaultValue(51 * 4)] public int resolution = 51 * 4; - */ } } \ No newline at end of file diff --git a/NewHorizons/Handlers/PlanetCreationHandler.cs b/NewHorizons/Handlers/PlanetCreationHandler.cs index f6e23438..8529e994 100644 --- a/NewHorizons/Handlers/PlanetCreationHandler.cs +++ b/NewHorizons/Handlers/PlanetCreationHandler.cs @@ -466,13 +466,10 @@ namespace NewHorizons.Handlers if (body.Config.HeightMap != null) { - /* // resolution = tris on edge per face // divide by 4 to account for all the way around the equator var res = body.Config.HeightMap.resolution / 4; HeightMapBuilder.Make(go, sector, body.Config.HeightMap, body.Mod, res); - */ - HeightMapBuilder.Make(go, sector, body.Config.HeightMap, body.Mod, 51); } if (body.Config.ProcGen != null) From 0be598b4e077f37cce7372d1f13eab16ec73b3f5 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Thu, 14 Jul 2022 21:43:53 +0000 Subject: [PATCH 3/8] Updated Schemas --- NewHorizons/Schemas/body_schema.json | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/NewHorizons/Schemas/body_schema.json b/NewHorizons/Schemas/body_schema.json index 61b75b12..8a8860eb 100644 --- a/NewHorizons/Schemas/body_schema.json +++ b/NewHorizons/Schemas/body_schema.json @@ -675,6 +675,14 @@ "textureMap": { "type": "string", "description": "Relative filepath to the texture used for the terrain." + }, + "resolution": { + "type": "integer", + "description": "Resolution of the heightmap.\nHigher values means more detail but also more memory/cpu/gpu usage.\nThis value will be 1:1 with the heightmap texture width, but only at the equator.", + "format": "int32", + "default": 204, + "maximum": 2000.0, + "minimum": 4.0 } } }, From 093200c60639c22d6da6fa2b5dc92242a7e88bb2 Mon Sep 17 00:00:00 2001 From: Nick Date: Thu, 14 Jul 2022 20:04:15 -0400 Subject: [PATCH 4/8] Add LOD to heightmaps --- NewHorizons/Builder/Body/HeightMapBuilder.cs | 62 ++++++++++++++----- NewHorizons/Handlers/PlanetCreationHandler.cs | 2 +- 2 files changed, 49 insertions(+), 15 deletions(-) diff --git a/NewHorizons/Builder/Body/HeightMapBuilder.cs b/NewHorizons/Builder/Body/HeightMapBuilder.cs index 6c505c04..71b9ecca 100644 --- a/NewHorizons/Builder/Body/HeightMapBuilder.cs +++ b/NewHorizons/Builder/Body/HeightMapBuilder.cs @@ -1,5 +1,7 @@ using NewHorizons.Builder.Body.Geometry; +using NewHorizons.External.Configs; using NewHorizons.External.Modules; +using NewHorizons.Handlers; using NewHorizons.Utility; using OWML.Common; using System; @@ -11,7 +13,7 @@ namespace NewHorizons.Builder.Body { public static Shader PlanetShader; - public static void Make(GameObject planetGO, Sector sector, HeightMapModule module, IModBehaviour mod, int resolution) + public static void Make(GameObject planetGO, Sector sector, HeightMapModule module, IModBehaviour mod, int resolution, bool useLOD = false) { var deleteHeightmapFlag = false; @@ -51,24 +53,35 @@ namespace NewHorizons.Builder.Body GameObject cubeSphere = new GameObject("CubeSphere"); cubeSphere.SetActive(false); cubeSphere.transform.parent = sector?.transform ?? planetGO.transform; - cubeSphere.transform.rotation = Quaternion.Euler(90, 0, 0); - - Vector3 stretch = module.stretch != null ? (Vector3)module.stretch : Vector3.one; - Mesh mesh = CubeSphere.Build(resolution, heightMap, module.minHeight, module.maxHeight, stretch); - - cubeSphere.AddComponent(); - cubeSphere.GetComponent().mesh = mesh; if (PlanetShader == null) PlanetShader = Main.NHAssetBundle.LoadAsset("Assets/Shaders/SphereTextureWrapper.shader"); - var cubeSphereMR = cubeSphere.AddComponent(); - var material = new Material(PlanetShader); - cubeSphereMR.material = material; - material.name = textureMap.name; - material.mainTexture = textureMap; + Vector3 stretch = module.stretch != null ? (Vector3)module.stretch : Vector3.one; + + var level1 = MakeLODTerrain(cubeSphere, heightMap, textureMap, module.minHeight, module.maxHeight, resolution, stretch); var cubeSphereMC = cubeSphere.AddComponent(); - cubeSphereMC.sharedMesh = mesh; + cubeSphereMC.sharedMesh = level1.gameObject.GetComponent().mesh; + + if (useLOD) + { + var level2Res = (int)Mathf.Clamp(resolution / 2f, 35, 100); + var level2 = MakeLODTerrain(cubeSphere, heightMap, textureMap, module.minHeight, module.maxHeight, level2Res, stretch); + + var LODGroup = cubeSphere.AddComponent(); + LODGroup.size = module.maxHeight; + + LODGroup.SetLODs(new LOD[] + { + new LOD(0.5f, new Renderer[] { level1 }), + new LOD(0.33f, new Renderer[] { level2 }) + }); + + level1.transform.name += "1"; + level2.transform.name += "2"; + + LODGroup.RecalculateBounds(); + } var cubeSphereSC = cubeSphere.AddComponent(); cubeSphereSC.radius = Mathf.Min(module.minHeight, module.maxHeight); @@ -86,5 +99,26 @@ namespace NewHorizons.Builder.Body // Now that we've made the mesh we can delete the heightmap texture if (deleteHeightmapFlag) ImageUtilities.DeleteTexture(mod, module.heightMap, heightMap); } + + public static MeshRenderer MakeLODTerrain(GameObject root, Texture2D heightMap, Texture2D textureMap, float minHeight, float maxHeight, int resolution, Vector3 stretch) + { + var LODCubeSphere = new GameObject("LODCubeSphere"); + + Mesh mesh = CubeSphere.Build(resolution, heightMap, minHeight, maxHeight, stretch); + + LODCubeSphere.AddComponent(); + LODCubeSphere.GetComponent().mesh = mesh; + + var cubeSphereMR = LODCubeSphere.AddComponent(); + var material = new Material(PlanetShader); + cubeSphereMR.material = material; + material.name = textureMap.name; + material.mainTexture = textureMap; + + LODCubeSphere.transform.parent = root.transform; + LODCubeSphere.transform.localPosition = Vector3.zero; + + return cubeSphereMR; + } } } diff --git a/NewHorizons/Handlers/PlanetCreationHandler.cs b/NewHorizons/Handlers/PlanetCreationHandler.cs index 8529e994..91b10b3f 100644 --- a/NewHorizons/Handlers/PlanetCreationHandler.cs +++ b/NewHorizons/Handlers/PlanetCreationHandler.cs @@ -469,7 +469,7 @@ namespace NewHorizons.Handlers // resolution = tris on edge per face // divide by 4 to account for all the way around the equator var res = body.Config.HeightMap.resolution / 4; - HeightMapBuilder.Make(go, sector, body.Config.HeightMap, body.Mod, res); + HeightMapBuilder.Make(go, sector, body.Config.HeightMap, body.Mod, res, true); } if (body.Config.ProcGen != null) From 7fdef89bf145ab088d3012290bee710938659e4d Mon Sep 17 00:00:00 2001 From: Nick Date: Fri, 15 Jul 2022 17:50:01 -0400 Subject: [PATCH 5/8] Update HeightMapBuilder.cs --- NewHorizons/Builder/Body/HeightMapBuilder.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/NewHorizons/Builder/Body/HeightMapBuilder.cs b/NewHorizons/Builder/Body/HeightMapBuilder.cs index 71b9ecca..ea40b51d 100644 --- a/NewHorizons/Builder/Body/HeightMapBuilder.cs +++ b/NewHorizons/Builder/Body/HeightMapBuilder.cs @@ -74,11 +74,11 @@ namespace NewHorizons.Builder.Body LODGroup.SetLODs(new LOD[] { new LOD(0.5f, new Renderer[] { level1 }), - new LOD(0.33f, new Renderer[] { level2 }) + new LOD(0.0f, new Renderer[] { level2 }) }); - level1.transform.name += "1"; - level2.transform.name += "2"; + level1.transform.name += "0"; + level2.transform.name += "1"; LODGroup.RecalculateBounds(); } From 5823919e4ca31f9f40e1cffe1fdae18215760826 Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Thu, 21 Jul 2022 16:09:42 -0700 Subject: [PATCH 6/8] name the gameobject instead of the transform --- NewHorizons/Builder/Body/HeightMapBuilder.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/NewHorizons/Builder/Body/HeightMapBuilder.cs b/NewHorizons/Builder/Body/HeightMapBuilder.cs index ea40b51d..fb0c1639 100644 --- a/NewHorizons/Builder/Body/HeightMapBuilder.cs +++ b/NewHorizons/Builder/Body/HeightMapBuilder.cs @@ -77,8 +77,8 @@ namespace NewHorizons.Builder.Body new LOD(0.0f, new Renderer[] { level2 }) }); - level1.transform.name += "0"; - level2.transform.name += "1"; + level1.name += "0"; + level2.name += "1"; LODGroup.RecalculateBounds(); } From 8773c43846459bf122c905fb7507912d42e3123b Mon Sep 17 00:00:00 2001 From: Nick Date: Thu, 21 Jul 2022 19:17:08 -0400 Subject: [PATCH 7/8] Lower screen size before LOD transition --- NewHorizons/Builder/Body/HeightMapBuilder.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NewHorizons/Builder/Body/HeightMapBuilder.cs b/NewHorizons/Builder/Body/HeightMapBuilder.cs index ea40b51d..4c518781 100644 --- a/NewHorizons/Builder/Body/HeightMapBuilder.cs +++ b/NewHorizons/Builder/Body/HeightMapBuilder.cs @@ -73,7 +73,7 @@ namespace NewHorizons.Builder.Body LODGroup.SetLODs(new LOD[] { - new LOD(0.5f, new Renderer[] { level1 }), + new LOD(0.33f, new Renderer[] { level1 }), new LOD(0.0f, new Renderer[] { level2 }) }); From 99f642db60b45021502314d17b18e0736db1b795 Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Thu, 21 Jul 2022 16:23:35 -0700 Subject: [PATCH 8/8] fraction moment (its okay it gets reduced to .33 repeating when compiled) --- NewHorizons/Builder/Body/HeightMapBuilder.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/NewHorizons/Builder/Body/HeightMapBuilder.cs b/NewHorizons/Builder/Body/HeightMapBuilder.cs index 1a311148..43f2d78b 100644 --- a/NewHorizons/Builder/Body/HeightMapBuilder.cs +++ b/NewHorizons/Builder/Body/HeightMapBuilder.cs @@ -73,8 +73,8 @@ namespace NewHorizons.Builder.Body LODGroup.SetLODs(new LOD[] { - new LOD(0.33f, new Renderer[] { level1 }), - new LOD(0.0f, new Renderer[] { level2 }) + new LOD(1 / 3f, new Renderer[] { level1 }), + new LOD(0, new Renderer[] { level2 }) }); level1.name += "0";