mirror of
https://github.com/drewcassidy/KSP-Conformal-Decals.git
synced 2024-09-01 18:23:54 +00:00
Get multi-projection to work
This commit is contained in:
parent
169f2e9bf1
commit
2f2b6fb692
Binary file not shown.
9
Source/ConformalDecals/IProjectionTarget.cs
Normal file
9
Source/ConformalDecals/IProjectionTarget.cs
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
namespace ConformalDecals {
|
||||||
|
public interface IProjectionTarget {
|
||||||
|
void Project(Matrix4x4 orthoMatrix, Transform projector, Bounds projectionBounds);
|
||||||
|
void Render(Material decalMaterial, MaterialPropertyBlock partMPB, Camera camera);
|
||||||
|
ConfigNode Save();
|
||||||
|
}
|
||||||
|
}
|
@ -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 List<ProjectionTarget> _targets;
|
private Dictionary<Part, ProjectionPartTarget> _targets;
|
||||||
|
|
||||||
private bool _isAttached;
|
private bool _isAttached;
|
||||||
private Matrix4x4 _orthoMatrix;
|
private Matrix4x4 _orthoMatrix;
|
||||||
@ -155,7 +155,7 @@ namespace ConformalDecals {
|
|||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override void OnIconCreate() {
|
public override void OnIconCreate() {
|
||||||
UpdateTextures();
|
UpdateTextures();
|
||||||
UpdateScale();
|
UpdateProjection();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
@ -226,12 +226,12 @@ namespace ConformalDecals {
|
|||||||
protected void OnProjectionTweakEvent(BaseField field, object obj) {
|
protected void OnProjectionTweakEvent(BaseField field, object obj) {
|
||||||
// scale or depth values have been changed, so update scale
|
// scale or depth values have been changed, so update scale
|
||||||
// and update projection matrices if attached
|
// and update projection matrices if attached
|
||||||
UpdateScale();
|
UpdateProjection();
|
||||||
UpdateTargets();
|
UpdateTargets();
|
||||||
|
|
||||||
foreach (var counterpart in part.symmetryCounterparts) {
|
foreach (var counterpart in part.symmetryCounterparts) {
|
||||||
var decal = counterpart.GetComponent<ModuleConformalDecal>();
|
var decal = counterpart.GetComponent<ModuleConformalDecal>();
|
||||||
decal.UpdateScale();
|
decal.UpdateProjection();
|
||||||
decal.UpdateTargets();
|
decal.UpdateTargets();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -256,38 +256,80 @@ namespace ConformalDecals {
|
|||||||
|
|
||||||
/// Called when a new variant is applied in the editor
|
/// Called when a new variant is applied in the editor
|
||||||
protected void OnVariantApplied(Part eventPart, PartVariant variant) {
|
protected void OnVariantApplied(Part eventPart, PartVariant variant) {
|
||||||
if (_isAttached && eventPart != null) {
|
if (_isAttached && eventPart != null && (!projectMultiple || eventPart == part.parent)) {
|
||||||
if (projectMultiple && eventPart != part.parent) return;
|
_targets.Remove(eventPart);
|
||||||
else if (!_targets.Select(o => o.targetPart).Contains(eventPart)) return;
|
|
||||||
|
|
||||||
UpdateTargets();
|
UpdateTargets();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Called when an editor event occurs
|
/// Called when an editor event occurs
|
||||||
protected void OnEditorEvent(ConstructionEventType eventType, Part eventPart) {
|
protected void OnEditorEvent(ConstructionEventType eventType, Part eventPart) {
|
||||||
if (this.part != eventPart && !part.symmetryCounterparts.Contains(eventPart)) return;
|
|
||||||
switch (eventType) {
|
switch (eventType) {
|
||||||
case ConstructionEventType.PartAttached:
|
case ConstructionEventType.PartAttached:
|
||||||
OnAttach();
|
OnPartAttached(eventPart);
|
||||||
break;
|
break;
|
||||||
case ConstructionEventType.PartDetached:
|
case ConstructionEventType.PartDetached:
|
||||||
OnDetach();
|
OnPartDetached(eventPart);
|
||||||
break;
|
break;
|
||||||
case ConstructionEventType.PartOffsetting:
|
case ConstructionEventType.PartOffsetting:
|
||||||
case ConstructionEventType.PartRotating:
|
case ConstructionEventType.PartRotating:
|
||||||
UpdateScale();
|
OnPartTransformed(eventPart);
|
||||||
UpdateTargets();
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Called when a part is transformed in the editor
|
||||||
|
protected void OnPartTransformed(Part eventPart) {
|
||||||
|
if (this.part == eventPart) {
|
||||||
|
UpdateProjection();
|
||||||
|
UpdateTargets();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
UpdatePartTarget(eventPart, _boundsRenderer.bounds);
|
||||||
|
// recursively call for child parts
|
||||||
|
foreach (var child in eventPart.children) {
|
||||||
|
OnPartTransformed(child);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Called when a part is attached in the editor
|
||||||
|
protected void OnPartAttached(Part eventPart) {
|
||||||
|
if (this.part == eventPart) {
|
||||||
|
OnAttach();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
UpdatePartTarget(eventPart, _boundsRenderer.bounds);
|
||||||
|
// recursively call for child parts
|
||||||
|
foreach (var child in eventPart.children) {
|
||||||
|
OnPartAttached(child);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Called when a part is detached in the editor
|
||||||
|
protected void OnPartDetached(Part eventPart) {
|
||||||
|
if (this.part == eventPart) {
|
||||||
|
OnDetach();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
_targets.Remove(eventPart);
|
||||||
|
// recursively call for child parts
|
||||||
|
foreach (var child in eventPart.children) {
|
||||||
|
OnPartDetached(child);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Called when part `willDie` will be destroyed
|
/// Called when part `willDie` will be destroyed
|
||||||
protected void OnPartWillDie(Part willDie) {
|
protected void OnPartWillDie(Part willDie) {
|
||||||
if (willDie == part.parent) {
|
if (willDie == part.parent) {
|
||||||
this.Log("Parent part about to be destroyed! Killing decal part.");
|
this.Log("Parent part about to be destroyed! Killing decal part.");
|
||||||
part.Die();
|
part.Die();
|
||||||
}
|
}
|
||||||
|
else if (projectMultiple) {
|
||||||
|
_targets.Remove(willDie);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Called when decal is attached to a new part
|
/// Called when decal is attached to a new part
|
||||||
@ -310,7 +352,7 @@ namespace ConformalDecals {
|
|||||||
Camera.onPreCull += Render;
|
Camera.onPreCull += Render;
|
||||||
|
|
||||||
UpdateMaterials();
|
UpdateMaterials();
|
||||||
UpdateScale();
|
UpdateProjection();
|
||||||
UpdateTargets();
|
UpdateTargets();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -328,11 +370,11 @@ namespace ConformalDecals {
|
|||||||
Camera.onPreCull -= Render;
|
Camera.onPreCull -= Render;
|
||||||
|
|
||||||
UpdateMaterials();
|
UpdateMaterials();
|
||||||
UpdateScale();
|
UpdateProjection();
|
||||||
}
|
}
|
||||||
|
|
||||||
// FUNCTIONS
|
// FUNCTIONS
|
||||||
|
|
||||||
/// Load any settings from the decal config
|
/// Load any settings from the decal config
|
||||||
protected virtual void LoadDecal(ConfigNode node) {
|
protected virtual void LoadDecal(ConfigNode node) {
|
||||||
// PARSE TRANSFORMS
|
// PARSE TRANSFORMS
|
||||||
@ -498,10 +540,10 @@ namespace ConformalDecals {
|
|||||||
protected virtual void UpdateAll() {
|
protected virtual void UpdateAll() {
|
||||||
UpdateTextures();
|
UpdateTextures();
|
||||||
UpdateMaterials();
|
UpdateMaterials();
|
||||||
UpdateScale();
|
UpdateProjection();
|
||||||
UpdateTargets();
|
UpdateTargets();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Update decal textures
|
/// Update decal textures
|
||||||
protected virtual void UpdateTextures() { }
|
protected virtual void UpdateTextures() { }
|
||||||
|
|
||||||
@ -521,7 +563,7 @@ namespace ConformalDecals {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Update decal scale and projection
|
/// Update decal scale and projection
|
||||||
protected void UpdateScale() {
|
protected void UpdateProjection() {
|
||||||
|
|
||||||
// Update scale and depth
|
// Update scale and depth
|
||||||
scale = Mathf.Max(0.01f, scale);
|
scale = Mathf.Max(0.01f, scale);
|
||||||
@ -559,7 +601,7 @@ namespace ConformalDecals {
|
|||||||
if (_isAttached) {
|
if (_isAttached) {
|
||||||
// Update projection targets
|
// Update projection targets
|
||||||
if (_targets == null) {
|
if (_targets == null) {
|
||||||
_targets = new List<ProjectionTarget>();
|
_targets = new Dictionary<Part, ProjectionPartTarget>();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
_targets.Clear();
|
_targets.Clear();
|
||||||
@ -587,6 +629,9 @@ namespace ConformalDecals {
|
|||||||
protected void UpdateTargets() {
|
protected void UpdateTargets() {
|
||||||
if (!_isAttached) return;
|
if (!_isAttached) return;
|
||||||
|
|
||||||
|
var projectionBounds = _boundsRenderer.bounds;
|
||||||
|
|
||||||
|
// collect list of potential targets
|
||||||
IEnumerable<Part> targetParts;
|
IEnumerable<Part> targetParts;
|
||||||
if (projectMultiple) {
|
if (projectMultiple) {
|
||||||
targetParts = HighLogic.LoadedSceneIsFlight ? part.vessel.parts : EditorLogic.fetch.ship.parts;
|
targetParts = HighLogic.LoadedSceneIsFlight ? part.vessel.parts : EditorLogic.fetch.ship.parts;
|
||||||
@ -596,25 +641,30 @@ namespace ConformalDecals {
|
|||||||
}
|
}
|
||||||
|
|
||||||
foreach (var targetPart in targetParts) {
|
foreach (var targetPart in targetParts) {
|
||||||
if (targetPart.GetComponent<ModuleConformalDecal>() != null) continue; // skip other decals
|
UpdatePartTarget(targetPart, projectionBounds);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
foreach (var renderer in targetPart.FindModelComponents<MeshRenderer>()) {
|
protected void UpdatePartTarget(Part targetPart, Bounds projectionBounds) {
|
||||||
var target = renderer.transform;
|
if (targetPart.GetComponent<ModuleConformalDecal>() != null) return; // skip other decals
|
||||||
var filter = target.GetComponent<MeshFilter>();
|
|
||||||
|
|
||||||
// check if the target has any missing data
|
this.Log($"Updating projection onto part {targetPart.name}");
|
||||||
if (!ProjectionTarget.ValidateTarget(target, renderer, filter)) continue;
|
|
||||||
|
|
||||||
// check bounds for intersection
|
if (!_targets.TryGetValue(targetPart, out var target)) {
|
||||||
if (_boundsRenderer.bounds.Intersects(renderer.bounds)) {
|
var rendererList = targetPart.FindModelComponents<MeshRenderer>();
|
||||||
// create new ProjectionTarget to represent the renderer
|
|
||||||
var projectionTarget = new ProjectionTarget(targetPart, target, renderer, filter, _orthoMatrix, decalProjectorTransform, useBaseNormal);
|
|
||||||
|
|
||||||
// add the target to the list
|
if (rendererList.Any(o => projectionBounds.Intersects(o.bounds))) {
|
||||||
_targets.Add(projectionTarget);
|
target = new ProjectionPartTarget(targetPart, useBaseNormal);
|
||||||
}
|
_targets.Add(targetPart, target);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.Log($"valid target: {targetPart.name}");
|
||||||
|
|
||||||
|
target.Project(_orthoMatrix, decalProjectorTransform, projectionBounds);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Render the decal
|
/// Render the decal
|
||||||
@ -622,7 +672,7 @@ namespace ConformalDecals {
|
|||||||
if (!_isAttached) return;
|
if (!_isAttached) return;
|
||||||
|
|
||||||
// render on each target object
|
// render on each target object
|
||||||
foreach (var target in _targets) {
|
foreach (var target in _targets.Values) {
|
||||||
target.Render(_decalMaterial, part.mpb, camera);
|
target.Render(_decalMaterial, part.mpb, camera);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -264,7 +264,7 @@ namespace ConformalDecals {
|
|||||||
|
|
||||||
UpdateTextures();
|
UpdateTextures();
|
||||||
UpdateMaterials();
|
UpdateMaterials();
|
||||||
UpdateScale();
|
UpdateProjection();
|
||||||
|
|
||||||
// QUEUE PART FOR ICON FIXING IN VAB
|
// QUEUE PART FOR ICON FIXING IN VAB
|
||||||
DecalIconFixer.QueuePart(part.name);
|
DecalIconFixer.QueuePart(part.name);
|
||||||
|
@ -6,69 +6,65 @@ using UnityEngine;
|
|||||||
using UnityEngine.Rendering;
|
using UnityEngine.Rendering;
|
||||||
|
|
||||||
namespace ConformalDecals {
|
namespace ConformalDecals {
|
||||||
public class ProjectionTarget {
|
public class ProjectionMeshTarget : IProjectionTarget {
|
||||||
|
// enabled flag
|
||||||
|
public bool enabled = true;
|
||||||
|
|
||||||
// Target object data
|
// Target object data
|
||||||
public readonly Transform target;
|
public readonly Transform target;
|
||||||
public readonly Part targetPart;
|
public readonly Transform root;
|
||||||
private readonly Mesh _targetMesh;
|
public readonly Mesh mesh;
|
||||||
private readonly Matrix4x4 _decalMatrix;
|
public readonly MeshRenderer renderer;
|
||||||
private readonly Vector3 _decalNormal;
|
|
||||||
private readonly Vector3 _decalTangent;
|
// Projection data
|
||||||
private readonly bool _useBaseNormal;
|
private Matrix4x4 _decalMatrix;
|
||||||
|
private Vector3 _decalNormal;
|
||||||
|
private Vector3 _decalTangent;
|
||||||
|
|
||||||
// property block
|
// property block
|
||||||
private readonly MaterialPropertyBlock _decalMPB;
|
private readonly MaterialPropertyBlock _decalMPB;
|
||||||
|
|
||||||
public ProjectionTarget(Part targetPart, Transform target, MeshRenderer renderer, MeshFilter filter,
|
public ProjectionMeshTarget(Transform target, Transform root, MeshRenderer renderer, Mesh mesh, bool useBaseNormal) {
|
||||||
Matrix4x4 orthoMatrix, Transform projector, bool useBaseNormal) {
|
this.root = root;
|
||||||
|
|
||||||
this.targetPart = targetPart;
|
|
||||||
this.target = target;
|
this.target = target;
|
||||||
_targetMesh = filter.sharedMesh;
|
this.renderer = renderer;
|
||||||
_useBaseNormal = useBaseNormal;
|
this.mesh = mesh;
|
||||||
_decalMPB = new MaterialPropertyBlock();
|
_decalMPB = new MaterialPropertyBlock();
|
||||||
|
|
||||||
var projectorToTargetMatrix = target.worldToLocalMatrix * projector.localToWorldMatrix;
|
SetNormalMap(renderer.sharedMaterial, useBaseNormal);
|
||||||
|
|
||||||
_decalMatrix = orthoMatrix * projectorToTargetMatrix.inverse;
|
|
||||||
_decalNormal = projectorToTargetMatrix.MultiplyVector(Vector3.back).normalized;
|
|
||||||
_decalTangent = projectorToTargetMatrix.MultiplyVector(Vector3.right).normalized;
|
|
||||||
|
|
||||||
SetupMPB(renderer.sharedMaterial);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public ProjectionTarget(ConfigNode node, Vessel vessel, bool useBaseNormal) {
|
public ProjectionMeshTarget(ConfigNode node, Transform root, bool useBaseNormal) {
|
||||||
var flightID = (uint) ParseUtil.ParseInt(node, "part");
|
if (node == null) throw new ArgumentNullException(nameof(node));
|
||||||
|
if (root == null) throw new ArgumentNullException(nameof(root));
|
||||||
|
|
||||||
var targetPath = ParseUtil.ParseString(node, "targetPath");
|
var targetPath = ParseUtil.ParseString(node, "targetPath");
|
||||||
var targetName = ParseUtil.ParseString(node, "targetName");
|
var targetName = ParseUtil.ParseString(node, "targetName");
|
||||||
|
|
||||||
_decalMatrix = ParseUtil.ParseMatrix4x4(node, "decalMatrix");
|
_decalMatrix = ParseUtil.ParseMatrix4x4(node, "decalMatrix");
|
||||||
_decalNormal = ParseUtil.ParseVector3(node, "decalNormal");
|
_decalNormal = ParseUtil.ParseVector3(node, "decalNormal");
|
||||||
_decalTangent = ParseUtil.ParseVector3(node, "decalTangent");
|
_decalTangent = ParseUtil.ParseVector3(node, "decalTangent");
|
||||||
_useBaseNormal = useBaseNormal;
|
|
||||||
_decalMPB = new MaterialPropertyBlock();
|
_decalMPB = new MaterialPropertyBlock();
|
||||||
|
|
||||||
targetPart = vessel[flightID];
|
target = LoadTransformPath(targetPath, root);
|
||||||
if (targetPart == null) throw new IndexOutOfRangeException("Vessel returned null part");
|
|
||||||
target = LoadTransformPath(targetPath, targetPart.transform);
|
|
||||||
if (target.name != targetName) throw new FormatException("Target name does not match");
|
if (target.name != targetName) throw new FormatException("Target name does not match");
|
||||||
|
|
||||||
var renderer = target.GetComponent<MeshRenderer>();
|
renderer = target.GetComponent<MeshRenderer>();
|
||||||
var filter = target.GetComponent<MeshFilter>();
|
var filter = target.GetComponent<MeshFilter>();
|
||||||
|
|
||||||
if (!ValidateTarget(target, renderer, filter)) throw new FormatException("Invalid target");
|
if (!ValidateTarget(target, renderer, filter)) throw new FormatException("Invalid target");
|
||||||
|
|
||||||
_targetMesh = filter.sharedMesh;
|
mesh = filter.sharedMesh;
|
||||||
|
|
||||||
SetupMPB(renderer.sharedMaterial);
|
SetNormalMap(renderer.sharedMaterial, useBaseNormal);
|
||||||
}
|
|
||||||
|
|
||||||
private void SetupMPB(Material targetMaterial) {
|
|
||||||
_decalMPB.SetMatrix(DecalPropertyIDs._ProjectionMatrix, _decalMatrix);
|
_decalMPB.SetMatrix(DecalPropertyIDs._ProjectionMatrix, _decalMatrix);
|
||||||
_decalMPB.SetVector(DecalPropertyIDs._DecalNormal, _decalNormal);
|
_decalMPB.SetVector(DecalPropertyIDs._DecalNormal, _decalNormal);
|
||||||
_decalMPB.SetVector(DecalPropertyIDs._DecalTangent, _decalTangent);
|
_decalMPB.SetVector(DecalPropertyIDs._DecalTangent, _decalTangent);
|
||||||
|
}
|
||||||
|
|
||||||
if (_useBaseNormal && targetMaterial.HasProperty(DecalPropertyIDs._BumpMap)) {
|
private void SetNormalMap(Material targetMaterial, bool useBaseNormal) {
|
||||||
|
if (useBaseNormal && targetMaterial.HasProperty(DecalPropertyIDs._BumpMap)) {
|
||||||
_decalMPB.SetTexture(DecalPropertyIDs._BumpMap, targetMaterial.GetTexture(DecalPropertyIDs._BumpMap));
|
_decalMPB.SetTexture(DecalPropertyIDs._BumpMap, targetMaterial.GetTexture(DecalPropertyIDs._BumpMap));
|
||||||
|
|
||||||
var normalScale = targetMaterial.GetTextureScale(DecalPropertyIDs._BumpMap);
|
var normalScale = targetMaterial.GetTextureScale(DecalPropertyIDs._BumpMap);
|
||||||
@ -81,20 +77,40 @@ namespace ConformalDecals {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void Project(Matrix4x4 orthoMatrix, Transform projector, Bounds projectionBounds) {
|
||||||
|
if (projectionBounds.Intersects(renderer.bounds)) {
|
||||||
|
enabled = true;
|
||||||
|
|
||||||
|
var projectorToTargetMatrix = target.worldToLocalMatrix * projector.localToWorldMatrix;
|
||||||
|
|
||||||
|
_decalMatrix = orthoMatrix * projectorToTargetMatrix.inverse;
|
||||||
|
_decalNormal = projectorToTargetMatrix.MultiplyVector(Vector3.back).normalized;
|
||||||
|
_decalTangent = projectorToTargetMatrix.MultiplyVector(Vector3.right).normalized;
|
||||||
|
|
||||||
|
_decalMPB.SetMatrix(DecalPropertyIDs._ProjectionMatrix, _decalMatrix);
|
||||||
|
_decalMPB.SetVector(DecalPropertyIDs._DecalNormal, _decalNormal);
|
||||||
|
_decalMPB.SetVector(DecalPropertyIDs._DecalTangent, _decalTangent);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
enabled = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void Render(Material decalMaterial, MaterialPropertyBlock partMPB, Camera camera) {
|
public void Render(Material decalMaterial, MaterialPropertyBlock partMPB, Camera camera) {
|
||||||
|
if (!enabled) return;
|
||||||
|
|
||||||
_decalMPB.SetFloat(PropertyIDs._RimFalloff, partMPB.GetFloat(PropertyIDs._RimFalloff));
|
_decalMPB.SetFloat(PropertyIDs._RimFalloff, partMPB.GetFloat(PropertyIDs._RimFalloff));
|
||||||
_decalMPB.SetColor(PropertyIDs._RimColor, partMPB.GetColor(PropertyIDs._RimColor));
|
_decalMPB.SetColor(PropertyIDs._RimColor, partMPB.GetColor(PropertyIDs._RimColor));
|
||||||
|
|
||||||
Graphics.DrawMesh(_targetMesh, 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() {
|
public ConfigNode Save() {
|
||||||
var node = new ConfigNode("TARGET");
|
var node = new ConfigNode("MESH_TARGET");
|
||||||
node.AddValue("part", targetPart.flightID);
|
|
||||||
node.AddValue("decalMatrix", _decalMatrix);
|
node.AddValue("decalMatrix", _decalMatrix);
|
||||||
node.AddValue("decalNormal", _decalNormal);
|
node.AddValue("decalNormal", _decalNormal);
|
||||||
node.AddValue("decalTangent", _decalTangent);
|
node.AddValue("decalTangent", _decalTangent);
|
||||||
node.AddValue("targetPath", SaveTransformPath(target, targetPart.transform)); // used to find the target transform
|
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
|
node.AddValue("targetName", target.name); // used to validate the mesh has not changed since last load
|
||||||
|
|
||||||
return node;
|
return node;
|
||||||
@ -116,7 +132,7 @@ namespace ConformalDecals {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static string SaveTransformPath(Transform leaf, Transform root) {
|
private static string SaveTransformPath(Transform leaf, Transform root) {
|
||||||
var builder = new StringBuilder(leaf.name);
|
var builder = new StringBuilder($"{leaf.GetSiblingIndex()}");
|
||||||
var current = leaf.parent;
|
var current = leaf.parent;
|
||||||
|
|
||||||
while (current != root) {
|
while (current != root) {
|
51
Source/ConformalDecals/ProjectionPartTarget.cs
Normal file
51
Source/ConformalDecals/ProjectionPartTarget.cs
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
namespace ConformalDecals {
|
||||||
|
public class ProjectionPartTarget : IProjectionTarget {
|
||||||
|
public readonly Part part;
|
||||||
|
public readonly List<ProjectionMeshTarget> meshTargets;
|
||||||
|
|
||||||
|
public ProjectionPartTarget(Part part, bool useBaseNormal) {
|
||||||
|
this.part = part;
|
||||||
|
meshTargets = new List<ProjectionMeshTarget>();
|
||||||
|
|
||||||
|
foreach (var renderer in part.FindModelComponents<MeshRenderer>()) {
|
||||||
|
var target = renderer.transform;
|
||||||
|
var filter = target.GetComponent<MeshFilter>();
|
||||||
|
|
||||||
|
// check if the target has any missing data
|
||||||
|
if (!ProjectionMeshTarget.ValidateTarget(target, renderer, filter)) continue;
|
||||||
|
|
||||||
|
// create new ProjectionTarget to represent the renderer
|
||||||
|
var projectionTarget = new ProjectionMeshTarget(target, part.transform, renderer, filter.sharedMesh, useBaseNormal);
|
||||||
|
|
||||||
|
// add the target to the list
|
||||||
|
meshTargets.Add(projectionTarget);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Project(Matrix4x4 orthoMatrix, Transform projector, Bounds projectionBounds) {
|
||||||
|
foreach (var meshTarget in meshTargets) {
|
||||||
|
meshTarget.Project(orthoMatrix, projector, projectionBounds);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Render(Material decalMaterial, MaterialPropertyBlock partMPB, Camera camera) {
|
||||||
|
foreach (var target in meshTargets) {
|
||||||
|
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