-
Notifications
You must be signed in to change notification settings - Fork 224
/
DecalPainter.cs
60 lines (54 loc) · 1.97 KB
/
DecalPainter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using UnityEngine;
public class DecalPainter : MonoBehaviour
{
public GameObject TargetObject;
public Shader DecalPainterShader;
public Texture2D DecalTexture;
public bool Debug = true;
private Matrix4x4 _DecalViewMatrix, _DecalProjectionMatrix;
private Material _TargetMaterial;
private Material _PaintMaterial; //unwrapped mesh
private Mesh _TargetMesh;
private Renderer _TargetRenderer;
private RenderTexture _RenderTexture;
void Start()
{
_RenderTexture = new RenderTexture(4096, 4096, 0);
_RenderTexture.Create();
_PaintMaterial = new Material(DecalPainterShader);
_TargetRenderer = TargetObject.GetComponent<Renderer>();
_TargetMesh = TargetObject.GetComponent<MeshFilter>().sharedMesh;
_TargetMaterial = _TargetRenderer.sharedMaterial;
}
void Update()
{
_PaintMaterial.SetMatrix("_MeshModelMatrix", _TargetRenderer.localToWorldMatrix);
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
_DecalViewMatrix = Matrix4x4.TRS(hit.point + hit.normal, Quaternion.LookRotation(-hit.normal, Vector3.up), Vector3.one).inverse;
_DecalProjectionMatrix = Matrix4x4.Ortho(-0.2f, 0.2f, -0.2f, 0.2f, 0.01f, 1.0f);
_PaintMaterial.SetMatrix("_DecalViewMatrix", _DecalViewMatrix);
_PaintMaterial.SetMatrix("_DecalProjectionMatrix", _DecalProjectionMatrix);
_PaintMaterial.SetTexture("_DecalTexture", DecalTexture);
}
RenderTexture currentRT = RenderTexture.active;
RenderTexture.active = _RenderTexture;
GL.Clear(false, true, Color.black, 1.0f);
_PaintMaterial.SetPass(0);
Graphics.DrawMeshNow(_TargetMesh, Vector3.zero, Quaternion.identity);
RenderTexture.active = currentRT;
_TargetMaterial.mainTexture = _RenderTexture;
}
void OnGUI()
{
if (Debug)
GUI.DrawTexture(new Rect(0, 0, 512, 512), _RenderTexture, ScaleMode.ScaleToFit);
}
void OnDestroy()
{
_RenderTexture.Release();
Destroy(_PaintMaterial);
}
}