mirror of
https://github.com/drewcassidy/KSP-Conformal-Decals.git
synced 2024-09-01 18:23:54 +00:00
Compare commits
1 Commits
feature-mu
...
feature-sa
Author | SHA1 | Date | |
---|---|---|---|
9295899787 |
Binary file not shown.
@ -4,5 +4,6 @@ namespace ConformalDecals {
|
||||
public interface IProjectionTarget {
|
||||
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 {isMain: true} textureProperty) {
|
||||
if (property is MaterialTextureProperty textureProperty && textureProperty.isMain) {
|
||||
_mainTexture = textureProperty;
|
||||
}
|
||||
}
|
||||
@ -208,6 +208,7 @@ namespace ConformalDecals.MaterialProperties {
|
||||
public T ParseProperty<T>(ConfigNode node) where T : MaterialProperty {
|
||||
string propertyName = "";
|
||||
if (!ParseUtil.ParseStringIndirect(ref propertyName, node, "name")) throw new ArgumentException("node has no name");
|
||||
Logging.Log($"Parsing material property {propertyName}");
|
||||
|
||||
if (ParseUtil.ParseBool(node, "remove", true)) RemoveProperty(propertyName);
|
||||
|
||||
@ -252,7 +253,7 @@ namespace ConformalDecals.MaterialProperties {
|
||||
|
||||
public void UpdateScale(Vector2 scale) {
|
||||
foreach (var entry in _materialProperties) {
|
||||
if (entry.Value is MaterialTextureProperty {autoScale: true} textureProperty) {
|
||||
if (entry.Value is MaterialTextureProperty textureProperty && textureProperty.autoScale) {
|
||||
textureProperty.SetScale(scale);
|
||||
}
|
||||
}
|
||||
@ -263,7 +264,7 @@ namespace ConformalDecals.MaterialProperties {
|
||||
var mainTexSize = _mainTexture.Dimensions;
|
||||
|
||||
foreach (var entry in _materialProperties) {
|
||||
if (entry.Value is MaterialTextureProperty {autoTile: true} textureProperty) {
|
||||
if (entry.Value is MaterialTextureProperty textureProperty && textureProperty.autoTile) {
|
||||
textureProperty.SetTile(tile, mainTexSize);
|
||||
}
|
||||
}
|
||||
|
@ -99,7 +99,7 @@ namespace ConformalDecals {
|
||||
private const int DecalQueueMax = 2400;
|
||||
private static int _decalQueueCounter = -1;
|
||||
|
||||
private readonly Dictionary<Part, ProjectionPartTarget> _targets = new Dictionary<Part, ProjectionPartTarget>();
|
||||
private Dictionary<Part, ProjectionPartTarget> _targets = new Dictionary<Part, ProjectionPartTarget>();
|
||||
|
||||
private bool _isAttached;
|
||||
private Matrix4x4 _orthoMatrix;
|
||||
@ -152,6 +152,18 @@ namespace ConformalDecals {
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void OnSave(ConfigNode node) {
|
||||
// SAVE TARGETS
|
||||
if (HighLogic.LoadedSceneIsFlight) {
|
||||
foreach (var partTarget in _targets.Values) {
|
||||
if (partTarget.enabled) node.AddNode(partTarget.Save());
|
||||
}
|
||||
}
|
||||
|
||||
base.OnSave(node);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void OnIconCreate() {
|
||||
UpdateTextures();
|
||||
@ -298,7 +310,7 @@ namespace ConformalDecals {
|
||||
if (this.part == eventPart) {
|
||||
OnAttach();
|
||||
}
|
||||
else if (_isAttached && projectMultiple) {
|
||||
else if (projectMultiple) {
|
||||
UpdatePartTarget(eventPart, _boundsRenderer.bounds);
|
||||
// recursively call for child parts
|
||||
foreach (var child in eventPart.children) {
|
||||
@ -312,7 +324,7 @@ namespace ConformalDecals {
|
||||
if (this.part == eventPart) {
|
||||
OnDetach();
|
||||
}
|
||||
else if (_isAttached && projectMultiple) {
|
||||
else if (projectMultiple) {
|
||||
_targets.Remove(eventPart);
|
||||
// recursively call for child parts
|
||||
foreach (var child in eventPart.children) {
|
||||
@ -327,7 +339,7 @@ namespace ConformalDecals {
|
||||
this.Log("Parent part about to be destroyed! Killing decal part.");
|
||||
part.Die();
|
||||
}
|
||||
else if (_isAttached && projectMultiple) {
|
||||
else if (projectMultiple) {
|
||||
_targets.Remove(willDie);
|
||||
}
|
||||
}
|
||||
@ -341,7 +353,6 @@ namespace ConformalDecals {
|
||||
}
|
||||
|
||||
_isAttached = true;
|
||||
_targets.Clear();
|
||||
|
||||
// hide model
|
||||
decalModelTransform.gameObject.SetActive(false);
|
||||
@ -432,6 +443,20 @@ namespace ConformalDecals {
|
||||
else if (tileIndex >= 0) {
|
||||
materialProperties.UpdateTile(tileIndex, tileSize);
|
||||
}
|
||||
|
||||
// PARSE TARGETS
|
||||
if (HighLogic.LoadedSceneIsFlight) {
|
||||
foreach (var partTargetNode in node.GetNodes(ProjectionPartTarget.NodeName)) {
|
||||
try {
|
||||
var partTarget = new ProjectionPartTarget(partTargetNode, part.vessel, useBaseNormal);
|
||||
_targets.Add(partTarget.part, partTarget);
|
||||
}
|
||||
catch (Exception e) {
|
||||
this.LogWarning($"Encountered error while parsing part node: {e}");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Setup decal by calling update functions relevent for the current situation
|
||||
@ -621,12 +646,8 @@ namespace ConformalDecals {
|
||||
/// Called when updating decal targets
|
||||
protected void UpdateTargets() {
|
||||
if (!_isAttached) return;
|
||||
var projectionBounds = _boundsRenderer.bounds;
|
||||
|
||||
// disable all targets
|
||||
foreach (var target in _targets.Values) {
|
||||
target.enabled = false;
|
||||
}
|
||||
var projectionBounds = _boundsRenderer.bounds;
|
||||
|
||||
// collect list of potential targets
|
||||
IEnumerable<Part> targetParts;
|
||||
|
@ -1,4 +1,5 @@
|
||||
using ConformalDecals.MaterialProperties;
|
||||
using ConformalDecals.Util;
|
||||
using UniLinq;
|
||||
using UnityEngine;
|
||||
|
||||
@ -45,8 +46,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>();
|
||||
if (flagBrowser is { }) flagBrowser.OnFlagSelected = OnCustomFlagSelected;
|
||||
var flagBrowser = (Instantiate((Object) (new FlagBrowserGUIButton(null, null, null, null)).FlagBrowserPrefab) as GameObject).GetComponent<FlagBrowser>();
|
||||
flagBrowser.OnFlagSelected = OnCustomFlagSelected;
|
||||
}
|
||||
|
||||
[KSPEvent(guiActive = false, guiActiveEditor = true, guiName = "#LOC_ConformalDecals_gui-reset-flag")]
|
||||
|
@ -1,3 +1,7 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
using ConformalDecals.Util;
|
||||
using UniLinq;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Rendering;
|
||||
|
||||
@ -32,6 +36,36 @@ 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));
|
||||
@ -76,6 +110,18 @@ 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;
|
||||
@ -89,5 +135,33 @@ 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,4 +1,6 @@
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using ConformalDecals.Util;
|
||||
using UnityEngine;
|
||||
|
||||
namespace ConformalDecals {
|
||||
@ -8,12 +10,16 @@ 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;
|
||||
@ -30,7 +36,26 @@ 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);
|
||||
@ -40,11 +65,21 @@ namespace ConformalDecals {
|
||||
}
|
||||
|
||||
public void Render(Material decalMaterial, MaterialPropertyBlock partMPB, Camera camera) {
|
||||
if (!enabled) return;
|
||||
|
||||
foreach (var target in meshTargets) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user