Documentation and cleanup

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

View File

@ -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;
}