Switch over to loading textures manually

This commit is contained in:
Nick J. Connors 2022-02-23 02:35:04 -05:00
parent b70107b3b9
commit 877e240d6b
15 changed files with 55 additions and 90 deletions

View File

@ -11,18 +11,18 @@ namespace NewHorizons.Atmosphere
static class CloudsBuilder
{
private static Shader _sphereShader = null;
public static void Make(GameObject body, Sector sector, AtmosphereModule atmo, IModAssets assets)
public static void Make(GameObject body, Sector sector, AtmosphereModule atmo, IModBehaviour mod)
{
Texture2D image, cap, ramp;
try
{
image = assets.GetTexture(atmo.Cloud);
image = ImageUtilities.GetTexture(mod, atmo.Cloud);
if (atmo.CloudCap == null) cap = ImageUtilities.ClearTexture(128, 128);
else cap = assets.GetTexture(atmo.CloudCap);
else cap = ImageUtilities.GetTexture(mod, atmo.CloudCap);
if (atmo.CloudRamp == null) ramp = ImageUtilities.CanvasScaled(image, 1, image.height);
else ramp = assets.GetTexture(atmo.CloudRamp);
else ramp = ImageUtilities.GetTexture(mod, atmo.CloudRamp);
}
catch (Exception e)
{

View File

@ -14,7 +14,7 @@ namespace NewHorizons.Builder.Body
{
static class AsteroidBeltBuilder
{
public static void Make(string bodyName, IPlanetConfig parentConfig, IModHelper mod)
public static void Make(string bodyName, IPlanetConfig parentConfig, IModBehaviour mod)
{
var belt = parentConfig.AsteroidBelt;

View File

@ -1,6 +1,7 @@
using NewHorizons.Body;
using NewHorizons.Body.Geometry;
using NewHorizons.External;
using NewHorizons.Utility;
using OWML.Common;
using System;
using System.Collections.Generic;
@ -16,15 +17,15 @@ namespace NewHorizons.Builder.Body
{
public static Shader PlanetShader;
public static void Make(GameObject go, HeightMapModule module, IModAssets assets)
public static void Make(GameObject go, HeightMapModule module, IModBehaviour mod)
{
Texture2D heightMap, textureMap;
try
{
if (module.HeightMap == null) heightMap = Texture2D.whiteTexture;
else heightMap = assets.GetTexture(module.HeightMap);
else heightMap = ImageUtilities.GetTexture(mod, module.HeightMap);
if (module.TextureMap == null) textureMap = Texture2D.whiteTexture;
else textureMap = assets.GetTexture(module.TextureMap);
else textureMap = ImageUtilities.GetTexture(mod, module.TextureMap);
}
catch(Exception e)
{

View File

@ -21,7 +21,7 @@ namespace NewHorizons.Builder.Body
public static Shader UnlitRingShader;
public static Shader UnlitRingShader1Pixel;
public static GameObject Make(GameObject body, RingModule ring, IModAssets assets)
public static GameObject Make(GameObject body, RingModule ring, IModBehaviour mod)
{
// Properly lit shader doesnt work yet
ring.Unlit = true;
@ -29,7 +29,7 @@ namespace NewHorizons.Builder.Body
Texture2D ringTexture;
try
{
ringTexture = assets.GetTexture(ring.Texture);
ringTexture = ImageUtilities.GetTexture(mod, ring.Texture);
}
catch (Exception e)
{

View File

@ -21,7 +21,7 @@ namespace NewHorizons.Builder.Body
private static Texture2D _colorOverTime;
public static StarController Make(GameObject body, Sector sector, StarModule starModule)
{
if (_colorOverTime == null) _colorOverTime = Main.Instance.ModHelper.Assets.GetTexture("AssetBundle/StarColorOverTime.png");
if (_colorOverTime == null) _colorOverTime = ImageUtilities.GetTexture(Main.Instance, "AssetBundle/StarColorOverTime.png");
var starGO = new GameObject("Star");
starGO.transform.parent = body.transform;

View File

@ -14,20 +14,20 @@ namespace NewHorizons.Builder.Props
{
public static class DetailBuilder
{
public static void Make(GameObject go, Sector sector, IPlanetConfig config, IModAssets assets, string uniqueModName, PropModule.DetailInfo detail)
public static void Make(GameObject go, Sector sector, IPlanetConfig config, IModBehaviour mod, string uniqueModName, PropModule.DetailInfo detail)
{
GameObject detailGO = null;
if (detail.assetBundle != null)
{
var prefab = PropBuildManager.LoadPrefab(detail.assetBundle, detail.path, uniqueModName, assets);
var prefab = PropBuildManager.LoadPrefab(detail.assetBundle, detail.path, uniqueModName, mod);
detailGO = MakeDetail(go, sector, prefab, detail.position, detail.rotation, detail.scale, detail.alignToNormal);
}
else if (detail.objFilePath != null)
{
try
{
var prefab = assets.Get3DObject(detail.objFilePath, detail.mtlFilePath);
var prefab = mod.ModHelper.Assets.Get3DObject(detail.objFilePath, detail.mtlFilePath);
prefab.SetActive(false);
detailGO = MakeDetail(go, sector, prefab, detail.position, detail.rotation, detail.scale, detail.alignToNormal);
}

View File

@ -13,11 +13,11 @@ namespace NewHorizons.Builder.Props
{
public static class DialogueBuilder
{
public static void Make(GameObject go, Sector sector, PropModule.DialogueInfo info, IModHelper mod)
public static void Make(GameObject go, Sector sector, PropModule.DialogueInfo info, IModBehaviour mod)
{
if (info.blockAfterPersistentCondition != null && PlayerData._currentGameSave.GetPersistentCondition(info.blockAfterPersistentCondition)) return;
var dialogue = MakeConversationZone(go, sector, info, mod);
var dialogue = MakeConversationZone(go, sector, info, mod.ModHelper);
if (info.remoteTriggerPosition != null) MakeRemoteDialogueTrigger(go, sector, info, dialogue);
}

View File

@ -15,17 +15,17 @@ namespace NewHorizons.Builder.Props
{
public static class PropBuildManager
{
public static void Make(GameObject go, Sector sector, IPlanetConfig config, IModHelper mod, string uniqueModName)
public static void Make(GameObject go, Sector sector, IPlanetConfig config, IModBehaviour mod, string uniqueModName)
{
if (config.Props.Scatter != null)
{
ScatterBuilder.Make(go, sector, config, mod.Assets, uniqueModName);
ScatterBuilder.Make(go, sector, config, mod, uniqueModName);
}
if(config.Props.Details != null)
{
foreach (var detail in config.Props.Details)
{
DetailBuilder.Make(go, sector, config, mod.Assets, uniqueModName, detail);
DetailBuilder.Make(go, sector, config, mod, uniqueModName, detail);
}
}
if(config.Props.Geysers != null)
@ -55,7 +55,7 @@ namespace NewHorizons.Builder.Props
}
}
public static GameObject LoadPrefab(string assetBundle, string path, string uniqueModName, IModAssets assets)
public static GameObject LoadPrefab(string assetBundle, string path, string uniqueModName, IModBehaviour mod)
{
string key = uniqueModName + "." + assetBundle;
AssetBundle bundle;
@ -66,7 +66,7 @@ namespace NewHorizons.Builder.Props
if (Main.AssetBundles.ContainsKey(key)) bundle = Main.AssetBundles[key];
else
{
bundle = assets.LoadBundle(assetBundle);
bundle = mod.ModHelper.Assets.LoadBundle(assetBundle);
Main.AssetBundles[key] = bundle;
}
}

View File

@ -14,12 +14,12 @@ namespace NewHorizons.Builder.Props
{
public static class ScatterBuilder
{
public static void Make(GameObject go, Sector sector, IPlanetConfig config, IModAssets assets, string uniqueModName)
public static void Make(GameObject go, Sector sector, IPlanetConfig config, IModBehaviour mod, string uniqueModName)
{
MakeScatter(go, config.Props.Scatter, config.Base.SurfaceSize, sector, assets, uniqueModName, config);
MakeScatter(go, config.Props.Scatter, config.Base.SurfaceSize, sector, mod, uniqueModName, config);
}
private static void MakeScatter(GameObject go, PropModule.ScatterInfo[] scatterInfo, float radius, Sector sector, IModAssets assets, string uniqueModName, IPlanetConfig config)
private static void MakeScatter(GameObject go, PropModule.ScatterInfo[] scatterInfo, float radius, Sector sector, IModBehaviour mod, string uniqueModName, IPlanetConfig config)
{
var heightMap = config.HeightMap;
@ -31,7 +31,7 @@ namespace NewHorizons.Builder.Props
{
try
{
heightMapTexture = assets.GetTexture(heightMap.HeightMap);
heightMapTexture = ImageUtilities.GetTexture(mod, heightMap.HeightMap);
}
catch (Exception) { }
}
@ -41,7 +41,7 @@ namespace NewHorizons.Builder.Props
Random.InitState(propInfo.seed);
GameObject prefab;
if (propInfo.assetBundle != null) prefab = PropBuildManager.LoadPrefab(propInfo.assetBundle, propInfo.path, uniqueModName, assets);
if (propInfo.assetBundle != null) prefab = PropBuildManager.LoadPrefab(propInfo.assetBundle, propInfo.path, uniqueModName, mod);
else prefab = GameObject.Find(propInfo.path);
for (int i = 0; i < propInfo.count; i++)
{

View File

@ -89,7 +89,7 @@ namespace NewHorizons.Builder.Props
return name;
}
public static void Make(GameObject body, Sector sector, SignalModule module, IModHelper mod)
public static void Make(GameObject body, Sector sector, SignalModule module, IModBehaviour mod)
{
foreach(var info in module.Signals)
{
@ -97,7 +97,7 @@ namespace NewHorizons.Builder.Props
}
}
public static void Make(GameObject body, Sector sector, SignalModule.SignalInfo info, IModHelper mod)
public static void Make(GameObject body, Sector sector, SignalModule.SignalInfo info, IModBehaviour mod)
{
var signalGO = new GameObject($"Signal_{info.Name}");
signalGO.SetActive(false);
@ -121,7 +121,7 @@ namespace NewHorizons.Builder.Props
{
try
{
clip = AudioUtility.LoadAudio(mod.Manifest.ModFolderPath + "/" + info.AudioFilePath);
clip = AudioUtility.LoadAudio(mod.ModHelper.Manifest.ModFolderPath + "/" + info.AudioFilePath);
//clip = mod.Assets.GetAudio(info.AudioFilePath);
}
catch(Exception e)

View File

@ -114,15 +114,13 @@ namespace NewHorizons.Components
{
if (uniqueName.Equals("SolarSystem"))
{
IModAssets assets = Main.Instance.ModHelper.Assets;
texture = assets.GetTexture("AssetBundle/hearthian system.png");
texture = ImageUtilities.GetTexture(Main.Instance, "AssetBundle/hearthian system.png");
}
else
{
IModAssets assets = Main.BodyDict[uniqueName][0].Mod.Assets;
var path = $"planets/{uniqueName}.png";
Logger.Log($"Trying to load {path}");
texture = assets.GetTexture(path);
texture = ImageUtilities.GetTexture(Main.SystemDict[uniqueName].Mod, path);
}
}
catch (Exception) { }

View File

@ -65,7 +65,7 @@ namespace NewHorizons
GlobalMessenger<DeathType>.AddListener("PlayerDeath", OnDeath);
ShaderBundle = Main.Instance.ModHelper.Assets.LoadBundle("AssetBundle/shader");
BodyDict["SolarSystem"] = new List<NewHorizonsBody>();
SystemDict["SolarSystem"] = new NewHorizonsSystem("SolarSystem", new StarSystemConfig(null), this.ModHelper);
SystemDict["SolarSystem"] = new NewHorizonsSystem("SolarSystem", new StarSystemConfig(null), this);
Tools.Patches.Apply();
Tools.WarpDrivePatches.Apply();
@ -319,7 +319,7 @@ namespace NewHorizons
heightMap.TextureMap = body.Config.Atmosphere.Cloud;
}
HeightMapBuilder.Make(titleScreenGO, heightMap, body.Mod.Assets);
HeightMapBuilder.Make(titleScreenGO, heightMap, body.Mod);
GameObject pivot = GameObject.Instantiate(GameObject.Find("Scene/Background/PlanetPivot"), GameObject.Find("Scene/Background").transform);
pivot.GetComponent<RotateTransform>()._degreesPerSecond = 10f;
@ -335,7 +335,7 @@ namespace NewHorizons
newRing.InnerRadius = size * 1.2f;
newRing.OuterRadius = size * 2f;
newRing.Texture = body.Config.Ring.Texture;
var ring = RingBuilder.Make(titleScreenGO, newRing, body.Mod.Assets);
var ring = RingBuilder.Make(titleScreenGO, newRing, body.Mod);
titleScreenGO.transform.localScale = Vector3.one * 0.8f;
}
@ -389,12 +389,12 @@ namespace NewHorizons
_currentStarSystem = config.StarSystem;
}
SystemDict.Add(config.StarSystem, new NewHorizonsSystem(config.StarSystem, starSystemConfig, mod.ModHelper));
SystemDict.Add(config.StarSystem, new NewHorizonsSystem(config.StarSystem, starSystemConfig, mod));
BodyDict.Add(config.StarSystem, new List<NewHorizonsBody>());
}
body = new NewHorizonsBody(config, mod.ModHelper);
body = new NewHorizonsBody(config, mod);
}
catch (Exception e)
{
@ -544,7 +544,7 @@ namespace NewHorizons
VolumesBuilder.Make(go, body.Config.Base.SurfaceSize, sphereOfInfluence, body.Config);
if (body.Config.HeightMap != null)
HeightMapBuilder.Make(go, body.Config.HeightMap, body.Mod.Assets);
HeightMapBuilder.Make(go, body.Config.HeightMap, body.Mod);
if (body.Config.ProcGen != null)
ProcGenBuilder.Make(go, body.Config.ProcGen);
@ -586,7 +586,7 @@ namespace NewHorizons
private GameObject SharedGenerateBody(NewHorizonsBody body, GameObject go, Sector sector, OWRigidbody rb)
{
if (body.Config.Ring != null)
RingBuilder.Make(go, body.Config.Ring, body.Mod.Assets);
RingBuilder.Make(go, body.Config.Ring, body.Mod);
if (body.Config.AsteroidBelt != null)
AsteroidBeltBuilder.Make(body.Config.Name, body.Config, body.Mod);
@ -626,7 +626,7 @@ namespace NewHorizons
if (body.Config.Atmosphere.Cloud != null)
{
CloudsBuilder.Make(go, sector, body.Config.Atmosphere, body.Mod.Assets);
CloudsBuilder.Make(go, sector, body.Config.Atmosphere, body.Mod);
SunOverrideBuilder.Make(go, sector, body.Config.Base.SurfaceSize, body.Config.Atmosphere);
}
@ -640,7 +640,7 @@ namespace NewHorizons
}
if (body.Config.Props != null)
PropBuildManager.Make(go, sector, body.Config, body.Mod, body.Mod.Manifest.UniqueName);
PropBuildManager.Make(go, sector, body.Config, body.Mod, body.Mod.ModHelper.Manifest.UniqueName);
if (body.Config.Signal != null)
SignalBuilder.Make(go, sector, body.Config.Signal, body.Mod);
@ -707,7 +707,7 @@ namespace NewHorizons
Logger.Log("Recieved API request to create planet " + (string)config["Name"], Logger.LogType.Log);
var planetConfig = new PlanetConfig(config);
var body = new NewHorizonsBody(planetConfig, mod != null ? mod.ModHelper : Main.Instance.ModHelper);
var body = new NewHorizonsBody(planetConfig, mod != null ? mod : Main.Instance);
if (!Main.BodyDict.ContainsKey(body.Config.StarSystem)) Main.BodyDict.Add(body.Config.StarSystem, new List<NewHorizonsBody>());
Main.BodyDict[body.Config.StarSystem].Add(body);

View File

@ -1,4 +1,5 @@
using System.IO;
using OWML.Common;
using System.IO;
using UnityEngine;
namespace NewHorizons.Utility
@ -74,49 +75,14 @@ namespace NewHorizons.Utility
return tex;
}
// Thank you PETERSVP
public static Texture2D Scaled(Texture2D src, int width, int height, FilterMode mode = FilterMode.Trilinear)
public static Texture2D GetTexture(IModBehaviour mod, string filename)
{
Rect texR = new Rect(0, 0, width, height);
_gpu_scale(src, width, height, mode);
//Get rendered data back to a new texture
Texture2D result = new Texture2D(width, height, TextureFormat.ARGB32, true);
result.Resize(width, height);
result.ReadPixels(texR, 0, 0, true);
return result;
}
public static void Scale(Texture2D tex, int width, int height, FilterMode mode = FilterMode.Trilinear)
{
Rect texR = new Rect(0, 0, width, height);
_gpu_scale(tex, width, height, mode);
// Update new texture
tex.Resize(width, height);
tex.ReadPixels(texR, 0, 0, true);
tex.Apply(true); //Remove this if you hate us applying textures for you :)
}
// Internal unility that renders the source texture into the RTT - the scaling method itself.
static void _gpu_scale(Texture2D src, int width, int height, FilterMode fmode)
{
//We need the source texture in VRAM because we render with it
src.filterMode = fmode;
src.Apply(true);
//Using RTT for best quality and performance. Thanks, Unity 5
RenderTexture rtt = new RenderTexture(width, height, 32);
//Set the RTT in order to render to it
Graphics.SetRenderTarget(rtt);
//Setup 2D matrix in range 0..1, so nobody needs to care about sized
GL.LoadPixelMatrix(0, 1, 1, 0);
//Then clear & draw the texture to fill the entire RTT.
GL.Clear(true, true, new Color(0, 0, 0, 0));
Graphics.DrawTexture(new Rect(0, 0, 1, 1), src);
// Copied from OWML but without the print statement lol
var path = mod.ModHelper.Manifest.ModFolderPath + filename;
var data = File.ReadAllBytes(path);
var texture = new Texture2D(2, 2);
texture.LoadImage(data);
return texture;
}
}
}

View File

@ -6,14 +6,14 @@ namespace NewHorizons.Utility
{
public class NewHorizonsBody
{
public NewHorizonsBody(IPlanetConfig config, IModHelper mod)
public NewHorizonsBody(IPlanetConfig config, IModBehaviour mod)
{
Config = config;
Mod = mod;
}
public IPlanetConfig Config;
public IModHelper Mod;
public IModBehaviour Mod;
public GameObject Object;
}

View File

@ -10,7 +10,7 @@ namespace NewHorizons.Utility
{
public class NewHorizonsSystem
{
public NewHorizonsSystem(string name, StarSystemConfig config, IModHelper mod)
public NewHorizonsSystem(string name, StarSystemConfig config, IModBehaviour mod)
{
Name = name;
Config = config;
@ -21,6 +21,6 @@ namespace NewHorizons.Utility
public SpawnModule Spawn = null;
public SpawnPoint SpawnPoint = null;
public StarSystemConfig Config;
public IModHelper Mod;
public IModBehaviour Mod;
}
}