mirror of
https://github.com/PorktoberRevolution/ReStocked
synced 2024-09-01 17:34:42 +00:00
Material modifers can be scoped by transform
Each MATERIAL node can take transform or baseTransform values which look for renderers on that transform or that transform and children respectively. If none are specified it will use every renderer in the model.
This commit is contained in:
parent
8d8795c57d
commit
ddb79541f5
Binary file not shown.
@ -1,4 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
using Restock.MaterialModifiers;
|
||||
|
||||
@ -12,18 +14,10 @@ namespace Restock
|
||||
|
||||
if (HighLogic.LoadedSceneIsEditor || HighLogic.LoadedSceneIsFlight) return;
|
||||
|
||||
Transform modelTransform = part.partTransform.Find("model");
|
||||
|
||||
Renderer[] renderers = modelTransform.GetComponentsInChildren<Renderer>();
|
||||
|
||||
if (modelTransform == null)
|
||||
{
|
||||
Debug.LogError("Couldn't find model transform");
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (ConfigNode node2 in node.GetNodes("MATERIAL"))
|
||||
{
|
||||
IEnumerable<Renderer> renderers = GetRenderers(node2);
|
||||
|
||||
if (node2.GetValue("shader") is string newShaderName)
|
||||
{
|
||||
if (Shader.Find(newShaderName) is Shader newShader)
|
||||
@ -66,5 +60,73 @@ namespace Restock
|
||||
isEnabled = false;
|
||||
moduleIsEnabled = false;
|
||||
}
|
||||
|
||||
private IEnumerable<Renderer> GetRenderers(ConfigNode node)
|
||||
{
|
||||
IEnumerable<Renderer> renderers = Enumerable.Empty<Renderer>();
|
||||
bool useAllRenderers = true;
|
||||
|
||||
foreach (ConfigNode.Value value in node.values)
|
||||
{
|
||||
if (value.name == "transform")
|
||||
{
|
||||
Transform[] modelTransforms = part.FindModelTransforms(value.name);
|
||||
|
||||
if (modelTransforms.Length == 0)
|
||||
{
|
||||
Debug.LogError($"Couldn't find transform named '{value.name}' on part");
|
||||
continue;
|
||||
}
|
||||
|
||||
List<Renderer> transformRenderers = new List<Renderer>(modelTransforms.Length);
|
||||
foreach (Transform transform in modelTransforms)
|
||||
{
|
||||
Renderer renderer = transform.GetComponent<Renderer>();
|
||||
|
||||
if (renderer == null)
|
||||
Debug.LogError($"No renderer found on transform '{transform.name}'");
|
||||
else
|
||||
transformRenderers.Add(renderer);
|
||||
}
|
||||
|
||||
renderers.Concat(transformRenderers);
|
||||
useAllRenderers = false;
|
||||
}
|
||||
else if (value.name == "baseTransform")
|
||||
{
|
||||
Transform[] modelTransforms = part.FindModelTransforms(value.name);
|
||||
|
||||
if (modelTransforms.Length == 0)
|
||||
{
|
||||
Debug.LogError($"Couldn't find transform named '{value.name}' on part");
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach (Transform transform in modelTransforms)
|
||||
{
|
||||
Renderer[] transformRenderers = transform.GetComponentsInChildren<Renderer>();
|
||||
|
||||
if (transformRenderers.Length == 0)
|
||||
Debug.LogError($"No renderers found on transform '{transform.name}' or its children");
|
||||
else
|
||||
renderers.Concat(transform.GetComponentsInChildren<Renderer>());
|
||||
}
|
||||
|
||||
useAllRenderers = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (useAllRenderers)
|
||||
{
|
||||
Transform modelTransform = part.partTransform.Find("model");
|
||||
|
||||
if (modelTransform == null)
|
||||
Debug.LogError("Couldn't find model transform");
|
||||
else
|
||||
renderers = modelTransform.GetComponentsInChildren<Renderer>();
|
||||
}
|
||||
|
||||
return renderers;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user