Tanoda
MousePainter.cs
Go to the documentation of this file.
1using System;
2using UnityEngine;
3
5{
6 public class MousePainter : MonoBehaviour
7 {
11 [Serializable]
12 private enum UseMethodType
13 {
14 RaycastHitInfo,
15 WorldPoint,
16 NearestSurfacePoint,
17 DirectUV
18 }
19
20 [SerializeField] private Brush brush;
21
22 [SerializeField] private UseMethodType useMethodType = UseMethodType.RaycastHitInfo;
23
24 [SerializeField] bool erase;
25
26 private void Update()
27 {
28 if (Input.GetMouseButton(0))
29 {
30 var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
31 var success = true;
32 RaycastHit hitInfo;
33 if (Physics.Raycast(ray, out hitInfo))
34 {
35 var paintObject = hitInfo.transform.GetComponent<InkCanvas>();
36 if (paintObject != null)
37 switch (useMethodType)
38 {
39 case UseMethodType.RaycastHitInfo:
40 success = erase ? paintObject.Erase(brush, hitInfo) : paintObject.Paint(brush, hitInfo);
41 break;
42
43 case UseMethodType.WorldPoint:
44 success = erase
45 ? paintObject.Erase(brush, hitInfo.point)
46 : paintObject.Paint(brush, hitInfo.point);
47 break;
48
49 case UseMethodType.NearestSurfacePoint:
50 success = erase
51 ? paintObject.EraseNearestTriangleSurface(brush, hitInfo.point)
52 : paintObject.PaintNearestTriangleSurface(brush, hitInfo.point);
53 break;
54
55 case UseMethodType.DirectUV:
56 if (!(hitInfo.collider is MeshCollider))
57 Debug.LogWarning("Raycast may be unexpected if you do not use MeshCollider.");
58 success = erase
59 ? paintObject.EraseUVDirect(brush, hitInfo.textureCoord)
60 : paintObject.PaintUVDirect(brush, hitInfo.textureCoord);
61 break;
62 }
63 if (!success)
64 Debug.LogError("Failed to paint.");
65 }
66 }
67 }
68
69 public void OnGUI()
70 {
71 if (GUILayout.Button("Reset"))
72 foreach (var canvas in FindObjectsOfType<InkCanvas>())
73 canvas.ResetPaint();
74 }
75 }
76}
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
Class managing brush information.
Definition: Brush.cs:11
Texture paint to canvas. To set the per-material.
Definition: InkCanvas.cs:23
bool Erase(Brush brush, Vector3 worldPos, Func< PaintSet, bool > materialSelector=null, Camera renderCamera=null)
Erase processing that use world-space surface position.
Definition: InkCanvas.cs:876