diff --git a/NewHorizons/Builder/Atmosphere/CloudsBuilder.cs b/NewHorizons/Builder/Atmosphere/CloudsBuilder.cs index 5c968483..898529b6 100644 --- a/NewHorizons/Builder/Atmosphere/CloudsBuilder.cs +++ b/NewHorizons/Builder/Atmosphere/CloudsBuilder.cs @@ -14,83 +14,13 @@ namespace NewHorizons.Builder.Atmosphere private static Material[] _gdCloudMaterials; public static void Make(GameObject planetGO, Sector sector, AtmosphereModule atmo, IModBehaviour mod) { - Texture2D image, cap, ramp; - - try - { - image = ImageUtilities.GetTexture(mod, atmo.Cloud); - - if (atmo.CloudCap == null) cap = ImageUtilities.ClearTexture(128, 128); - else cap = ImageUtilities.GetTexture(mod, atmo.CloudCap); - if (atmo.CloudRamp == null) ramp = ImageUtilities.CanvasScaled(image, 1, image.height); - else ramp = ImageUtilities.GetTexture(mod, atmo.CloudRamp); - } - catch (Exception e) - { - Logger.LogError($"Couldn't load Cloud textures, {e.Message}, {e.StackTrace}"); - return; - } - Color cloudTint = atmo.CloudTint == null ? Color.white : (Color)atmo.CloudTint.ToColor32(); GameObject cloudsMainGO = new GameObject("Clouds"); cloudsMainGO.SetActive(false); cloudsMainGO.transform.parent = sector?.transform ?? planetGO.transform; - GameObject cloudsTopGO = new GameObject("TopClouds"); - cloudsTopGO.SetActive(false); - cloudsTopGO.transform.parent = cloudsMainGO.transform; - cloudsTopGO.transform.localScale = Vector3.one * atmo.Size; - - MeshFilter topMF = cloudsTopGO.AddComponent(); - topMF.mesh = GameObject.Find("CloudsTopLayer_GD").GetComponent().mesh; - - MeshRenderer topMR = cloudsTopGO.AddComponent(); - - if (_sphereShader == null) _sphereShader = Main.NHAssetBundle.LoadAsset("Assets/Shaders/SphereTextureWrapper.shader"); - if (_gdCloudMaterials == null) _gdCloudMaterials = GameObject.Find("CloudsTopLayer_GD").GetComponent().sharedMaterials; - var tempArray = new Material[2]; - - if (atmo.UseBasicCloudShader) - { - var material = new Material(_sphereShader); - if (!atmo.ShadowsOnClouds) material.renderQueue = 2550; - material.name = atmo.ShadowsOnClouds ? "BasicShadowCloud" : "BasicCloud"; - - tempArray[0] = material; - } - else - { - var material = new Material(_gdCloudMaterials[0]); - if (!atmo.ShadowsOnClouds) material.renderQueue = 2550; - material.name = atmo.ShadowsOnClouds ? "AdvancedShadowCloud" : "AdvancedCloud"; - tempArray[0] = material; - } - - // This is the stencil material for the fog under the clouds - tempArray[1] = new Material(_gdCloudMaterials[1]); - topMR.sharedMaterials = tempArray; - - foreach (var material in topMR.sharedMaterials) - { - material.SetColor("_Color", cloudTint); - material.SetColor("_TintColor", cloudTint); - - material.SetTexture("_MainTex", image); - material.SetTexture("_RampTex", ramp); - material.SetTexture("_CapTex", cap); - } - - if(!atmo.ShadowsOnClouds) - { - cloudsTopGO.layer = LayerMask.NameToLayer("IgnoreSun"); - } - - RotateTransform topRT = cloudsTopGO.AddComponent(); - // Idk why but the axis is weird - topRT._localAxis = atmo.UseBasicCloudShader ? Vector3.forward : Vector3.up; - topRT._degreesPerSecond = 10; - topRT._randomizeRotationRate = false; + MakeTopClouds(cloudsMainGO, atmo, mod); GameObject cloudsBottomGO = new GameObject("BottomClouds"); cloudsBottomGO.SetActive(false); @@ -104,7 +34,7 @@ namespace NewHorizons.Builder.Atmosphere // If they set a colour apply it to all the materials else keep the default QM one if (atmo.CloudTint != null) { - var bottomColor = atmo.CloudTint.ToColor32(); + var bottomColor = cloudTint; var bottomTSRTempArray = new Material[2]; @@ -170,12 +100,93 @@ namespace NewHorizons.Builder.Atmosphere cloudsMainGO.transform.position = planetGO.transform.TransformPoint(Vector3.zero); cloudsBottomGO.transform.position = planetGO.transform.TransformPoint(Vector3.zero); cloudsFluidGO.transform.position = planetGO.transform.TransformPoint(Vector3.zero); - cloudsTopGO.transform.position = planetGO.transform.TransformPoint(Vector3.zero); - cloudsTopGO.SetActive(true); cloudsBottomGO.SetActive(true); cloudsFluidGO.SetActive(true); cloudsMainGO.SetActive(true); } + + public static GameObject MakeTopClouds(GameObject rootObject, AtmosphereModule atmo, IModBehaviour mod) + { + Color cloudTint = atmo.CloudTint == null ? Color.white : (Color)atmo.CloudTint.ToColor32(); + + Texture2D image, cap, ramp; + + try + { + image = ImageUtilities.GetTexture(mod, atmo.Cloud); + + if (atmo.CloudCap == null) cap = ImageUtilities.ClearTexture(128, 128); + else cap = ImageUtilities.GetTexture(mod, atmo.CloudCap); + if (atmo.CloudRamp == null) ramp = ImageUtilities.CanvasScaled(image, 1, image.height); + else ramp = ImageUtilities.GetTexture(mod, atmo.CloudRamp); + } + catch (Exception e) + { + Logger.LogError($"Couldn't load Cloud textures, {e.Message}, {e.StackTrace}"); + return null; + } + + GameObject cloudsTopGO = new GameObject("TopClouds"); + cloudsTopGO.SetActive(false); + cloudsTopGO.transform.parent = rootObject.transform; + cloudsTopGO.transform.localScale = Vector3.one * atmo.Size; + + MeshFilter topMF = cloudsTopGO.AddComponent(); + topMF.mesh = GameObject.Find("CloudsTopLayer_GD").GetComponent().mesh; + + MeshRenderer topMR = cloudsTopGO.AddComponent(); + + if (_sphereShader == null) _sphereShader = Main.NHAssetBundle.LoadAsset("Assets/Shaders/SphereTextureWrapper.shader"); + if (_gdCloudMaterials == null) _gdCloudMaterials = GameObject.Find("CloudsTopLayer_GD").GetComponent().sharedMaterials; + var tempArray = new Material[2]; + + if (atmo.UseBasicCloudShader) + { + var material = new Material(_sphereShader); + if (!atmo.ShadowsOnClouds) material.renderQueue = 2550; + material.name = atmo.ShadowsOnClouds ? "BasicShadowCloud" : "BasicCloud"; + + tempArray[0] = material; + } + else + { + var material = new Material(_gdCloudMaterials[0]); + if (!atmo.ShadowsOnClouds) material.renderQueue = 2550; + material.name = atmo.ShadowsOnClouds ? "AdvancedShadowCloud" : "AdvancedCloud"; + tempArray[0] = material; + } + + // This is the stencil material for the fog under the clouds + tempArray[1] = new Material(_gdCloudMaterials[1]); + topMR.sharedMaterials = tempArray; + + foreach (var material in topMR.sharedMaterials) + { + material.SetColor("_Color", cloudTint); + material.SetColor("_TintColor", cloudTint); + + material.SetTexture("_MainTex", image); + material.SetTexture("_RampTex", ramp); + material.SetTexture("_CapTex", cap); + } + + if (!atmo.ShadowsOnClouds) + { + cloudsTopGO.layer = LayerMask.NameToLayer("IgnoreSun"); + } + + RotateTransform topRT = cloudsTopGO.AddComponent(); + // Idk why but the axis is weird + topRT._localAxis = atmo.UseBasicCloudShader ? Vector3.forward : Vector3.up; + topRT._degreesPerSecond = 10; + topRT._randomizeRotationRate = false; + + cloudsTopGO.transform.localPosition = Vector3.zero; + + cloudsTopGO.SetActive(true); + + return cloudsTopGO; + } } } diff --git a/NewHorizons/Builder/Body/HeightMapBuilder.cs b/NewHorizons/Builder/Body/HeightMapBuilder.cs index 88d02e83..b0034f88 100644 --- a/NewHorizons/Builder/Body/HeightMapBuilder.cs +++ b/NewHorizons/Builder/Body/HeightMapBuilder.cs @@ -17,7 +17,7 @@ namespace NewHorizons.Builder.Body { public static Shader PlanetShader; - public static void Make(GameObject planetGO, Sector sector, HeightMapModule module, IModBehaviour mod) + public static void Make(GameObject planetGO, Sector sector, HeightMapModule module, IModBehaviour mod, int resolution = 51) { Texture2D heightMap, textureMap; try @@ -39,7 +39,7 @@ namespace NewHorizons.Builder.Body cubeSphere.transform.rotation = Quaternion.Euler(90, 0, 0); Vector3 stretch = module.Stretch != null ? (Vector3)module.Stretch : Vector3.one; - Mesh mesh = CubeSphere.Build(51, heightMap, module.MinHeight, module.MaxHeight, stretch); + Mesh mesh = CubeSphere.Build(resolution, heightMap, module.MinHeight, module.MaxHeight, stretch); cubeSphere.AddComponent(); cubeSphere.GetComponent().mesh = mesh; diff --git a/NewHorizons/Builder/Body/ProxyBuilder.cs b/NewHorizons/Builder/Body/ProxyBuilder.cs index 56234629..c6d0c803 100644 --- a/NewHorizons/Builder/Body/ProxyBuilder.cs +++ b/NewHorizons/Builder/Body/ProxyBuilder.cs @@ -1,4 +1,5 @@ -using NewHorizons.Components; +using NewHorizons.Builder.Atmosphere; +using NewHorizons.Components; using NewHorizons.Utility; using OWML.Common; using UnityEngine; @@ -10,7 +11,7 @@ namespace NewHorizons.Builder.Body public static void Make(GameObject gameObject, NewHorizonsBody body) { var proxyName = $"{body.Config.Name}_Proxy"; - + // Stars don't work yet so uh /* if (body.Config.Star != null) @@ -30,23 +31,62 @@ namespace NewHorizons.Builder.Body }); } */ - // TODO: Make it not do it on vanilla bodies - var newModel = GameObject.CreatePrimitive(PrimitiveType.Sphere); - Object.Destroy(newModel.GetComponent()); + var newProxy = new GameObject(proxyName); - newModel.transform.SetParent(newProxy.transform); - newModel.transform.position = Vector3.zero; - newModel.transform.localScale = 1000 * Vector3.one; - newModel.transform.rotation = Quaternion.identity; + + // We want to take the largest size I think + var realSize = body.Config.Base.SurfaceSize; + + if (body.Config.HeightMap != null) + { + HeightMapBuilder.Make(newProxy, null, body.Config.HeightMap, body.Mod, 20); + if(realSize < body.Config.HeightMap.MaxHeight) realSize = body.Config.HeightMap.MaxHeight; + } + if (body.Config.Base.GroundSize != 0) + { + GeometryBuilder.Make(newProxy, null, body.Config.Base.GroundSize); + if (realSize < body.Config.Base.GroundSize) realSize = body.Config.Base.GroundSize; + } + if (body.Config.Atmosphere != null) + { + CloudsBuilder.MakeTopClouds(newProxy, body.Config.Atmosphere, body.Mod); + if (realSize < body.Config.Atmosphere.Size) realSize = body.Config.Atmosphere.Size; + } + if (body.Config.Ring != null) + { + RingBuilder.MakeRingGraphics(newProxy, null, body.Config.Ring, body.Mod); + if (realSize < body.Config.Ring.OuterRadius) realSize = body.Config.Ring.OuterRadius; + } + if (body.Config.Star != null) + { + StarBuilder.MakeStarGraphics(newProxy, null, body.Config.Star); + if (realSize < body.Config.Star.Size) realSize = body.Config.Star.Size; + } + if(body.Config.ProcGen != null) + { + ProcGenBuilder.Make(newProxy, null, body.Config.ProcGen); + if (realSize < body.Config.ProcGen.Scale) realSize = body.Config.ProcGen.Scale; + } + + // Remove all collisions if there are any + foreach (var col in newProxy.GetComponentsInChildren()) + { + GameObject.Destroy(col); + } + // Fix render idk + foreach (var renderer in newProxy.GetComponentsInChildren()) + { + renderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off; + renderer.receiveShadows = false; + renderer.lightProbeUsage = UnityEngine.Rendering.LightProbeUsage.BlendProbes; + renderer.enabled = false; + } + var proxyController = newProxy.AddComponent(); proxyController.astroName = body.Config.Name; - proxyController._realObjectDiameter = body.Config.Base?.SurfaceSize ?? 100f; - proxyController.renderer = newProxy.GetComponentInChildren(); - } - - private static void MakeHeightMapProxy(NewHorizonsBody body, IModBehaviour mod) - { - + proxyController._realObjectDiameter = realSize; + proxyController.renderers = newProxy.GetComponentsInChildren(); + proxyController.tessellatedRenderers = newProxy.GetComponentsInChildren(); } } } \ No newline at end of file diff --git a/NewHorizons/Builder/Body/RingBuilder.cs b/NewHorizons/Builder/Body/RingBuilder.cs index 91684217..10f3bc59 100644 --- a/NewHorizons/Builder/Body/RingBuilder.cs +++ b/NewHorizons/Builder/Body/RingBuilder.cs @@ -23,59 +23,7 @@ namespace NewHorizons.Builder.Body public static GameObject Make(GameObject planetGO, Sector sector, RingModule ring, IModBehaviour mod) { - // Properly lit shader doesnt work yet - ring.Unlit = true; - - Texture2D ringTexture; - try - { - ringTexture = ImageUtilities.GetTexture(mod, ring.Texture); - } - catch (Exception e) - { - Logger.LogError($"Couldn't load Ring texture, {e.Message}, {e.StackTrace}"); - return null; - } - - var ringGO = new GameObject("Ring"); - ringGO.transform.parent = sector?.transform ?? planetGO.transform; - ringGO.transform.position = planetGO.transform.position; - ringGO.transform.rotation = planetGO.transform.rotation; - ringGO.transform.Rotate(ringGO.transform.TransformDirection(Vector3.up), ring.LongitudeOfAscendingNode); - ringGO.transform.Rotate(ringGO.transform.TransformDirection(Vector3.left), ring.Inclination); - - var ringMF = ringGO.AddComponent(); - var ringMesh = ringMF.mesh; - var ringMR = ringGO.AddComponent(); - var texture = ringTexture; - - if (RingShader == null) RingShader = Main.NHAssetBundle.LoadAsset("Assets/Shaders/Ring.shader"); - if (UnlitRingShader == null) UnlitRingShader = Main.NHAssetBundle.LoadAsset("Assets/Shaders/UnlitTransparent.shader"); - if (RingShader1Pixel == null) RingShader1Pixel = Main.NHAssetBundle.LoadAsset("Assets/Shaders/Ring1Pixel.shader"); - if (UnlitRingShader1Pixel == null) UnlitRingShader1Pixel = Main.NHAssetBundle.LoadAsset("Assets/Shaders/UnlitRing1Pixel.shader"); - - var mat = new Material(ring.Unlit ? UnlitRingShader : RingShader); - if (texture.width == 1) - { - mat = new Material(ring.Unlit ? UnlitRingShader1Pixel : RingShader1Pixel); - mat.SetFloat("_InnerRadius", 0); - } - ringMR.receiveShadows = !ring.Unlit; - - mat.mainTexture = texture; - mat.renderQueue = 3000; - ringMR.material = mat; - - // Make mesh - var segments = (int)Mathf.Clamp(ring.OuterRadius, 20, 2000); - BuildRingMesh(ringMesh, segments, ring.InnerRadius, ring.OuterRadius); - - if (ring.RotationSpeed != 0) - { - var rot = ringGO.AddComponent(); - rot._degreesPerSecond = ring.RotationSpeed; - rot._localAxis = Vector3.down; - } + var ringGO = MakeRingGraphics(planetGO, sector, ring, mod); // Funny collider thing var ringVolume = new GameObject("RingVolume"); @@ -119,6 +67,67 @@ namespace NewHorizons.Builder.Body ringVolume.SetActive(true); + + + return ringGO; + } + + public static GameObject MakeRingGraphics(GameObject rootObject, Sector sector, RingModule ring, IModBehaviour mod) + { + // Properly lit shader doesnt work yet + ring.Unlit = true; + + Texture2D ringTexture; + try + { + ringTexture = ImageUtilities.GetTexture(mod, ring.Texture); + } + catch (Exception e) + { + Logger.LogError($"Couldn't load Ring texture, {e.Message}, {e.StackTrace}"); + return null; + } + + var ringGO = new GameObject("Ring"); + ringGO.transform.parent = sector?.transform ?? rootObject.transform; + ringGO.transform.position = rootObject.transform.position; + ringGO.transform.rotation = rootObject.transform.rotation; + ringGO.transform.Rotate(ringGO.transform.TransformDirection(Vector3.up), ring.LongitudeOfAscendingNode); + ringGO.transform.Rotate(ringGO.transform.TransformDirection(Vector3.left), ring.Inclination); + + var ringMF = ringGO.AddComponent(); + var ringMesh = ringMF.mesh; + var ringMR = ringGO.AddComponent(); + var texture = ringTexture; + + if (RingShader == null) RingShader = Main.NHAssetBundle.LoadAsset("Assets/Shaders/Ring.shader"); + if (UnlitRingShader == null) UnlitRingShader = Main.NHAssetBundle.LoadAsset("Assets/Shaders/UnlitTransparent.shader"); + if (RingShader1Pixel == null) RingShader1Pixel = Main.NHAssetBundle.LoadAsset("Assets/Shaders/Ring1Pixel.shader"); + if (UnlitRingShader1Pixel == null) UnlitRingShader1Pixel = Main.NHAssetBundle.LoadAsset("Assets/Shaders/UnlitRing1Pixel.shader"); + + var mat = new Material(ring.Unlit ? UnlitRingShader : RingShader); + if (texture.width == 1) + { + mat = new Material(ring.Unlit ? UnlitRingShader1Pixel : RingShader1Pixel); + mat.SetFloat("_InnerRadius", 0); + } + ringMR.receiveShadows = !ring.Unlit; + + mat.mainTexture = texture; + mat.renderQueue = 3000; + ringMR.material = mat; + + // Make mesh + var segments = (int)Mathf.Clamp(ring.OuterRadius, 20, 2000); + BuildRingMesh(ringMesh, segments, ring.InnerRadius, ring.OuterRadius); + + if (ring.RotationSpeed != 0) + { + var rot = ringGO.AddComponent(); + rot._degreesPerSecond = ring.RotationSpeed; + rot._localAxis = Vector3.down; + } + if (ring.Curve != null) { var levelController = ringGO.AddComponent(); diff --git a/NewHorizons/Builder/Body/StarBuilder.cs b/NewHorizons/Builder/Body/StarBuilder.cs index 92b28696..04d5d99a 100644 --- a/NewHorizons/Builder/Body/StarBuilder.cs +++ b/NewHorizons/Builder/Body/StarBuilder.cs @@ -23,37 +23,7 @@ namespace NewHorizons.Builder.Body { if (_colorOverTime == null) _colorOverTime = ImageUtilities.GetTexture(Main.Instance, "AssetBundle/StarColorOverTime.png"); - var starGO = new GameObject("Star"); - starGO.transform.parent = sector?.transform ?? planetGO.transform; - - var sunSurface = GameObject.Instantiate(GameObject.Find("Sun_Body/Sector_SUN/Geometry_SUN/Surface"), starGO.transform); - sunSurface.transform.position = planetGO.transform.position; - sunSurface.transform.localScale = Vector3.one; - sunSurface.name = "Surface"; - - var sunLight = new GameObject(); - sunLight.transform.parent = starGO.transform; - sunLight.transform.localPosition = Vector3.zero; - sunLight.transform.localScale = Vector3.one; - sunLight.name = "StarLight"; - var light = sunLight.AddComponent(); - light.CopyPropertiesFrom(GameObject.Find("Sun_Body/Sector_SUN/Effects_SUN/SunLight").GetComponent()); - light.intensity *= starModule.SolarLuminosity; - light.range *= Mathf.Sqrt(starModule.SolarLuminosity); - - var faceActiveCamera = sunLight.AddComponent(); - faceActiveCamera.CopyPropertiesFrom(GameObject.Find("Sun_Body/Sector_SUN/Effects_SUN/SunLight").GetComponent()); - var csmTextureCacher = sunLight.AddComponent(); - csmTextureCacher.CopyPropertiesFrom(GameObject.Find("Sun_Body/Sector_SUN/Effects_SUN/SunLight").GetComponent()); - csmTextureCacher._light = light; - var proxyShadowLight = sunLight.AddComponent(); - proxyShadowLight.CopyPropertiesFrom(GameObject.Find("Sun_Body/Sector_SUN/Effects_SUN/SunLight").GetComponent()); - proxyShadowLight._light = light; - - var solarFlareEmitter = GameObject.Instantiate(GameObject.Find("Sun_Body/Sector_SUN/Effects_SUN/SolarFlareEmitter"), starGO.transform); - solarFlareEmitter.transform.localPosition = Vector3.zero; - solarFlareEmitter.transform.localScale = Vector3.one; - solarFlareEmitter.name = "SolarFlareEmitter"; + var starGO = MakeStarGraphics(planetGO, sector, starModule); var sunAudio = GameObject.Instantiate(GameObject.Find("Sun_Body/Sector_SUN/Audio_SUN"), starGO.transform); sunAudio.transform.localPosition = Vector3.zero; @@ -112,9 +82,19 @@ namespace NewHorizons.Builder.Body deathVolume.GetComponent()._shrinkBodies = false; deathVolume.name = "DestructionVolume"; - TessellatedSphereRenderer surface = sunSurface.GetComponent(); Light ambientLight = ambientLightGO.GetComponent(); + var sunLight = new GameObject(); + sunLight.transform.parent = starGO.transform; + sunLight.transform.localPosition = Vector3.zero; + sunLight.transform.localScale = Vector3.one; + sunLight.name = "StarLight"; + + var light = sunLight.AddComponent(); + light.CopyPropertiesFrom(GameObject.Find("Sun_Body/Sector_SUN/Effects_SUN/SunLight").GetComponent()); + light.intensity *= starModule.SolarLuminosity; + light.range *= Mathf.Sqrt(starModule.SolarLuminosity); + Color lightColour = light.color; if (starModule.LightTint != null) lightColour = starModule.LightTint.ToColor(); if (lightColour == null && starModule.Tint != null) @@ -130,8 +110,69 @@ namespace NewHorizons.Builder.Body light.color = lightColour; ambientLight.color = lightColour; - if(starModule.Tint != null) + var faceActiveCamera = sunLight.AddComponent(); + faceActiveCamera.CopyPropertiesFrom(GameObject.Find("Sun_Body/Sector_SUN/Effects_SUN/SunLight").GetComponent()); + var csmTextureCacher = sunLight.AddComponent(); + csmTextureCacher.CopyPropertiesFrom(GameObject.Find("Sun_Body/Sector_SUN/Effects_SUN/SunLight").GetComponent()); + csmTextureCacher._light = light; + var proxyShadowLight = sunLight.AddComponent(); + proxyShadowLight.CopyPropertiesFrom(GameObject.Find("Sun_Body/Sector_SUN/Effects_SUN/SunLight").GetComponent()); + proxyShadowLight._light = light; + + StarController starController = null; + if (starModule.SolarLuminosity != 0) { + starController = planetGO.AddComponent(); + starController.Light = light; + starController.AmbientLight = ambientLight; + starController.FaceActiveCamera = faceActiveCamera; + starController.CSMTextureCacher = csmTextureCacher; + starController.ProxyShadowLight = proxyShadowLight; + starController.Intensity = starModule.SolarLuminosity; + starController.SunColor = lightColour; + } + + return starController; + } + + public static GameObject MakeStarGraphics(GameObject rootObject, Sector sector, StarModule starModule) + { + var starGO = new GameObject("Star"); + starGO.transform.parent = sector?.transform ?? rootObject.transform; + + var sunSurface = GameObject.Instantiate(GameObject.Find("Sun_Body/Sector_SUN/Geometry_SUN/Surface"), starGO.transform); + sunSurface.transform.position = rootObject.transform.position; + sunSurface.transform.localScale = Vector3.one; + sunSurface.name = "Surface"; + + var solarFlareEmitter = GameObject.Instantiate(GameObject.Find("Sun_Body/Sector_SUN/Effects_SUN/SolarFlareEmitter"), starGO.transform); + solarFlareEmitter.transform.localPosition = Vector3.zero; + solarFlareEmitter.transform.localScale = Vector3.one; + solarFlareEmitter.name = "SolarFlareEmitter"; + + if (starModule.SolarFlareTint != null) + { + solarFlareEmitter.GetComponent().tint = starModule.SolarFlareTint.ToColor(); + } + + starGO.transform.position = rootObject.transform.position; + starGO.transform.localScale = starModule.Size * Vector3.one; + + if (starModule.Curve != null) + { + var levelController = starGO.AddComponent(); + var curve = new AnimationCurve(); + foreach (var pair in starModule.Curve) + { + curve.AddKey(new Keyframe(pair.Time, starModule.Size * pair.Value)); + } + levelController._scaleCurve = curve; + } + + if (starModule.Tint != null) + { + TessellatedSphereRenderer surface = sunSurface.GetComponent(); + var colour = starModule.Tint.ToColor(); var sun = GameObject.Find("Sun_Body"); @@ -148,37 +189,7 @@ namespace NewHorizons.Builder.Body surface.sharedMaterial.SetTexture("_ColorRamp", ImageUtilities.LerpGreyscaleImage(_colorOverTime, adjustedColour, darkenedColor)); } - if(starModule.SolarFlareTint != null) - solarFlareEmitter.GetComponent().tint = starModule.SolarFlareTint.ToColor(); - - starGO.transform.position = planetGO.transform.position; - starGO.transform.localScale = starModule.Size * Vector3.one; - - StarController starController = null; - if (starModule.SolarLuminosity != 0) - { - starController = planetGO.AddComponent(); - starController.Light = light; - starController.AmbientLight = ambientLight; - starController.FaceActiveCamera = faceActiveCamera; - starController.CSMTextureCacher = csmTextureCacher; - starController.ProxyShadowLight = proxyShadowLight; - starController.Intensity = starModule.SolarLuminosity; - starController.SunColor = lightColour; - } - - if (starModule.Curve != null) - { - var levelController = starGO.AddComponent(); - var curve = new AnimationCurve(); - foreach (var pair in starModule.Curve) - { - curve.AddKey(new Keyframe(pair.Time, starModule.Size * pair.Value)); - } - levelController._scaleCurve = curve; - } - - return starController; + return starGO; } } } diff --git a/NewHorizons/Components/NHProxy.cs b/NewHorizons/Components/NHProxy.cs index 608be57a..f71d3b25 100644 --- a/NewHorizons/Components/NHProxy.cs +++ b/NewHorizons/Components/NHProxy.cs @@ -1,4 +1,5 @@ using NewHorizons.Utility; +using System.Collections.Generic; using UnityEngine; namespace NewHorizons.Components @@ -6,8 +7,9 @@ namespace NewHorizons.Components public class NHProxy : ProxyPlanet { public string astroName; - public Renderer renderer; - + public Renderer[] renderers; + public TessellatedRenderer[] tessellatedRenderers; + public override void Initialize() { AstroObject astroObject = AstroObjectLocator.GetAstroObject(astroName); @@ -32,7 +34,10 @@ namespace NewHorizons.Components public override void ToggleRendering(bool on) { base.ToggleRendering(on); - renderer.enabled = on; + foreach (Transform child in transform) + { + child.gameObject.SetActive(on); + } } } } \ No newline at end of file diff --git a/NewHorizons/Handlers/PlanetCreationHandler.cs b/NewHorizons/Handlers/PlanetCreationHandler.cs index a95697d1..9b595b69 100644 --- a/NewHorizons/Handlers/PlanetCreationHandler.cs +++ b/NewHorizons/Handlers/PlanetCreationHandler.cs @@ -237,6 +237,7 @@ namespace NewHorizons.Handlers return true; } + // Called when updating an existing planet public static GameObject UpdateBody(NewHorizonsBody body, GameObject go) { Logger.Log($"Updating existing Object {go.name}"); @@ -264,6 +265,7 @@ namespace NewHorizons.Handlers return go; } + // Only called when making new planets public static GameObject GenerateBody(NewHorizonsBody body, bool defaultPrimaryToSun = false) { AstroObject primaryBody; @@ -357,6 +359,11 @@ namespace NewHorizons.Handlers AstroObjectLocator.RegisterCustomAstroObject(ao); } + Main.Instance.ModHelper.Events.Unity.FireOnNextUpdate(() => + { + ProxyBuilder.Make(go, body); + }); + return go; } @@ -369,6 +376,7 @@ namespace NewHorizons.Handlers return sphereOfInfluence; } + // What is called both on existing planets and new planets private static GameObject SharedGenerateBody(NewHorizonsBody body, GameObject go, Sector sector, OWRigidbody rb) { var sphereOfInfluence = GetSphereOfInfluence(body); @@ -499,11 +507,6 @@ namespace NewHorizons.Handlers { CloakBuilder.Make(go, sector, rb, body.Config.Base.CloakRadius); } - - Main.Instance.ModHelper.Events.Unity.FireOnNextUpdate(() => - { - ProxyBuilder.Make(go, body); - }); return go; } diff --git a/NewHorizons/Handlers/PlanetDestructionHandler.cs b/NewHorizons/Handlers/PlanetDestructionHandler.cs index a30a50da..8ecfe562 100644 --- a/NewHorizons/Handlers/PlanetDestructionHandler.cs +++ b/NewHorizons/Handlers/PlanetDestructionHandler.cs @@ -206,16 +206,16 @@ namespace NewHorizons.Handlers private static void RemoveProxy(string name) { - // if (name.Equals("TowerTwin")) name = "AshTwin"; - // if (name.Equals("CaveTwin")) name = "EmberTwin"; - // var distantProxy = GameObject.Find(name + "_DistantProxy"); - // var distantProxyClone = GameObject.Find(name + "_DistantProxy(Clone)"); - // - // if (distantProxy != null) GameObject.Destroy(distantProxy.gameObject); - // if (distantProxyClone != null) GameObject.Destroy(distantProxyClone.gameObject); - // - // if (distantProxy == null && distantProxyClone == null) - // Logger.Log($"Couldn't find proxy for {name}"); + if (name.Equals("TowerTwin")) name = "AshTwin"; + if (name.Equals("CaveTwin")) name = "EmberTwin"; + var distantProxy = GameObject.Find(name + "_DistantProxy"); + var distantProxyClone = GameObject.Find(name + "_DistantProxy(Clone)"); + + if (distantProxy != null) GameObject.Destroy(distantProxy.gameObject); + if (distantProxyClone != null) GameObject.Destroy(distantProxyClone.gameObject); + + if (distantProxy == null && distantProxyClone == null) + Logger.Log($"Couldn't find proxy for {name}"); } } } diff --git a/NewHorizons/Patches/ProxyBodyPatches.cs b/NewHorizons/Patches/ProxyBodyPatches.cs new file mode 100644 index 00000000..4ab32d5f --- /dev/null +++ b/NewHorizons/Patches/ProxyBodyPatches.cs @@ -0,0 +1,56 @@ +using HarmonyLib; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace NewHorizons.Patches +{ + [HarmonyPatch] + public static class ProxyBodyPatches + { + [HarmonyPrefix] + [HarmonyPatch(typeof(ProxyBody), nameof(ProxyBody.Awake))] + public static void ProxyBody_Awake(ProxyBody __instance) + { + // Mobius rly used the wrong event name + GlobalMessenger.AddListener("EnterMapView", __instance.OnEnterMapView); + GlobalMessenger.AddListener("ExitMapView", __instance.OnExitMapView); + } + + [HarmonyPrefix] + [HarmonyPatch(typeof(ProxyBody), nameof(ProxyBody.OnDestroy))] + public static void ProxyBody_OnDestroy(ProxyBody __instance) + { + GlobalMessenger.RemoveListener("EnterMapView", __instance.OnEnterMapView); + GlobalMessenger.RemoveListener("ExitMapView", __instance.OnExitMapView); + } + + [HarmonyPrefix] + [HarmonyPatch(typeof(ProxyBody), nameof(ProxyBody.OnEnterMapView))] + public static void ProxyBody_OnEnterMapView(ProxyBody __instance) + { + // Set this to false before the method sets the rendering to false so it matches + __instance._outOfRange = false; + } + + // Mobius why doesn't ProxyOrbiter inherit from ProxyBody + [HarmonyPrefix] + [HarmonyPatch(typeof(ProxyOrbiter), nameof(ProxyOrbiter.Awake))] + public static void ProxyOrbiter_Awake(ProxyOrbiter __instance) + { + // Mobius rly used the wrong event name + GlobalMessenger.AddListener("EnterMapView", __instance.OnEnterMapView); + GlobalMessenger.AddListener("ExitMapView", __instance.OnExitMapView); + } + + [HarmonyPrefix] + [HarmonyPatch(typeof(ProxyOrbiter), nameof(ProxyOrbiter.OnDestroy))] + public static void ProxyOrbiter_OnDestroy(ProxyOrbiter __instance) + { + GlobalMessenger.RemoveListener("EnterMapView", __instance.OnEnterMapView); + GlobalMessenger.RemoveListener("ExitMapView", __instance.OnExitMapView); + } + } +} diff --git a/NewHorizons/Utility/ImageUtilities.cs b/NewHorizons/Utility/ImageUtilities.cs index c3b2bff2..32509f85 100644 --- a/NewHorizons/Utility/ImageUtilities.cs +++ b/NewHorizons/Utility/ImageUtilities.cs @@ -20,13 +20,23 @@ namespace NewHorizons.Utility Logger.Log($"Already loaded image at path: {path}"); return _loadedTextures[path]; } + Logger.Log($"Loading image at path: {path}"); - var data = File.ReadAllBytes(path); - var texture = new Texture2D(2, 2); - texture.name = Path.GetFileNameWithoutExtension(path); - texture.LoadImage(data); - _loadedTextures.Add(path, texture); - return texture; + try + { + var data = File.ReadAllBytes(path); + var texture = new Texture2D(2, 2); + texture.name = Path.GetFileNameWithoutExtension(path); + texture.LoadImage(data); + _loadedTextures.Add(path, texture); + + return texture; + } + catch (Exception ex) + { + Logger.LogError($"Exception thrown while loading texture [{filename}]: {ex.Message}, {ex.StackTrace}"); + return null; + } } public static void ClearCache() @@ -40,7 +50,7 @@ namespace NewHorizons.Utility } _loadedTextures.Clear(); - foreach(var texture in _generatedTextures) + foreach (var texture in _generatedTextures) { if (texture == null) continue; UnityEngine.Object.Destroy(texture); @@ -57,7 +67,7 @@ namespace NewHorizons.Utility var y = (int)Mathf.Floor(i / texture.height); // Needs a black border - if(x == 0 || y == 0 || x == texture.width-1 || y == texture.height-1) + if (x == 0 || y == 0 || x == texture.width - 1 || y == texture.height - 1) { pixels[i].r = 1; pixels[i].g = 1; @@ -92,20 +102,20 @@ namespace NewHorizons.Utility texture.name = "SlideReelAtlas"; Color[] fillPixels = new Color[size * size * 4 * 4]; - for(int xIndex = 0; xIndex < 4; xIndex++) + for (int xIndex = 0; xIndex < 4; xIndex++) { - for(int yIndex = 0; yIndex < 4; yIndex++) + for (int yIndex = 0; yIndex < 4; yIndex++) { int index = yIndex * 4 + xIndex; var srcTexture = index < textures.Length ? textures[index] : null; - for(int i = 0; i < size; i++) + for (int i = 0; i < size; i++) { - for(int j = 0; j < size; j++) + for (int j = 0; j < size; j++) { var colour = Color.black; - if(srcTexture) + if (srcTexture) { var srcX = i * srcTexture.width / (float)size; var srcY = j * srcTexture.height / (float)size; @@ -118,11 +128,11 @@ namespace NewHorizons.Utility var x = xIndex * size + i; // Want it to start from the first row from the bottom then go down then modulo around idk // 5 because no pos mod idk - var y = ((5 - yIndex)%4) * size + j; + var y = ((5 - yIndex) % 4) * size + j; var pixelIndex = x + y * (size * 4); - if(pixelIndex < fillPixels.Length && pixelIndex >= 0) fillPixels[pixelIndex] = colour; + if (pixelIndex < fillPixels.Length && pixelIndex >= 0) fillPixels[pixelIndex] = colour; } } } @@ -149,7 +159,7 @@ namespace NewHorizons.Utility { var fillColor = new Color(0, 0, 0, 0); - if(pixels[x + y * texture.width].a == 1 && CloseToTransparent(pixels, texture.width, texture.height, x, y, thickness)) + if (pixels[x + y * texture.width].a == 1 && CloseToTransparent(pixels, texture.width, texture.height, x, y, thickness)) { fillColor = color; } @@ -168,10 +178,10 @@ namespace NewHorizons.Utility private static bool CloseToTransparent(Color[] pixels, int width, int height, int x, int y, int thickness) { // Check nearby - var minX = Math.Max(0, x - thickness/2); - var minY = Math.Max(0, y - thickness/2); - var maxX = Math.Min(width, x + thickness/2); - var maxY = Math.Min(height, y + thickness/2); + var minX = Math.Max(0, x - thickness / 2); + var minY = Math.Max(0, y - thickness / 2); + var maxX = Math.Min(width, x + thickness / 2); + var maxY = Math.Min(height, y + thickness / 2); for (int i = minX; i < maxX; i++) { @@ -229,7 +239,7 @@ namespace NewHorizons.Utility tex.name = "Clear"; Color fillColor = Color.clear; Color[] fillPixels = new Color[tex.width * tex.height]; - for(int i = 0; i < fillPixels.Length; i++) + for (int i = 0; i < fillPixels.Length; i++) { fillPixels[i] = fillColor; } @@ -248,7 +258,7 @@ namespace NewHorizons.Utility Color[] fillPixels = new Color[tex.width * tex.height]; for (int i = 0; i < tex.width; i++) { - for(int j = 0; j < tex.height; j++) + for (int j = 0; j < tex.height; j++) { var x = i + (src.width - width) / 2; var y = j + (src.height - height) / 2; @@ -274,7 +284,7 @@ namespace NewHorizons.Utility var g = 0f; var b = 0f; var length = pixels.Length; - for(int i = 0; i < pixels.Length; i++) + for (int i = 0; i < pixels.Length; i++) { var color = pixels[i]; r += (float)color.r / length;