diff --git a/Distribution/Restock/GameData/ReStock/Patches/Resource/restock-isrus.cfg b/Distribution/Restock/GameData/ReStock/Patches/Resource/restock-isrus.cfg index 4da6a133..e47a212d 100644 --- a/Distribution/Restock/GameData/ReStock/Patches/Resource/restock-isrus.cfg +++ b/Distribution/Restock/GameData/ReStock/Patches/Resource/restock-isrus.cfg @@ -19,4 +19,35 @@ name = ModuleRestockISRUAnimation deployAnimationName = heater } + + MODULE + { + name = ModuleRestockHeatEffects + enableHeatEmissive = true + useCoreTemp = true + shaderProperty = _EmissiveColor + excludedRenderer = ISRU-125_heater + lerpMax = 2700 + redCurve + { + key = 0 0 2 2 + key = 1 1 0 0 + } + greenCurve + { + key = 0 0 1 1 + key = 1 1 1 1 + } + blueCurve + { + key = 0 0 1 1 + key = 1 1 1 1 + } + alphaCurve + { + key = 0 1 0 0 + key = 1 1 0 0 + } + disableBlackbody = true + } } \ No newline at end of file diff --git a/Distribution/Restock/GameData/ReStock/Plugins/Restock.dll b/Distribution/Restock/GameData/ReStock/Plugins/Restock.dll index 70255aee..3b94b8b0 100644 Binary files a/Distribution/Restock/GameData/ReStock/Plugins/Restock.dll and b/Distribution/Restock/GameData/ReStock/Plugins/Restock.dll differ diff --git a/Source/Restock/ModuleRestockHeatEffects.cs b/Source/Restock/ModuleRestockHeatEffects.cs new file mode 100644 index 00000000..951a9671 --- /dev/null +++ b/Source/Restock/ModuleRestockHeatEffects.cs @@ -0,0 +1,152 @@ +using System; +using UnityEngine; +using System.Collections.Generic; + +namespace Restock +{ + public class ModuleRestockHeatEffects : PartModule + { + [KSPField] public bool enableHeatEmissive = false; + + [KSPField] public string shaderProperty = string.Empty; + + [KSPField] public FloatCurve redCurve= new FloatCurve(); + + [KSPField] public FloatCurve greenCurve = new FloatCurve(); + + [KSPField] public FloatCurve blueCurve = new FloatCurve(); + + [KSPField] public FloatCurve alphaCurve = new FloatCurve(); + + [KSPField] public double draperPoint = 798.0; + + [KSPField] public double lerpMax = double.NaN; + + [KSPField] public double lerpMin = 0.0; + + [KSPField] public bool useCoreTemp = false; + + [KSPField] public bool useSkinTemp = false; + + [KSPField] public bool disableBlackbody = false; + + [KSPField] public List renderers = new List(); + + private readonly string _shaderBlackbody = "_TemperatureColor"; + + private ModuleCoreHeat _coreHeatModule = null; + + private int _shaderPropertyID; + + private int _shaderBlackbodyID; + + private double _lerpRange; + + private Color _emissiveColor = new Color(); + private MaterialPropertyBlock _propertyBlock = new MaterialPropertyBlock(); + + public void Start() + { + Debug.Log("Start()"); + if (base.vessel == null) return; + + if (enableHeatEmissive) + { + if (useCoreTemp) + { + _coreHeatModule = base.part.FindModuleImplementing(); + if (_coreHeatModule == null) + { + this.LogError("Part has no Core Heat module, skipping"); + useCoreTemp = false; + } + } + + if (double.IsNaN(lerpMax)) + { + if (useCoreTemp) + { + lerpMax = _coreHeatModule.CoreShutdownTemp; + } + else + { + lerpMax = useSkinTemp ? part.skinMaxTemp : part.maxTemp; + } + } + + _lerpRange = lerpMax - lerpMin - draperPoint; + + _shaderPropertyID = Shader.PropertyToID(shaderProperty); + } + + if (disableBlackbody) + { + _shaderBlackbodyID = Shader.PropertyToID(_shaderBlackbody); + } + } + + public override void OnLoad(ConfigNode node) + { + if (HighLogic.LoadedSceneIsEditor || HighLogic.LoadedSceneIsFlight) return; + + Debug.Log("OnLoad()"); + Debug.Log(node.ToString()); + + renderers = base.part.FindModelComponents(); + + if (node.HasValue("excludedRenderer")) + { + var excludedRenderers = new List(); + + excludedRenderers.AddRange(node.GetValues("excludedRenderer")); + + for (int i = renderers.Count - 1; i >= 0; i--) + { + if (renderers[i] == null || excludedRenderers.Contains(renderers[i].name)) + { + renderers.RemoveAt(i); + } + } + } + } + + public void LateUpdate() + { + if (!HighLogic.LoadedSceneIsFlight) return; + + if (enableHeatEmissive) + { + var temp = 0.0; + if (useCoreTemp) + { + temp = _coreHeatModule.CoreTemperature; + } + else + { + temp = useSkinTemp ? base.part.skinTemperature : base.part.temperature; + } + + var temp2 = (float) ((temp - draperPoint) / _lerpRange); + temp2 = Mathf.Clamp01(temp2); + + _emissiveColor.r = redCurve.Evaluate(temp2); + _emissiveColor.g = greenCurve.Evaluate(temp2); + _emissiveColor.b = blueCurve.Evaluate(temp2); + _emissiveColor.a = alphaCurve.Evaluate(temp2); + + _propertyBlock.SetColor(_shaderPropertyID, _emissiveColor); + } + + if (disableBlackbody) + { + _propertyBlock.SetColor(_shaderBlackbodyID, Color.black); + + } + + foreach (var r in renderers) + { + r.SetPropertyBlock(_propertyBlock); + } + } + } +} \ No newline at end of file diff --git a/Source/Restock/ModuleRestockISRUAnimation.cs b/Source/Restock/ModuleRestockISRUAnimation.cs index 729978e4..520235dc 100644 --- a/Source/Restock/ModuleRestockISRUAnimation.cs +++ b/Source/Restock/ModuleRestockISRUAnimation.cs @@ -4,38 +4,40 @@ using UnityEngine; namespace Restock { - public class ModuleRestockISRUAnimation: PartModule + public class ModuleRestockISRUAnimation : PartModule { - [KSPField] public string activeAnimationName = ""; - [KSPField] public string deployAnimationName = ""; + [KSPField] public float deploySpeed = 1.0f; + + [KSPField] public float retractSpeed = 1.0f; + public bool isDeployed = false; - - public Animation DeployAnimation - { - get; - private set; - } + + public Animation DeployAnimation { get; private set; } private List _modules; private bool _deployAnimationPresent = false; - + public void Start() { - if (base.vessel == null) return; _modules = base.part.FindModulesImplementing(); _deployAnimationPresent = (deployAnimationName != string.Empty); DeployAnimation = ((_deployAnimationPresent) ? base.part.FindModelAnimators(deployAnimationName)[0] : null); - foreach (var a in base.part.FindModelAnimators()) a.Stop(); - + foreach (var a in base.part.FindModelAnimators()) a.Stop(); } public override void OnLoad(ConfigNode node) { + if (!HighLogic.LoadedSceneIsFlight || base.vessel == null) + { + isDeployed = false; + return; + } + if (isDeployed) { PlayDeployAnimation(1000); @@ -45,17 +47,16 @@ namespace Restock PlayDeployAnimation(-1000); } } - + public void Update() { - if (!HighLogic.LoadedSceneIsFlight || base.vessel == null) return; + if (!HighLogic.LoadedSceneIsFlight) return; try { if (!CheatOptions.InfiniteElectricity) { var ecHash = PartResourceLibrary.ElectricityHashcode; base.vessel.GetConnectedResourceTotals(ecHash, out var ecAmount, out _, true); - Debug.Log(ecAmount); if (ecAmount < 0.1) { if (isDeployed) RetractModule(); @@ -68,6 +69,7 @@ namespace Restock { if (m.ModuleIsActive()) enabledCount++; } + if (isDeployed && enabledCount == 0) RetractModule(); else if (!isDeployed && enabledCount != 0) DeployModule(); } @@ -80,16 +82,16 @@ namespace Restock public void DeployModule() { isDeployed = true; - PlayDeployAnimation(1); + PlayDeployAnimation(1 * deploySpeed); } public void RetractModule() { isDeployed = false; - PlayDeployAnimation(-1); + PlayDeployAnimation(-1 * retractSpeed); } - private void PlayDeployAnimation(int speed) + private void PlayDeployAnimation(float speed) { if (_deployAnimationPresent) { @@ -102,6 +104,7 @@ namespace Restock { deployAnimationState.time = 0.0f; } + deployAnimationState.speed = speed; DeployAnimation.Play(deployAnimationName); } diff --git a/Source/Restock/Restock.csproj b/Source/Restock/Restock.csproj index 489f1437..5d3e8ee7 100644 --- a/Source/Restock/Restock.csproj +++ b/Source/Restock/Restock.csproj @@ -47,6 +47,7 @@ +