Advertisement
Guest User

Decal.cs

a guest
Apr 12th, 2021
528
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.83 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEditor;
  3.  
  4. using UnityEngine.Rendering;
  5. using UnityEngine.Rendering.Universal;
  6.  
  7. [ExecuteInEditMode]
  8. public class Decal : MonoBehaviour
  9. {
  10.     public Material m_Material;
  11.     public Texture2D texture;
  12.     public Color tinting = Color.white;
  13.     [SerializeField]
  14.     Camera cam;
  15.     public Mesh m_CubeMesh;
  16.     MaterialPropertyBlock props;
  17.  
  18.     public void OnEnable()
  19.     {
  20.         props = new MaterialPropertyBlock();
  21.     }
  22.  
  23.       void LateUpdate()
  24.         {
  25.             // drawing to all cameras, but you can set it to just a specific one here
  26.        Draw(cam);
  27.    }
  28. #if UNITY_EDITOR
  29.     private void DrawGizmo(bool selected)
  30.     {
  31.         var col = new Color(0.0f, 0.7f, 1f, 1.0f);
  32.         col.a = selected ? 0.3f : 0.1f;
  33.         Gizmos.color = col;
  34.         Gizmos.matrix = transform.localToWorldMatrix;
  35.         Gizmos.DrawCube(Vector3.zero, Vector3.one);
  36.         col.a = selected ? 0.5f : 0.2f;
  37.         Gizmos.color = col;
  38.         Gizmos.DrawWireCube(Vector3.zero, Vector3.one);
  39.         Handles.matrix = transform.localToWorldMatrix;
  40.         Handles.DrawBezier(Vector3.zero, Vector3.down, Vector3.zero, Vector3.down, Color.red, null, selected ? 4f : 2f);
  41.     }
  42. #endif
  43.  
  44.  
  45. #if UNITY_EDITOR
  46.     public void OnDrawGizmos()
  47.     {
  48.         DrawGizmo(false);
  49.     }
  50.     public void OnDrawGizmosSelected()
  51.     {
  52.         DrawGizmo(true);
  53.     }
  54. #endif
  55.  
  56.  
  57.     private void Draw(Camera camera) {
  58.  
  59.   // set up property block for decal texture and tinting
  60.         if (texture != null)
  61.         {
  62.             props.SetTexture("_MainTex", texture);
  63.         }
  64.         props.SetColor("_Tint", tinting);
  65.         // draw the decal on the main camera
  66.         Graphics.DrawMesh(m_CubeMesh, transform.localToWorldMatrix, m_Material, 0, null, 0, props, false, true, false);
  67.        
  68.        
  69.     }
  70. }
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement