mirror of
https://github.com/drewcassidy/KSP-DepthMask
synced 2024-09-01 14:54:35 +00:00
Allow for multiple body and mask transforms
using a comma-seperated list
This commit is contained in:
parent
97c5ef53de
commit
30f898f714
@ -14,4 +14,28 @@ Global
|
||||
{AF7B4795-7A4F-4238-BF1D-F0AC763C15C8}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{AF7B4795-7A4F-4238-BF1D-F0AC763C15C8}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(MonoDevelopProperties) = preSolution
|
||||
Policies = $0
|
||||
$0.DotNetNamingPolicy = $1
|
||||
$1.DirectoryNamespaceAssociation = PrefixedHierarchical
|
||||
$0.TextStylePolicy = $2
|
||||
$2.FileWidth = 80
|
||||
$2.TabsToSpaces = True
|
||||
$2.scope = text/x-csharp
|
||||
$2.NoTabsAfterNonTabs = True
|
||||
$2.EolMarker = Unix
|
||||
$0.CSharpFormattingPolicy = $3
|
||||
$3.NewLinesForBracesInTypes = False
|
||||
$3.NewLinesForBracesInMethods = False
|
||||
$3.NewLinesForBracesInProperties = False
|
||||
$3.NewLinesForBracesInAccessors = False
|
||||
$3.NewLinesForBracesInAnonymousMethods = False
|
||||
$3.NewLinesForBracesInControlBlocks = False
|
||||
$3.NewLinesForBracesInAnonymousTypes = False
|
||||
$3.NewLinesForBracesInObjectCollectionArrayInitializers = False
|
||||
$3.NewLinesForBracesInLambdaExpressionBody = False
|
||||
$3.scope = text/x-csharp
|
||||
$3.SpaceAfterCast = True
|
||||
$0.StandardHeader = $4
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
|
@ -1,42 +1,36 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.Serialization;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace DepthMask
|
||||
{
|
||||
public class ModuleDepthMask : PartModule
|
||||
{
|
||||
namespace DepthMask {
|
||||
public class ModuleDepthMask : PartModule {
|
||||
// The name of the transform that has your mask mesh. The only strictly required property
|
||||
[KSPField]
|
||||
public string maskTransform = "";
|
||||
[KSPField] public string maskTransform = "";
|
||||
|
||||
[KSPField]
|
||||
public string bodyTransform = "";
|
||||
[KSPField] public string bodyTransform = "";
|
||||
|
||||
// The name of the depth mask shader
|
||||
[KSPField]
|
||||
public string shaderName = "DepthMask";
|
||||
[KSPField] public string shaderName = "DepthMask";
|
||||
|
||||
// The render queue value for the mesh, should be less than maskRenderQueue
|
||||
[KSPField]
|
||||
public int meshRenderQueue = 1000;
|
||||
[KSPField] public int meshRenderQueue = 1000;
|
||||
|
||||
// the render queue value for the mask, should be less than 2000
|
||||
[KSPField]
|
||||
public int maskRenderQueue = 1999;
|
||||
[KSPField] public int maskRenderQueue = 1999;
|
||||
|
||||
|
||||
// depth mask object transforms
|
||||
public Transform[] maskTransformObjects;
|
||||
public List<Transform> maskTransformObjects;
|
||||
|
||||
// body object transform
|
||||
public Transform bodyTransformObject;
|
||||
public List<Transform> bodyTransformObjects;
|
||||
|
||||
// depth mask shader object
|
||||
public Shader depthShader;
|
||||
|
||||
|
||||
public override void OnStart(StartState state)
|
||||
{
|
||||
public override void OnStart(StartState state) {
|
||||
base.OnStart(state);
|
||||
UpdateAllMaterials();
|
||||
|
||||
@ -47,83 +41,93 @@ namespace DepthMask
|
||||
}
|
||||
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
private void OnDestroy() {
|
||||
GameEvents.onVariantApplied.Remove(OnVariantApplied);
|
||||
GameEvents.onPartRepaired.Remove(OnPartRepaired);
|
||||
}
|
||||
|
||||
|
||||
public override void OnLoad(ConfigNode node)
|
||||
{
|
||||
public override void OnLoad(ConfigNode node) {
|
||||
base.OnLoad(node);
|
||||
|
||||
if (HighLogic.LoadedSceneIsEditor || HighLogic.LoadedSceneIsFlight) return;
|
||||
|
||||
this.maskTransformObjects = base.part.FindModelTransforms(maskTransform);
|
||||
if (this.maskTransformObjects.Length == 0 || this.maskTransformObjects == null)
|
||||
{
|
||||
this.LogError($"Can't find transform {maskTransform}");
|
||||
this.maskTransformObjects = new List<Transform>();
|
||||
this.bodyTransformObjects = new List<Transform>();
|
||||
|
||||
foreach (string name in maskTransform.Split(',')) {
|
||||
var trimmed = name.Trim();
|
||||
var transforms = base.part.FindModelTransforms(trimmed);
|
||||
if (transforms.Length == 0) {
|
||||
this.LogError($"Can't find any mask transforms named {trimmed}");
|
||||
}
|
||||
|
||||
this.maskTransformObjects.AddRange(transforms);
|
||||
}
|
||||
|
||||
if (this.maskTransformObjects.Count == 0) {
|
||||
this.LogError($"Can't find any mask transforms");
|
||||
return;
|
||||
}
|
||||
|
||||
if (bodyTransform.Length == 0)
|
||||
{
|
||||
this.bodyTransformObject = base.part.partTransform;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.bodyTransformObject = base.part.FindModelTransform(bodyTransform);
|
||||
if (this.bodyTransformObject == null)
|
||||
{
|
||||
this.LogError($"Can't find transform {bodyTransform}");
|
||||
this.bodyTransformObject = base.part.partTransform;
|
||||
if (bodyTransform.Length == 0) {
|
||||
this.bodyTransformObjects.Add(base.part.partTransform);
|
||||
} else {
|
||||
foreach (string name in bodyTransform.Split(',')) {
|
||||
var trimmed = name.Trim();
|
||||
var transforms = base.part.FindModelTransforms(trimmed);
|
||||
if (transforms.Length == 0) {
|
||||
this.LogError($"Can't find any body transforms named {trimmed}");
|
||||
}
|
||||
|
||||
this.bodyTransformObjects.AddRange(transforms);
|
||||
}
|
||||
}
|
||||
|
||||
if (this.bodyTransformObjects.Count == 0) {
|
||||
this.LogError($"Can't find any body transforms");
|
||||
return;
|
||||
}
|
||||
|
||||
this.depthShader = Shader.Find(shaderName);
|
||||
if (this.depthShader == null)
|
||||
{
|
||||
if (this.depthShader == null) {
|
||||
this.LogError($"Can't find shader {shaderName}");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void OnVariantApplied(Part appliedPart, PartVariant variant)
|
||||
{
|
||||
public void OnVariantApplied(Part appliedPart, PartVariant variant) {
|
||||
// I dont know why changing part variants resets all the materials to their as-loaded state, but it does
|
||||
if (appliedPart == this.part) UpdateAllMaterials();
|
||||
}
|
||||
|
||||
public void OnPartRepaired(Part repairedPart)
|
||||
{
|
||||
public void OnPartRepaired(Part repairedPart) {
|
||||
// Part repair resets part of the mesh from the prefab, so it needs to be reapplied
|
||||
if (repairedPart == this.part) UpdateAllMaterials();
|
||||
}
|
||||
|
||||
private void UpdateAllMaterials()
|
||||
{
|
||||
var renderers = bodyTransformObject.GetComponentsInChildren<Renderer>(true);
|
||||
private void UpdateAllMaterials() {
|
||||
var renderers = new List<Renderer>();
|
||||
foreach (var body in bodyTransformObjects) {
|
||||
renderers.AddRange(body.GetComponentsInChildren<Renderer>(true));
|
||||
}
|
||||
|
||||
foreach (var renderer in renderers)
|
||||
{
|
||||
foreach (var renderer in renderers) {
|
||||
var queue = renderer.material.renderQueue;
|
||||
if (queue <= maskRenderQueue) continue;
|
||||
queue = meshRenderQueue + ((queue - 2000) / 2);
|
||||
renderer.material.renderQueue = queue;
|
||||
}
|
||||
|
||||
foreach (var maskObject in maskTransformObjects)
|
||||
{
|
||||
foreach (var maskObject in maskTransformObjects) {
|
||||
var renderer = maskObject.GetComponent<Renderer>();
|
||||
renderer.material.shader = depthShader;
|
||||
renderer.material.renderQueue = maskRenderQueue;
|
||||
}
|
||||
}
|
||||
|
||||
private void LogError(string message)
|
||||
{
|
||||
private void LogError(string message) {
|
||||
Debug.LogError($"[{part.partInfo?.name ?? part.name} {this.GetType()}] {message}");
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user