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