Tanoda
CollisionReflectionPainter.cs
Go to the documentation of this file.
1using System.Linq;
3using UnityEngine;
4
6{
7 [RequireComponent(typeof(Collider))]
8 public class CollisionReflectionPainter : MonoBehaviour
9 {
10 [SerializeField] private Brush brush;
11
12 [SerializeField] private Camera cam;
13
14 [SerializeField] private Vector3 offset;
15
16 [SerializeField] private bool debugMode;
17
18 private RenderTexture rt;
19 private RenderTexture debug;
20
21 public void OnGUI()
22 {
23 if (debugMode)
24 {
25 GUI.Box(new Rect(0, 0, 200, 200), "ReflectionImage");
26 GUI.DrawTexture(new Rect(0, 0, 200, 200), debug);
27 }
28 }
29
30 private void Awake()
31 {
32 rt = RenderTexture.GetTemporary(Screen.width, Screen.height, 16);
33 if (debugMode)
34 debug = new RenderTexture(brush.BrushTexture.width, brush.BrushTexture.height, 16);
35 cam.targetTexture = rt;
36 }
37
38 public void OnDestroy()
39 {
40 RenderTexture.ReleaseTemporary(rt);
41 }
42
43 public void OnCollisionStay(Collision collision)
44 {
45 if (cam == null)
46 return;
47
48 if (!collision.contacts.Any(p => p.otherCollider.GetComponent<InkCanvas>() != null))
49 return;
50
51 cam.transform.position = transform.position + offset;
52
53 var contact = collision.contacts.First(p => p.otherCollider.GetComponent<InkCanvas>() != null);
54 var canvas = contact.otherCollider.GetComponent<InkCanvas>();
55
56 var buf = RenderTexture.GetTemporary(brush.BrushTexture.width, brush.BrushTexture.height);
57 GrabArea.Clip(brush.BrushTexture, brush.Scale, rt, Vector3.one * 0.5f, brush.RotateAngle,
58 GrabArea.GrabTextureWrapMode.Clamp, buf);
59 ReverseUV.Vertical(buf, buf);
60
61 if (debugMode)
62 Graphics.Blit(buf, debug);
63
64 var brushBuf = brush.BrushTexture;
65 brush.BrushTexture = buf;
66
67 canvas.Paint(brush, contact.point);
68
69 RenderTexture.ReleaseTemporary(buf);
70 brush.BrushTexture = brushBuf;
71 }
72 }
73}
Class managing brush information.
Definition: Brush.cs:11
float RotateAngle
Rotate angle of the brush.
Definition: Brush.cs:168
float Scale
The size of the brush. It takes a range from 0 to 1.
Definition: Brush.cs:159
Texture BrushTexture
Brush texture.
Definition: Brush.cs:131
Texture paint to canvas. To set the per-material.
Definition: InkCanvas.cs:23
Definition: ClipPainter.cs:5