mirror of
https://github.com/drewcassidy/KSP-Conformal-Decals.git
synced 2024-09-01 18:23:54 +00:00
Add test config and get the mod almost working
This commit is contained in:
parent
3c8f102f5d
commit
01e046a7f1
Binary file not shown.
BIN
Distribution/GameData/ConformalDecals/Assets/Tortilla-normal.dds
Normal file
BIN
Distribution/GameData/ConformalDecals/Assets/Tortilla-normal.dds
Normal file
Binary file not shown.
Binary file not shown.
@ -7,7 +7,7 @@ PART
|
|||||||
author = Andrew Cassidy
|
author = Andrew Cassidy
|
||||||
MODEL
|
MODEL
|
||||||
{
|
{
|
||||||
model = ConformalDecals/decal-blank
|
model = ConformalDecals/Assets/decal-blank
|
||||||
scale = 1.0, 1.0, 1.0
|
scale = 1.0, 1.0, 1.0
|
||||||
}
|
}
|
||||||
scale = 1
|
scale = 1
|
||||||
@ -15,7 +15,7 @@ PART
|
|||||||
|
|
||||||
// Attachment
|
// Attachment
|
||||||
attachRules = 1,1,1,1,1
|
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
|
// Tech
|
||||||
TechRequired = specializedConstruction
|
TechRequired = specializedConstruction
|
||||||
@ -27,10 +27,8 @@ PART
|
|||||||
title = Blank Decal
|
title = Blank Decal
|
||||||
manufacturer = #autoLOC_501648 // Maxo Construction Toys
|
manufacturer = #autoLOC_501648 // Maxo Construction Toys
|
||||||
description = foo
|
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
|
tags = foo
|
||||||
// affix anchor mount secure restock
|
bulkheadProfiles = srf
|
||||||
bulkheadProfiles = size0, srf
|
|
||||||
|
|
||||||
// Parameters
|
// Parameters
|
||||||
mass = 0.015
|
mass = 0.015
|
||||||
@ -42,4 +40,28 @@ PART
|
|||||||
maxTemp = 2000
|
maxTemp = 2000
|
||||||
breakingForce = 350
|
breakingForce = 350
|
||||||
breakingTorque = 150
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,22 +3,22 @@ using System.Collections.Generic;
|
|||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
namespace ConformalDecals.MaterialModifiers {
|
namespace ConformalDecals.MaterialModifiers {
|
||||||
public class MaterialPropertyCollection {
|
public class MaterialPropertyCollection : ScriptableObject {
|
||||||
public TextureMaterialProperty MainTextureProperty { get; }
|
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<MaterialProperty> _materialProperties;
|
private List<MaterialProperty> _materialProperties;
|
||||||
private readonly List<TextureMaterialProperty> _textureMaterialProperties;
|
private List<TextureMaterialProperty> _textureMaterialProperties;
|
||||||
|
|
||||||
public String BaseNormalSrc { get; }
|
public String BaseNormalSrc { get; private set; }
|
||||||
public String BaseNormalDest { get; }
|
public String BaseNormalDest { get; private set; }
|
||||||
|
|
||||||
private const string _normalTextureName = "_BumpMap";
|
private const string _normalTextureName = "_BumpMap";
|
||||||
|
|
||||||
public MaterialPropertyCollection(ConfigNode node, PartModule module) {
|
public void Initialize(ConfigNode node, PartModule module) {
|
||||||
|
|
||||||
// Initialize fields
|
// Initialize fields
|
||||||
_materialProperties = new List<MaterialProperty>();
|
_materialProperties = new List<MaterialProperty>();
|
||||||
@ -34,7 +34,7 @@ namespace ConformalDecals.MaterialModifiers {
|
|||||||
throw new FormatException($"Shader not found: '{shaderString}'");
|
throw new FormatException($"Shader not found: '{shaderString}'");
|
||||||
}
|
}
|
||||||
|
|
||||||
ParsedMaterial = new Material(shaderRef);
|
parsedMaterial = new Material(shaderRef);
|
||||||
|
|
||||||
// Get useBaseNormal value
|
// Get useBaseNormal value
|
||||||
var useBaseNormalString = node.GetValue("useBaseNormal");
|
var useBaseNormalString = node.GetValue("useBaseNormal");
|
||||||
@ -92,7 +92,7 @@ namespace ConformalDecals.MaterialModifiers {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_materialProperties.Add(property);
|
_materialProperties.Add(property);
|
||||||
property.Modify(ParsedMaterial);
|
property.Modify(parsedMaterial);
|
||||||
}
|
}
|
||||||
|
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
@ -101,11 +101,13 @@ namespace ConformalDecals.MaterialModifiers {
|
|||||||
module.LogException("Exception while parsing material node", e);
|
module.LogException("Exception while parsing material node", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
module.Log($"Parsed {_materialProperties.Count} properties");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void UpdateMaterial(Vector2 scale) {
|
public void UpdateMaterial(Vector2 scale) {
|
||||||
foreach (var textureProperty in _textureMaterialProperties) {
|
foreach (var textureProperty in _textureMaterialProperties) {
|
||||||
textureProperty.UpdateScale(ParsedMaterial, scale);
|
textureProperty.UpdateScale(parsedMaterial, scale);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,49 +20,49 @@ namespace ConformalDecals {
|
|||||||
[KSPField(guiActive = true, guiFormat = "F2", guiName = "Aspect Ratio")]
|
[KSPField(guiActive = true, guiFormat = "F2", guiName = "Aspect Ratio")]
|
||||||
public float aspectRatio = 1.0f;
|
public float aspectRatio = 1.0f;
|
||||||
|
|
||||||
private List<ProjectionTarget> _targets;
|
[KSPField] public MaterialPropertyCollection materialProperties;
|
||||||
private MaterialPropertyCollection _materialProperties;
|
|
||||||
private Material _material;
|
[KSPField] public Transform decalPreviewTransformRef;
|
||||||
|
[KSPField] public Transform decalModelTransformRef;
|
||||||
|
|
||||||
|
private List<ProjectionTarget> _targets;
|
||||||
|
|
||||||
private Matrix4x4 _orthoMatrix;
|
private Matrix4x4 _orthoMatrix;
|
||||||
private Bounds _decalBounds;
|
private Bounds _decalBounds;
|
||||||
|
|
||||||
private Transform _decalPreviewTransform;
|
|
||||||
private Transform _decalModelTransform;
|
|
||||||
|
|
||||||
private bool IsAttached => part.parent != null;
|
private bool IsAttached => part.parent != null;
|
||||||
|
|
||||||
public override void OnLoad(ConfigNode node) {
|
public override void OnLoad(ConfigNode node) {
|
||||||
if (HighLogic.LoadedSceneIsGame) {
|
this.Log("Loading module");
|
||||||
try {
|
try {
|
||||||
// parse MATERIAL node
|
// parse MATERIAL node
|
||||||
var materialNode = node.GetNode("MATERIAL") ?? throw new FormatException("Missing MATERIAL node in module");
|
var materialNode = node.GetNode("MATERIAL") ?? throw new FormatException("Missing MATERIAL node in module");
|
||||||
_materialProperties = new MaterialPropertyCollection(materialNode, this);
|
materialProperties = ScriptableObject.CreateInstance<MaterialPropertyCollection>();
|
||||||
_material = _materialProperties.ParsedMaterial;
|
materialProperties.Initialize(materialNode, this);
|
||||||
|
|
||||||
// get aspect ratio from main texture, if it exists
|
// get aspect ratio from main texture, if it exists
|
||||||
var mainTexture = _materialProperties.MainTextureProperty;
|
var mainTexture = materialProperties.MainTextureProperty;
|
||||||
if (mainTexture != null) {
|
if (mainTexture != null) {
|
||||||
aspectRatio = mainTexture.AspectRatio;
|
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");
|
|
||||||
}
|
}
|
||||||
catch (Exception e) {
|
else {
|
||||||
this.LogException("Exception parsing partmodule", e);
|
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) {
|
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
|
// generate orthogonal projection matrix and offset it by 0.5 on x and y axes
|
||||||
_orthoMatrix = Matrix4x4.identity;
|
_orthoMatrix = Matrix4x4.identity;
|
||||||
_orthoMatrix[0, 3] = 0.5f;
|
_orthoMatrix[0, 3] = 0.5f;
|
||||||
@ -113,13 +113,37 @@ namespace ConformalDecals {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.Log($"Decal attached to {part.parent.partName}");
|
||||||
|
this.Log($"{materialProperties == null}");
|
||||||
|
this.Log($"{decalModelTransformRef == null}");
|
||||||
|
|
||||||
|
if (_targets == null) {
|
||||||
|
_targets = new List<ProjectionTarget>();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
_targets.Clear();
|
||||||
|
}
|
||||||
|
|
||||||
// find all valid renderers
|
// find all valid renderers
|
||||||
var renderers = part.parent.transform.GetComponentsInChildren<MeshRenderer>(false).Where(o => o.GetComponent<MeshFilter>() != null);
|
var renderers = part.parent.FindModelComponents<MeshRenderer>();
|
||||||
// generate ProjectionTarget objects for each valid meshrenderer
|
foreach (var renderer in renderers) {
|
||||||
_targets = renderers.Select(o => new ProjectionTarget(o, o.GetComponent<MeshFilter>().mesh, _materialProperties)).ToList();
|
var meshFilter = renderer.GetComponent<MeshFilter>();
|
||||||
|
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
|
// hide preview model
|
||||||
_decalModelTransform.gameObject.SetActive(false);
|
//decalModelTransformRef.gameObject.SetActive(false);
|
||||||
|
|
||||||
// add to preCull delegate
|
// add to preCull delegate
|
||||||
Camera.onPreCull += Render;
|
Camera.onPreCull += Render;
|
||||||
@ -132,7 +156,7 @@ namespace ConformalDecals {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// unhide preview model
|
// unhide preview model
|
||||||
_decalModelTransform.gameObject.SetActive(true);
|
//decalModelTransformRef.gameObject.SetActive(true);
|
||||||
|
|
||||||
// remove from preCull delegate
|
// remove from preCull delegate
|
||||||
Camera.onPreCull -= Render;
|
Camera.onPreCull -= Render;
|
||||||
@ -162,7 +186,7 @@ namespace ConformalDecals {
|
|||||||
|
|
||||||
// render on each target object
|
// render on each target object
|
||||||
foreach (var target in _targets) {
|
foreach (var target in _targets) {
|
||||||
target.Render(_material, part.mpb, camera);
|
target.Render(materialProperties.parsedMaterial, part.mpb, camera);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -61,7 +61,7 @@ namespace ConformalDecals {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public bool Render(Material decalMaterial, MaterialPropertyBlock partMPB, Camera camera) {
|
public bool Render(Material decalMaterial, MaterialPropertyBlock partMPB, Camera camera) {
|
||||||
if (_projectionEnabled) {
|
if (true) {
|
||||||
decalMPB.SetFloat(PropertyIDs._RimFalloff, partMPB.GetFloat(PropertyIDs._RimFalloff));
|
decalMPB.SetFloat(PropertyIDs._RimFalloff, partMPB.GetFloat(PropertyIDs._RimFalloff));
|
||||||
decalMPB.SetColor(PropertyIDs._RimFalloff, partMPB.GetColor(PropertyIDs._RimFalloff));
|
decalMPB.SetColor(PropertyIDs._RimFalloff, partMPB.GetColor(PropertyIDs._RimFalloff));
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user