From eff2ab8ad9f286524e1efcf155b7de101301ffb4 Mon Sep 17 00:00:00 2001 From: Nick Date: Fri, 21 Jul 2023 23:48:20 -0400 Subject: [PATCH] Make cloud fog default to prefab if no tint is given #182 --- .../Builder/Atmosphere/CloudsBuilder.cs | 65 +++++++++++-------- .../Builder/Atmosphere/VolumesBuilder.cs | 24 +++++-- 2 files changed, 55 insertions(+), 34 deletions(-) diff --git a/NewHorizons/Builder/Atmosphere/CloudsBuilder.cs b/NewHorizons/Builder/Atmosphere/CloudsBuilder.cs index c52c4164..ceef7fd5 100644 --- a/NewHorizons/Builder/Atmosphere/CloudsBuilder.cs +++ b/NewHorizons/Builder/Atmosphere/CloudsBuilder.cs @@ -8,6 +8,7 @@ using NewHorizons.Utility.OWML; using OWML.Common; using System; using System.Collections.Generic; +using System.Linq; using Tessellation; using UnityEngine; @@ -15,11 +16,12 @@ namespace NewHorizons.Builder.Atmosphere { public static class CloudsBuilder { - private static Material[] _gdCloudMaterials; - private static Material[] _qmCloudMaterials; - private static Material[] _qmBottomMaterials; + private static Material[] _gdCloudMaterials, _gdBottomMaterials; private static Mesh _gdTopCloudMesh; - private static Tessellation.MeshGroup _qmBottomMeshGroup; + + private static Material[] _qmCloudMaterials, _qmBottomMaterials; + private static MeshGroup _qmBottomMeshGroup; + private static Material _transparentCloud; private static GameObject _lightningPrefab; private static Texture2D _colorRamp; @@ -41,10 +43,14 @@ namespace NewHorizons.Builder.Atmosphere _isInit = true; 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().mesh.DontDestroyOnLoad(); if (_gdCloudMaterials == null) _gdCloudMaterials = SearchUtilities.Find("CloudsTopLayer_GD").GetComponent().sharedMaterials.MakePrefabMaterials(); + if (_gdBottomMaterials == null) _gdBottomMaterials = SearchUtilities.Find("CloudsBottomLayer_GD").GetComponent().sharedMaterials.MakePrefabMaterials(); + if (_qmCloudMaterials == null) _qmCloudMaterials = SearchUtilities.Find("CloudsTopLayer_QM").GetComponent().sharedMaterials.MakePrefabMaterials(); if (_qmBottomMaterials == null) _qmBottomMaterials = SearchUtilities.Find("CloudsBottomLayer_QM").GetComponent().sharedMaterials.MakePrefabMaterials(); + if (_qmBottomMeshGroup == null) { var originalMeshGroup = SearchUtilities.Find("CloudsBottomLayer_QM").GetComponent().tessellationMeshGroup; @@ -67,7 +73,7 @@ namespace NewHorizons.Builder.Atmosphere { InitPrefabs(); - GameObject cloudsMainGO = new GameObject("Clouds"); + var cloudsMainGO = new GameObject("Clouds"); cloudsMainGO.SetActive(false); cloudsMainGO.transform.parent = sector?.transform ?? planetGO.transform; @@ -81,20 +87,24 @@ namespace NewHorizons.Builder.Atmosphere return; } - GameObject cloudsBottomGO = new GameObject("BottomClouds"); + var cloudsBottomGO = new GameObject("BottomClouds"); cloudsBottomGO.SetActive(false); cloudsBottomGO.transform.parent = cloudsMainGO.transform; cloudsBottomGO.transform.localScale = Vector3.one * atmo.clouds.innerCloudRadius; - TessellatedSphereRenderer bottomTSR = cloudsBottomGO.AddComponent(); + var bottomTSR = cloudsBottomGO.AddComponent(); bottomTSR.tessellationMeshGroup = _qmBottomMeshGroup; - var bottomTSRMaterials = _qmBottomMaterials; - // If they set a colour apply it to all the materials else keep the default QM one - if (atmo.clouds.tint != null) + var bottomTSRMaterials = atmo.clouds.cloudsPrefab == CloudPrefabType.GiantsDeep ? _gdBottomMaterials : _qmBottomMaterials; + + // 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 bottomTSRTempArray = new Material[2]; bottomTSRTempArray[0] = new Material(bottomTSRMaterials[0]); @@ -105,34 +115,34 @@ namespace NewHorizons.Builder.Atmosphere bottomTSR.sharedMaterials = bottomTSRTempArray; } - else - { - bottomTSR.sharedMaterials = bottomTSRMaterials; - } bottomTSR.maxLOD = 6; bottomTSR.LODBias = 0; bottomTSR.LODRadius = 1f; if (cloaked) + { cloudsBottomGO.AddComponent()._sector = sector; + } else + { cloudsBottomGO.AddComponent()._sector = sector; + } - GameObject cloudsFluidGO = new GameObject("CloudsFluid"); + var cloudsFluidGO = new GameObject("CloudsFluid"); cloudsFluidGO.SetActive(false); cloudsFluidGO.layer = Layer.BasicEffectVolume; cloudsFluidGO.transform.parent = cloudsMainGO.transform; - SphereCollider fluidSC = cloudsFluidGO.AddComponent(); + var fluidSC = cloudsFluidGO.AddComponent(); fluidSC.isTrigger = true; fluidSC.radius = atmo.clouds.outerCloudRadius; - OWShellCollider fluidOWSC = cloudsFluidGO.AddComponent(); + var fluidOWSC = cloudsFluidGO.AddComponent(); fluidOWSC._innerRadius = atmo.clouds.innerCloudRadius; // copied from gd - CloudLayerFluidVolume fluidCLFV = cloudsFluidGO.AddComponent(); + var fluidCLFV = cloudsFluidGO.AddComponent(); fluidCLFV._layer = 5; fluidCLFV._priority = 1; fluidCLFV._density = 1.2f; @@ -212,6 +222,7 @@ namespace NewHorizons.Builder.Atmosphere if (atmo.clouds.capPath == null) cap = ImageUtilities.ClearTexture(128, 128, wrap: true); else cap = ImageUtilities.GetTexture(mod, atmo.clouds.capPath, wrap: true); + if (atmo.clouds.rampPath == null) ramp = ImageUtilities.CanvasScaled(image, 1, image.height); else ramp = ImageUtilities.GetTexture(mod, atmo.clouds.rampPath); } @@ -221,17 +232,17 @@ namespace NewHorizons.Builder.Atmosphere return null; } - GameObject cloudsTopGO = new GameObject("TopClouds"); + var cloudsTopGO = new GameObject("TopClouds"); cloudsTopGO.SetActive(false); cloudsTopGO.transform.parent = rootObject.transform; cloudsTopGO.transform.localScale = Vector3.one * atmo.clouds.outerCloudRadius; - MeshFilter topMF = cloudsTopGO.AddComponent(); + var topMF = cloudsTopGO.AddComponent(); topMF.mesh = _gdTopCloudMesh; - MeshRenderer topMR = cloudsTopGO.AddComponent(); + var topMR = cloudsTopGO.AddComponent(); - Material[] prefabMaterials = atmo.clouds.cloudsPrefab == CloudPrefabType.GiantsDeep ? _gdCloudMaterials : _qmCloudMaterials; + var prefabMaterials = atmo.clouds.cloudsPrefab == CloudPrefabType.GiantsDeep ? _gdCloudMaterials : _qmCloudMaterials; var tempArray = new Material[2]; if (atmo.clouds.cloudsPrefab == CloudPrefabType.Basic) @@ -297,7 +308,7 @@ namespace NewHorizons.Builder.Atmosphere return null; } - GameObject cloudsTransparentGO = new GameObject("TransparentClouds"); + var cloudsTransparentGO = new GameObject("TransparentClouds"); cloudsTransparentGO.SetActive(false); cloudsTransparentGO.transform.parent = rootObject.transform; cloudsTransparentGO.transform.localScale = Vector3.one * atmo.clouds.outerCloudRadius; @@ -305,7 +316,7 @@ namespace NewHorizons.Builder.Atmosphere MeshFilter filter = cloudsTransparentGO.AddComponent(); filter.mesh = _gdTopCloudMesh; - MeshRenderer renderer = cloudsTransparentGO.AddComponent(); + var renderer = cloudsTransparentGO.AddComponent(); var material = new Material(_transparentCloud); material.name = "TransparentClouds_" + image.name; material.SetTexture(MainTex, image); @@ -313,7 +324,7 @@ namespace NewHorizons.Builder.Atmosphere if (!isProxy) { - GameObject tcrqcGO = new GameObject("TransparentCloudRenderQueueController"); + var tcrqcGO = new GameObject("TransparentCloudRenderQueueController"); tcrqcGO.transform.SetParent(cloudsTransparentGO.transform, false); tcrqcGO.layer = Layer.BasicEffectVolume; @@ -323,7 +334,7 @@ namespace NewHorizons.Builder.Atmosphere var owTriggerVolume = tcrqcGO.AddComponent(); owTriggerVolume._shape = shape; - TransparentCloudRenderQueueController tcrqc = tcrqcGO.AddComponent(); + var tcrqc = tcrqcGO.AddComponent(); tcrqc.renderer = renderer; } diff --git a/NewHorizons/Builder/Atmosphere/VolumesBuilder.cs b/NewHorizons/Builder/Atmosphere/VolumesBuilder.cs index d2edd47b..1e4f9e32 100644 --- a/NewHorizons/Builder/Atmosphere/VolumesBuilder.cs +++ b/NewHorizons/Builder/Atmosphere/VolumesBuilder.cs @@ -7,8 +7,7 @@ namespace NewHorizons.Builder.Atmosphere { private static readonly int FogColor = Shader.PropertyToID("_FogColor"); - private static Material _gdMaterial; - private static Material _gdCloudMaterial; + private static Material _gdMaterial, _gdCloudMaterial; 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()._material).DontDestroyOnLoad(); if (_gdCloudMaterial == null) _gdCloudMaterial = new Material(SearchUtilities.Find("GiantsDeep_Body/Sector_GD/Volumes_GD/RulesetVolumes_GD").GetComponent()._cloudMaterial).DontDestroyOnLoad(); } - + public static void Make(GameObject planetGO, OWRigidbody owrb, PlanetConfig config, float sphereOfInfluence) { InitPrefabs(); @@ -59,12 +58,23 @@ namespace NewHorizons.Builder.Atmosphere ER._material = _gdMaterial; - var cloudMaterial = new Material(_gdCloudMaterial); - if (config.Atmosphere?.clouds?.tint != null) + if (config.Atmosphere?.clouds != 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; rulesetGO.SetActive(true);