2020-05-30 04:02:58 +00:00
|
|
|
using UnityEngine;
|
|
|
|
using UnityEngine.Rendering;
|
|
|
|
|
2022-03-15 04:36:50 +00:00
|
|
|
namespace ConformalDecals.Targets {
|
2021-01-01 05:05:30 +00:00
|
|
|
public class ProjectionMeshTarget : IProjectionTarget {
|
2021-01-01 22:02:46 +00:00
|
|
|
public const string NodeName = "MESH_TARGET";
|
|
|
|
|
2021-01-01 05:05:30 +00:00
|
|
|
// enabled flag
|
2021-01-01 22:02:46 +00:00
|
|
|
public bool enabled;
|
2021-01-01 05:05:30 +00:00
|
|
|
|
2020-05-30 04:02:58 +00:00
|
|
|
// Target object data
|
2021-01-01 05:05:30 +00:00
|
|
|
public readonly Transform target;
|
|
|
|
public readonly Transform root;
|
|
|
|
public readonly Mesh mesh;
|
|
|
|
public readonly MeshRenderer renderer;
|
|
|
|
|
|
|
|
// Projection data
|
|
|
|
private Matrix4x4 _decalMatrix;
|
|
|
|
private Vector3 _decalNormal;
|
|
|
|
private Vector3 _decalTangent;
|
2020-05-30 04:02:58 +00:00
|
|
|
|
|
|
|
// property block
|
2020-06-02 07:53:17 +00:00
|
|
|
private readonly MaterialPropertyBlock _decalMPB;
|
2020-06-04 07:12:09 +00:00
|
|
|
|
2021-01-01 05:05:30 +00:00
|
|
|
public ProjectionMeshTarget(Transform target, Transform root, MeshRenderer renderer, Mesh mesh, bool useBaseNormal) {
|
|
|
|
this.root = root;
|
2020-11-10 03:34:34 +00:00
|
|
|
this.target = target;
|
2021-01-01 05:05:30 +00:00
|
|
|
this.renderer = renderer;
|
|
|
|
this.mesh = mesh;
|
2020-06-02 07:53:17 +00:00
|
|
|
_decalMPB = new MaterialPropertyBlock();
|
2020-10-10 02:30:59 +00:00
|
|
|
|
2021-01-01 05:05:30 +00:00
|
|
|
SetNormalMap(renderer.sharedMaterial, useBaseNormal);
|
2020-05-30 04:02:58 +00:00
|
|
|
}
|
|
|
|
|
2021-01-01 05:05:30 +00:00
|
|
|
private void SetNormalMap(Material targetMaterial, bool useBaseNormal) {
|
|
|
|
if (useBaseNormal && targetMaterial.HasProperty(DecalPropertyIDs._BumpMap)) {
|
2020-10-10 02:30:59 +00:00
|
|
|
_decalMPB.SetTexture(DecalPropertyIDs._BumpMap, targetMaterial.GetTexture(DecalPropertyIDs._BumpMap));
|
|
|
|
|
|
|
|
var normalScale = targetMaterial.GetTextureScale(DecalPropertyIDs._BumpMap);
|
|
|
|
var normalOffset = targetMaterial.GetTextureOffset(DecalPropertyIDs._BumpMap);
|
|
|
|
|
|
|
|
_decalMPB.SetVector(DecalPropertyIDs._BumpMap_ST, new Vector4(normalScale.x, normalScale.y, normalOffset.x, normalOffset.y));
|
2020-06-11 21:15:37 +00:00
|
|
|
}
|
|
|
|
else {
|
2020-10-10 02:30:59 +00:00
|
|
|
_decalMPB.SetTexture(DecalPropertyIDs._BumpMap, DecalConfig.BlankNormal);
|
2020-05-31 05:44:44 +00:00
|
|
|
}
|
2020-05-30 04:02:58 +00:00
|
|
|
}
|
|
|
|
|
2021-01-01 22:02:46 +00:00
|
|
|
public bool Project(Matrix4x4 orthoMatrix, Transform projector, Bounds projectionBounds) {
|
2021-01-01 05:05:30 +00:00
|
|
|
if (projectionBounds.Intersects(renderer.bounds)) {
|
|
|
|
enabled = true;
|
|
|
|
|
|
|
|
var projectorToTargetMatrix = target.worldToLocalMatrix * projector.localToWorldMatrix;
|
|
|
|
|
|
|
|
_decalMatrix = orthoMatrix * projectorToTargetMatrix.inverse;
|
|
|
|
_decalNormal = projectorToTargetMatrix.MultiplyVector(Vector3.back).normalized;
|
|
|
|
_decalTangent = projectorToTargetMatrix.MultiplyVector(Vector3.right).normalized;
|
|
|
|
|
|
|
|
_decalMPB.SetMatrix(DecalPropertyIDs._ProjectionMatrix, _decalMatrix);
|
|
|
|
_decalMPB.SetVector(DecalPropertyIDs._DecalNormal, _decalNormal);
|
|
|
|
_decalMPB.SetVector(DecalPropertyIDs._DecalTangent, _decalTangent);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
enabled = false;
|
|
|
|
}
|
2021-01-01 22:02:46 +00:00
|
|
|
|
|
|
|
return enabled;
|
2021-01-01 05:05:30 +00:00
|
|
|
}
|
|
|
|
|
2020-10-10 02:30:59 +00:00
|
|
|
public void Render(Material decalMaterial, MaterialPropertyBlock partMPB, Camera camera) {
|
2021-01-01 05:05:30 +00:00
|
|
|
if (!enabled) return;
|
|
|
|
|
2020-10-10 02:30:59 +00:00
|
|
|
_decalMPB.SetFloat(PropertyIDs._RimFalloff, partMPB.GetFloat(PropertyIDs._RimFalloff));
|
|
|
|
_decalMPB.SetColor(PropertyIDs._RimColor, partMPB.GetColor(PropertyIDs._RimColor));
|
|
|
|
|
2021-01-01 05:05:30 +00:00
|
|
|
Graphics.DrawMesh(mesh, target.localToWorldMatrix, decalMaterial, 0, camera, 0, _decalMPB, ShadowCastingMode.Off, true);
|
2020-10-10 02:30:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static bool ValidateTarget(Transform target, MeshRenderer renderer, MeshFilter filter) {
|
|
|
|
if (renderer == null) return false;
|
|
|
|
if (filter == null) return false;
|
|
|
|
if (!target.gameObject.activeInHierarchy) return false;
|
|
|
|
|
|
|
|
var material = renderer.material;
|
|
|
|
if (material == null) return false;
|
|
|
|
if (DecalConfig.IsBlacklisted(material.shader)) return false;
|
|
|
|
|
|
|
|
if (filter.sharedMesh == null) return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2020-05-30 04:02:58 +00:00
|
|
|
}
|
|
|
|
}
|