KSP-Conformal-Decals/Source/ConformalDecals/ProjectionTarget.cs

82 lines
3.6 KiB
C#
Raw Normal View History

2020-05-30 04:02:58 +00:00
using System;
using ConformalDecals.MaterialModifiers;
using UnityEngine;
using UnityEngine.Rendering;
namespace ConformalDecals {
public class ProjectionTarget {
private static readonly int _projectionMatrixID = Shader.PropertyToID("_ProjectionMatrix");
private static readonly int _decalNormalID = Shader.PropertyToID("_DecalNormal");
private static readonly int _decalTangentID = Shader.PropertyToID("_DecalTangent");
// Target object data
2020-05-30 20:46:03 +00:00
public readonly Transform target;
2020-05-30 04:02:58 +00:00
private readonly Renderer _targetRenderer;
private readonly Mesh _targetMesh;
private bool _projectionEnabled;
2020-05-30 04:02:58 +00:00
// property block
2020-05-30 20:46:03 +00:00
public readonly MaterialPropertyBlock decalMPB;
2020-05-30 04:02:58 +00:00
public ProjectionTarget(MeshRenderer targetRenderer, Mesh targetMesh, MaterialPropertyCollection properties) {
2020-05-30 20:46:03 +00:00
target = targetRenderer.transform;
2020-05-30 04:02:58 +00:00
_targetRenderer = targetRenderer;
_targetMesh = targetMesh;
var targetMaterial = targetRenderer.sharedMaterial;
2020-05-30 20:46:03 +00:00
decalMPB = new MaterialPropertyBlock();
2020-05-30 04:02:58 +00:00
if (properties.UseBaseNormal) {
var normalSrcID = Shader.PropertyToID(properties.BaseNormalSrc);
var normalDestID = Shader.PropertyToID(properties.BaseNormalDest);
var normalDestIDST = Shader.PropertyToID(properties.BaseNormalDest + "_ST");
var normal = targetMaterial.GetTexture(normalSrcID);
if (normal != null) {
2020-05-30 20:46:03 +00:00
decalMPB.SetTexture(normalDestID, targetMaterial.GetTexture(normalSrcID));
2020-05-30 04:02:58 +00:00
var normalScale = targetMaterial.GetTextureScale(normalSrcID);
var normalOffset = targetMaterial.GetTextureOffset(normalSrcID);
2020-05-30 20:46:03 +00:00
decalMPB.SetVector(normalDestIDST, new Vector4(normalScale.x, normalScale.y, normalOffset.x, normalOffset.y));
2020-05-30 04:02:58 +00:00
}
}
}
public void Project(Matrix4x4 orthoMatrix, OrientedBounds projectorBounds, Transform projector) {
2020-05-31 05:44:44 +00:00
var targetBounds = _targetRenderer.bounds;
if (projectorBounds.Intersects(targetBounds)) {
2020-05-31 05:44:44 +00:00
_projectionEnabled = true;
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);
Debug.Log($"Projection enabled for {target.gameObject}");
2020-05-31 05:44:44 +00:00
}
else {
_projectionEnabled = false;
Debug.Log($"Projection disabled for {target.gameObject}");
2020-05-31 05:44:44 +00:00
}
2020-05-30 04:02:58 +00:00
}
public bool Render(Material decalMaterial, MaterialPropertyBlock partMPB, Camera camera) {
2020-05-31 05:44:44 +00:00
if (_projectionEnabled) {
2020-05-30 20:46:03 +00:00
decalMPB.SetFloat(PropertyIDs._RimFalloff, partMPB.GetFloat(PropertyIDs._RimFalloff));
decalMPB.SetColor(PropertyIDs._RimColor, partMPB.GetColor(PropertyIDs._RimColor));
2020-05-30 20:46:03 +00:00
Graphics.DrawMesh(_targetMesh, target.localToWorldMatrix, decalMaterial, 0, camera, 0, decalMPB, ShadowCastingMode.Off, true);
2020-05-30 04:02:58 +00:00
return true;
2020-05-30 04:02:58 +00:00
}
return false;
}
}
}