using System.Collections.Generic; using UnityEngine; using UnityEngine.Rendering; namespace EPOOutline { /// /// The target to render outline for /// [System.Serializable] public class OutlineTarget { private static List TempSharedMaterials = new List(); internal bool IsVisible = false; /// /// to use during the cutout process. /// [SerializeField] public ColorMask CutoutMask = ColorMask.A; [SerializeField] internal Renderer renderer; /// /// Sub-mesh index of the renderer. /// [SerializeField] public int SubmeshIndex; /// /// to use for this target. /// [SerializeField] public BoundsMode BoundsMode = BoundsMode.Default; /// /// Bounds of this target that will be used for rendering. ///
Only applicable if is set to ///
[SerializeField] public Bounds Bounds = new Bounds(Vector3.zero, Vector3.one); /// /// The threshold for the cutout to be used. /// [SerializeField] [Range(0.0f, 1.0f)] public float CutoutThreshold = 0.5f; /// /// The to be used for rendering of this target. /// [SerializeField] public CullMode CullMode; [SerializeField] private string cutoutTextureName; [SerializeField] private int cutoutTextureIndex; private int? cutoutTextureId; /// /// The renderer of this outline target. /// public Renderer Renderer => renderer; internal bool UsesCutout => !string.IsNullOrEmpty(cutoutTextureName); internal Material SharedMaterial { get { if (renderer == null) return null; TempSharedMaterials.Clear(); renderer.GetSharedMaterials(TempSharedMaterials); return TempSharedMaterials.Count == 0 ? null : TempSharedMaterials[ShiftedSubmeshIndex % TempSharedMaterials.Count]; } } internal Texture CutoutTexture { get { var sharedMaterial = SharedMaterial; return sharedMaterial == null ? null : sharedMaterial.GetTexture(CutoutTextureId); } } internal bool IsValidForCutout { get { var materialToGetTextureFrom = SharedMaterial; return UsesCutout && materialToGetTextureFrom != null && materialToGetTextureFrom.HasProperty(CutoutTextureId) && CutoutTexture != null; } } /// /// The cutout texture index. Only applicable if the texture is TexArray. /// public int CutoutTextureIndex { get => cutoutTextureIndex; set { cutoutTextureIndex = value; if (cutoutTextureIndex >= 0) return; Debug.LogError("Trying to set cutout texture index less than zero"); cutoutTextureIndex = 0; } } internal int ShiftedSubmeshIndex => SubmeshIndex; internal int CutoutTextureId { get { if (!cutoutTextureId.HasValue) cutoutTextureId = Shader.PropertyToID(cutoutTextureName); return cutoutTextureId.Value; } } /// /// The name of the texture that is going to be used while rendering as a cutout source. /// public string CutoutTextureName { get => cutoutTextureName; set { cutoutTextureName = value; cutoutTextureId = null; } } public OutlineTarget() { } /// /// The constructor of the target. /// /// The renderer of the target. /// The sub-mesh of the target. public OutlineTarget(Renderer renderer, int submesh = 0) { SubmeshIndex = submesh; this.renderer = renderer; CutoutThreshold = 0.5f; cutoutTextureId = null; cutoutTextureName = string.Empty; CullMode = renderer is SpriteRenderer ? CullMode.Off : CullMode.Back; } /// /// The constructor of the target. /// /// The renderer of the target. /// The name of the texture to be used to render the cutout. /// The cutout threshold. public OutlineTarget(Renderer renderer, string cutoutTextureName, float cutoutThreshold = 0.5f) { SubmeshIndex = 0; this.renderer = renderer; cutoutTextureId = Shader.PropertyToID(cutoutTextureName); CutoutThreshold = cutoutThreshold; this.cutoutTextureName = cutoutTextureName; CullMode = renderer is SpriteRenderer ? CullMode.Off : CullMode.Back; } /// /// The constructor of the target. /// /// The renderer of the target. /// The sub-mesh of the target. /// The name of the texture to be used to render the cutout. /// The cutout threshold. public OutlineTarget(Renderer renderer, int submeshIndex, string cutoutTextureName, float cutoutThreshold = 0.5f) { SubmeshIndex = submeshIndex; this.renderer = renderer; cutoutTextureId = Shader.PropertyToID(cutoutTextureName); CutoutThreshold = cutoutThreshold; this.cutoutTextureName = cutoutTextureName; CullMode = renderer is SpriteRenderer ? CullMode.Off : CullMode.Back; } } }