More tweaks

feature-saving
Andrew Cassidy 3 years ago
parent 2f2b6fb692
commit e069f85e56

@ -98,7 +98,7 @@ PART
MODULE { MODULE {
IDENTIFIER { name = ModuleConformalDecal } IDENTIFIER { name = ModuleConformalDecal }
DATA { DATA {
shader = ConformalDecals/Paint/DiffuseSDF KEYWORD { name = DECAL_SDF_ALPHA }
tile = 0, 2, 128, 112 tile = 0, 2, 128, 112
} }
} }

@ -8,7 +8,7 @@ namespace ConformalDecals.MaterialProperties {
public override void ParseNode(ConfigNode node) { public override void ParseNode(ConfigNode node) {
base.ParseNode(node); base.ParseNode(node);
ParseUtil.ParseBoolIndirect(ref value, node, "value"); value = ParseUtil.ParseBool(node, "value", true, true);
} }
public override void Modify(Material material) { public override void Modify(Material material) {

@ -99,7 +99,7 @@ namespace ConformalDecals {
private const int DecalQueueMax = 2400; private const int DecalQueueMax = 2400;
private static int _decalQueueCounter = -1; private static int _decalQueueCounter = -1;
private Dictionary<Part, ProjectionPartTarget> _targets; private Dictionary<Part, ProjectionPartTarget> _targets = new Dictionary<Part, ProjectionPartTarget>();
private bool _isAttached; private bool _isAttached;
private Matrix4x4 _orthoMatrix; private Matrix4x4 _orthoMatrix;
@ -284,7 +284,7 @@ namespace ConformalDecals {
UpdateProjection(); UpdateProjection();
UpdateTargets(); UpdateTargets();
} }
else { else if (_isAttached && projectMultiple) {
UpdatePartTarget(eventPart, _boundsRenderer.bounds); UpdatePartTarget(eventPart, _boundsRenderer.bounds);
// recursively call for child parts // recursively call for child parts
foreach (var child in eventPart.children) { foreach (var child in eventPart.children) {
@ -298,7 +298,7 @@ namespace ConformalDecals {
if (this.part == eventPart) { if (this.part == eventPart) {
OnAttach(); OnAttach();
} }
else { else if (projectMultiple) {
UpdatePartTarget(eventPart, _boundsRenderer.bounds); UpdatePartTarget(eventPart, _boundsRenderer.bounds);
// recursively call for child parts // recursively call for child parts
foreach (var child in eventPart.children) { foreach (var child in eventPart.children) {
@ -312,7 +312,7 @@ namespace ConformalDecals {
if (this.part == eventPart) { if (this.part == eventPart) {
OnDetach(); OnDetach();
} }
else { else if (projectMultiple) {
_targets.Remove(eventPart); _targets.Remove(eventPart);
// recursively call for child parts // recursively call for child parts
foreach (var child in eventPart.children) { foreach (var child in eventPart.children) {
@ -599,14 +599,6 @@ namespace ConformalDecals {
materialProperties.UpdateScale(size); materialProperties.UpdateScale(size);
if (_isAttached) { if (_isAttached) {
// Update projection targets
if (_targets == null) {
_targets = new Dictionary<Part, ProjectionPartTarget>();
}
else {
_targets.Clear();
}
// update orthogonal matrix // update orthogonal matrix
_orthoMatrix = Matrix4x4.identity; _orthoMatrix = Matrix4x4.identity;
_orthoMatrix[0, 3] = 0.5f; _orthoMatrix[0, 3] = 0.5f;

@ -94,6 +94,7 @@ namespace ConformalDecals {
// EVENTS // EVENTS
/// <inheritdoc />
public override void OnSave(ConfigNode node) { public override void OnSave(ConfigNode node) {
node.AddValue("text", WebUtility.UrlEncode(text)); node.AddValue("text", WebUtility.UrlEncode(text));
node.AddValue("fontName", font.Name); node.AddValue("fontName", font.Name);

@ -7,8 +7,10 @@ using UnityEngine.Rendering;
namespace ConformalDecals { namespace ConformalDecals {
public class ProjectionMeshTarget : IProjectionTarget { public class ProjectionMeshTarget : IProjectionTarget {
public const string NodeName = "MESH_TARGET";
// enabled flag // enabled flag
public bool enabled = true; public bool enabled;
// Target object data // Target object data
public readonly Transform target; public readonly Transform target;
@ -37,6 +39,7 @@ namespace ConformalDecals {
public ProjectionMeshTarget(ConfigNode node, Transform root, bool useBaseNormal) { public ProjectionMeshTarget(ConfigNode node, Transform root, bool useBaseNormal) {
if (node == null) throw new ArgumentNullException(nameof(node)); if (node == null) throw new ArgumentNullException(nameof(node));
if (root == null) throw new ArgumentNullException(nameof(root)); if (root == null) throw new ArgumentNullException(nameof(root));
enabled = true;
var targetPath = ParseUtil.ParseString(node, "targetPath"); var targetPath = ParseUtil.ParseString(node, "targetPath");
var targetName = ParseUtil.ParseString(node, "targetName"); 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)) { if (projectionBounds.Intersects(renderer.bounds)) {
enabled = true; enabled = true;
@ -94,6 +97,8 @@ namespace ConformalDecals {
else { else {
enabled = false; enabled = false;
} }
return enabled;
} }
public void Render(Material decalMaterial, MaterialPropertyBlock partMPB, Camera camera) { public void Render(Material decalMaterial, MaterialPropertyBlock partMPB, Camera camera) {
@ -106,7 +111,7 @@ namespace ConformalDecals {
} }
public ConfigNode Save() { public ConfigNode Save() {
var node = new ConfigNode("MESH_TARGET"); var node = new ConfigNode(NodeName);
node.AddValue("decalMatrix", _decalMatrix); node.AddValue("decalMatrix", _decalMatrix);
node.AddValue("decalNormal", _decalNormal); node.AddValue("decalNormal", _decalNormal);
node.AddValue("decalTangent", _decalTangent); node.AddValue("decalTangent", _decalTangent);
@ -148,10 +153,12 @@ namespace ConformalDecals {
private static Transform LoadTransformPath(string path, Transform root) { private static Transform LoadTransformPath(string path, Transform root) {
var indices = path.Split('/').Select(int.Parse); var indices = path.Split('/').Select(int.Parse);
var current = root; var current = root;
Logging.Log($"root transform: {current.name}");
foreach (var index in indices) { foreach (var index in indices) {
if (index > current.childCount) throw new FormatException("Child index path is invalid"); if (index > current.childCount) throw new FormatException("Child index path is invalid");
current = current.GetChild(index); current = current.GetChild(index);
Logging.Log($"found child {current.name} at index {index}");
} }
return current; return current;

@ -1,15 +1,25 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using ConformalDecals.Util;
using UnityEngine; using UnityEngine;
namespace ConformalDecals { namespace ConformalDecals {
public class ProjectionPartTarget : IProjectionTarget { 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 Part part;
public readonly List<ProjectionMeshTarget> meshTargets; public readonly List<ProjectionMeshTarget> meshTargets = new List<ProjectionMeshTarget>();
public ProjectionPartTarget(Part part, bool useBaseNormal) { public ProjectionPartTarget(Part part, bool useBaseNormal) {
this.part = part; this.part = part;
meshTargets = new List<ProjectionMeshTarget>(); locked = false;
foreach (var renderer in part.FindModelComponents<MeshRenderer>()) { foreach (var renderer in part.FindModelComponents<MeshRenderer>()) {
var target = renderer.transform; 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) { 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) { public void Render(Material decalMaterial, MaterialPropertyBlock partMPB, Camera camera) {
@ -39,12 +71,14 @@ namespace ConformalDecals {
} }
public ConfigNode Save() { public ConfigNode Save() {
var node = new ConfigNode("PART_TARGET"); var node = new ConfigNode(NodeName);
node.AddValue("part", part.flightID); node.AddValue("part", part.flightID);
foreach (var meshTarget in meshTargets) { 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; return node;
} }
} }

Loading…
Cancel
Save