spiral meshse and positions are now being cached

This commit is contained in:
FreezeDriedMangoes 2023-01-31 21:13:31 -05:00
parent 57b4f32bbc
commit f99de094c1
6 changed files with 105 additions and 26 deletions

View File

@ -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<NomaiTextArcArranger>();
// 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<NomaiTextArcArranger>();
// 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<MeshFilter>().sharedMesh, // TODO: create a serializable version of Mesh and pass this: spiralManipulator.GetComponent<MeshFilter>().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)

View File

@ -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)
{

View File

@ -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<NomaiWallText>();
}, nhBody).GetComponent<NomaiWallText>();
wallText._showTextOnStart = false;
component._nomaiTexts[i] = wallText;
}

View File

@ -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)

View File

@ -5,7 +5,7 @@ using System.Runtime.Serialization;
namespace NewHorizons.Utility
{
public class Cache : Dictionary<string, ISerializable>
public class Cache : Dictionary<string, object>
{
[NonSerialized] string filepath;
@ -24,7 +24,7 @@ namespace NewHorizons.Utility
public void WriteToFile()
{
NewHorizons.Main.Instance.ModHelper.Storage.Save<Dictionary<string, ISerializable>>(this, filepath);
NewHorizons.Main.Instance.ModHelper.Storage.Save<Dictionary<string, object>>(this, filepath);
}
}
}

View File

@ -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;
}
}
}