Files
2026-03-04 10:03:45 +08:00

75 lines
1.8 KiB
C#

using Es.InkPainter.Effective;
using UnityEngine;
namespace Es.InkPainter.Sample
{
public class ReflectPainter : MonoBehaviour
{
[SerializeField]
private Brush brush;
[SerializeField]
private GameObject camPref;
private RenderTexture rt;
private Camera cam;
private Vector2 uv;
private InkCanvas paintObject;
public void Awake()
{
rt = new RenderTexture(Screen.width, Screen.height, 16, RenderTextureFormat.ARGB32);
brush.ColorBlending = Brush.ColorBlendType.UseBrush;
}
public void OnGUI()
{
if (GUILayout.Button("Reset"))
{
if (paintObject != null)
{
paintObject.ResetPaint();
}
Object.Destroy(cam);
cam = null;
}
}
private void Update()
{
if (cam == null && Input.GetMouseButtonDown(0))
{
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out var hitInfo))
{
paintObject = hitInfo.transform.GetComponent<InkCanvas>();
if (paintObject != null)
{
uv = hitInfo.textureCoord;
GameObject gameObject = Object.Instantiate(camPref, hitInfo.point, Quaternion.LookRotation(hitInfo.normal), hitInfo.transform);
cam = gameObject.GetComponent<Camera>();
cam.targetTexture = rt;
gameObject.SetActive(value: true);
}
}
}
else if (cam != null)
{
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.Clip, temporary);
ReverseUV.Horizontal(temporary, temporary);
Texture brushTexture = brush.BrushTexture;
brush.BrushTexture = temporary;
if (paintObject != null)
{
paintObject.PaintUVDirect(brush, uv);
}
RenderTexture.ReleaseTemporary(temporary);
brush.BrushTexture = brushTexture;
}
}
}
}