mirror of
https://github.com/drewcassidy/KSP-Conformal-Decals.git
synced 2024-09-01 18:23:54 +00:00
Compare commits
2 Commits
2f2b6fb692
...
7b9ed99325
Author | SHA1 | Date | |
---|---|---|---|
7b9ed99325 | |||
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Binary file not shown.
@ -2,8 +2,7 @@
|
|||||||
|
|
||||||
namespace ConformalDecals {
|
namespace ConformalDecals {
|
||||||
public interface IProjectionTarget {
|
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);
|
void Render(Material decalMaterial, MaterialPropertyBlock partMPB, Camera camera);
|
||||||
ConfigNode Save();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -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) {
|
||||||
|
@ -95,7 +95,7 @@ namespace ConformalDecals.MaterialProperties {
|
|||||||
var property = MaterialProperty.Instantiate(_serializedProperties[i]);
|
var property = MaterialProperty.Instantiate(_serializedProperties[i]);
|
||||||
_materialProperties.Add(_serializedNames[i], property);
|
_materialProperties.Add(_serializedNames[i], property);
|
||||||
|
|
||||||
if (property is MaterialTextureProperty textureProperty && textureProperty.isMain) {
|
if (property is MaterialTextureProperty {isMain: true} textureProperty) {
|
||||||
_mainTexture = textureProperty;
|
_mainTexture = textureProperty;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -214,7 +214,7 @@ namespace ConformalDecals.MaterialProperties {
|
|||||||
var newProperty = AddOrGetProperty<T>(propertyName);
|
var newProperty = AddOrGetProperty<T>(propertyName);
|
||||||
newProperty.ParseNode(node);
|
newProperty.ParseNode(node);
|
||||||
|
|
||||||
if (newProperty is MaterialTextureProperty textureProperty && textureProperty.isMain) {
|
if (newProperty is MaterialTextureProperty {isMain: true} textureProperty) {
|
||||||
_mainTexture = textureProperty;
|
_mainTexture = textureProperty;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -252,7 +252,7 @@ namespace ConformalDecals.MaterialProperties {
|
|||||||
|
|
||||||
public void UpdateScale(Vector2 scale) {
|
public void UpdateScale(Vector2 scale) {
|
||||||
foreach (var entry in _materialProperties) {
|
foreach (var entry in _materialProperties) {
|
||||||
if (entry.Value is MaterialTextureProperty textureProperty && textureProperty.autoScale) {
|
if (entry.Value is MaterialTextureProperty {autoScale: true} textureProperty) {
|
||||||
textureProperty.SetScale(scale);
|
textureProperty.SetScale(scale);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -263,7 +263,7 @@ namespace ConformalDecals.MaterialProperties {
|
|||||||
var mainTexSize = _mainTexture.Dimensions;
|
var mainTexSize = _mainTexture.Dimensions;
|
||||||
|
|
||||||
foreach (var entry in _materialProperties) {
|
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);
|
textureProperty.SetTile(tile, mainTexSize);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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 readonly 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;
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
using ConformalDecals.MaterialProperties;
|
using ConformalDecals.MaterialProperties;
|
||||||
using ConformalDecals.Util;
|
|
||||||
using UniLinq;
|
using UniLinq;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
@ -46,8 +45,8 @@ namespace ConformalDecals {
|
|||||||
|
|
||||||
[KSPEvent(guiActive = false, guiActiveEditor = true, guiName = "#LOC_ConformalDecals_gui-select-flag")]
|
[KSPEvent(guiActive = false, guiActiveEditor = true, guiName = "#LOC_ConformalDecals_gui-select-flag")]
|
||||||
public void SelectFlag() {
|
public void SelectFlag() {
|
||||||
var flagBrowser = (Instantiate((Object) (new FlagBrowserGUIButton(null, null, null, null)).FlagBrowserPrefab) as GameObject).GetComponent<FlagBrowser>();
|
var flagBrowser = (Instantiate((Object) (new FlagBrowserGUIButton(null, null, null, null)).FlagBrowserPrefab) as GameObject)?.GetComponent<FlagBrowser>();
|
||||||
flagBrowser.OnFlagSelected = OnCustomFlagSelected;
|
if (flagBrowser is { }) flagBrowser.OnFlagSelected = OnCustomFlagSelected;
|
||||||
}
|
}
|
||||||
|
|
||||||
[KSPEvent(guiActive = false, guiActiveEditor = true, guiName = "#LOC_ConformalDecals_gui-reset-flag")]
|
[KSPEvent(guiActive = false, guiActiveEditor = true, guiName = "#LOC_ConformalDecals_gui-reset-flag")]
|
||||||
|
@ -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);
|
||||||
|
@ -1,14 +1,12 @@
|
|||||||
using System;
|
|
||||||
using System.Text;
|
|
||||||
using ConformalDecals.Util;
|
|
||||||
using UniLinq;
|
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using UnityEngine.Rendering;
|
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;
|
||||||
@ -34,35 +32,6 @@ namespace ConformalDecals {
|
|||||||
SetNormalMap(renderer.sharedMaterial, useBaseNormal);
|
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) {
|
private void SetNormalMap(Material targetMaterial, bool useBaseNormal) {
|
||||||
if (useBaseNormal && targetMaterial.HasProperty(DecalPropertyIDs._BumpMap)) {
|
if (useBaseNormal && targetMaterial.HasProperty(DecalPropertyIDs._BumpMap)) {
|
||||||
_decalMPB.SetTexture(DecalPropertyIDs._BumpMap, targetMaterial.GetTexture(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)) {
|
if (projectionBounds.Intersects(renderer.bounds)) {
|
||||||
enabled = true;
|
enabled = true;
|
||||||
|
|
||||||
@ -94,6 +63,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) {
|
||||||
@ -105,18 +76,6 @@ namespace ConformalDecals {
|
|||||||
Graphics.DrawMesh(mesh, target.localToWorldMatrix, decalMaterial, 0, camera, 0, _decalMPB, ShadowCastingMode.Off, true);
|
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) {
|
public static bool ValidateTarget(Transform target, MeshRenderer renderer, MeshFilter filter) {
|
||||||
if (renderer == null) return false;
|
if (renderer == null) return false;
|
||||||
if (filter == null) return false;
|
if (filter == null) return false;
|
||||||
@ -130,31 +89,5 @@ namespace ConformalDecals {
|
|||||||
|
|
||||||
return true;
|
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,15 +1,19 @@
|
|||||||
using System;
|
using System.Collections.Generic;
|
||||||
using System.Collections.Generic;
|
|
||||||
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;
|
||||||
|
|
||||||
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>();
|
|
||||||
|
|
||||||
foreach (var renderer in part.FindModelComponents<MeshRenderer>()) {
|
foreach (var renderer in part.FindModelComponents<MeshRenderer>()) {
|
||||||
var target = renderer.transform;
|
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) {
|
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) {
|
||||||
@ -37,15 +44,5 @@ namespace ConformalDecals {
|
|||||||
target.Render(decalMaterial, partMPB, camera);
|
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user