cherry picked commit that made useBasicShader obsolete

This commit is contained in:
FreezeDriedMangoes 2022-06-06 12:32:27 -04:00
parent 12700fc932
commit 51a6077180
3 changed files with 232 additions and 218 deletions

View File

@ -1,227 +1,227 @@
using NewHorizons.External.Modules; using NewHorizons.External.Modules;
using NewHorizons.Utility; using NewHorizons.Utility;
using OWML.Common; using OWML.Common;
using System; using System;
using UnityEngine; using UnityEngine;
using Logger = NewHorizons.Utility.Logger; using Logger = NewHorizons.Utility.Logger;
namespace NewHorizons.Builder.Atmosphere namespace NewHorizons.Builder.Atmosphere
{ {
public static class CloudsBuilder public static class CloudsBuilder
{ {
private static Shader _sphereShader = null; private static Shader _sphereShader = null;
private static Material[] _gdCloudMaterials; private static Material[] _gdCloudMaterials;
private static Material[] _qmCloudMaterials; private static Material[] _qmCloudMaterials;
private static GameObject _lightningPrefab; private static GameObject _lightningPrefab;
private static Texture2D _colorRamp; private static Texture2D _colorRamp;
private static readonly int Color1 = Shader.PropertyToID("_Color"); private static readonly int Color1 = Shader.PropertyToID("_Color");
private static readonly int TintColor = Shader.PropertyToID("_TintColor"); private static readonly int TintColor = Shader.PropertyToID("_TintColor");
private static readonly int MainTex = Shader.PropertyToID("_MainTex"); private static readonly int MainTex = Shader.PropertyToID("_MainTex");
private static readonly int RampTex = Shader.PropertyToID("_RampTex"); private static readonly int RampTex = Shader.PropertyToID("_RampTex");
private static readonly int CapTex = Shader.PropertyToID("_CapTex"); private static readonly int CapTex = Shader.PropertyToID("_CapTex");
private static readonly int ColorRamp = Shader.PropertyToID("_ColorRamp"); private static readonly int ColorRamp = Shader.PropertyToID("_ColorRamp");
public static void Make(GameObject planetGO, Sector sector, AtmosphereModule atmo, IModBehaviour mod) public static void Make(GameObject planetGO, Sector sector, AtmosphereModule atmo, IModBehaviour mod)
{ {
if (_lightningPrefab == null) _lightningPrefab = SearchUtilities.Find("GiantsDeep_Body/Sector_GD/Clouds_GD/LightningGenerator_GD"); if (_lightningPrefab == null) _lightningPrefab = SearchUtilities.Find("GiantsDeep_Body/Sector_GD/Clouds_GD/LightningGenerator_GD");
if (_colorRamp == null) _colorRamp = ImageUtilities.GetTexture(Main.Instance, "AssetBundle/textures/Clouds_Bottom_ramp.png"); if (_colorRamp == null) _colorRamp = ImageUtilities.GetTexture(Main.Instance, "AssetBundle/textures/Clouds_Bottom_ramp.png");
GameObject cloudsMainGO = new GameObject("Clouds"); GameObject cloudsMainGO = new GameObject("Clouds");
cloudsMainGO.SetActive(false); cloudsMainGO.SetActive(false);
cloudsMainGO.transform.parent = sector?.transform ?? planetGO.transform; cloudsMainGO.transform.parent = sector?.transform ?? planetGO.transform;
MakeTopClouds(cloudsMainGO, atmo, mod); MakeTopClouds(cloudsMainGO, atmo, mod);
GameObject cloudsBottomGO = new GameObject("BottomClouds"); GameObject cloudsBottomGO = new GameObject("BottomClouds");
cloudsBottomGO.SetActive(false); cloudsBottomGO.SetActive(false);
cloudsBottomGO.transform.parent = cloudsMainGO.transform; cloudsBottomGO.transform.parent = cloudsMainGO.transform;
cloudsBottomGO.transform.localScale = Vector3.one * atmo.clouds.innerCloudRadius; cloudsBottomGO.transform.localScale = Vector3.one * atmo.clouds.innerCloudRadius;
TessellatedSphereRenderer bottomTSR = cloudsBottomGO.AddComponent<TessellatedSphereRenderer>(); TessellatedSphereRenderer bottomTSR = cloudsBottomGO.AddComponent<TessellatedSphereRenderer>();
bottomTSR.tessellationMeshGroup = SearchUtilities.Find("CloudsBottomLayer_QM").GetComponent<TessellatedSphereRenderer>().tessellationMeshGroup; bottomTSR.tessellationMeshGroup = SearchUtilities.Find("CloudsBottomLayer_QM").GetComponent<TessellatedSphereRenderer>().tessellationMeshGroup;
var bottomTSRMaterials = SearchUtilities.Find("CloudsBottomLayer_QM").GetComponent<TessellatedSphereRenderer>().sharedMaterials; var bottomTSRMaterials = SearchUtilities.Find("CloudsBottomLayer_QM").GetComponent<TessellatedSphereRenderer>().sharedMaterials;
// If they set a colour apply it to all the materials else keep the default QM one // If they set a colour apply it to all the materials else keep the default QM one
if (atmo.clouds.tint != null) if (atmo.clouds.tint != null)
{ {
var bottomColor = atmo.clouds.tint.ToColor(); var bottomColor = atmo.clouds.tint.ToColor();
var bottomTSRTempArray = new Material[2]; var bottomTSRTempArray = new Material[2];
bottomTSRTempArray[0] = new Material(bottomTSRMaterials[0]); bottomTSRTempArray[0] = new Material(bottomTSRMaterials[0]);
bottomTSRTempArray[0].SetColor(Color1, bottomColor); bottomTSRTempArray[0].SetColor(Color1, bottomColor);
bottomTSRTempArray[0].SetColor(TintColor, bottomColor); bottomTSRTempArray[0].SetColor(TintColor, bottomColor);
bottomTSRTempArray[0].SetTexture(ColorRamp, ImageUtilities.TintImage(_colorRamp, bottomColor)); bottomTSRTempArray[0].SetTexture(ColorRamp, ImageUtilities.TintImage(_colorRamp, bottomColor));
bottomTSRTempArray[1] = new Material(bottomTSRMaterials[1]); bottomTSRTempArray[1] = new Material(bottomTSRMaterials[1]);
bottomTSR.sharedMaterials = bottomTSRTempArray; bottomTSR.sharedMaterials = bottomTSRTempArray;
} }
else else
{ {
bottomTSR.sharedMaterials = bottomTSRMaterials; bottomTSR.sharedMaterials = bottomTSRMaterials;
} }
bottomTSR.maxLOD = 6; bottomTSR.maxLOD = 6;
bottomTSR.LODBias = 0; bottomTSR.LODBias = 0;
bottomTSR.LODRadius = 1f; bottomTSR.LODRadius = 1f;
TessSphereSectorToggle bottomTSST = cloudsBottomGO.AddComponent<TessSphereSectorToggle>(); TessSphereSectorToggle bottomTSST = cloudsBottomGO.AddComponent<TessSphereSectorToggle>();
bottomTSST._sector = sector; bottomTSST._sector = sector;
GameObject cloudsFluidGO = new GameObject("CloudsFluid"); GameObject cloudsFluidGO = new GameObject("CloudsFluid");
cloudsFluidGO.SetActive(false); cloudsFluidGO.SetActive(false);
cloudsFluidGO.layer = 17; cloudsFluidGO.layer = 17;
cloudsFluidGO.transform.parent = cloudsMainGO.transform; cloudsFluidGO.transform.parent = cloudsMainGO.transform;
SphereCollider fluidSC = cloudsFluidGO.AddComponent<SphereCollider>(); SphereCollider fluidSC = cloudsFluidGO.AddComponent<SphereCollider>();
fluidSC.isTrigger = true; fluidSC.isTrigger = true;
fluidSC.radius = atmo.size; fluidSC.radius = atmo.size;
OWShellCollider fluidOWSC = cloudsFluidGO.AddComponent<OWShellCollider>(); OWShellCollider fluidOWSC = cloudsFluidGO.AddComponent<OWShellCollider>();
fluidOWSC._innerRadius = atmo.size * 0.9f; fluidOWSC._innerRadius = atmo.size * 0.9f;
CloudLayerFluidVolume fluidCLFV = cloudsFluidGO.AddComponent<CloudLayerFluidVolume>(); CloudLayerFluidVolume fluidCLFV = cloudsFluidGO.AddComponent<CloudLayerFluidVolume>();
fluidCLFV._layer = 5; fluidCLFV._layer = 5;
fluidCLFV._priority = 1; fluidCLFV._priority = 1;
fluidCLFV._density = 1.2f; fluidCLFV._density = 1.2f;
var fluidType = FluidVolume.Type.CLOUD; var fluidType = FluidVolume.Type.CLOUD;
try try
{ {
fluidType = (FluidVolume.Type)Enum.Parse(typeof(FluidVolume.Type), Enum.GetName(typeof(CloudFluidType), atmo.clouds.fluidType).ToUpper()); fluidType = (FluidVolume.Type)Enum.Parse(typeof(FluidVolume.Type), Enum.GetName(typeof(CloudFluidType), atmo.clouds.fluidType).ToUpper());
} }
catch (Exception ex) catch (Exception ex)
{ {
Logger.LogError($"Couldn't parse fluid volume type [{atmo.clouds.fluidType}]: {ex.Message}, {ex.StackTrace}"); Logger.LogError($"Couldn't parse fluid volume type [{atmo.clouds.fluidType}]: {ex.Message}, {ex.StackTrace}");
} }
fluidCLFV._fluidType = fluidType; fluidCLFV._fluidType = fluidType;
fluidCLFV._allowShipAutoroll = true; fluidCLFV._allowShipAutoroll = true;
fluidCLFV._disableOnStart = false; fluidCLFV._disableOnStart = false;
// Fix the rotations once the rest is done // Fix the rotations once the rest is done
cloudsMainGO.transform.rotation = planetGO.transform.TransformRotation(Quaternion.Euler(0, 0, 0)); cloudsMainGO.transform.rotation = planetGO.transform.TransformRotation(Quaternion.Euler(0, 0, 0));
// For the base shader it has to be rotated idk // For the base shader it has to be rotated idk
if (atmo.clouds.useBasicCloudShader) cloudsMainGO.transform.rotation = planetGO.transform.TransformRotation(Quaternion.Euler(90, 0, 0)); if (atmo.clouds.useBasicCloudShader) cloudsMainGO.transform.rotation = planetGO.transform.TransformRotation(Quaternion.Euler(90, 0, 0));
// Lightning // Lightning
if (atmo.clouds.hasLightning) if (atmo.clouds.hasLightning)
{ {
var lightning = _lightningPrefab.InstantiateInactive(); var lightning = _lightningPrefab.InstantiateInactive();
lightning.transform.parent = cloudsMainGO.transform; lightning.transform.parent = cloudsMainGO.transform;
lightning.transform.localPosition = Vector3.zero; lightning.transform.localPosition = Vector3.zero;
var lightningGenerator = lightning.GetComponent<CloudLightningGenerator>(); var lightningGenerator = lightning.GetComponent<CloudLightningGenerator>();
lightningGenerator._altitude = (atmo.clouds.outerCloudRadius + atmo.clouds.innerCloudRadius) / 2f; lightningGenerator._altitude = (atmo.clouds.outerCloudRadius + atmo.clouds.innerCloudRadius) / 2f;
lightningGenerator._audioSector = sector; lightningGenerator._audioSector = sector;
if (atmo.clouds.lightningGradient != null) if (atmo.clouds.lightningGradient != null)
{ {
var gradient = new GradientColorKey[atmo.clouds.lightningGradient.Length]; var gradient = new GradientColorKey[atmo.clouds.lightningGradient.Length];
for(int i = 0; i < atmo.clouds.lightningGradient.Length; i++) for(int i = 0; i < atmo.clouds.lightningGradient.Length; i++)
{ {
var pair = atmo.clouds.lightningGradient[i]; var pair = atmo.clouds.lightningGradient[i];
gradient[i] = new GradientColorKey(pair.tint.ToColor(), pair.time); gradient[i] = new GradientColorKey(pair.tint.ToColor(), pair.time);
} }
lightningGenerator._lightColor.colorKeys = gradient; lightningGenerator._lightColor.colorKeys = gradient;
} }
lightning.SetActive(true); lightning.SetActive(true);
} }
cloudsMainGO.transform.position = planetGO.transform.TransformPoint(Vector3.zero); cloudsMainGO.transform.position = planetGO.transform.TransformPoint(Vector3.zero);
cloudsBottomGO.transform.position = planetGO.transform.TransformPoint(Vector3.zero); cloudsBottomGO.transform.position = planetGO.transform.TransformPoint(Vector3.zero);
cloudsFluidGO.transform.position = planetGO.transform.TransformPoint(Vector3.zero); cloudsFluidGO.transform.position = planetGO.transform.TransformPoint(Vector3.zero);
cloudsBottomGO.SetActive(true); cloudsBottomGO.SetActive(true);
cloudsFluidGO.SetActive(true); cloudsFluidGO.SetActive(true);
cloudsMainGO.SetActive(true); cloudsMainGO.SetActive(true);
} }
public static GameObject MakeTopClouds(GameObject rootObject, AtmosphereModule atmo, IModBehaviour mod) public static GameObject MakeTopClouds(GameObject rootObject, AtmosphereModule atmo, IModBehaviour mod)
{ {
Color cloudTint = atmo.clouds.tint?.ToColor() ?? Color.white; Color cloudTint = atmo.clouds.tint?.ToColor() ?? Color.white;
Texture2D image, cap, ramp; Texture2D image, cap, ramp;
try try
{ {
image = ImageUtilities.GetTexture(mod, atmo.clouds.texturePath); image = ImageUtilities.GetTexture(mod, atmo.clouds.texturePath);
if (atmo.clouds.capPath == null) cap = ImageUtilities.ClearTexture(128, 128); if (atmo.clouds.capPath == null) cap = ImageUtilities.ClearTexture(128, 128);
else cap = ImageUtilities.GetTexture(mod, atmo.clouds.capPath); else cap = ImageUtilities.GetTexture(mod, atmo.clouds.capPath);
if (atmo.clouds.rampPath == null) ramp = ImageUtilities.CanvasScaled(image, 1, image.height); if (atmo.clouds.rampPath == null) ramp = ImageUtilities.CanvasScaled(image, 1, image.height);
else ramp = ImageUtilities.GetTexture(mod, atmo.clouds.rampPath); else ramp = ImageUtilities.GetTexture(mod, atmo.clouds.rampPath);
} }
catch (Exception e) catch (Exception e)
{ {
Logger.LogError($"Couldn't load Cloud textures for [{rootObject.name}], {e.Message}, {e.StackTrace}"); Logger.LogError($"Couldn't load Cloud textures for [{rootObject.name}], {e.Message}, {e.StackTrace}");
return null; return null;
} }
GameObject cloudsTopGO = new GameObject("TopClouds"); GameObject cloudsTopGO = new GameObject("TopClouds");
cloudsTopGO.SetActive(false); cloudsTopGO.SetActive(false);
cloudsTopGO.transform.parent = rootObject.transform; cloudsTopGO.transform.parent = rootObject.transform;
cloudsTopGO.transform.localScale = Vector3.one * atmo.clouds.outerCloudRadius; cloudsTopGO.transform.localScale = Vector3.one * atmo.clouds.outerCloudRadius;
MeshFilter topMF = cloudsTopGO.AddComponent<MeshFilter>(); MeshFilter topMF = cloudsTopGO.AddComponent<MeshFilter>();
topMF.mesh = SearchUtilities.Find("CloudsTopLayer_GD").GetComponent<MeshFilter>().mesh; topMF.mesh = SearchUtilities.Find("CloudsTopLayer_GD").GetComponent<MeshFilter>().mesh;
MeshRenderer topMR = cloudsTopGO.AddComponent<MeshRenderer>(); MeshRenderer topMR = cloudsTopGO.AddComponent<MeshRenderer>();
if (_sphereShader == null) _sphereShader = Main.NHAssetBundle.LoadAsset<Shader>("Assets/Shaders/SphereTextureWrapper.shader"); if (_sphereShader == null) _sphereShader = Main.NHAssetBundle.LoadAsset<Shader>("Assets/Shaders/SphereTextureWrapper.shader");
if (_gdCloudMaterials == null) _gdCloudMaterials = SearchUtilities.Find("CloudsTopLayer_GD").GetComponent<MeshRenderer>().sharedMaterials; if (_gdCloudMaterials == null) _gdCloudMaterials = SearchUtilities.Find("CloudsTopLayer_GD").GetComponent<MeshRenderer>().sharedMaterials;
if (_qmCloudMaterials == null) _qmCloudMaterials = SearchUtilities.Find("CloudsTopLayer_QM").GetComponent<MeshRenderer>().sharedMaterials; if (_qmCloudMaterials == null) _qmCloudMaterials = SearchUtilities.Find("CloudsTopLayer_QM").GetComponent<MeshRenderer>().sharedMaterials;
Material[] prefabMaterials = atmo.clouds.cloudsPrefab == CloudPrefabType.GiantsDeep ? _gdCloudMaterials : _qmCloudMaterials; Material[] prefabMaterials = atmo.clouds.cloudsPrefab == CloudPrefabType.GiantsDeep ? _gdCloudMaterials : _qmCloudMaterials;
var tempArray = new Material[2]; var tempArray = new Material[2];
if (atmo.clouds.useBasicCloudShader) if (atmo.clouds.cloudsPrefab == CloudPrefabType.Basic)
{ {
var material = new Material(_sphereShader); var material = new Material(_sphereShader);
if (atmo.clouds.unlit) material.renderQueue = 2550; if (atmo.clouds.unlit) material.renderQueue = 2550;
material.name = atmo.clouds.unlit ? "BasicCloud" : "BasicShadowCloud"; material.name = atmo.clouds.unlit ? "BasicCloud" : "BasicShadowCloud";
tempArray[0] = material; tempArray[0] = material;
} }
else else
{ {
var material = new Material(prefabMaterials[0]); var material = new Material(prefabMaterials[0]);
if (atmo.clouds.unlit) material.renderQueue = 2550; if (atmo.clouds.unlit) material.renderQueue = 2550;
material.name = atmo.clouds.unlit ? "AdvancedCloud" : "AdvancedShadowCloud"; material.name = atmo.clouds.unlit ? "AdvancedCloud" : "AdvancedShadowCloud";
tempArray[0] = material; tempArray[0] = material;
} }
// This is the stencil material for the fog under the clouds // This is the stencil material for the fog under the clouds
tempArray[1] = new Material(prefabMaterials[1]); tempArray[1] = new Material(prefabMaterials[1]);
topMR.sharedMaterials = tempArray; topMR.sharedMaterials = tempArray;
foreach (var material in topMR.sharedMaterials) foreach (var material in topMR.sharedMaterials)
{ {
material.SetColor(Color1, cloudTint); material.SetColor(Color1, cloudTint);
material.SetColor(TintColor, cloudTint); material.SetColor(TintColor, cloudTint);
material.SetTexture(MainTex, image); material.SetTexture(MainTex, image);
material.SetTexture(RampTex, ramp); material.SetTexture(RampTex, ramp);
material.SetTexture(CapTex, cap); material.SetTexture(CapTex, cap);
} }
if (atmo.clouds.unlit) if (atmo.clouds.unlit)
{ {
cloudsTopGO.layer = LayerMask.NameToLayer("IgnoreSun"); cloudsTopGO.layer = LayerMask.NameToLayer("IgnoreSun");
} }
RotateTransform topRT = cloudsTopGO.AddComponent<RotateTransform>(); RotateTransform topRT = cloudsTopGO.AddComponent<RotateTransform>();
// Idk why but the axis is weird // Idk why but the axis is weird
topRT._localAxis = atmo.clouds.useBasicCloudShader ? Vector3.forward : Vector3.up; topRT._localAxis = atmo.clouds.useBasicCloudShader ? Vector3.forward : Vector3.up;
topRT._degreesPerSecond = 10; topRT._degreesPerSecond = 10;
topRT._randomizeRotationRate = false; topRT._randomizeRotationRate = false;
cloudsTopGO.transform.localPosition = Vector3.zero; cloudsTopGO.transform.localPosition = Vector3.zero;
cloudsTopGO.SetActive(true); cloudsTopGO.SetActive(true);
return cloudsTopGO; return cloudsTopGO;
} }
} }
} }

View File

@ -234,7 +234,11 @@ namespace NewHorizons.External.Configs
// Former is obsolete, latter is to validate // Former is obsolete, latter is to validate
if (Atmosphere.hasAtmosphere || Atmosphere.atmosphereTint != null) if (Atmosphere.hasAtmosphere || Atmosphere.atmosphereTint != null)
Atmosphere.useAtmosphereShader = true; Atmosphere.useAtmosphereShader = true;
// useBasicCloudShader is obsolete
if (Atmosphere.clouds != null && Atmosphere.clouds.useBasicCloudShader)
Atmosphere.clouds.cloudsPrefab = CloudPrefabType.Basic;
} }
if (Props?.tornados != null) if (Props?.tornados != null)

View File

@ -27,7 +27,9 @@ namespace NewHorizons.External.Modules
{ {
[EnumMember(Value = @"giantsDeep")] GiantsDeep = 0, [EnumMember(Value = @"giantsDeep")] GiantsDeep = 0,
[EnumMember(Value = @"quantumMoon")] QuantumMoon = 1, [EnumMember(Value = @"quantumMoon")] QuantumMoon = 1,
[EnumMember(Value = @"basic")] Basic = 2,
} }
[JsonObject] [JsonObject]
@ -151,11 +153,19 @@ namespace NewHorizons.External.Modules
/// If the top layer shouldn't have shadows. Set to true if you're making a brown dwarf for example. /// If the top layer shouldn't have shadows. Set to true if you're making a brown dwarf for example.
/// </summary> /// </summary>
public bool unlit; public bool unlit;
#region Obsolete
/// <summary> /// <summary>
/// Set to `false` in order to use Giant's Deep's shader. Set to `true` to just apply the cloud texture as is. /// Set to `false` in order to use Giant's Deep's shader. Set to `true` to just apply the cloud texture as is.
/// </summary> /// </summary>
[Obsolete("useBasicCloudShader is deprecated, please use cloudsPrefab=\"basic\" instead")]
public bool useBasicCloudShader; public bool useBasicCloudShader;
#endregion Obsolete
} }