/
SSTUAnimateEngineHeat.cs
203 lines (166 loc) · 6.53 KB
/
SSTUAnimateEngineHeat.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
using System;
using System.Collections.Generic;
using UnityEngine;
namespace SSTUTools
{
public class SSTUAnimateEngineHeat : PartModule
{
//amount of 'heat' added per second at full throttle
[KSPField]
public float heatOutput = 300;
//amount of heat dissipated per second, adjusted by the heatDissipationCurve below
[KSPField]
public float heatDissipation = 100;
//point at which the object will begin to glow
[KSPField]
public float draperPoint = 400;
//maximum amount of heat allowed in this engine
//will reach max glow at this temp, and begin dissipating even faster past this point
[KSPField]
public float maxHeat = 2400;
//maxStoredHeat
//storedHeat will not go beyond this, sets retention period for maximum glow
[KSPField]
public float maxStoredHeat = 3600;
//curve to adjust heat dissipation; should generally expel heat faster when hotter
[KSPField]
public FloatCurve heatDissipationCurve = new FloatCurve();
//the heat-output curve for an engine (varies with thrust/throttle), in case it is not linear
[KSPField]
public FloatCurve heatAccumulationCurve = new FloatCurve();
[KSPField]
public FloatCurve redCurve = new FloatCurve();
[KSPField]
public FloatCurve blueCurve = new FloatCurve();
[KSPField]
public FloatCurve greenCurve = new FloatCurve();
[KSPField]
public string engineID = "Engine";
[KSPField]
public String meshName = String.Empty;
[KSPField(isPersistant = true)]
public float currentHeat = 0;
int shaderEmissiveID;
private ModuleEngines engineModule;
private Renderer[] animatedRenderers;
private Color emissiveColor = new Color(0f, 0f, 0f, 1f);
public override void OnAwake()
{
base.OnAwake();
heatDissipationCurve.Add(0f, 0.2f);
heatDissipationCurve.Add(1f, 1f);
heatAccumulationCurve.Add(0f, 0f);
heatAccumulationCurve.Add(1f, 1f);
redCurve.Add(0f, 0f);
redCurve.Add(1f, 1f);
blueCurve.Add(0f, 0f);
blueCurve.Add(1f, 1f);
greenCurve.Add(0f, 0f);
greenCurve.Add(1f, 1f);
shaderEmissiveID = Shader.PropertyToID("_EmissiveColor");
}
public override void OnStart(StartState state)
{
base.OnStart(state);
initialize();
}
public void FixedUpdate()
{
if (!HighLogic.LoadedSceneIsFlight) { return; }
updateHeat();
}
private void initialize()
{
locateAnimatedTransforms();
locateEngineModule();
}
public void reInitialize()
{
animatedRenderers = null;
engineModule = null;
initialize();
}
private void locateEngineModule()
{
engineModule = null;
ModuleEngines[] engines = part.GetComponents<ModuleEngines>();
int len = engines.Length;
for (int i = 0; i < len; i++)
{
if (engines[i].engineID == engineID)
{
engineModule = engines[i];
}
}
if (engineModule == null)
{
MonoBehaviour.print("ERROR: Could not locate engine by ID: " + engineID + " for part: " + part + " for SSTUAnimateEngineHeat. This will cause errors during gameplay. Setting engine to first engine module (if present)");
if (engines.Length > 0) { engineModule = engines[0]; }
}
}
private void locateAnimatedTransforms()
{
List<Renderer> renderers = new List<Renderer>();
Transform[] animatedTransforms = part.transform.FindChildren(meshName);
int len = animatedTransforms.Length;
for (int i = 0; i < len; i++)
{
renderers.AddRange(animatedTransforms[i].GetComponentsInChildren<Renderer>(false));
}
animatedRenderers = renderers.ToArray();
if (animatedRenderers == null || animatedRenderers.Length == 0) { print("ERROR: Could not locate any emissive meshes for name: " + meshName); }
}
private void updateHeat()
{
if (engineModule == null) { return; }
//add heat from engine
if (engineModule.EngineIgnited && !engineModule.flameout && engineModule.currentThrottle > 0)
{
float throttle = vessel.ctrlState.mainThrottle;
float heatIn = heatAccumulationCurve.Evaluate(throttle) * heatOutput * TimeWarp.fixedDeltaTime;
currentHeat += heatIn;
}
//dissipate heat
float heatPercent = currentHeat / maxHeat;
if (currentHeat > 0f)
{
float heatOut = heatDissipationCurve.Evaluate(heatPercent) * heatDissipation * TimeWarp.fixedDeltaTime;
if (heatOut > currentHeat) { heatOut = currentHeat; }
currentHeat -= heatOut;
}
if (currentHeat > maxStoredHeat) { currentHeat = maxStoredHeat; }
float emissivePercent = 0f;
float mhd = maxHeat - draperPoint;
float chd = currentHeat - draperPoint;
if (chd < 0f) { chd = 0f; }
emissivePercent = chd / mhd;
if (emissivePercent > 1f) { emissivePercent = 1f; }
emissiveColor.r = redCurve.Evaluate(emissivePercent);
emissiveColor.g = greenCurve.Evaluate(emissivePercent);
emissiveColor.b = blueCurve.Evaluate(emissivePercent);
setEmissiveColors();
}
private void setEmissiveColors()
{
if (animatedRenderers != null)
{
bool rebuild = false;
int len = animatedRenderers.Length;
for (int i = 0; i < len; i++)
{
if (animatedRenderers[i] == null)
{
rebuild = true;
continue;
}
animatedRenderers[i].sharedMaterial.SetColor(shaderEmissiveID, emissiveColor);
}
if (rebuild)
{
animatedRenderers = null;
locateAnimatedTransforms();
}
}
}
}
}