Skip to content

Commit

Permalink
Material loading
Browse files Browse the repository at this point in the history
  • Loading branch information
cheese3660 committed Jan 5, 2024
1 parent 509f00d commit 1760f65
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/Shoemaker/Patches/CelestialBodyBehaviorPatches.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using HarmonyLib;
using KSP;
using KSP.Game;
using KSP.Rendering.Planets;
using KSP.Sim.impl;
using UnityEngine;

Expand All @@ -9,11 +10,27 @@ namespace Shoemaker.Patches;
[HarmonyPatch(typeof(CelestialBodyBehavior))]
internal static class CelestialBodyBehaviourPatches
{
private const string Ksp2UnityToolsLocalPath = "KSP2/Planets/Local";
private const string Ksp2UnityToolsScaledPath = "KSP2/Planets/Local";
private const string Ksp2LocalPath = "KSP2/Environment/CelestialBody/CelestialBody_Local";
private const string Ksp2ScaledPath = "KSP2/Environment/CelestialBody/CelestialBody_Scaled";

[HarmonyPatch(nameof(CelestialBodyBehavior.OnScaledSpaceViewInstantiated))]
[HarmonyPrefix]
internal static void MergeData(CelestialBodyBehavior __instance, GameObject instance)
{
foreach (var renderer in instance.GetComponents<Renderer>())
{
if (renderer.material.shader.name != Ksp2UnityToolsScaledPath) continue;
var material = renderer.material;
var mat = new Material(Shader.Find(Ksp2ScaledPath))
{
name = material.name
};
mat.CopyPropertiesFromMaterial(material);
renderer.material = mat;
}

var data = instance.GetComponent<CoreCelestialBodyData>();
var oldRadius = data.Data.radius;
var name = data.Data.bodyName;
Expand All @@ -22,6 +39,37 @@ internal static void MergeData(CelestialBodyBehavior __instance, GameObject inst
OverrideManager.Scales.TryAdd(name.ToLowerInvariant(), newRadius / oldRadius);
data.core = newData;
}

[HarmonyPatch(nameof(CelestialBodyBehavior.OnLocalSpaceViewInstantiated))]
[HarmonyPrefix]
internal static void ChangeMaterials(CelestialBodyBehavior __instance, GameObject obj)
{
foreach (var pqs in obj.GetComponents<PQS>())
{
var data = pqs.data;
if (data.materialSettings.surfaceMaterial.shader.name == Ksp2UnityToolsLocalPath)
{
var material = data.materialSettings.surfaceMaterial;
var mat = new Material(Shader.Find(Ksp2LocalPath))
{
name = material.name
};
mat.CopyPropertiesFromMaterial(material);
data.materialSettings.surfaceMaterial = mat;
}

if (data.materialSettings.scaledSpaceMaterial.shader.name == Ksp2UnityToolsScaledPath)
{
var material = data.materialSettings.scaledSpaceMaterial;
var mat = new Material(Shader.Find(Ksp2ScaledPath))
{
name = material.name
};
mat.CopyPropertiesFromMaterial(material);
data.materialSettings.scaledSpaceMaterial = mat;
}
}
}
/*
[HarmonyPatch(nameof(CelestialBodyBehavior.OnLocalSpaceViewInstantiated))]
[HarmonyPrefix]
Expand Down
2 changes: 2 additions & 0 deletions src/Shoemaker/Patches/ScaledCloudDataModelComponentPatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ namespace Shoemaker.Patches;
[HarmonyPatch(typeof(ScaledCloudDataModelComponent))]
internal static class ScaledCloudDataModelComponentPatches
{


[HarmonyPatch(nameof(ScaledCloudDataModelComponent.Initialize))]
[HarmonyPrefix]
public static void UpdateCoreCelestialBodyData(ScaledCloudDataModelComponent __instance)
Expand Down

0 comments on commit 1760f65

Please sign in to comment.