From d8419416e8c30328ce5fcef8e9fef95c49bb79fe Mon Sep 17 00:00:00 2001 From: drewcassidy Date: Mon, 10 Jun 2019 20:38:11 -0700 Subject: [PATCH] Add project code --- DepthMask.sln | 25 ++++++ DepthMask/DepthMask.csproj | 48 ++++++++++ DepthMask/ModuleDepthMask.cs | 130 +++++++++++++++++++++++++++ DepthMask/Properties/AssemblyInfo.cs | 38 ++++++++ 4 files changed, 241 insertions(+) create mode 100644 DepthMask.sln create mode 100644 DepthMask/DepthMask.csproj create mode 100644 DepthMask/ModuleDepthMask.cs create mode 100644 DepthMask/Properties/AssemblyInfo.cs diff --git a/DepthMask.sln b/DepthMask.sln new file mode 100644 index 0000000..f91243b --- /dev/null +++ b/DepthMask.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.28307.136 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{3CD2A114-2C93-4D39-95B4-214DCC386A37}") = "DepthMask", "DepthMask\DepthMask.csproj", "{AF7B4795-7A4F-4238-BF1D-F0AC763C15C8}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {AF7B4795-7A4F-4238-BF1D-F0AC763C15C8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {AF7B4795-7A4F-4238-BF1D-F0AC763C15C8}.Debug|Any CPU.Build.0 = Debug|Any CPU + {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(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {7633F515-D77A-4A4D-998B-4EB65CD3A32C} + EndGlobalSection +EndGlobal diff --git a/DepthMask/DepthMask.csproj b/DepthMask/DepthMask.csproj new file mode 100644 index 0000000..d18d66b --- /dev/null +++ b/DepthMask/DepthMask.csproj @@ -0,0 +1,48 @@ + + + + + Debug + AnyCPU + {AF7B4795-7A4F-4238-BF1D-F0AC763C15C8} + Library + Properties + DepthMask + DepthMask + v3.5 + 512 + true + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + False + + + + + False + + + + + + + + diff --git a/DepthMask/ModuleDepthMask.cs b/DepthMask/ModuleDepthMask.cs new file mode 100644 index 0000000..bbd2cce --- /dev/null +++ b/DepthMask/ModuleDepthMask.cs @@ -0,0 +1,130 @@ +using UnityEngine; +using UnityEngine.Serialization; + +namespace Restock +{ + public class ModuleRestockDepthMask : PartModule + { + // The name of the transform that has your mask mesh. The only strictly required property + [KSPField] + public string maskTransform = ""; + + [KSPField] + public string bodyTransform = ""; + + // The name of the depth mask shader + [KSPField] + public string shaderName = "DepthMask"; + + // The render queue value for the mesh, should be less than maskRenderQueue + [KSPField] + public int meshRenderQueue = 1000; + + // the render queue value for the mask, should be less than 2000 + [KSPField] + public int maskRenderQueue = 1999; + + + // depth mask object transform + public Transform maskTransformObject; + + // body object transform + public Transform bodyTransformObject; + + // depth mask shader object + public Shader depthShader; + + + public override void OnStart(StartState state) + { + base.OnStart(state); + UpdatematerialQueue(); + + // the part variant system is implemented extremely stupidly + // so we have to make this whole module more complicated as a result + GameEvents.onVariantApplied.Add(OnVariantApplied); + } + + + private void OnDestroy() + { + GameEvents.onVariantApplied.Remove(OnVariantApplied); + } + + + public override void OnLoad(ConfigNode node) + { + base.OnLoad(node); + + if (HighLogic.LoadedSceneIsEditor || HighLogic.LoadedSceneIsFlight) return; + + this.maskTransformObject = base.part.FindModelTransform(maskTransform); + if (this.maskTransformObject == null) + { + this.LogError($"Can't find transform {maskTransform}"); + return; + } + + if (bodyTransform == "") + { + 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; + } + } + + this.depthShader = Shader.Find(shaderName); + if (this.depthShader == null) + { + this.LogError($"Can't find shader {shaderName}"); + return; + } + } + + + 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) UpdatematerialQueue(); + } + + + private void UpdatematerialQueue() + { + var windowRenderer = maskTransformObject.GetComponent(); + + windowRenderer.material.shader = depthShader; + windowRenderer.material.renderQueue = maskRenderQueue; + + var meshRenderers = bodyTransformObject.GetComponentsInChildren(true); + var skinnedMeshRenderers = bodyTransformObject.GetComponentsInChildren(true); + + foreach (var renderer in meshRenderers) + { + if (renderer == windowRenderer) continue; + var queue = renderer.material.renderQueue; + queue = meshRenderQueue + ((queue - 2000) / 2); + renderer.material.renderQueue = queue; + } + + foreach (var renderer in skinnedMeshRenderers) + { + if (renderer == windowRenderer) continue; + var queue = renderer.material.renderQueue; + queue = meshRenderQueue + ((queue - 2000) / 2); + renderer.material.renderQueue = queue; + } + } + + private void LogError(string message) + { + Debug.LogError($"[{part.partInfo?.name ?? part.name} {this.GetType()}] {message}"); + } + } +} \ No newline at end of file diff --git a/DepthMask/Properties/AssemblyInfo.cs b/DepthMask/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..077743e --- /dev/null +++ b/DepthMask/Properties/AssemblyInfo.cs @@ -0,0 +1,38 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("Depth Mask")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("Cineboxandrew")] +[assembly: AssemblyProduct("DepthMask")] +[assembly: AssemblyCopyright("Copyright © Andrew Cassidy 2019")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("AF7B4795-7A4F-4238-BF1D-F0AC763C15C8")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] + +[assembly: KSPAssembly("DepthMask", 1, 0, 0)]