Make cloud fog default to prefab if no tint is given #182

This commit is contained in:
Nick 2023-07-21 23:48:20 -04:00
parent b8e326ad63
commit eff2ab8ad9
2 changed files with 55 additions and 34 deletions

View File

@ -8,6 +8,7 @@ using NewHorizons.Utility.OWML;
using OWML.Common; using OWML.Common;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using Tessellation; using Tessellation;
using UnityEngine; using UnityEngine;
@ -15,11 +16,12 @@ namespace NewHorizons.Builder.Atmosphere
{ {
public static class CloudsBuilder public static class CloudsBuilder
{ {
private static Material[] _gdCloudMaterials; private static Material[] _gdCloudMaterials, _gdBottomMaterials;
private static Material[] _qmCloudMaterials;
private static Material[] _qmBottomMaterials;
private static Mesh _gdTopCloudMesh; private static Mesh _gdTopCloudMesh;
private static Tessellation.MeshGroup _qmBottomMeshGroup;
private static Material[] _qmCloudMaterials, _qmBottomMaterials;
private static MeshGroup _qmBottomMeshGroup;
private static Material _transparentCloud; private static Material _transparentCloud;
private static GameObject _lightningPrefab; private static GameObject _lightningPrefab;
private static Texture2D _colorRamp; private static Texture2D _colorRamp;
@ -41,10 +43,14 @@ namespace NewHorizons.Builder.Atmosphere
_isInit = true; _isInit = true;
if (_lightningPrefab == null) _lightningPrefab = SearchUtilities.Find("GiantsDeep_Body/Sector_GD/Clouds_GD/LightningGenerator_GD").InstantiateInactive().Rename("LightningGenerator").DontDestroyOnLoad(); if (_lightningPrefab == null) _lightningPrefab = SearchUtilities.Find("GiantsDeep_Body/Sector_GD/Clouds_GD/LightningGenerator_GD").InstantiateInactive().Rename("LightningGenerator").DontDestroyOnLoad();
if (_gdTopCloudMesh == null) _gdTopCloudMesh = SearchUtilities.Find("CloudsTopLayer_GD").GetComponent<MeshFilter>().mesh.DontDestroyOnLoad(); if (_gdTopCloudMesh == null) _gdTopCloudMesh = SearchUtilities.Find("CloudsTopLayer_GD").GetComponent<MeshFilter>().mesh.DontDestroyOnLoad();
if (_gdCloudMaterials == null) _gdCloudMaterials = SearchUtilities.Find("CloudsTopLayer_GD").GetComponent<MeshRenderer>().sharedMaterials.MakePrefabMaterials(); if (_gdCloudMaterials == null) _gdCloudMaterials = SearchUtilities.Find("CloudsTopLayer_GD").GetComponent<MeshRenderer>().sharedMaterials.MakePrefabMaterials();
if (_gdBottomMaterials == null) _gdBottomMaterials = SearchUtilities.Find("CloudsBottomLayer_GD").GetComponent<TessellatedSphereRenderer>().sharedMaterials.MakePrefabMaterials();
if (_qmCloudMaterials == null) _qmCloudMaterials = SearchUtilities.Find("CloudsTopLayer_QM").GetComponent<MeshRenderer>().sharedMaterials.MakePrefabMaterials(); if (_qmCloudMaterials == null) _qmCloudMaterials = SearchUtilities.Find("CloudsTopLayer_QM").GetComponent<MeshRenderer>().sharedMaterials.MakePrefabMaterials();
if (_qmBottomMaterials == null) _qmBottomMaterials = SearchUtilities.Find("CloudsBottomLayer_QM").GetComponent<TessellatedSphereRenderer>().sharedMaterials.MakePrefabMaterials(); if (_qmBottomMaterials == null) _qmBottomMaterials = SearchUtilities.Find("CloudsBottomLayer_QM").GetComponent<TessellatedSphereRenderer>().sharedMaterials.MakePrefabMaterials();
if (_qmBottomMeshGroup == null) if (_qmBottomMeshGroup == null)
{ {
var originalMeshGroup = SearchUtilities.Find("CloudsBottomLayer_QM").GetComponent<TessellatedSphereRenderer>().tessellationMeshGroup; var originalMeshGroup = SearchUtilities.Find("CloudsBottomLayer_QM").GetComponent<TessellatedSphereRenderer>().tessellationMeshGroup;
@ -67,7 +73,7 @@ namespace NewHorizons.Builder.Atmosphere
{ {
InitPrefabs(); InitPrefabs();
GameObject cloudsMainGO = new GameObject("Clouds"); var cloudsMainGO = new GameObject("Clouds");
cloudsMainGO.SetActive(false); cloudsMainGO.SetActive(false);
cloudsMainGO.transform.parent = sector?.transform ?? planetGO.transform; cloudsMainGO.transform.parent = sector?.transform ?? planetGO.transform;
@ -81,20 +87,24 @@ namespace NewHorizons.Builder.Atmosphere
return; return;
} }
GameObject cloudsBottomGO = new GameObject("BottomClouds"); var 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>(); var bottomTSR = cloudsBottomGO.AddComponent<TessellatedSphereRenderer>();
bottomTSR.tessellationMeshGroup = _qmBottomMeshGroup; bottomTSR.tessellationMeshGroup = _qmBottomMeshGroup;
var bottomTSRMaterials = _qmBottomMaterials;
// If they set a colour apply it to all the materials else keep the default QM one var bottomTSRMaterials = atmo.clouds.cloudsPrefab == CloudPrefabType.GiantsDeep ? _gdBottomMaterials : _qmBottomMaterials;
if (atmo.clouds.tint != null)
// If they set a colour apply it to all the materials else keep the defaults
if (atmo.clouds.tint == null)
{
bottomTSR.sharedMaterials = bottomTSRMaterials.Select(x => new Material(x)).ToArray();
}
else
{ {
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]);
@ -105,34 +115,34 @@ namespace NewHorizons.Builder.Atmosphere
bottomTSR.sharedMaterials = bottomTSRTempArray; bottomTSR.sharedMaterials = bottomTSRTempArray;
} }
else
{
bottomTSR.sharedMaterials = bottomTSRMaterials;
}
bottomTSR.maxLOD = 6; bottomTSR.maxLOD = 6;
bottomTSR.LODBias = 0; bottomTSR.LODBias = 0;
bottomTSR.LODRadius = 1f; bottomTSR.LODRadius = 1f;
if (cloaked) if (cloaked)
{
cloudsBottomGO.AddComponent<CloakedTessSphereSectorToggle>()._sector = sector; cloudsBottomGO.AddComponent<CloakedTessSphereSectorToggle>()._sector = sector;
}
else else
{
cloudsBottomGO.AddComponent<TessSphereSectorToggle>()._sector = sector; cloudsBottomGO.AddComponent<TessSphereSectorToggle>()._sector = sector;
}
GameObject cloudsFluidGO = new GameObject("CloudsFluid"); var cloudsFluidGO = new GameObject("CloudsFluid");
cloudsFluidGO.SetActive(false); cloudsFluidGO.SetActive(false);
cloudsFluidGO.layer = Layer.BasicEffectVolume; cloudsFluidGO.layer = Layer.BasicEffectVolume;
cloudsFluidGO.transform.parent = cloudsMainGO.transform; cloudsFluidGO.transform.parent = cloudsMainGO.transform;
SphereCollider fluidSC = cloudsFluidGO.AddComponent<SphereCollider>(); var fluidSC = cloudsFluidGO.AddComponent<SphereCollider>();
fluidSC.isTrigger = true; fluidSC.isTrigger = true;
fluidSC.radius = atmo.clouds.outerCloudRadius; fluidSC.radius = atmo.clouds.outerCloudRadius;
OWShellCollider fluidOWSC = cloudsFluidGO.AddComponent<OWShellCollider>(); var fluidOWSC = cloudsFluidGO.AddComponent<OWShellCollider>();
fluidOWSC._innerRadius = atmo.clouds.innerCloudRadius; fluidOWSC._innerRadius = atmo.clouds.innerCloudRadius;
// copied from gd // copied from gd
CloudLayerFluidVolume fluidCLFV = cloudsFluidGO.AddComponent<CloudLayerFluidVolume>(); var fluidCLFV = cloudsFluidGO.AddComponent<CloudLayerFluidVolume>();
fluidCLFV._layer = 5; fluidCLFV._layer = 5;
fluidCLFV._priority = 1; fluidCLFV._priority = 1;
fluidCLFV._density = 1.2f; fluidCLFV._density = 1.2f;
@ -212,6 +222,7 @@ namespace NewHorizons.Builder.Atmosphere
if (atmo.clouds.capPath == null) cap = ImageUtilities.ClearTexture(128, 128, wrap: true); if (atmo.clouds.capPath == null) cap = ImageUtilities.ClearTexture(128, 128, wrap: true);
else cap = ImageUtilities.GetTexture(mod, atmo.clouds.capPath, wrap: true); else cap = ImageUtilities.GetTexture(mod, atmo.clouds.capPath, wrap: true);
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);
} }
@ -221,17 +232,17 @@ namespace NewHorizons.Builder.Atmosphere
return null; return null;
} }
GameObject cloudsTopGO = new GameObject("TopClouds"); var 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>(); var topMF = cloudsTopGO.AddComponent<MeshFilter>();
topMF.mesh = _gdTopCloudMesh; topMF.mesh = _gdTopCloudMesh;
MeshRenderer topMR = cloudsTopGO.AddComponent<MeshRenderer>(); var topMR = cloudsTopGO.AddComponent<MeshRenderer>();
Material[] prefabMaterials = atmo.clouds.cloudsPrefab == CloudPrefabType.GiantsDeep ? _gdCloudMaterials : _qmCloudMaterials; var prefabMaterials = atmo.clouds.cloudsPrefab == CloudPrefabType.GiantsDeep ? _gdCloudMaterials : _qmCloudMaterials;
var tempArray = new Material[2]; var tempArray = new Material[2];
if (atmo.clouds.cloudsPrefab == CloudPrefabType.Basic) if (atmo.clouds.cloudsPrefab == CloudPrefabType.Basic)
@ -297,7 +308,7 @@ namespace NewHorizons.Builder.Atmosphere
return null; return null;
} }
GameObject cloudsTransparentGO = new GameObject("TransparentClouds"); var cloudsTransparentGO = new GameObject("TransparentClouds");
cloudsTransparentGO.SetActive(false); cloudsTransparentGO.SetActive(false);
cloudsTransparentGO.transform.parent = rootObject.transform; cloudsTransparentGO.transform.parent = rootObject.transform;
cloudsTransparentGO.transform.localScale = Vector3.one * atmo.clouds.outerCloudRadius; cloudsTransparentGO.transform.localScale = Vector3.one * atmo.clouds.outerCloudRadius;
@ -305,7 +316,7 @@ namespace NewHorizons.Builder.Atmosphere
MeshFilter filter = cloudsTransparentGO.AddComponent<MeshFilter>(); MeshFilter filter = cloudsTransparentGO.AddComponent<MeshFilter>();
filter.mesh = _gdTopCloudMesh; filter.mesh = _gdTopCloudMesh;
MeshRenderer renderer = cloudsTransparentGO.AddComponent<MeshRenderer>(); var renderer = cloudsTransparentGO.AddComponent<MeshRenderer>();
var material = new Material(_transparentCloud); var material = new Material(_transparentCloud);
material.name = "TransparentClouds_" + image.name; material.name = "TransparentClouds_" + image.name;
material.SetTexture(MainTex, image); material.SetTexture(MainTex, image);
@ -313,7 +324,7 @@ namespace NewHorizons.Builder.Atmosphere
if (!isProxy) if (!isProxy)
{ {
GameObject tcrqcGO = new GameObject("TransparentCloudRenderQueueController"); var tcrqcGO = new GameObject("TransparentCloudRenderQueueController");
tcrqcGO.transform.SetParent(cloudsTransparentGO.transform, false); tcrqcGO.transform.SetParent(cloudsTransparentGO.transform, false);
tcrqcGO.layer = Layer.BasicEffectVolume; tcrqcGO.layer = Layer.BasicEffectVolume;
@ -323,7 +334,7 @@ namespace NewHorizons.Builder.Atmosphere
var owTriggerVolume = tcrqcGO.AddComponent<OWTriggerVolume>(); var owTriggerVolume = tcrqcGO.AddComponent<OWTriggerVolume>();
owTriggerVolume._shape = shape; owTriggerVolume._shape = shape;
TransparentCloudRenderQueueController tcrqc = tcrqcGO.AddComponent<TransparentCloudRenderQueueController>(); var tcrqc = tcrqcGO.AddComponent<TransparentCloudRenderQueueController>();
tcrqc.renderer = renderer; tcrqc.renderer = renderer;
} }

View File

@ -7,8 +7,7 @@ namespace NewHorizons.Builder.Atmosphere
{ {
private static readonly int FogColor = Shader.PropertyToID("_FogColor"); private static readonly int FogColor = Shader.PropertyToID("_FogColor");
private static Material _gdMaterial; private static Material _gdMaterial, _gdCloudMaterial;
private static Material _gdCloudMaterial;
private static bool _isInit; private static bool _isInit;
@ -21,7 +20,7 @@ namespace NewHorizons.Builder.Atmosphere
if (_gdMaterial == null) _gdMaterial = new Material(SearchUtilities.Find("GiantsDeep_Body/Sector_GD/Volumes_GD/RulesetVolumes_GD").GetComponent<EffectRuleset>()._material).DontDestroyOnLoad(); if (_gdMaterial == null) _gdMaterial = new Material(SearchUtilities.Find("GiantsDeep_Body/Sector_GD/Volumes_GD/RulesetVolumes_GD").GetComponent<EffectRuleset>()._material).DontDestroyOnLoad();
if (_gdCloudMaterial == null) _gdCloudMaterial = new Material(SearchUtilities.Find("GiantsDeep_Body/Sector_GD/Volumes_GD/RulesetVolumes_GD").GetComponent<EffectRuleset>()._cloudMaterial).DontDestroyOnLoad(); if (_gdCloudMaterial == null) _gdCloudMaterial = new Material(SearchUtilities.Find("GiantsDeep_Body/Sector_GD/Volumes_GD/RulesetVolumes_GD").GetComponent<EffectRuleset>()._cloudMaterial).DontDestroyOnLoad();
} }
public static void Make(GameObject planetGO, OWRigidbody owrb, PlanetConfig config, float sphereOfInfluence) public static void Make(GameObject planetGO, OWRigidbody owrb, PlanetConfig config, float sphereOfInfluence)
{ {
InitPrefabs(); InitPrefabs();
@ -59,12 +58,23 @@ namespace NewHorizons.Builder.Atmosphere
ER._material = _gdMaterial; ER._material = _gdMaterial;
var cloudMaterial = new Material(_gdCloudMaterial); if (config.Atmosphere?.clouds != null)
if (config.Atmosphere?.clouds?.tint != null)
{ {
cloudMaterial.SetColor(FogColor, config.Atmosphere.clouds.tint.ToColor()); var cloudMaterial = new Material(_gdCloudMaterial);
if (config.Atmosphere?.clouds?.tint != null)
{
cloudMaterial.SetColor(FogColor, config.Atmosphere.clouds.tint.ToColor());
}
// For all prefabs but GD we want grey fog between clouds
// I can't find an EffectsRuleset on the QM so I don't know how it works
else if (config.Atmosphere.clouds.cloudsPrefab != External.Modules.CloudPrefabType.GiantsDeep)
{
cloudMaterial.SetColor(FogColor, new Color(43f/255f, 51f/255f, 57f/255f));
}
ER._cloudMaterial = cloudMaterial;
} }
ER._cloudMaterial = cloudMaterial;
volumesGO.transform.position = planetGO.transform.position; volumesGO.transform.position = planetGO.transform.position;
rulesetGO.SetActive(true); rulesetGO.SetActive(true);