Skip to content

Commit

Permalink
Merge pull request #336 from Kopernicus/noLinq
Browse files Browse the repository at this point in the history
fix editor ground
StollD authored Dec 31, 2018
2 parents 65a9b6b + b2ca5ed commit b4b2073
Showing 3 changed files with 194 additions and 23 deletions.
153 changes: 132 additions & 21 deletions src/Kopernicus.Components/KSC.cs
Original file line number Diff line number Diff line change
@@ -77,12 +77,32 @@ public class KSC : SerializableMonoBehaviour
[SerializeField]
public Double? decalLongitude;

// Material
// PQSCity Ground Material
[SerializeField]
public Texture2D mainTexture;
[SerializeField]
public Color? color;

// Editor Ground Material
[SerializeField]
public Texture2D editorGroundTex;
[SerializeField]
public Color? editorGroundColor;
[SerializeField]
public Vector2? editorGroundTexScale;
[SerializeField]
public Vector2? editorGroundTexOffset;

public Material groundMaterial;

// Current Instance
public static KSC Instance = null;

void Awake()
{
Instance = this;
}

// Mods
private CelestialBody body;
private PQSCity ksc;
@@ -97,7 +117,7 @@ public void Start()
Destroy(this);
return;
}

ksc = body.pqsController.GetComponentsInChildren<PQSCity>(true).First(m => m.name == "KSC");
mapDecal = body.pqsController.GetComponentsInChildren<PQSMod_MapDecalTangent>(true)
.First(m => m.name == "KSC");
@@ -186,7 +206,7 @@ public void Start()
if (lodvisibleRangeMult.HasValue)
{
foreach (PQSCity.LODRange lodRange in ksc.lod)
lodRange.visibleRange *= (Single) lodvisibleRangeMult.Value;
lodRange.visibleRange *= (Single)lodvisibleRangeMult.Value;
}
else
{
@@ -300,26 +320,46 @@ void Update()
// Loop through all Materials and change their settings
try
{
foreach (Material material in Resources.FindObjectsOfTypeAll<Material>().Where(m => m.HasProperty("_Color") && m.color.ToString() == new Color(0.640f, 0.728f, 0.171f, 0.729f).ToString()))
Material[] materials = Resources.FindObjectsOfTypeAll<Material>();

int? n = materials?.Length;

for (int i = 0; i < n; i++)
{
// Patch the texture
if (ksc.mainTexture != null)
{
material.mainTexture = ksc.mainTexture;
}
else
{
ksc.mainTexture = material.mainTexture as Texture2D;
}

// Patch the color
if (ksc.color.HasValue)
{
material.color = ksc.color.Value;
}
else
Material material = materials[i];

if (material?.HasProperty("_Color") == true && material?.color.ToString() == new Color(0.640f, 0.728f, 0.171f, 0.729f).ToString())
{
ksc.color = material.color;
if (ksc.groundMaterial != null)
{
material.shader = ksc.groundMaterial.shader;
material.CopyPropertiesFromMaterial(ksc.groundMaterial);
}
else
{
// Remember this material
ksc.groundMaterial = material;

// Patch the texture
if (ksc.mainTexture != null)
{
material.mainTexture = ksc.mainTexture;
}
else
{
ksc.mainTexture = material.mainTexture as Texture2D;
}

// Patch the color
if (ksc.color.HasValue)
{
material.color = ksc.color.Value;
}
else
{
ksc.color = material.color;
}
}
}
}
}
@@ -331,5 +371,76 @@ void Update()
}
}
}

[KSPAddon(KSPAddon.Startup.EditorAny, false)]
public class EditorMaterialFixer : MonoBehaviour
{
EditorFacility editor;

void Update()
{
if (editor != EditorDriver.editorFacility)
{
editor = EditorDriver.editorFacility;
FixMaterials();
}
}

void FixMaterials()
{
KSC ksc = KSC.Instance;

if (ksc == null) return;

Material[] materials = Resources.FindObjectsOfTypeAll<Material>();

int? n = materials?.Length;

for (int i = 0; i < n; i++)
{
Material material = materials[i];

if (material?.name == "ksc_exterior_terrain_grass_02")
{
if (ksc.groundMaterial != null)
{
material.shader = ksc.groundMaterial.shader;
material.CopyPropertiesFromMaterial(ksc.groundMaterial);
}
}

else

if (material?.name == "ksc_terrain_TX")
{
if (ksc.groundMaterial != null)
{
material.shader = ksc.groundMaterial.shader;
material.CopyPropertiesFromMaterial(ksc.groundMaterial);
}

if (ksc.editorGroundColor.HasValue)
{
material.color = ksc.editorGroundColor.Value;
}

if (ksc.editorGroundTex != null)
{
material.mainTexture = ksc.editorGroundTex;
}

if (ksc.editorGroundTexScale.HasValue)
{
material.mainTextureScale = ksc.editorGroundTexScale.Value;
}

if (ksc.editorGroundTexOffset.HasValue)
{
material.mainTextureOffset = ksc.editorGroundTexOffset.Value;
}
}
}
}
}
}
}
60 changes: 60 additions & 0 deletions src/Kopernicus/Configuration/SpaceCenterLoader.cs
Original file line number Diff line number Diff line change
@@ -346,6 +346,66 @@ public Texture2DParser groundTextureParser
}
}

// Editor Ground Color
[ParserTarget("editorGroundColor")]
[KittopiaDescription("The color of the grass all around the KSC (editor only).")]
public ColorParser editorGroundColorParser
{
get { return Value.editorGroundColor; }
set
{
Value.editorGroundColor = value;
if (!Injector.IsInPrefab)
{
Value.Start();
}
}
}

// Editor Ground Texture
[ParserTarget("editorGroundTex")]
[KittopiaDescription("The surface texture of the grass all around the KSC (editor only).")]
public Texture2DParser editorGroundTexParser
{
get { return Value.editorGroundTex; }
set
{
Value.editorGroundTex = value;
if (!Injector.IsInPrefab)
{
Value.Start();
}
}
}
[ParserTarget("editorGroundTexScale")]
[KittopiaDescription("The scale of the surface texture of the grass all around the KSC (editor only).")]
public Vector2Parser editorGroundTexScaleParser
{
get { return Value.editorGroundTexScale; }
set
{
Value.editorGroundTexScale = value;
if (!Injector.IsInPrefab)
{
Value.Start();
}
}
}
[ParserTarget("editorGroundTexOffset")]
[KittopiaDescription("The offset of the surface texture of the grass all around the KSC (editor only).")]
public Vector2Parser editorGroundTexOffsetParser
{
get { return Value.editorGroundTexOffset; }
set
{
Value.editorGroundTexOffset = value;
if (!Injector.IsInPrefab)
{
Value.Start();
}
}
}

// Apply event
void IParserEventSubscriber.Apply(ConfigNode node)
{
4 changes: 2 additions & 2 deletions src/Kopernicus/RuntimeUtility/MainMenuFixer.cs
Original file line number Diff line number Diff line change
@@ -41,15 +41,15 @@ void Awake()
MainMenu main = FindObjectOfType<MainMenu>();
if (main == null)
{
Debug.LogError("[SigmaLog] No main menu object!");
Debug.LogError("[Kopernicus] No main menu object!");
return;
}
MainMenuEnvLogic logic = main.envLogic;

// Set it to Space, because the Mun-Area won't work with sth else than Mun
if (logic.areas.Length < 2)
{
Debug.LogError("[SigmaLog] Not enough scenes");
Debug.LogError("[Kopernicus] Not enough scenes");
return;
}

0 comments on commit b4b2073

Please sign in to comment.