Documentation and cleanup

This commit is contained in:
Andrew Cassidy 2020-05-30 13:46:03 -07:00
parent e504936896
commit 8cd456f16e
2 changed files with 23 additions and 20 deletions

View File

@ -35,10 +35,12 @@ namespace ConformalDecals {
public override void OnLoad(ConfigNode node) { public override void OnLoad(ConfigNode node) {
if (HighLogic.LoadedSceneIsGame) { if (HighLogic.LoadedSceneIsGame) {
try { try {
// 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 = new MaterialPropertyCollection(materialNode, this);
_material = _materialProperties.ParsedMaterial; _material = _materialProperties.ParsedMaterial;
// 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;
@ -47,6 +49,7 @@ namespace ConformalDecals {
aspectRatio = 1; aspectRatio = 1;
} }
// find preview object references
_decalPreviewTransform = part.FindModelTransform(decalPreviewTransform); _decalPreviewTransform = part.FindModelTransform(decalPreviewTransform);
if (_decalPreviewTransform == null) throw new FormatException("Missing decal preview reference"); if (_decalPreviewTransform == null) throw new FormatException("Missing decal preview reference");
@ -60,16 +63,19 @@ namespace ConformalDecals {
} }
public override void OnStart(StartState state) { public override void OnStart(StartState state) {
// 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;
_orthoMatrix[1, 3] = 0.5f; _orthoMatrix[1, 3] = 0.5f;
if ((state & StartState.Editor) != 0) { if ((state & StartState.Editor) != 0) {
// setup OnTweakEvent for scale and depth fields in editor
GameEvents.onEditorPartEvent.Add(OnEditorEvent); GameEvents.onEditorPartEvent.Add(OnEditorEvent);
Fields[nameof(scale)].uiControlEditor.onFieldChanged = OnTweakEvent; Fields[nameof(scale)].uiControlEditor.onFieldChanged = OnTweakEvent;
Fields[nameof(depth)].uiControlEditor.onFieldChanged = OnTweakEvent; Fields[nameof(depth)].uiControlEditor.onFieldChanged = OnTweakEvent;
} }
else { else {
// if we start in the flight scene attached, call Attach
if (IsAttached) Attach(); if (IsAttached) Attach();
} }
} }
@ -132,7 +138,7 @@ namespace ConformalDecals {
Camera.onPreCull -= Render; Camera.onPreCull -= Render;
} }
[KSPEvent(guiActive = false, guiName = "Project", guiActiveEditor =true, active = true)] [KSPEvent(guiActive = false, guiName = "Project", guiActiveEditor = true, active = true)]
public void Project() { public void Project() {
if (!IsAttached) return; if (!IsAttached) return;
@ -147,7 +153,7 @@ namespace ConformalDecals {
// project to each target object // project to each target object
foreach (var target in _targets) { foreach (var target in _targets) {
target.Project(_orthoMatrix, _decalBounds); target.Project(_orthoMatrix, _decalBounds, this.transform);
} }
} }

View File

@ -9,26 +9,23 @@ namespace ConformalDecals {
private static readonly int _decalNormalID = Shader.PropertyToID("_DecalNormal"); private static readonly int _decalNormalID = Shader.PropertyToID("_DecalNormal");
private static readonly int _decalTangentID = Shader.PropertyToID("_DecalTangent"); private static readonly int _decalTangentID = Shader.PropertyToID("_DecalTangent");
// Projector object data
public Transform Projector;
// Target object data // Target object data
public readonly Transform Target; public readonly Transform target;
private readonly Renderer _targetRenderer; private readonly Renderer _targetRenderer;
private readonly Mesh _targetMesh; private readonly Mesh _targetMesh;
private bool _projectionEnabled; private bool _projectionEnabled;
// property block // property block
public readonly MaterialPropertyBlock DecalMPB; public readonly MaterialPropertyBlock decalMPB;
public ProjectionTarget(MeshRenderer targetRenderer, Mesh targetMesh, MaterialPropertyCollection properties) { public ProjectionTarget(MeshRenderer targetRenderer, Mesh targetMesh, MaterialPropertyCollection properties) {
Target = targetRenderer.transform; target = targetRenderer.transform;
_targetRenderer = targetRenderer; _targetRenderer = targetRenderer;
_targetMesh = targetMesh; _targetMesh = targetMesh;
var targetMaterial = targetRenderer.sharedMaterial; var targetMaterial = targetRenderer.sharedMaterial;
DecalMPB = new MaterialPropertyBlock(); decalMPB = new MaterialPropertyBlock();
if (properties.UseBaseNormal) { if (properties.UseBaseNormal) {
var normalSrcID = Shader.PropertyToID(properties.BaseNormalSrc); var normalSrcID = Shader.PropertyToID(properties.BaseNormalSrc);
@ -38,37 +35,37 @@ namespace ConformalDecals {
var normal = targetMaterial.GetTexture(normalSrcID); var normal = targetMaterial.GetTexture(normalSrcID);
if (normal != null) { if (normal != null) {
DecalMPB.SetTexture(normalDestID, targetMaterial.GetTexture(normalSrcID)); decalMPB.SetTexture(normalDestID, targetMaterial.GetTexture(normalSrcID));
var normalScale = targetMaterial.GetTextureScale(normalSrcID); var normalScale = targetMaterial.GetTextureScale(normalSrcID);
var normalOffset = targetMaterial.GetTextureOffset(normalSrcID); var normalOffset = targetMaterial.GetTextureOffset(normalSrcID);
DecalMPB.SetVector(normalDestIDST, new Vector4(normalScale.x, normalScale.y, normalOffset.x, normalOffset.y)); decalMPB.SetVector(normalDestIDST, new Vector4(normalScale.x, normalScale.y, normalOffset.x, normalOffset.y));
} }
} }
} }
public void Project(Matrix4x4 orthoMatrix, Bounds projectorBounds) { public void Project(Matrix4x4 orthoMatrix, Bounds projectorBounds, Transform projector) {
var projectorToTargetMatrix = Target.worldToLocalMatrix * Projector.localToWorldMatrix; var projectorToTargetMatrix = target.worldToLocalMatrix * projector.localToWorldMatrix;
var projectionMatrix = orthoMatrix * projectorToTargetMatrix.inverse; var projectionMatrix = orthoMatrix * projectorToTargetMatrix.inverse;
var decalNormal = projectorToTargetMatrix.MultiplyVector(Vector3.back).normalized; var decalNormal = projectorToTargetMatrix.MultiplyVector(Vector3.back).normalized;
var decalTangent = projectorToTargetMatrix.MultiplyVector(Vector3.right).normalized; var decalTangent = projectorToTargetMatrix.MultiplyVector(Vector3.right).normalized;
DecalMPB.SetMatrix(_projectionMatrixID, projectionMatrix); decalMPB.SetMatrix(_projectionMatrixID, projectionMatrix);
DecalMPB.SetVector(_decalNormalID, decalNormal); decalMPB.SetVector(_decalNormalID, decalNormal);
DecalMPB.SetVector(_decalTangentID, decalTangent); decalMPB.SetVector(_decalTangentID, decalTangent);
var targetBounds = new OrientedBounds(Target.localToWorldMatrix, _targetRenderer.bounds); var targetBounds = new OrientedBounds(target.localToWorldMatrix, _targetRenderer.bounds);
_projectionEnabled = targetBounds.Intersects(projectorBounds); _projectionEnabled = targetBounds.Intersects(projectorBounds);
} }
public bool Render(Material decalMaterial, MaterialPropertyBlock partMPB, Camera camera) { public bool Render(Material decalMaterial, MaterialPropertyBlock partMPB, Camera camera) {
if (_projectionEnabled) { if (_projectionEnabled) {
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));
Graphics.DrawMesh(_targetMesh, Target.localToWorldMatrix, decalMaterial, 0, camera, 0, DecalMPB, ShadowCastingMode.Off, true); Graphics.DrawMesh(_targetMesh, target.localToWorldMatrix, decalMaterial, 0, camera, 0, decalMPB, ShadowCastingMode.Off, true);
return true; return true;
} }