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
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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<MaterialProperty> _materialProperties;
|
||||
private readonly List<TextureMaterialProperty> _textureMaterialProperties;
|
||||
private List<MaterialProperty> _materialProperties;
|
||||
private List<TextureMaterialProperty> _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<MaterialProperty>();
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -20,49 +20,49 @@ namespace ConformalDecals {
|
||||
[KSPField(guiActive = true, guiFormat = "F2", guiName = "Aspect Ratio")]
|
||||
public float aspectRatio = 1.0f;
|
||||
|
||||
private List<ProjectionTarget> _targets;
|
||||
private MaterialPropertyCollection _materialProperties;
|
||||
private Material _material;
|
||||
[KSPField] public MaterialPropertyCollection materialProperties;
|
||||
|
||||
[KSPField] public Transform decalPreviewTransformRef;
|
||||
[KSPField] public Transform decalModelTransformRef;
|
||||
|
||||
private List<ProjectionTarget> _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<MaterialPropertyCollection>();
|
||||
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<ProjectionTarget>();
|
||||
}
|
||||
else {
|
||||
_targets.Clear();
|
||||
}
|
||||
|
||||
// find all valid renderers
|
||||
var renderers = part.parent.transform.GetComponentsInChildren<MeshRenderer>(false).Where(o => o.GetComponent<MeshFilter>() != null);
|
||||
// generate ProjectionTarget objects for each valid meshrenderer
|
||||
_targets = renderers.Select(o => new ProjectionTarget(o, o.GetComponent<MeshFilter>().mesh, _materialProperties)).ToList();
|
||||
var renderers = part.parent.FindModelComponents<MeshRenderer>();
|
||||
foreach (var renderer in renderers) {
|
||||
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
|
||||
_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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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));
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user