diff --git a/GameData/ConformalDecals/Plugins/ConformalDecals.dll b/GameData/ConformalDecals/Plugins/ConformalDecals.dll index 2336c5f..98f7ec6 100644 Binary files a/GameData/ConformalDecals/Plugins/ConformalDecals.dll and b/GameData/ConformalDecals/Plugins/ConformalDecals.dll differ diff --git a/Source/ConformalDecals/IProjectionTarget.cs b/Source/ConformalDecals/IProjectionTarget.cs index 31f6f58..861833e 100644 --- a/Source/ConformalDecals/IProjectionTarget.cs +++ b/Source/ConformalDecals/IProjectionTarget.cs @@ -2,8 +2,7 @@ namespace ConformalDecals { public interface IProjectionTarget { - void Project(Matrix4x4 orthoMatrix, Transform projector, Bounds projectionBounds); + bool Project(Matrix4x4 orthoMatrix, Transform projector, Bounds projectionBounds); void Render(Material decalMaterial, MaterialPropertyBlock partMPB, Camera camera); - ConfigNode Save(); } } \ No newline at end of file diff --git a/Source/ConformalDecals/MaterialProperties/MaterialPropertyCollection.cs b/Source/ConformalDecals/MaterialProperties/MaterialPropertyCollection.cs index 5ffafbd..6d2e10f 100644 --- a/Source/ConformalDecals/MaterialProperties/MaterialPropertyCollection.cs +++ b/Source/ConformalDecals/MaterialProperties/MaterialPropertyCollection.cs @@ -95,7 +95,7 @@ namespace ConformalDecals.MaterialProperties { var property = MaterialProperty.Instantiate(_serializedProperties[i]); _materialProperties.Add(_serializedNames[i], property); - if (property is MaterialTextureProperty textureProperty && textureProperty.isMain) { + if (property is MaterialTextureProperty {isMain: true} textureProperty) { _mainTexture = textureProperty; } } @@ -214,7 +214,7 @@ namespace ConformalDecals.MaterialProperties { var newProperty = AddOrGetProperty(propertyName); newProperty.ParseNode(node); - if (newProperty is MaterialTextureProperty textureProperty && textureProperty.isMain) { + if (newProperty is MaterialTextureProperty {isMain: true} textureProperty) { _mainTexture = textureProperty; } @@ -252,7 +252,7 @@ namespace ConformalDecals.MaterialProperties { public void UpdateScale(Vector2 scale) { foreach (var entry in _materialProperties) { - if (entry.Value is MaterialTextureProperty textureProperty && textureProperty.autoScale) { + if (entry.Value is MaterialTextureProperty {autoScale: true} textureProperty) { textureProperty.SetScale(scale); } } @@ -263,7 +263,7 @@ namespace ConformalDecals.MaterialProperties { var mainTexSize = _mainTexture.Dimensions; foreach (var entry in _materialProperties) { - if (entry.Value is MaterialTextureProperty textureProperty && textureProperty.autoTile) { + if (entry.Value is MaterialTextureProperty {autoTile: true} textureProperty) { textureProperty.SetTile(tile, mainTexSize); } } diff --git a/Source/ConformalDecals/ModuleConformalDecal.cs b/Source/ConformalDecals/ModuleConformalDecal.cs index 0e9ccdc..291971b 100644 --- a/Source/ConformalDecals/ModuleConformalDecal.cs +++ b/Source/ConformalDecals/ModuleConformalDecal.cs @@ -99,7 +99,7 @@ namespace ConformalDecals { private const int DecalQueueMax = 2400; private static int _decalQueueCounter = -1; - private Dictionary _targets = new Dictionary(); + private readonly Dictionary _targets = new Dictionary(); private bool _isAttached; private Matrix4x4 _orthoMatrix; diff --git a/Source/ConformalDecals/ModuleConformalFlag.cs b/Source/ConformalDecals/ModuleConformalFlag.cs index ecbefd4..857f56f 100644 --- a/Source/ConformalDecals/ModuleConformalFlag.cs +++ b/Source/ConformalDecals/ModuleConformalFlag.cs @@ -1,5 +1,4 @@ using ConformalDecals.MaterialProperties; -using ConformalDecals.Util; using UniLinq; using UnityEngine; @@ -46,8 +45,8 @@ namespace ConformalDecals { [KSPEvent(guiActive = false, guiActiveEditor = true, guiName = "#LOC_ConformalDecals_gui-select-flag")] public void SelectFlag() { - var flagBrowser = (Instantiate((Object) (new FlagBrowserGUIButton(null, null, null, null)).FlagBrowserPrefab) as GameObject).GetComponent(); - flagBrowser.OnFlagSelected = OnCustomFlagSelected; + var flagBrowser = (Instantiate((Object) (new FlagBrowserGUIButton(null, null, null, null)).FlagBrowserPrefab) as GameObject)?.GetComponent(); + if (flagBrowser is { }) flagBrowser.OnFlagSelected = OnCustomFlagSelected; } [KSPEvent(guiActive = false, guiActiveEditor = true, guiName = "#LOC_ConformalDecals_gui-reset-flag")] diff --git a/Source/ConformalDecals/ProjectionMeshTarget.cs b/Source/ConformalDecals/ProjectionMeshTarget.cs index 84ed75c..61c1bb9 100644 --- a/Source/ConformalDecals/ProjectionMeshTarget.cs +++ b/Source/ConformalDecals/ProjectionMeshTarget.cs @@ -1,7 +1,3 @@ -using System; -using System.Text; -using ConformalDecals.Util; -using UniLinq; using UnityEngine; using UnityEngine.Rendering; @@ -36,36 +32,6 @@ namespace ConformalDecals { SetNormalMap(renderer.sharedMaterial, useBaseNormal); } - public ProjectionMeshTarget(ConfigNode node, Transform root, bool useBaseNormal) { - if (node == null) throw new ArgumentNullException(nameof(node)); - if (root == null) throw new ArgumentNullException(nameof(root)); - enabled = true; - - var targetPath = ParseUtil.ParseString(node, "targetPath"); - var targetName = ParseUtil.ParseString(node, "targetName"); - - _decalMatrix = ParseUtil.ParseMatrix4x4(node, "decalMatrix"); - _decalNormal = ParseUtil.ParseVector3(node, "decalNormal"); - _decalTangent = ParseUtil.ParseVector3(node, "decalTangent"); - _decalMPB = new MaterialPropertyBlock(); - - target = LoadTransformPath(targetPath, root); - if (target.name != targetName) throw new FormatException("Target name does not match"); - - renderer = target.GetComponent(); - var filter = target.GetComponent(); - - if (!ValidateTarget(target, renderer, filter)) throw new FormatException("Invalid target"); - - mesh = filter.sharedMesh; - - SetNormalMap(renderer.sharedMaterial, useBaseNormal); - - _decalMPB.SetMatrix(DecalPropertyIDs._ProjectionMatrix, _decalMatrix); - _decalMPB.SetVector(DecalPropertyIDs._DecalNormal, _decalNormal); - _decalMPB.SetVector(DecalPropertyIDs._DecalTangent, _decalTangent); - } - private void SetNormalMap(Material targetMaterial, bool useBaseNormal) { if (useBaseNormal && targetMaterial.HasProperty(DecalPropertyIDs._BumpMap)) { _decalMPB.SetTexture(DecalPropertyIDs._BumpMap, targetMaterial.GetTexture(DecalPropertyIDs._BumpMap)); @@ -110,18 +76,6 @@ namespace ConformalDecals { Graphics.DrawMesh(mesh, target.localToWorldMatrix, decalMaterial, 0, camera, 0, _decalMPB, ShadowCastingMode.Off, true); } - public ConfigNode Save() { - var node = new ConfigNode(NodeName); - node.AddValue("decalMatrix", _decalMatrix); - node.AddValue("decalNormal", _decalNormal); - node.AddValue("decalTangent", _decalTangent); - node.AddValue("targetPath", SaveTransformPath(target, root)); // used to find the target transform - node.AddValue("targetName", target.name); // used to validate the mesh has not changed since last load - - return node; - } - - public static bool ValidateTarget(Transform target, MeshRenderer renderer, MeshFilter filter) { if (renderer == null) return false; if (filter == null) return false; @@ -135,33 +89,5 @@ namespace ConformalDecals { return true; } - - private static string SaveTransformPath(Transform leaf, Transform root) { - var builder = new StringBuilder($"{leaf.GetSiblingIndex()}"); - var current = leaf.parent; - - while (current != root) { - builder.Insert(0, "/"); - builder.Insert(0, current.GetSiblingIndex()); - current = current.parent; - if (current == null) throw new FormatException("Leaf does not exist as a child of root"); - } - - return builder.ToString(); - } - - private static Transform LoadTransformPath(string path, Transform root) { - var indices = path.Split('/').Select(int.Parse); - var current = root; - Logging.Log($"root transform: {current.name}"); - - foreach (var index in indices) { - if (index > current.childCount) throw new FormatException("Child index path is invalid"); - current = current.GetChild(index); - Logging.Log($"found child {current.name} at index {index}"); - } - - return current; - } } } \ No newline at end of file diff --git a/Source/ConformalDecals/ProjectionPartTarget.cs b/Source/ConformalDecals/ProjectionPartTarget.cs index 3070279..8dae1f9 100644 --- a/Source/ConformalDecals/ProjectionPartTarget.cs +++ b/Source/ConformalDecals/ProjectionPartTarget.cs @@ -1,6 +1,4 @@ -using System; -using System.Collections.Generic; -using ConformalDecals.Util; +using System.Collections.Generic; using UnityEngine; namespace ConformalDecals { @@ -10,16 +8,12 @@ namespace ConformalDecals { // enabled flag public bool enabled; - // locked flag, to prevent re-projection of loaded targets - public readonly bool locked; - public readonly Part part; public readonly List meshTargets = new List(); public ProjectionPartTarget(Part part, bool useBaseNormal) { this.part = part; - locked = false; foreach (var renderer in part.FindModelComponents()) { var target = renderer.transform; @@ -36,26 +30,7 @@ namespace ConformalDecals { } } - public ProjectionPartTarget(ConfigNode node, Vessel vessel, bool useBaseNormal) { - if (node == null) throw new ArgumentNullException(nameof(node)); - locked = true; - enabled = true; - - var flightID = ParseUtil.ParseUint(node, "part"); - - part = vessel[flightID]; - if (part == null) throw new IndexOutOfRangeException("Vessel returned null part, part must be destroyed or detached"); - var root = part.transform; - - foreach (var meshTargetNode in node.GetNodes(ProjectionMeshTarget.NodeName)) { - meshTargets.Add(new ProjectionMeshTarget(meshTargetNode, root, useBaseNormal)); - } - - Logging.Log($"Loaded target for part {part.name}"); - } - public bool Project(Matrix4x4 orthoMatrix, Transform projector, Bounds projectionBounds) { - if (locked) return true; // dont overwrite saved targets in flight mode enabled = false; foreach (var meshTarget in meshTargets) { enabled |= meshTarget.Project(orthoMatrix, projector, projectionBounds); @@ -69,17 +44,5 @@ namespace ConformalDecals { target.Render(decalMaterial, partMPB, camera); } } - - public ConfigNode Save() { - var node = new ConfigNode(NodeName); - node.AddValue("part", part.flightID); - foreach (var meshTarget in meshTargets) { - if (meshTarget.enabled) node.AddNode(meshTarget.Save()); - } - - Logging.Log($"Saved target for part {part.name}"); - - return node; - } } } \ No newline at end of file