mirror of
https://github.com/drewcassidy/KSP-Conformal-Decals.git
synced 2024-09-01 18:23:54 +00:00
Documentation and cleanup
This commit is contained in:
parent
e504936896
commit
8cd456f16e
@ -35,10 +35,12 @@ namespace ConformalDecals {
|
||||
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;
|
||||
|
||||
// get aspect ratio from main texture, if it exists
|
||||
var mainTexture = _materialProperties.MainTextureProperty;
|
||||
if (mainTexture != null) {
|
||||
aspectRatio = mainTexture.AspectRatio;
|
||||
@ -47,6 +49,7 @@ namespace ConformalDecals {
|
||||
aspectRatio = 1;
|
||||
}
|
||||
|
||||
// find preview object references
|
||||
_decalPreviewTransform = part.FindModelTransform(decalPreviewTransform);
|
||||
if (_decalPreviewTransform == null) throw new FormatException("Missing decal preview reference");
|
||||
|
||||
@ -60,16 +63,19 @@ namespace ConformalDecals {
|
||||
}
|
||||
|
||||
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[0, 3] = 0.5f;
|
||||
_orthoMatrix[1, 3] = 0.5f;
|
||||
|
||||
if ((state & StartState.Editor) != 0) {
|
||||
// setup OnTweakEvent for scale and depth fields in editor
|
||||
GameEvents.onEditorPartEvent.Add(OnEditorEvent);
|
||||
Fields[nameof(scale)].uiControlEditor.onFieldChanged = OnTweakEvent;
|
||||
Fields[nameof(depth)].uiControlEditor.onFieldChanged = OnTweakEvent;
|
||||
}
|
||||
else {
|
||||
// if we start in the flight scene attached, call Attach
|
||||
if (IsAttached) Attach();
|
||||
}
|
||||
}
|
||||
@ -132,7 +138,7 @@ namespace ConformalDecals {
|
||||
Camera.onPreCull -= Render;
|
||||
}
|
||||
|
||||
[KSPEvent(guiActive = false, guiName = "Project", guiActiveEditor =true, active = true)]
|
||||
[KSPEvent(guiActive = false, guiName = "Project", guiActiveEditor = true, active = true)]
|
||||
public void Project() {
|
||||
if (!IsAttached) return;
|
||||
|
||||
@ -147,7 +153,7 @@ namespace ConformalDecals {
|
||||
|
||||
// project to each target object
|
||||
foreach (var target in _targets) {
|
||||
target.Project(_orthoMatrix, _decalBounds);
|
||||
target.Project(_orthoMatrix, _decalBounds, this.transform);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -9,26 +9,23 @@ namespace ConformalDecals {
|
||||
private static readonly int _decalNormalID = Shader.PropertyToID("_DecalNormal");
|
||||
private static readonly int _decalTangentID = Shader.PropertyToID("_DecalTangent");
|
||||
|
||||
// Projector object data
|
||||
public Transform Projector;
|
||||
|
||||
// Target object data
|
||||
public readonly Transform Target;
|
||||
public readonly Transform target;
|
||||
|
||||
private readonly Renderer _targetRenderer;
|
||||
private readonly Mesh _targetMesh;
|
||||
private bool _projectionEnabled;
|
||||
|
||||
// property block
|
||||
public readonly MaterialPropertyBlock DecalMPB;
|
||||
public readonly MaterialPropertyBlock decalMPB;
|
||||
|
||||
public ProjectionTarget(MeshRenderer targetRenderer, Mesh targetMesh, MaterialPropertyCollection properties) {
|
||||
Target = targetRenderer.transform;
|
||||
target = targetRenderer.transform;
|
||||
_targetRenderer = targetRenderer;
|
||||
_targetMesh = targetMesh;
|
||||
var targetMaterial = targetRenderer.sharedMaterial;
|
||||
|
||||
DecalMPB = new MaterialPropertyBlock();
|
||||
decalMPB = new MaterialPropertyBlock();
|
||||
|
||||
if (properties.UseBaseNormal) {
|
||||
var normalSrcID = Shader.PropertyToID(properties.BaseNormalSrc);
|
||||
@ -38,37 +35,37 @@ namespace ConformalDecals {
|
||||
var normal = targetMaterial.GetTexture(normalSrcID);
|
||||
if (normal != null) {
|
||||
|
||||
DecalMPB.SetTexture(normalDestID, targetMaterial.GetTexture(normalSrcID));
|
||||
decalMPB.SetTexture(normalDestID, targetMaterial.GetTexture(normalSrcID));
|
||||
|
||||
var normalScale = targetMaterial.GetTextureScale(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) {
|
||||
var projectorToTargetMatrix = Target.worldToLocalMatrix * Projector.localToWorldMatrix;
|
||||
public void Project(Matrix4x4 orthoMatrix, Bounds projectorBounds, Transform projector) {
|
||||
var projectorToTargetMatrix = target.worldToLocalMatrix * projector.localToWorldMatrix;
|
||||
|
||||
var projectionMatrix = orthoMatrix * projectorToTargetMatrix.inverse;
|
||||
var decalNormal = projectorToTargetMatrix.MultiplyVector(Vector3.back).normalized;
|
||||
var decalTangent = projectorToTargetMatrix.MultiplyVector(Vector3.right).normalized;
|
||||
|
||||
DecalMPB.SetMatrix(_projectionMatrixID, projectionMatrix);
|
||||
DecalMPB.SetVector(_decalNormalID, decalNormal);
|
||||
DecalMPB.SetVector(_decalTangentID, decalTangent);
|
||||
decalMPB.SetMatrix(_projectionMatrixID, projectionMatrix);
|
||||
decalMPB.SetVector(_decalNormalID, decalNormal);
|
||||
decalMPB.SetVector(_decalTangentID, decalTangent);
|
||||
|
||||
var targetBounds = new OrientedBounds(Target.localToWorldMatrix, _targetRenderer.bounds);
|
||||
var targetBounds = new OrientedBounds(target.localToWorldMatrix, _targetRenderer.bounds);
|
||||
_projectionEnabled = targetBounds.Intersects(projectorBounds);
|
||||
}
|
||||
|
||||
public bool Render(Material decalMaterial, MaterialPropertyBlock partMPB, Camera camera) {
|
||||
if (_projectionEnabled) {
|
||||
DecalMPB.SetFloat(PropertyIDs._RimFalloff, partMPB.GetFloat(PropertyIDs._RimFalloff));
|
||||
DecalMPB.SetColor(PropertyIDs._RimFalloff, partMPB.GetColor(PropertyIDs._RimFalloff));
|
||||
decalMPB.SetFloat(PropertyIDs._RimFalloff, partMPB.GetFloat(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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user