From e069f85e5604c9d42eacfaaf1b998ed5d734bf62 Mon Sep 17 00:00:00 2001 From: drewcassidy Date: Fri, 1 Jan 2021 14:02:46 -0800 Subject: [PATCH] More tweaks --- .../Parts/Generic/decal-generic.cfg | 2 +- .../MaterialKeywordProperty.cs | 2 +- .../ConformalDecals/ModuleConformalDecal.cs | 16 ++----- Source/ConformalDecals/ModuleConformalText.cs | 1 + .../ConformalDecals/ProjectionMeshTarget.cs | 13 ++++-- .../ConformalDecals/ProjectionPartTarget.cs | 46 ++++++++++++++++--- 6 files changed, 57 insertions(+), 23 deletions(-) diff --git a/GameData/ConformalDecals/Parts/Generic/decal-generic.cfg b/GameData/ConformalDecals/Parts/Generic/decal-generic.cfg index f43000b..8ab8362 100644 --- a/GameData/ConformalDecals/Parts/Generic/decal-generic.cfg +++ b/GameData/ConformalDecals/Parts/Generic/decal-generic.cfg @@ -98,7 +98,7 @@ PART MODULE { IDENTIFIER { name = ModuleConformalDecal } DATA { - shader = ConformalDecals/Paint/DiffuseSDF + KEYWORD { name = DECAL_SDF_ALPHA } tile = 0, 2, 128, 112 } } diff --git a/Source/ConformalDecals/MaterialProperties/MaterialKeywordProperty.cs b/Source/ConformalDecals/MaterialProperties/MaterialKeywordProperty.cs index c9ee240..132d423 100644 --- a/Source/ConformalDecals/MaterialProperties/MaterialKeywordProperty.cs +++ b/Source/ConformalDecals/MaterialProperties/MaterialKeywordProperty.cs @@ -8,7 +8,7 @@ namespace ConformalDecals.MaterialProperties { public override void ParseNode(ConfigNode node) { base.ParseNode(node); - ParseUtil.ParseBoolIndirect(ref value, node, "value"); + value = ParseUtil.ParseBool(node, "value", true, true); } public override void Modify(Material material) { diff --git a/Source/ConformalDecals/ModuleConformalDecal.cs b/Source/ConformalDecals/ModuleConformalDecal.cs index 4ffb31e..0e9ccdc 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; + private Dictionary _targets = new Dictionary(); private bool _isAttached; private Matrix4x4 _orthoMatrix; @@ -284,7 +284,7 @@ namespace ConformalDecals { UpdateProjection(); UpdateTargets(); } - else { + else if (_isAttached && projectMultiple) { UpdatePartTarget(eventPart, _boundsRenderer.bounds); // recursively call for child parts foreach (var child in eventPart.children) { @@ -298,7 +298,7 @@ namespace ConformalDecals { if (this.part == eventPart) { OnAttach(); } - else { + else if (projectMultiple) { UpdatePartTarget(eventPart, _boundsRenderer.bounds); // recursively call for child parts foreach (var child in eventPart.children) { @@ -312,7 +312,7 @@ namespace ConformalDecals { if (this.part == eventPart) { OnDetach(); } - else { + else if (projectMultiple) { _targets.Remove(eventPart); // recursively call for child parts foreach (var child in eventPart.children) { @@ -599,14 +599,6 @@ namespace ConformalDecals { materialProperties.UpdateScale(size); if (_isAttached) { - // Update projection targets - if (_targets == null) { - _targets = new Dictionary(); - } - else { - _targets.Clear(); - } - // update orthogonal matrix _orthoMatrix = Matrix4x4.identity; _orthoMatrix[0, 3] = 0.5f; diff --git a/Source/ConformalDecals/ModuleConformalText.cs b/Source/ConformalDecals/ModuleConformalText.cs index 9e60841..56e0d88 100644 --- a/Source/ConformalDecals/ModuleConformalText.cs +++ b/Source/ConformalDecals/ModuleConformalText.cs @@ -94,6 +94,7 @@ namespace ConformalDecals { // EVENTS + /// public override void OnSave(ConfigNode node) { node.AddValue("text", WebUtility.UrlEncode(text)); node.AddValue("fontName", font.Name); diff --git a/Source/ConformalDecals/ProjectionMeshTarget.cs b/Source/ConformalDecals/ProjectionMeshTarget.cs index 49b3dea..84ed75c 100644 --- a/Source/ConformalDecals/ProjectionMeshTarget.cs +++ b/Source/ConformalDecals/ProjectionMeshTarget.cs @@ -7,8 +7,10 @@ using UnityEngine.Rendering; namespace ConformalDecals { public class ProjectionMeshTarget : IProjectionTarget { + public const string NodeName = "MESH_TARGET"; + // enabled flag - public bool enabled = true; + public bool enabled; // Target object data public readonly Transform target; @@ -37,6 +39,7 @@ namespace ConformalDecals { 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"); @@ -77,7 +80,7 @@ namespace ConformalDecals { } } - public void Project(Matrix4x4 orthoMatrix, Transform projector, Bounds projectionBounds) { + public bool Project(Matrix4x4 orthoMatrix, Transform projector, Bounds projectionBounds) { if (projectionBounds.Intersects(renderer.bounds)) { enabled = true; @@ -94,6 +97,8 @@ namespace ConformalDecals { else { enabled = false; } + + return enabled; } public void Render(Material decalMaterial, MaterialPropertyBlock partMPB, Camera camera) { @@ -106,7 +111,7 @@ namespace ConformalDecals { } public ConfigNode Save() { - var node = new ConfigNode("MESH_TARGET"); + var node = new ConfigNode(NodeName); node.AddValue("decalMatrix", _decalMatrix); node.AddValue("decalNormal", _decalNormal); node.AddValue("decalTangent", _decalTangent); @@ -148,10 +153,12 @@ namespace ConformalDecals { 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; diff --git a/Source/ConformalDecals/ProjectionPartTarget.cs b/Source/ConformalDecals/ProjectionPartTarget.cs index 3b060e1..3070279 100644 --- a/Source/ConformalDecals/ProjectionPartTarget.cs +++ b/Source/ConformalDecals/ProjectionPartTarget.cs @@ -1,15 +1,25 @@ using System; using System.Collections.Generic; +using ConformalDecals.Util; using UnityEngine; namespace ConformalDecals { public class ProjectionPartTarget : IProjectionTarget { + public const string NodeName = "PART_TARGET"; + + // 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; + public readonly List meshTargets = new List(); + public ProjectionPartTarget(Part part, bool useBaseNormal) { this.part = part; - meshTargets = new List(); + locked = false; foreach (var renderer in part.FindModelComponents()) { var target = renderer.transform; @@ -26,10 +36,32 @@ namespace ConformalDecals { } } - public void Project(Matrix4x4 orthoMatrix, Transform projector, Bounds projectionBounds) { + 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) { - meshTarget.Project(orthoMatrix, projector, projectionBounds); + enabled |= meshTarget.Project(orthoMatrix, projector, projectionBounds); } + + return enabled; } public void Render(Material decalMaterial, MaterialPropertyBlock partMPB, Camera camera) { @@ -39,12 +71,14 @@ namespace ConformalDecals { } public ConfigNode Save() { - var node = new ConfigNode("PART_TARGET"); + var node = new ConfigNode(NodeName); node.AddValue("part", part.flightID); foreach (var meshTarget in meshTargets) { - node.AddNode(meshTarget.Save()); + if (meshTarget.enabled) node.AddNode(meshTarget.Save()); } + Logging.Log($"Saved target for part {part.name}"); + return node; } }