Compare commits

...

2 Commits

Author SHA1 Message Date
7b9ed99325 Cleanup of abandoned saving code 2021-01-01 14:28:30 -08:00
e069f85e56 More tweaks 2021-01-01 14:02:46 -08:00
10 changed files with 33 additions and 112 deletions

View File

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

View File

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

View File

@ -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) {

View File

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

View File

@ -99,7 +99,7 @@ namespace ConformalDecals {
private const int DecalQueueMax = 2400;
private static int _decalQueueCounter = -1;
private Dictionary<Part, ProjectionPartTarget> _targets;
private readonly Dictionary<Part, ProjectionPartTarget> _targets = new Dictionary<Part, ProjectionPartTarget>();
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<Part, ProjectionPartTarget>();
}
else {
_targets.Clear();
}
// update orthogonal matrix
_orthoMatrix = Matrix4x4.identity;
_orthoMatrix[0, 3] = 0.5f;

View File

@ -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")]

View File

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

View File

@ -1,14 +1,12 @@
using System;
using System.Text;
using ConformalDecals.Util;
using UniLinq;
using UnityEngine;
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;
@ -34,35 +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));
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));
@ -77,7 +46,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 +63,8 @@ namespace ConformalDecals {
else {
enabled = false;
}
return enabled;
}
public void Render(Material decalMaterial, MaterialPropertyBlock partMPB, Camera camera) {
@ -105,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("MESH_TARGET");
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;
@ -130,31 +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;
foreach (var index in indices) {
if (index > current.childCount) throw new FormatException("Child index path is invalid");
current = current.GetChild(index);
}
return current;
}
}
}

View File

@ -1,15 +1,19 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using UnityEngine;
namespace ConformalDecals {
public class ProjectionPartTarget : IProjectionTarget {
public const string NodeName = "PART_TARGET";
// enabled flag
public bool enabled;
public readonly Part part;
public readonly List<ProjectionMeshTarget> meshTargets;
public readonly List<ProjectionMeshTarget> meshTargets = new List<ProjectionMeshTarget>();
public ProjectionPartTarget(Part part, bool useBaseNormal) {
this.part = part;
meshTargets = new List<ProjectionMeshTarget>();
foreach (var renderer in part.FindModelComponents<MeshRenderer>()) {
var target = renderer.transform;
@ -26,10 +30,13 @@ namespace ConformalDecals {
}
}
public void Project(Matrix4x4 orthoMatrix, Transform projector, Bounds projectionBounds) {
public bool Project(Matrix4x4 orthoMatrix, Transform projector, Bounds projectionBounds) {
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) {
@ -37,15 +44,5 @@ namespace ConformalDecals {
target.Render(decalMaterial, partMPB, camera);
}
}
public ConfigNode Save() {
var node = new ConfigNode("PART_TARGET");
node.AddValue("part", part.flightID);
foreach (var meshTarget in meshTargets) {
node.AddNode(meshTarget.Save());
}
return node;
}
}
}