Advertisement
Guest User

PaintDecal.cs

a guest
Oct 13th, 2018
1,023
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.76 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PaintDecal : MonoBehaviour
  6. {
  7.     public int resolution = 512;
  8.     public string textureToPaintTo = "_MainTex";  
  9.     public float decalSize = 0.2f;
  10.     public bool overrideTexture = true;
  11.     public Texture2D decalTexture;
  12.     public Color decalColor = Color.white;
  13.     Texture2D clearMap;
  14.     RenderTexture rTexture;
  15.     RenderTexture resultTex;
  16.     Projector proj;
  17.     Camera cam;
  18.  
  19.     public static Dictionary<Collider, RenderTexture> paintTextures = new Dictionary<Collider, RenderTexture>();
  20.     void Start()
  21.     {
  22.         proj = GetComponent<Projector>();
  23.         cam = GetComponentInChildren<Camera>();
  24.         cam.orthographicSize = 1000;
  25.         SetProjector();      
  26.         CreateClearTexture();// clear white texture to draw on    
  27.         rTexture = GetClearRT(rTexture);
  28.         resultTex = GetClearRT(resultTex);
  29.         cam.targetTexture = rTexture;
  30.     }
  31.  
  32.     void Update()
  33.     {
  34.  
  35.         Debug.DrawRay(transform.position, transform.forward * 20f, Color.magenta);
  36.         RaycastHit hit;
  37.         //if (Physics.Raycast(transform.position, transform.forward, out hit))
  38.         if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit)) // delete previous and uncomment for mouse painting
  39.         {          
  40.            transform.LookAt(hit.point);
  41.            Collider coll = hit.collider;
  42.            if (Input.GetMouseButtonDown(0))
  43.            {
  44.                 if (coll != null && coll.gameObject.layer == 30)
  45.                 {
  46.                     Renderer rend = hit.transform.GetComponentInChildren<Renderer>();                    
  47.                     if (!paintTextures.ContainsKey(coll)) // if there is already paint on the material, add to that, otherwise add new one
  48.                     {
  49.                         paintTextures.Add(coll, GetClearRT(new RenderTexture(resolution, resolution, 32)));
  50.                     }                
  51.                  ProjectDecal(decalTexture, decalSize, decalColor, paintTextures[coll], rend, textureToPaintTo);          
  52.                 }
  53.            }
  54.         }
  55.     }
  56.  
  57.     void ProjectDecal(Texture decalTex, float decalSize, Color decalColor, RenderTexture rt, Renderer rend, string textureString)
  58.     {
  59.         SetProjector();
  60.         if (overrideTexture)
  61.         {
  62.             DrawTexture(rTexture, rt, rend.material.GetTexture(textureString), true);
  63.         }
  64.         else
  65.         {
  66.             DrawTexture(rTexture, rt, null, false);
  67.         }      
  68.        rend.material.SetTexture(textureString, rt);      
  69.     }
  70.  
  71.  
  72.     void DrawTexture(RenderTexture rt, RenderTexture result, Texture baseMaterial, bool drawOver)
  73.     {        
  74.         RenderTexture.active = result; // activate rendertexture for drawtexture;
  75.         GL.PushMatrix();                       // save matrixes
  76.         GL.LoadPixelMatrix(0, rt.width, rt.height, 0);      // setup matrix for correct size
  77.         if (drawOver)
  78.         {
  79.             Graphics.Blit(baseMaterial, result);
  80.         }      
  81.         Graphics.DrawTexture(new Rect(0, 0, rt.width, rt.height), rt);
  82.         GL.PopMatrix();
  83.         RenderTexture.active = null;// turn off rendertexture              
  84.     }
  85.  
  86.     RenderTexture GetClearRT(RenderTexture rt)
  87.     {
  88.         rt = new RenderTexture(resolution, resolution, 32);
  89.         Graphics.Blit(clearMap, rt);
  90.         return rt;
  91.     }
  92.  
  93.     void CreateClearTexture()
  94.     {
  95.         clearMap = new Texture2D(1, 1);
  96.         clearMap.SetPixel(0, 0, Color.clear);
  97.         clearMap.Apply();
  98.     }
  99.  
  100.     void SetProjector()
  101.     {
  102.         proj.orthographicSize = decalSize;
  103.         proj.material.SetTexture("_Decal", decalTexture);
  104.         proj.material.color = decalColor;
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement