using System.Linq; using Es.InkPainter.Effective; using UnityEngine; namespace Es.InkPainter.Sample { [RequireComponent(typeof(Collider))] public class CollisionReflectionPainter : MonoBehaviour { [SerializeField] private Brush brush; [SerializeField] private Camera cam; [SerializeField] private Vector3 offset; [SerializeField] private bool debugMode; private RenderTexture rt; private RenderTexture debug; public void OnGUI() { if (debugMode) { GUI.Box(new Rect(0f, 0f, 200f, 200f), "ReflectionImage"); GUI.DrawTexture(new Rect(0f, 0f, 200f, 200f), debug); } } private void Awake() { rt = RenderTexture.GetTemporary(Screen.width, Screen.height, 16); if (debugMode) { debug = new RenderTexture(brush.BrushTexture.width, brush.BrushTexture.height, 16); } cam.targetTexture = rt; } public void OnDestroy() { RenderTexture.ReleaseTemporary(rt); } public void OnCollisionStay(Collision collision) { if (!(cam == null) && collision.contacts.Any((ContactPoint p) => p.otherCollider.GetComponent() != null)) { cam.transform.position = base.transform.position + offset; ContactPoint contactPoint = collision.contacts.First((ContactPoint p) => p.otherCollider.GetComponent() != null); InkCanvas component = contactPoint.otherCollider.GetComponent(); RenderTexture temporary = RenderTexture.GetTemporary(brush.BrushTexture.width, brush.BrushTexture.height); GrabArea.Clip(brush.BrushTexture, brush.Scale, rt, Vector3.one * 0.5f, brush.RotateAngle, GrabArea.GrabTextureWrapMode.Clamp, temporary); ReverseUV.Vertical(temporary, temporary); if (debugMode) { Graphics.Blit(temporary, debug); } Texture brushTexture = brush.BrushTexture; brush.BrushTexture = temporary; component.Paint(brush, contactPoint.point); RenderTexture.ReleaseTemporary(temporary); brush.BrushTexture = brushTexture; } } } }