/
Atmosphere.cs
195 lines (172 loc) · 6.84 KB
/
Atmosphere.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
/**
* Kopernicus Planetary System Modifier
* Copyright (C) 2014 Bryce C Schroeder (bryce.schroeder@gmail.com), Nathaniel R. Lewis (linux.robotdude@gmail.com)
*
* http://www.ferazelhosting.net/~bryce/contact.html
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*
* This library is intended to be used as a plugin for Kerbal Space Program
* which is copyright 2011-2014 Squad. Your usage of Kerbal Space Program
* itself is governed by the terms of its EULA, not the license above.
*
* https://kerbalspaceprogram.com
*/
using System;
using System.Linq;
using System.Reflection;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Kopernicus
{
namespace Configuration
{
[RequireConfigType(ConfigType.Node)]
public class Atmosphere : IParserEventSubscriber
{
// Resoruces that will be edited
private GameObject scaledVersion;
private CelestialBody celestialBody;
// Do we have an atmosphere?
[PreApply]
[ParserTarget("enabled", optional = true)]
private NumericParser<bool> enabled
{
set { celestialBody.atmosphere = value.value; }
}
// Does this atmosphere contain oxygen
[ParserTarget("oxygen", optional = true)]
private NumericParser<bool> oxygen
{
set { celestialBody.atmosphereContainsOxygen = value.value; }
}
// Temperature curve (see below)
[ParserTarget("temperatureCurve", optional = true)]
private AnimationCurveParser temperatureCurve
{
set { celestialBody.temperatureCurve = value.curve; }
}
// Temperature multipler - I'm going to go out on a limb and suggest that this probably
// functions similarly to the new atmosphere model. Essentially
// (temperature = temperatureMultipler * temperatureCurve[altitude])
[ParserTarget("temperatureMultiplier", optional = true)]
private NumericParser<float> temperatureMultiplier
{
set { celestialBody.atmoshpereTemperatureMultiplier = value.value; }
}
// Static pressure at sea level (all worlds are set to 1.0f?)
[ParserTarget("staticPressureASL", optional = true)]
private NumericParser<float> staticPressureASL
{
set { celestialBody.staticPressureASL = value.value; }
}
// ditto (all worlds set to 1.4285f). Could be a *really* ancient atmosphere model
[ParserTarget("altitudeMultiplier", optional = true)]
private NumericParser<float> altitudeMultiplier
{
set { celestialBody.altitudeMultiplier = value.value; }
}
// Pressure curve (pressure = pressure multipler * pressureCurve[altitude])
[ParserTarget("pressureCurve", optional = true)]
private AnimationCurveParser pressureCurve
{
set { celestialBody.pressureCurve = value.curve; }
}
// Pressure multipler (pressure = pressure multipler * pressureCurve[altitude])
[ParserTarget("pressureMultiplier", optional = true)]
private NumericParser<float> pressureMultiplier
{
set { celestialBody.pressureMultiplier = value.value; }
}
// Use legacy atmosphere - the fact that every stock world uses legacy may suggest that
// the new atmosphere model may not work....
[ParserTarget("enableLegacyAtmosphere", optional = true)]
private NumericParser<bool> enableLegacyAtmosphere
{
set { celestialBody.useLegacyAtmosphere = value.value; }
}
// pressure (in atm) = multipler * e ^ -(altitude / (scaleHeight * 1000))
[ParserTarget("multiplier", optional = true)]
private NumericParser<float> multiplier
{
set { celestialBody.atmosphereMultiplier = value.value; }
}
// pressure (in atm) = atmosphereMultipler * e ^ -(altitude / (atmosphereScaleHeight * 1000))
[ParserTarget("scaleHeight", optional = true)]
private NumericParser<float> scaleHeight
{
set { celestialBody.atmosphereScaleHeight = value.value; }
}
// I honestly think this may actually offset the altitude. all stock worlds
// have it set to 0f, but the sun has it set to 700f (but doesn't use an
// atmosphere)
[ParserTarget("altitudeOffset", optional = true)]
private NumericParser<float> altitudeOffset
{
set { celestialBody.altitudeOffset = value.value; }
}
// atmosphere cutoff altitude
[ParserTarget("maxAltitude", optional = true)]
private NumericParser<float> maxAltitude
{
set { celestialBody.maxAtmosphereAltitude = value.value; }
}
// ambient atmosphere color
[ParserTarget("ambientColor", optional = true)]
private ColorParser ambientColor
{
set { celestialBody.atmosphericAmbientColor = value.value; }
}
// light color
[ParserTarget("lightColor", optional = true)]
private ColorParser lightColor
{
set { scaledVersion.GetComponentsInChildren<AtmosphereFromGround> (true) [0].waveLength = value.value; }
}
// Parser apply event
void IParserEventSubscriber.Apply (ConfigNode node)
{
// If we don't want an atmosphere, ignore this step
if(!celestialBody.atmosphere)
return;
// If we don't already have an atmospheric shell generated
if (scaledVersion.GetComponentsInChildren<AtmosphereFromGround> (true).Length == 0)
{
// Add the material light direction behavior
MaterialSetDirection materialLightDirection = scaledVersion.AddComponent<MaterialSetDirection>();
materialLightDirection.valueName = "_localLightDirection";
// Create the atmosphere shell game object
GameObject scaledAtmosphere = new GameObject("atmosphere");
scaledAtmosphere.transform.parent = scaledVersion.transform;
scaledAtmosphere.layer = Constants.GameLayers.ScaledSpaceAtmosphere;
MeshRenderer renderer = scaledAtmosphere.AddComponent<MeshRenderer>();
renderer.material = new Kopernicus.MaterialWrapper.AtmosphereFromGround();
scaledAtmosphere.AddComponent<MeshFilter>();
scaledAtmosphere.AddComponent<AtmosphereFromGround>();
}
}
// Parser post apply event
void IParserEventSubscriber.PostApply (ConfigNode node) { }
// Store the scaled version and celestial body we are modifying internally
public Atmosphere (CelestialBody celestialBody, GameObject scaledVersion)
{
this.scaledVersion = scaledVersion;
this.celestialBody = celestialBody;
}
}
}
}