From 00908c9e1307fa320f17721c554636c3cac88ab2 Mon Sep 17 00:00:00 2001 From: drewcassidy Date: Tue, 2 Jun 2020 22:45:48 -0700 Subject: [PATCH] Get flag module working --- .../ConformalDecals/Parts/decal-blank.cfg | 6 +- .../ConformalDecals/Parts/decal-flag.cfg | 53 ++++++++++++++++++ .../Plugins/ConformalDecals.dll | 4 +- .../ModuleConformalDecalBase.cs | 56 +++++++------------ .../ModuleConformalDecalFlag.cs | 37 +++++++++--- .../ModuleConformalDecalGeneric.cs | 15 ++++- 6 files changed, 122 insertions(+), 49 deletions(-) create mode 100644 Distribution/GameData/ConformalDecals/Parts/decal-flag.cfg diff --git a/Distribution/GameData/ConformalDecals/Parts/decal-blank.cfg b/Distribution/GameData/ConformalDecals/Parts/decal-blank.cfg index ca47e70..4125aa1 100644 --- a/Distribution/GameData/ConformalDecals/Parts/decal-blank.cfg +++ b/Distribution/GameData/ConformalDecals/Parts/decal-blank.cfg @@ -43,7 +43,7 @@ PART MODULE { - name = ModuleConformalDecal + name = ModuleConformalDecalGeneric decalFront = Decal-Front decalBack = Decal-Back @@ -61,7 +61,7 @@ PART MODULE { IDENTIFIER { - name = ModuleConformalDecal + name = ModuleConformalDecalGeneric } DATA { decalShader = ConformalDecals/Feature/Bumped @@ -87,7 +87,7 @@ PART MODULE { IDENTIFIER { - name = ModuleConformalDecal + name = ModuleConformalDecalGeneric } DATA { decalShader = ConformalDecals/Paint/Diffuse diff --git a/Distribution/GameData/ConformalDecals/Parts/decal-flag.cfg b/Distribution/GameData/ConformalDecals/Parts/decal-flag.cfg new file mode 100644 index 0000000..aff1d20 --- /dev/null +++ b/Distribution/GameData/ConformalDecals/Parts/decal-flag.cfg @@ -0,0 +1,53 @@ +// ReStock+ 1.2.0 +// 0.625m Radial Attachment Node +PART +{ + name = decal-flag + module = Part + author = Andrew Cassidy + MODEL + { + model = ConformalDecals/Assets/decal-blank + scale = 1.0, 1.0, 1.0 + } + scale = 1 + rescaleFactor = 1 + + // Attachment + attachRules = 1,1,0,0,1 + node_attach = 0.0, 0.0, 0.1, 0.0, 0.0, -1.0 + + // Tech + TechRequired = specializedConstruction + entryCost = 600 + + // Info + cost = 75 + category = Structural + title = Blank Flag + manufacturer = #autoLOC_501648 // Maxo Construction Toys + description = foo + tags = foo + bulkheadProfiles = srf + + // Parameters + mass = 0.015 + dragModelType = default + maximum_drag = 0.1 + minimum_drag = 0.05 + angularDrag = 1.0 + crashTolerance = 10 + maxTemp = 2000 + breakingForce = 350 + breakingTorque = 150 + + MODULE + { + name = ModuleConformalDecalFlag + + decalFront = Decal-Front + decalBack = Decal-Back + + useBaseNormal = true + } +} diff --git a/Distribution/GameData/ConformalDecals/Plugins/ConformalDecals.dll b/Distribution/GameData/ConformalDecals/Plugins/ConformalDecals.dll index 0d9832d..558a128 100644 --- a/Distribution/GameData/ConformalDecals/Plugins/ConformalDecals.dll +++ b/Distribution/GameData/ConformalDecals/Plugins/ConformalDecals.dll @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:ad647a749e7c04e13234735a12958e628cae94e3275dcac1ab8117652a1ecefa -size 29696 +oid sha256:86aa2ce6f0d2889caae9f3d9ca47a047964ca7d1ed86e30ec6150569bf6455ff +size 30208 diff --git a/Source/ConformalDecals/ModuleConformalDecalBase.cs b/Source/ConformalDecals/ModuleConformalDecalBase.cs index 63a4b23..e3715ad 100644 --- a/Source/ConformalDecals/ModuleConformalDecalBase.cs +++ b/Source/ConformalDecals/ModuleConformalDecalBase.cs @@ -60,12 +60,6 @@ namespace ConformalDecals { private Bounds _decalBounds; private Vector2 _backTextureBaseScale; - public ModuleConformalDecalBase() { - decalBackTransform = null; - decalModelTransform = null; - decalProjectorTransform = null; - } - private int DecalQueue { get { _decalQueueCounter++; @@ -83,36 +77,6 @@ namespace ConformalDecals { if (HighLogic.LoadedSceneIsEditor) { UpdateTweakables(); } - - if (materialProperties == null) { - // materialProperties is null, so make a new one - materialProperties = ScriptableObject.CreateInstance(); - materialProperties.Initialize(); - } - else { - // materialProperties already exists, so make a copy - materialProperties = ScriptableObject.Instantiate(materialProperties); - } - - // set shader - materialProperties.SetShader(decalShader); - - // get back material if necessary - if (updateBackScale) { - this.Log("Getting material and base scale for back material"); - var backRenderer = decalBackTransform.GetComponent(); - if (backRenderer == null) { - this.LogError($"Specified decalBack transform {decalBack} has no renderer attached! Setting updateBackScale to false."); - updateBackScale = false; - } - else if ((backMaterial = backRenderer.material) == null) { - this.LogError($"Specified decalBack transform {decalBack} has a renderer but no material! Setting updateBackScale to false."); - updateBackScale = false; - } - else { - _backTextureBaseScale = backMaterial.GetTextureScale(PropertyIDs._MainTex); - } - } // find front transform decalFrontTransform = part.FindModelTransform(decalFront); @@ -148,6 +112,26 @@ namespace ConformalDecals { decalProjectorTransform = part.FindModelTransform(decalProjector); if (decalProjectorTransform == null) throw new FormatException($"Could not find decalProjector transform: '{decalProjector}'."); } + + // get back material if necessary + if (updateBackScale) { + this.Log("Getting material and base scale for back material"); + var backRenderer = decalBackTransform.GetComponent(); + if (backRenderer == null) { + this.LogError($"Specified decalBack transform {decalBack} has no renderer attached! Setting updateBackScale to false."); + updateBackScale = false; + } + else { + backMaterial = backRenderer.material; + if (backMaterial == null) { + this.LogError($"Specified decalBack transform {decalBack} has a renderer but no material! Setting updateBackScale to false."); + updateBackScale = false; + } + else { + _backTextureBaseScale = backMaterial.GetTextureScale(PropertyIDs._MainTex); + } + } + } // update EVERYTHING if currently attached if (_isAttached) { diff --git a/Source/ConformalDecals/ModuleConformalDecalFlag.cs b/Source/ConformalDecals/ModuleConformalDecalFlag.cs index f00bc62..1585c4e 100644 --- a/Source/ConformalDecals/ModuleConformalDecalFlag.cs +++ b/Source/ConformalDecals/ModuleConformalDecalFlag.cs @@ -5,15 +5,24 @@ using UnityEngine; namespace ConformalDecals { public class ModuleConformalDecalFlag : ModuleConformalDecalBase { - - [KSPField] - private MaterialTextureProperty _flagTextureProperty; + [KSPField] public MaterialTextureProperty flagTextureProperty; public override void OnLoad(ConfigNode node) { - base.OnLoad(node); - _flagTextureProperty = new MaterialTextureProperty("_MainTex", Texture2D.whiteTexture); - materialProperties.AddProperty(_flagTextureProperty); + if (materialProperties == null) { + // materialProperties is null, so make a new one + materialProperties = ScriptableObject.CreateInstance(); + materialProperties.Initialize(); + } + else { + // materialProperties already exists, so make a copy + materialProperties = ScriptableObject.Instantiate(materialProperties); + } + + // set shader + materialProperties.SetShader(decalShader); + + base.OnLoad(node); } public override void OnStart(StartState state) { @@ -24,7 +33,21 @@ namespace ConformalDecals { } private void UpdateFlag(string flagUrl) { - _flagTextureProperty.texture = GameDatabase.Instance.GetTexture(flagUrl, false); + this.Log($"Loading flag texture '{flagUrl}'."); + var flagTexture = GameDatabase.Instance.GetTexture(flagUrl, false); + if (flagTexture == null) { + this.LogWarning($"Unable to find flag texture '{flagUrl}'."); + return; + } + + if (flagTextureProperty == null) { + this.Log("Initializing flag property"); + flagTextureProperty = new MaterialTextureProperty("_Decal", flagTexture, isMain: true); + materialProperties.AddProperty(flagTextureProperty); + } + else { + flagTextureProperty.texture = flagTexture; + } materialProperties.UpdateMaterials(); } diff --git a/Source/ConformalDecals/ModuleConformalDecalGeneric.cs b/Source/ConformalDecals/ModuleConformalDecalGeneric.cs index 4b523fa..7a2fa76 100644 --- a/Source/ConformalDecals/ModuleConformalDecalGeneric.cs +++ b/Source/ConformalDecals/ModuleConformalDecalGeneric.cs @@ -5,8 +5,19 @@ using UnityEngine; namespace ConformalDecals { public class ModuleConformalDecalGeneric : ModuleConformalDecalBase { public override void OnLoad(ConfigNode node) { - base.OnLoad(node); + if (materialProperties == null) { + // materialProperties is null, so make a new one + materialProperties = ScriptableObject.CreateInstance(); + materialProperties.Initialize(); + } + else { + // materialProperties already exists, so make a copy + materialProperties = ScriptableObject.Instantiate(materialProperties); + } + + // set shader + materialProperties.SetShader(decalShader); // add texture nodes foreach (var textureNode in node.GetNodes("TEXTURE")) { materialProperties.AddProperty(new MaterialTextureProperty(textureNode)); @@ -21,6 +32,8 @@ namespace ConformalDecals { foreach (var colorNode in node.GetNodes("COLOR")) { materialProperties.AddProperty(new MaterialColorProperty(colorNode)); } + + base.OnLoad(node); } } } \ No newline at end of file