From d9b4b97653817820a751fdd71003c2c3e6d6e35a Mon Sep 17 00:00:00 2001 From: cvusmo Date: Wed, 6 Sep 2023 22:47:25 -0500 Subject: [PATCH] Adding volumetric shader information and slowly integrating the shader to overhaul VFX --- FFTProject/Controllers/AnimationBridge.cs | 83 +++++++++++++++++++++++ FFTProject/FFT - Backup.csproj | 43 ------------ FFTProject/FFT.csproj | 4 +- FFTProject/FFTPlugin.cs | 2 +- FFTProject/Modules/Module_VentValve.cs | 62 +++++++++-------- FFTProject/Utilities/RefreshVesselData.cs | 2 +- FFTProject/Utilities/Utility.cs | 2 +- 7 files changed, 123 insertions(+), 75 deletions(-) create mode 100644 FFTProject/Controllers/AnimationBridge.cs delete mode 100644 FFTProject/FFT - Backup.csproj diff --git a/FFTProject/Controllers/AnimationBridge.cs b/FFTProject/Controllers/AnimationBridge.cs new file mode 100644 index 0000000..ec18111 --- /dev/null +++ b/FFTProject/Controllers/AnimationBridge.cs @@ -0,0 +1,83 @@ +//|=====================Summary========================|0| +//| Animation Bridge |1| +//|by cvusmo===========================================|4| +//|====================================================|2| +using FFT.Modules; +using FFT.Utilities; +using UnityEngine; + +namespace FFT.Controllers +{ + public class AnimationBridge : MonoBehaviour + { + public Material targetMaterial; + public Texture3D[] vaporTextures; + public float animationValue; + + public float intensity = 0.3f; + public float stepDistance = 0.01f; + + private int currentTextureIndex = 0; + private float timeSinceLastChange = 0.0f; + private void Awake() + { + SetTexture(); + } + private void Update() + { + if (targetMaterial != null) + { + timeSinceLastChange += Time.deltaTime; + + if (timeSinceLastChange >= animationValue) + { + SetNextTexture(); + timeSinceLastChange = 0f; + } + } + } + private void SetTexture() + { + if (vaporTextures != null && vaporTextures.Length > 0) + { + targetMaterial.SetTexture("_Volume", vaporTextures[currentTextureIndex]); + } + } + private void SetNextTexture() + { + currentTextureIndex = (currentTextureIndex + 1) % vaporTextures.Length; + SetTexture(); + } + public void UpdateShaderProperties(Material cachedMaterial, float customIntensity, float customStepDistance, float customASL, float customAGL, float customVV, float customHV, float customDP, float customSP, float customAT, float customET, float customFL) + { + if (targetMaterial) + { + targetMaterial.SetFloat("_Intensity", customIntensity); + targetMaterial.SetFloat("_StepDistance", customStepDistance); + targetMaterial.SetFloat("_ASL", customASL); + targetMaterial.SetFloat("_AGL", customAGL); + targetMaterial.SetFloat("_VV", customVV); + targetMaterial.SetFloat("_HV", customHV); + targetMaterial.SetFloat("_DP", customDP); + targetMaterial.SetFloat("_SP", customSP); + targetMaterial.SetFloat("_AT", customAT); + targetMaterial.SetFloat("_ET", customET); + targetMaterial.SetFloat("_FL", customFL); + } + if (cachedMaterial) + { + cachedMaterial.SetFloat("_Intensity", customIntensity); + cachedMaterial.SetFloat("_StepDistance", customStepDistance); + cachedMaterial.SetFloat("_ASL", customASL); + cachedMaterial.SetFloat("_AGL", customAGL); + cachedMaterial.SetFloat("_VV", customVV); + cachedMaterial.SetFloat("_HV", customHV); + cachedMaterial.SetFloat("_DP", customDP); + cachedMaterial.SetFloat("_SP", customSP); + cachedMaterial.SetFloat("_AT", customAT); + cachedMaterial.SetFloat("_ET", customET); + cachedMaterial.SetFloat("_FL", customFL); + } + } + } +} \ No newline at end of file diff --git a/FFTProject/FFT - Backup.csproj b/FFTProject/FFT - Backup.csproj deleted file mode 100644 index fac7c20..0000000 --- a/FFTProject/FFT - Backup.csproj +++ /dev/null @@ -1,43 +0,0 @@ - - - - netstandard2.0 - true - latest - true - com.github.cvusmo.fancyfueltanks - FFT - Fancy Fuel Tanks - 0.1.4 - - https://nuget.spacewarp.org/v3/index.json - - FFT - FFT - https://github.com/cvusmo/FFT - - KSP2-mods - - - True - - - - - - - - - - - - - ..\..\..\..\..\..\..\Program Files (x86)\Steam\steamapps\common\Kerbal Space Program 2\KSP2_x64_Data\Managed\Assembly-CSharp.dll - true - false - - - - - - \ No newline at end of file diff --git a/FFTProject/FFT.csproj b/FFTProject/FFT.csproj index 54b1f2a..2eb51cb 100644 --- a/FFTProject/FFT.csproj +++ b/FFTProject/FFT.csproj @@ -26,8 +26,8 @@ - - + + diff --git a/FFTProject/FFTPlugin.cs b/FFTProject/FFTPlugin.cs index 616a8dd..bb8478f 100644 --- a/FFTProject/FFTPlugin.cs +++ b/FFTProject/FFTPlugin.cs @@ -1,7 +1,7 @@ //|=====================Summary========================|0| //| Initializer |1| //|by cvusmo===========================================|4| -//|====================================================|1| +//|====================================================|2| using BepInEx; using BepInEx.Configuration; using BepInEx.Logging; diff --git a/FFTProject/Modules/Module_VentValve.cs b/FFTProject/Modules/Module_VentValve.cs index aa359bd..dbdfa7e 100644 --- a/FFTProject/Modules/Module_VentValve.cs +++ b/FFTProject/Modules/Module_VentValve.cs @@ -1,7 +1,8 @@ //|=====================Summary========================|0| //| Module for Cooling/Vent VFX |1| //|by cvusmo===========================================|4| -//|====================================================|1| +//|====================================================|2| +using FFT.Controllers; using FFT.Utilities; using KSP.Sim.Definitions; using UnityEngine; @@ -17,7 +18,7 @@ public class Module_VentValve : PartBehaviourModule public Dictionary fuelTankDict = new Dictionary(); public Animator Animator; - public Material vaporShaderMaterial; + public AnimationBridge animationBridge; private Material _cachedMaterial; private Data_VentValve _dataVentValve; private Data_ValveParts _dataValveParts; @@ -36,11 +37,12 @@ public override void OnInitialize() { base.OnInitialize(); _cachedRenderer = GetComponent(); - _cachedMaterial = _cachedRenderer.material; + _cachedMaterial = _cachedRenderer.material; AddDataModules(); if (PartBackingMode == PartBackingModes.Flight) { + animationBridge = GetComponentInChildren(); LazyInitializeVFX(); } } @@ -118,56 +120,62 @@ private float GetCurveValue(AnimationCurve curve, float inputValue) private void UpdateVFX() { var vesselData = RefreshVesselData.Instance; + Material vaporMaterial = _cachedRenderer.material; ASL = GetCurveValue(_dataVentValve.VFXASLCurve, (float)vesselData.AltitudeAsl); - Animator.SetFloat("ASL", ASL); - AGL = GetCurveValue(_dataVentValve.VFXAGLCurve, (float)vesselData.AltitudeAgl); - Animator.SetFloat("AGL", AGL); - VV = GetCurveValue(_dataVentValve.VFXVerticalVelocity, (float)vesselData.VerticalVelocity); - Animator.SetFloat("VV", VV); - HV = GetCurveValue(_dataVentValve.VFXHorizontalVelocity, (float)vesselData.HorizontalVelocity); - Animator.SetFloat("HV", HV); - DP = GetCurveValue(_dataVentValve.VFXDynamicPressure, (float)vesselData.DynamicPressure_kPa); - Animator.SetFloat("DP", DP); - SP = GetCurveValue(_dataVentValve.VFXStaticPressure, (float)vesselData.StaticPressure_kPa); - Animator.SetFloat("SP", SP); - AT = GetCurveValue(_dataVentValve.VFXAtmosphericTemperature, (float)vesselData.AtmosphericTemperature); - Animator.SetFloat("AT", AT); - ET = GetCurveValue(_dataVentValve.VFXExternalTemperature, (float)vesselData.ExternalTemperature); - Animator.SetFloat("ET", ET); - - InAtmo = vesselData.IsInAtmosphere; - double scaledFuelPercentage = vesselData.FuelPercentage / 100.0; FL = _dataVentValve.VFXFuelPercentage.Evaluate((float)scaledFuelPercentage); + + // Updating Animator + Animator.SetFloat("ASL", ASL); + Animator.SetFloat("AGL", AGL); + Animator.SetFloat("VV", VV); + Animator.SetFloat("HV", HV); + Animator.SetFloat("DP", DP); + Animator.SetFloat("SP", SP); + Animator.SetFloat("AT", AT); + Animator.SetFloat("ET", ET); Animator.SetFloat("FL", FL); + // Update _cachedMaterial _cachedMaterial.SetFloat("_ASL", ASL); _cachedMaterial.SetFloat("_AGL", AGL); - - Material vaporShaderMaterial = _cachedRenderer.material; - vaporShaderMaterial.SetFloat("_ASL", ASL); - vaporShaderMaterial.SetFloat("_AGL", AGL); + _cachedMaterial.SetFloat("_VV", VV); + _cachedMaterial.SetFloat("_HV", HV); + _cachedMaterial.SetFloat("_DP", DP); + _cachedMaterial.SetFloat("_SP", SP); + _cachedMaterial.SetFloat("_AT", AT); + _cachedMaterial.SetFloat("_ET", ET); + _cachedMaterial.SetFloat("_FL", FL); + + if (animationBridge != null) + { + float adjustedIntensity = animationBridge.intensity * ASL * 0.1f; + float adjustedStepDistance = animationBridge.stepDistance * AGL * 0.1f; + animationBridge.UpdateShaderProperties(_cachedMaterial, adjustedIntensity, adjustedStepDistance, ASL, AGL, VV, HV, DP, SP, AT, ET, FL); + } } internal void Activate() { + Material vaporMaterial = _cachedRenderer.material; _cachedMaterial.SetFloat("_IsVFXActive", 0.0f); - vaporShaderMaterial.SetFloat("_IsVFXActive", 1.0f); + vaporMaterial.SetFloat("_IsVFXActive", 1.0f); RefreshDataAndVFX(); ActivateModule = true; Debug.Log("Module_VentValve activated."); } internal void Deactivate() { + Material vaporMaterial = _cachedRenderer.material; _cachedMaterial.SetFloat("_IsVFXActive", 1.0f); - vaporShaderMaterial.SetFloat("_IsVFXActive", 0.0f); + vaporMaterial.SetFloat("_IsVFXActive", 0.0f); ActivateModule = false; Debug.Log("Module_VentValve deactivated."); } diff --git a/FFTProject/Utilities/RefreshVesselData.cs b/FFTProject/Utilities/RefreshVesselData.cs index c42aa77..85db4d3 100644 --- a/FFTProject/Utilities/RefreshVesselData.cs +++ b/FFTProject/Utilities/RefreshVesselData.cs @@ -1,7 +1,7 @@ //|=====================Summary========================|0| //| Refreshes Data & gets values to update VFX |1| //|by cvusmo===========================================|4| -//|====================================================|1| +//|====================================================|2| using FFT.Managers; using KSP.Game; diff --git a/FFTProject/Utilities/Utility.cs b/FFTProject/Utilities/Utility.cs index f104a9b..7f806c2 100644 --- a/FFTProject/Utilities/Utility.cs +++ b/FFTProject/Utilities/Utility.cs @@ -2,7 +2,7 @@ //| helper methods & properties |1| //|by cvusmo===========================================|4| //|====================================================|1| - +//|====================================================|2| using BepInEx.Logging; using KSP.Game; using KSP.Messages;