diff --git a/Distribution/GameData/ConformalDecals/Assets/Tortilla-diffuse.dds b/Distribution/GameData/ConformalDecals/Assets/Tortilla-diffuse.dds new file mode 100644 index 0000000..c54bbbc Binary files /dev/null and b/Distribution/GameData/ConformalDecals/Assets/Tortilla-diffuse.dds differ diff --git a/Distribution/GameData/ConformalDecals/Assets/Tortilla-normal.dds b/Distribution/GameData/ConformalDecals/Assets/Tortilla-normal.dds new file mode 100644 index 0000000..ccc9704 Binary files /dev/null and b/Distribution/GameData/ConformalDecals/Assets/Tortilla-normal.dds differ diff --git a/Distribution/GameData/ConformalDecals/Assets/decal-blank.mu b/Distribution/GameData/ConformalDecals/Assets/decal-blank.mu index 45e2b36..56b379e 100644 Binary files a/Distribution/GameData/ConformalDecals/Assets/decal-blank.mu and b/Distribution/GameData/ConformalDecals/Assets/decal-blank.mu differ diff --git a/Distribution/GameData/ConformalDecals/Parts/decal-blank.cfg b/Distribution/GameData/ConformalDecals/Parts/decal-blank.cfg index ff31b10..5787488 100644 --- a/Distribution/GameData/ConformalDecals/Parts/decal-blank.cfg +++ b/Distribution/GameData/ConformalDecals/Parts/decal-blank.cfg @@ -7,7 +7,7 @@ PART author = Andrew Cassidy MODEL { - model = ConformalDecals/decal-blank + model = ConformalDecals/Assets/decal-blank scale = 1.0, 1.0, 1.0 } scale = 1 @@ -15,7 +15,7 @@ PART // Attachment attachRules = 1,1,1,1,1 - node_attach = 0.0, 0.0, 0.0, 0.0, -1.0, 0.0 + node_attach = 0.0, -0.1, 0.0, 0.0, -1.0, 0.0 // Tech TechRequired = specializedConstruction @@ -27,10 +27,8 @@ PART title = Blank Decal manufacturer = #autoLOC_501648 // Maxo Construction Toys description = foo - // After the surprise success of the BZ-52, the Maxo Construction Toys engineering team came out of hiding to reveal the new pocket model. tags = foo - // affix anchor mount secure restock - bulkheadProfiles = size0, srf + bulkheadProfiles = srf // Parameters mass = 0.015 @@ -42,4 +40,28 @@ PART maxTemp = 2000 breakingForce = 350 breakingTorque = 150 + + MODULE + { + name = ModuleConformalDecal + + MATERIAL + { + shader = ConformalDecals/Feature/Bumped + + TEXTURE + { + name = _Decal + textureURL = ConformalDecals/Assets/Tortilla-diffuse + isMain = true + } + + TEXTURE + { + name = _BumpMap + textureURL = ConformalDecals/Assets/Tortilla-normal + isNormalMap = true + } + } + } } diff --git a/Source/ConformalDecals/MaterialModifiers/MaterialPropertyCollection.cs b/Source/ConformalDecals/MaterialModifiers/MaterialPropertyCollection.cs index 866e8af..711d8a2 100644 --- a/Source/ConformalDecals/MaterialModifiers/MaterialPropertyCollection.cs +++ b/Source/ConformalDecals/MaterialModifiers/MaterialPropertyCollection.cs @@ -3,22 +3,22 @@ using System.Collections.Generic; using UnityEngine; namespace ConformalDecals.MaterialModifiers { - public class MaterialPropertyCollection { - public TextureMaterialProperty MainTextureProperty { get; } + public class MaterialPropertyCollection : ScriptableObject { + public TextureMaterialProperty MainTextureProperty { get; set; } - public Material ParsedMaterial { get; } + public Material parsedMaterial; - public bool UseBaseNormal { get; } + public bool UseBaseNormal { get; private set; } - private readonly List _materialProperties; - private readonly List _textureMaterialProperties; + private List _materialProperties; + private List _textureMaterialProperties; - public String BaseNormalSrc { get; } - public String BaseNormalDest { get; } + public String BaseNormalSrc { get; private set; } + public String BaseNormalDest { get; private set; } private const string _normalTextureName = "_BumpMap"; - public MaterialPropertyCollection(ConfigNode node, PartModule module) { + public void Initialize(ConfigNode node, PartModule module) { // Initialize fields _materialProperties = new List(); @@ -34,7 +34,7 @@ namespace ConformalDecals.MaterialModifiers { throw new FormatException($"Shader not found: '{shaderString}'"); } - ParsedMaterial = new Material(shaderRef); + parsedMaterial = new Material(shaderRef); // Get useBaseNormal value var useBaseNormalString = node.GetValue("useBaseNormal"); @@ -92,7 +92,7 @@ namespace ConformalDecals.MaterialModifiers { } _materialProperties.Add(property); - property.Modify(ParsedMaterial); + property.Modify(parsedMaterial); } catch (Exception e) { @@ -101,11 +101,13 @@ namespace ConformalDecals.MaterialModifiers { module.LogException("Exception while parsing material node", e); } } + + module.Log($"Parsed {_materialProperties.Count} properties"); } public void UpdateMaterial(Vector2 scale) { foreach (var textureProperty in _textureMaterialProperties) { - textureProperty.UpdateScale(ParsedMaterial, scale); + textureProperty.UpdateScale(parsedMaterial, scale); } } } diff --git a/Source/ConformalDecals/ModuleConformalDecal.cs b/Source/ConformalDecals/ModuleConformalDecal.cs index 1cde998..466e9eb 100644 --- a/Source/ConformalDecals/ModuleConformalDecal.cs +++ b/Source/ConformalDecals/ModuleConformalDecal.cs @@ -20,49 +20,49 @@ namespace ConformalDecals { [KSPField(guiActive = true, guiFormat = "F2", guiName = "Aspect Ratio")] public float aspectRatio = 1.0f; - private List _targets; - private MaterialPropertyCollection _materialProperties; - private Material _material; + [KSPField] public MaterialPropertyCollection materialProperties; + + [KSPField] public Transform decalPreviewTransformRef; + [KSPField] public Transform decalModelTransformRef; + + private List _targets; private Matrix4x4 _orthoMatrix; private Bounds _decalBounds; - private Transform _decalPreviewTransform; - private Transform _decalModelTransform; - private bool IsAttached => part.parent != null; public override void OnLoad(ConfigNode node) { - if (HighLogic.LoadedSceneIsGame) { - try { - // parse MATERIAL node - var materialNode = node.GetNode("MATERIAL") ?? throw new FormatException("Missing MATERIAL node in module"); - _materialProperties = new MaterialPropertyCollection(materialNode, this); - _material = _materialProperties.ParsedMaterial; + this.Log("Loading module"); + try { + // parse MATERIAL node + var materialNode = node.GetNode("MATERIAL") ?? throw new FormatException("Missing MATERIAL node in module"); + materialProperties = ScriptableObject.CreateInstance(); + materialProperties.Initialize(materialNode, this); - // get aspect ratio from main texture, if it exists - var mainTexture = _materialProperties.MainTextureProperty; - if (mainTexture != null) { - aspectRatio = mainTexture.AspectRatio; - } - else { - aspectRatio = 1; - } - - // find preview object references - _decalPreviewTransform = part.FindModelTransform(decalPreviewTransform); - if (_decalPreviewTransform == null) throw new FormatException("Missing decal preview reference"); - - _decalModelTransform = part.FindModelTransform(decalModelTransform); - if (_decalModelTransform == null) throw new FormatException("Missing decal mesh reference"); + // get aspect ratio from main texture, if it exists + var mainTexture = materialProperties.MainTextureProperty; + if (mainTexture != null) { + aspectRatio = mainTexture.AspectRatio; } - catch (Exception e) { - this.LogException("Exception parsing partmodule", e); + else { + aspectRatio = 1; } + + // find preview object references + decalPreviewTransformRef = part.FindModelTransform(decalPreviewTransform); + if (decalPreviewTransformRef == null) throw new FormatException("Missing decal preview reference"); + + decalModelTransformRef = part.FindModelTransform(decalModelTransform); + if (decalModelTransformRef == null) throw new FormatException("Missing decal mesh reference"); + } + catch (Exception e) { + this.LogException("Exception parsing partmodule", e); } } public override void OnStart(StartState state) { + this.Log("Starting module"); // generate orthogonal projection matrix and offset it by 0.5 on x and y axes _orthoMatrix = Matrix4x4.identity; _orthoMatrix[0, 3] = 0.5f; @@ -113,13 +113,37 @@ namespace ConformalDecals { return; } + this.Log($"Decal attached to {part.parent.partName}"); + this.Log($"{materialProperties == null}"); + this.Log($"{decalModelTransformRef == null}"); + + if (_targets == null) { + _targets = new List(); + } + else { + _targets.Clear(); + } + // find all valid renderers - var renderers = part.parent.transform.GetComponentsInChildren(false).Where(o => o.GetComponent() != null); - // generate ProjectionTarget objects for each valid meshrenderer - _targets = renderers.Select(o => new ProjectionTarget(o, o.GetComponent().mesh, _materialProperties)).ToList(); + var renderers = part.parent.FindModelComponents(); + foreach (var renderer in renderers) { + var meshFilter = renderer.GetComponent(); + if (meshFilter == null) continue; // object has a meshRenderer with no filter, invalid + var mesh = meshFilter.mesh; + if (mesh == null) continue; // object has a null mesh, invalid + + this.Log($"Adding target for object {meshFilter.gameObject.name} with the mesh {mesh.name}"); + // create new ProjectionTarget to represent the renderer + var target = new ProjectionTarget(renderer, mesh, materialProperties); + + this.Log("done."); + + // add the target to the list + _targets.Add(target); + } // hide preview model - _decalModelTransform.gameObject.SetActive(false); + //decalModelTransformRef.gameObject.SetActive(false); // add to preCull delegate Camera.onPreCull += Render; @@ -132,7 +156,7 @@ namespace ConformalDecals { } // unhide preview model - _decalModelTransform.gameObject.SetActive(true); + //decalModelTransformRef.gameObject.SetActive(true); // remove from preCull delegate Camera.onPreCull -= Render; @@ -162,7 +186,7 @@ namespace ConformalDecals { // render on each target object foreach (var target in _targets) { - target.Render(_material, part.mpb, camera); + target.Render(materialProperties.parsedMaterial, part.mpb, camera); } } } diff --git a/Source/ConformalDecals/ProjectionTarget.cs b/Source/ConformalDecals/ProjectionTarget.cs index 2887f3d..826585b 100644 --- a/Source/ConformalDecals/ProjectionTarget.cs +++ b/Source/ConformalDecals/ProjectionTarget.cs @@ -61,7 +61,7 @@ namespace ConformalDecals { } public bool Render(Material decalMaterial, MaterialPropertyBlock partMPB, Camera camera) { - if (_projectionEnabled) { + if (true) { decalMPB.SetFloat(PropertyIDs._RimFalloff, partMPB.GetFloat(PropertyIDs._RimFalloff)); decalMPB.SetColor(PropertyIDs._RimFalloff, partMPB.GetColor(PropertyIDs._RimFalloff));