From f99de094c1d4af269cddc8e863f12af5a8edfcc1 Mon Sep 17 00:00:00 2001 From: FreezeDriedMangoes Date: Tue, 31 Jan 2023 21:13:31 -0500 Subject: [PATCH] spiral meshse and positions are now being cached --- .../Props/NomaiText/NomaiTextBuilder.cs | 55 +++++++++++++------ NewHorizons/Builder/Props/PropBuildManager.cs | 10 +++- NewHorizons/Builder/Props/RemoteBuilder.cs | 9 +-- NewHorizons/Handlers/PlanetCreationHandler.cs | 2 +- NewHorizons/Utility/Cache.cs | 4 +- NewHorizons/Utility/MMesh.cs | 51 +++++++++++++++++ 6 files changed, 105 insertions(+), 26 deletions(-) create mode 100644 NewHorizons/Utility/MMesh.cs diff --git a/NewHorizons/Builder/Props/NomaiText/NomaiTextBuilder.cs b/NewHorizons/Builder/Props/NomaiText/NomaiTextBuilder.cs index ccf664db..f9fb00f3 100644 --- a/NewHorizons/Builder/Props/NomaiText/NomaiTextBuilder.cs +++ b/NewHorizons/Builder/Props/NomaiText/NomaiTextBuilder.cs @@ -11,6 +11,8 @@ using Enum = System.Enum; using Logger = NewHorizons.Utility.Logger; using Random = UnityEngine.Random; using OWML.Utils; +using Newtonsoft.Json; +using System; namespace NewHorizons.Builder.Props { @@ -118,17 +120,17 @@ namespace NewHorizons.Builder.Props } } - public static GameObject Make(GameObject planetGO, Sector sector, PropModule.NomaiTextInfo info, NewHorizonsBody body, IModBehaviour mod) + public static GameObject Make(GameObject planetGO, Sector sector, PropModule.NomaiTextInfo info, NewHorizonsBody nhBody) { InitPrefabs(); - var xmlPath = File.ReadAllText(Path.Combine(mod.ModHelper.Manifest.ModFolderPath, info.xmlFile)); + var xmlPath = File.ReadAllText(Path.Combine(nhBody.Mod.ModHelper.Manifest.ModFolderPath, info.xmlFile)); switch (info.type) { case PropModule.NomaiTextInfo.NomaiTextType.Wall: { - var nomaiWallTextObj = MakeWallText(planetGO, sector, info, xmlPath, body).gameObject; + var nomaiWallTextObj = MakeWallText(planetGO, sector, info, xmlPath, nhBody).gameObject; if (!string.IsNullOrEmpty(info.rename)) { @@ -209,7 +211,7 @@ namespace NewHorizons.Builder.Props customScroll.name = _scrollPrefab.name; } - var nomaiWallText = MakeWallText(planetGO, sector, info, xmlPath, body); + var nomaiWallText = MakeWallText(planetGO, sector, info, xmlPath, nhBody); nomaiWallText.transform.parent = customScroll.transform; nomaiWallText.transform.localPosition = Vector3.zero; nomaiWallText.transform.localRotation = Quaternion.identity; @@ -570,7 +572,7 @@ namespace NewHorizons.Builder.Props } } - private static NomaiWallText MakeWallText(GameObject go, Sector sector, PropModule.NomaiTextInfo info, string xmlPath, NewHorizonsBody body) + private static NomaiWallText MakeWallText(GameObject go, Sector sector, PropModule.NomaiTextInfo info, string xmlPath, NewHorizonsBody nhBody) { GameObject nomaiWallTextObj = new GameObject("NomaiWallText"); nomaiWallTextObj.SetActive(false); @@ -592,7 +594,7 @@ namespace NewHorizons.Builder.Props // Text assets need a name to be used with VoiceMod text.name = Path.GetFileNameWithoutExtension(info.xmlFile); - BuildArcs(xmlPath, nomaiWallText, nomaiWallTextObj, info, body); + BuildArcs(xmlPath, nomaiWallText, nomaiWallTextObj, info, nhBody); AddTranslation(xmlPath); nomaiWallText._nomaiTextAsset = text; @@ -607,16 +609,26 @@ namespace NewHorizons.Builder.Props return nomaiWallText; } - internal static void BuildArcs(string xml, NomaiWallText nomaiWallText, GameObject conversationZone, PropModule.NomaiTextInfo info, NewHorizonsBody body) + internal static void BuildArcs(string xml, NomaiWallText nomaiWallText, GameObject conversationZone, PropModule.NomaiTextInfo info, NewHorizonsBody nhBody) { var dict = MakeNomaiTextDict(xml); nomaiWallText._dictNomaiTextData = dict; - RefreshArcs(nomaiWallText, conversationZone, info, body); + var cacheKey = xml.GetHashCode() + " " + JsonConvert.SerializeObject(info).GetHashCode(); + RefreshArcs(nomaiWallText, conversationZone, info, nhBody, cacheKey); } - internal static void RefreshArcs(NomaiWallText nomaiWallText, GameObject conversationZone, PropModule.NomaiTextInfo info, NewHorizonsBody body) + [Serializable] + private struct ArcCacheData + { + public MMesh mesh; + public MVector3[] skeletonPoints; + public MVector3 position; + public float zRotation; + } + + internal static void RefreshArcs(NomaiWallText nomaiWallText, GameObject conversationZone, PropModule.NomaiTextInfo info, NewHorizonsBody nhBody, string cacheKey) { var dict = nomaiWallText._dictNomaiTextData; Random.InitState(info.seed == 0 ? info.xmlFile.GetHashCode() : info.seed); @@ -629,17 +641,16 @@ namespace NewHorizons.Builder.Props return; } - var arranger = nomaiWallText.gameObject.AddComponent(); - - // Generate spiral meshes/GOs - - var cacheKey = ""; // info.tojson.stringhashcode + "-" + xmlContents.stringhashcode - if (body.Cache?[cacheKey] != null) + if (nhBody.Cache?.ContainsKey(cacheKey) ?? false) { // TODO: use cache data // return; } + var arranger = nomaiWallText.gameObject.AddComponent(); + + // Generate spiral meshes/GOs + var i = 0; foreach (var textData in dict.Values) { @@ -687,7 +698,19 @@ namespace NewHorizons.Builder.Props else arc.transform.localScale = new Vector3( 1, 1, 1); } - // TODO: body.Cache?[cacheKey] = cacheData; + // cache data + if (nhBody.Cache != null) + { + var cacheData = arranger.spirals.Select(spiralManipulator => new ArcCacheData() + { + mesh = spiralManipulator.GetComponent().sharedMesh, // TODO: create a serializable version of Mesh and pass this: spiralManipulator.GetComponent().mesh + skeletonPoints = spiralManipulator.NomaiTextLine._points.Select(v => (MVector3)v).ToArray(), + position = spiralManipulator.transform.localPosition, + zRotation = spiralManipulator.transform.localEulerAngles.z + }).ToArray(); + + nhBody.Cache[cacheKey] = cacheData; + } } internal static GameObject MakeArc(PropModule.NomaiTextArcInfo arcInfo, GameObject conversationZone, GameObject parent, int textEntryID) diff --git a/NewHorizons/Builder/Props/PropBuildManager.cs b/NewHorizons/Builder/Props/PropBuildManager.cs index d3dcbf4f..b1018554 100644 --- a/NewHorizons/Builder/Props/PropBuildManager.cs +++ b/NewHorizons/Builder/Props/PropBuildManager.cs @@ -1,6 +1,7 @@ using NewHorizons.Builder.Body; using NewHorizons.Builder.ShipLog; using NewHorizons.External.Configs; +using NewHorizons.Utility; using OWML.Common; using System; using System.Collections.Generic; @@ -10,8 +11,11 @@ namespace NewHorizons.Builder.Props { public static class PropBuildManager { - public static void Make(GameObject go, Sector sector, OWRigidbody planetBody, PlanetConfig config, IModBehaviour mod) + public static void Make(GameObject go, Sector sector, OWRigidbody planetBody, NewHorizonsBody nhBody) { + PlanetConfig config = nhBody.Config; + IModBehaviour mod = nhBody.Mod; + if (config.Props.scatter != null) { try @@ -128,7 +132,7 @@ namespace NewHorizons.Builder.Props { try { - NomaiTextBuilder.Make(go, sector, nomaiTextInfo, mod); + NomaiTextBuilder.Make(go, sector, nomaiTextInfo, nhBody); } catch (Exception ex) { @@ -205,7 +209,7 @@ namespace NewHorizons.Builder.Props { try { - RemoteBuilder.Make(go, sector, remoteInfo, mod); + RemoteBuilder.Make(go, sector, remoteInfo, nhBody); } catch (Exception ex) { diff --git a/NewHorizons/Builder/Props/RemoteBuilder.cs b/NewHorizons/Builder/Props/RemoteBuilder.cs index c8cd3334..f179cdd9 100644 --- a/NewHorizons/Builder/Props/RemoteBuilder.cs +++ b/NewHorizons/Builder/Props/RemoteBuilder.cs @@ -116,10 +116,11 @@ namespace NewHorizons.Builder.Props } } - public static void Make(GameObject go, Sector sector, PropModule.RemoteInfo info, IModBehaviour mod) + public static void Make(GameObject go, Sector sector, PropModule.RemoteInfo info, NewHorizonsBody nhBody) { InitPrefabs(); + var mod = nhBody.Mod; var id = RemoteHandler.GetPlatformID(info.id); Texture2D decal = Texture2D.whiteTexture; @@ -142,7 +143,7 @@ namespace NewHorizons.Builder.Props { try { - RemoteBuilder.MakeWhiteboard(go, sector, id, decal, info.whiteboard, mod); + RemoteBuilder.MakeWhiteboard(go, sector, id, decal, info.whiteboard, nhBody); } catch (Exception ex) { @@ -166,7 +167,7 @@ namespace NewHorizons.Builder.Props } } - public static void MakeWhiteboard(GameObject go, Sector sector, NomaiRemoteCameraPlatform.ID id, Texture2D decal, PropModule.RemoteInfo.WhiteboardInfo info, IModBehaviour mod) + public static void MakeWhiteboard(GameObject go, Sector sector, NomaiRemoteCameraPlatform.ID id, Texture2D decal, PropModule.RemoteInfo.WhiteboardInfo info, NewHorizonsBody nhBody) { var detailInfo = new PropModule.DetailInfo() { @@ -204,7 +205,7 @@ namespace NewHorizons.Builder.Props seed = textInfo.seed, type = PropModule.NomaiTextInfo.NomaiTextType.Wall, xmlFile = textInfo.xmlFile - }, mod).GetComponent(); + }, nhBody).GetComponent(); wallText._showTextOnStart = false; component._nomaiTexts[i] = wallText; } diff --git a/NewHorizons/Handlers/PlanetCreationHandler.cs b/NewHorizons/Handlers/PlanetCreationHandler.cs index 6c36fdec..ee7cbba5 100644 --- a/NewHorizons/Handlers/PlanetCreationHandler.cs +++ b/NewHorizons/Handlers/PlanetCreationHandler.cs @@ -638,7 +638,7 @@ namespace NewHorizons.Handlers if (body.Config.Props != null) { - PropBuildManager.Make(go, sector, rb, body.Config, body.Mod); + PropBuildManager.Make(go, sector, rb, body); } if (body.Config.Volumes != null) diff --git a/NewHorizons/Utility/Cache.cs b/NewHorizons/Utility/Cache.cs index 8113575e..26ef3535 100644 --- a/NewHorizons/Utility/Cache.cs +++ b/NewHorizons/Utility/Cache.cs @@ -5,7 +5,7 @@ using System.Runtime.Serialization; namespace NewHorizons.Utility { - public class Cache : Dictionary + public class Cache : Dictionary { [NonSerialized] string filepath; @@ -24,7 +24,7 @@ namespace NewHorizons.Utility public void WriteToFile() { - NewHorizons.Main.Instance.ModHelper.Storage.Save>(this, filepath); + NewHorizons.Main.Instance.ModHelper.Storage.Save>(this, filepath); } } } diff --git a/NewHorizons/Utility/MMesh.cs b/NewHorizons/Utility/MMesh.cs new file mode 100644 index 00000000..6ab39722 --- /dev/null +++ b/NewHorizons/Utility/MMesh.cs @@ -0,0 +1,51 @@ +using System.ComponentModel; +using System.Linq; +using Newtonsoft.Json; +using UnityEngine; +namespace NewHorizons.Utility +{ + [JsonObject] + public class MMesh + { + public MMesh(MVector3[] vertices, int[] triangles, MVector3[] normals, MVector2[] uv, MVector2[] uv2) + { + this.vertices = vertices; + this.triangles = triangles; + this.normals = normals; + this.uv = uv; + this.uv2 = uv2; + } + + public MVector3[] vertices; + public int[] triangles; + public MVector3[] normals; + public MVector2[] uv; + public MVector2[] uv2; + + public static implicit operator MMesh(Mesh mesh) + { + return new MMesh + ( + mesh.vertices.Select(v => (MVector3)v).ToArray(), + mesh.triangles, + mesh.normals.Select(v => (MVector3)v).ToArray(), + mesh.uv.Select(v => (MVector2)v).ToArray(), + mesh.uv2.Select(v => (MVector2)v).ToArray() + ); + } + + public static implicit operator Mesh(MMesh mmesh) + { + var mesh = new Mesh(); + + mesh.vertices = mmesh.vertices.Select(mv => (Vector3)mv).ToArray(); + mesh.triangles = mmesh.triangles; + mesh.normals = mmesh.normals.Select(mv => (Vector3)mv).ToArray(); + mesh.uv = mmesh.uv.Select(mv => (Vector2)mv).ToArray(); + mesh.uv2 = mmesh.uv2.Select(mv => (Vector2)mv).ToArray(); + mesh.RecalculateBounds(); + + return mesh; + } + } +}