Cleanup of abandoned saving code

feature-multiProject
Andrew Cassidy 3 years ago
parent e069f85e56
commit 7b9ed99325

@ -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();
}
}

@ -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<T>(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);
}
}

@ -99,7 +99,7 @@ namespace ConformalDecals {
private const int DecalQueueMax = 2400;
private static int _decalQueueCounter = -1;
private Dictionary<Part, ProjectionPartTarget> _targets = new Dictionary<Part, ProjectionPartTarget>();
private readonly Dictionary<Part, ProjectionPartTarget> _targets = new Dictionary<Part, ProjectionPartTarget>();
private bool _isAttached;
private Matrix4x4 _orthoMatrix;

@ -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>();
flagBrowser.OnFlagSelected = OnCustomFlagSelected;
var flagBrowser = (Instantiate((Object) (new FlagBrowserGUIButton(null, null, null, null)).FlagBrowserPrefab) as GameObject)?.GetComponent<FlagBrowser>();
if (flagBrowser is { }) flagBrowser.OnFlagSelected = OnCustomFlagSelected;
}
[KSPEvent(guiActive = false, guiActiveEditor = true, guiName = "#LOC_ConformalDecals_gui-reset-flag")]

@ -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<MeshRenderer>();
var filter = target.GetComponent<MeshFilter>();
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;
}
}
}

@ -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<ProjectionMeshTarget> meshTargets = new List<ProjectionMeshTarget>();
public ProjectionPartTarget(Part part, bool useBaseNormal) {
this.part = part;
locked = false;
foreach (var renderer in part.FindModelComponents<MeshRenderer>()) {
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;
}
}
}
Loading…
Cancel
Save