diff --git a/Source/ConformalDecals/ConformalDecals.csproj b/Source/ConformalDecals/ConformalDecals.csproj index be0f319..1ef5737 100644 --- a/Source/ConformalDecals/ConformalDecals.csproj +++ b/Source/ConformalDecals/ConformalDecals.csproj @@ -39,8 +39,8 @@ - - dlls/UnityEngine.dll + + dlls\UnityEngine.CoreModule.dll diff --git a/Source/ConformalDecals/MaterialModifiers/MaterialPropertyCollection.cs b/Source/ConformalDecals/MaterialModifiers/MaterialPropertyCollection.cs index 7dabc81..a07d947 100644 --- a/Source/ConformalDecals/MaterialModifiers/MaterialPropertyCollection.cs +++ b/Source/ConformalDecals/MaterialModifiers/MaterialPropertyCollection.cs @@ -108,7 +108,5 @@ namespace ConformalDecals.MaterialModifiers { textureProperty.UpdateScale(ParsedMaterial, scale); } } - - } } \ No newline at end of file diff --git a/Source/ConformalDecals/MaterialModifiers/TextureMaterialProperty.cs b/Source/ConformalDecals/MaterialModifiers/TextureMaterialProperty.cs index a13592f..759f5b6 100644 --- a/Source/ConformalDecals/MaterialModifiers/TextureMaterialProperty.cs +++ b/Source/ConformalDecals/MaterialModifiers/TextureMaterialProperty.cs @@ -5,13 +5,13 @@ namespace ConformalDecals.MaterialModifiers { public class TextureMaterialProperty : MaterialProperty { public string TextureUrl { get; } public Texture2D TextureRef { get; } - + public bool IsNormal { get; } public bool IsMain { get; } public bool AutoScale { get; } public Rect TileRect { get; } - + public float AspectRatio => TileRect.height / TileRect.width; private readonly Vector2 _textureOffset; diff --git a/Source/ConformalDecals/ModuleConformalDecal.cs b/Source/ConformalDecals/ModuleConformalDecal.cs index 1904e83..397ba01 100644 --- a/Source/ConformalDecals/ModuleConformalDecal.cs +++ b/Source/ConformalDecals/ModuleConformalDecal.cs @@ -1,5 +1,61 @@ +using System; +using UnityEngine; + namespace ConformalDecals { public class ModuleConformalDecal : PartModule { [KSPField] public string decalPreviewTransform = ""; + + public void OnStart(StartState state) { + if ((state & StartState.Editor) != 0) { + GameEvents.onEditorPartEvent.Add(OnEditorEvent); + } + else { + Attach(); + } + } + + public void OnEditorEvent(ConstructionEventType eventType, Part eventPart) { + if (eventPart != this.part) return; + switch (eventType) { + case ConstructionEventType.PartAttached: + Attach(); + break; + case ConstructionEventType.PartDetached: + Detach(); + break; + case ConstructionEventType.PartOffsetting: + case ConstructionEventType.PartRotated: + case ConstructionEventType.PartDragging: + Project(); + break; + } + + } + + public void Attach() { + if (part.parent == null) { + this.LogError("Attach function called but part has no parent!"); + return; + } + + Camera.onPreCull += Render; + } + + public void Detach() { + if (part.parent != null) { + this.LogError("Detach function called but part still has parent!"); + return; + } + + Camera.onPreCull -= Render; + } + + public void OnDisable() { + Camera.onPreCull -= Render; + } + + public void Project() { } + + public void Render(Camera camera) { } } } \ No newline at end of file diff --git a/Source/ConformalDecals/ProjectionTarget.cs b/Source/ConformalDecals/ProjectionTarget.cs index e9b227b..6add0c0 100644 --- a/Source/ConformalDecals/ProjectionTarget.cs +++ b/Source/ConformalDecals/ProjectionTarget.cs @@ -63,11 +63,12 @@ namespace ConformalDecals { _projectionEnabled = projectorBounds.Intersects(targetBounds); } - public bool Render(Material decalMaterial) { + public bool Render(Material decalMaterial, MaterialPropertyBlock partMPB, Camera camera) { if (_projectionEnabled) { - foreach (var camera in Camera.allCameras) { - Graphics.DrawMesh(_targetMesh, Target.worldToLocalMatrix, decalMaterial, 0, camera, 0, DecalMPB, ShadowCastingMode.Off, true); - } + DecalMPB.SetFloat(PropertyIDs._RimFalloff, partMPB.GetFloat(PropertyIDs._RimFalloff)); + DecalMPB.SetColor(PropertyIDs._RimFalloff, partMPB.GetColor(PropertyIDs._RimFalloff)); + + Graphics.DrawMesh(_targetMesh, Target.localToWorldMatrix, decalMaterial, 0, camera, 0, DecalMPB, ShadowCastingMode.Off, true); return true; }