Skip to content

Commit

Permalink
Merge pull request #13 from cvusmo/dev
Browse files Browse the repository at this point in the history
Adding volumetric shader information and slowly integrating the shade…
  • Loading branch information
blacksheepcosmo authored Sep 7, 2023
2 parents 6baba10 + d9b4b97 commit 9be9467
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 75 deletions.
83 changes: 83 additions & 0 deletions FFTProject/Controllers/AnimationBridge.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
}
43 changes: 0 additions & 43 deletions FFTProject/FFT - Backup.csproj

This file was deleted.

4 changes: 2 additions & 2 deletions FFTProject/FFT.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
<PackageReference Include="BepInEx.AssemblyPublicizer.MSBuild" Version="0.4.1" PrivateAssets="all" />
<PackageReference Include="BepInEx.Core" Version="5.*" />
<PackageReference Include="BepInEx.PluginInfoProps" Version="2.*" />
<PackageReference Include="HarmonyX" Version="2.10.1" />
<PackageReference Include="SpaceWarp" Version="1.4.0" />
<PackageReference Include="HarmonyX" Version="2.10.2" />
<PackageReference Include="SpaceWarp" Version="1.4.*" />
<PackageReference Include="UnityEngine.Modules" Version="2020.3.33.1" IncludeAssets="compile" />
</ItemGroup>
<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion FFTProject/FFTPlugin.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//|=====================Summary========================|0|
//| Initializer |1|
//|by cvusmo===========================================|4|
//|====================================================|1|
//|====================================================|2|
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
Expand Down
62 changes: 35 additions & 27 deletions FFTProject/Modules/Module_VentValve.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -17,7 +18,7 @@ public class Module_VentValve : PartBehaviourModule
public Dictionary<string, GameObject> fuelTankDict = new Dictionary<string, GameObject>();

public Animator Animator;
public Material vaporShaderMaterial;
public AnimationBridge animationBridge;
private Material _cachedMaterial;
private Data_VentValve _dataVentValve;
private Data_ValveParts _dataValveParts;
Expand All @@ -36,11 +37,12 @@ public override void OnInitialize()
{
base.OnInitialize();
_cachedRenderer = GetComponent<Renderer>();
_cachedMaterial = _cachedRenderer.material;
_cachedMaterial = _cachedRenderer.material;
AddDataModules();

if (PartBackingMode == PartBackingModes.Flight)
{
animationBridge = GetComponentInChildren<AnimationBridge>();
LazyInitializeVFX();
}
}
Expand Down Expand Up @@ -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.");
}
Expand Down
2 changes: 1 addition & 1 deletion FFTProject/Utilities/RefreshVesselData.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion FFTProject/Utilities/Utility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//| helper methods & properties |1|
//|by cvusmo===========================================|4|
//|====================================================|1|

//|====================================================|2|
using BepInEx.Logging;
using KSP.Game;
using KSP.Messages;
Expand Down

0 comments on commit 9be9467

Please sign in to comment.