Guest User

cleaningInteractionMoreOptions

a guest
Feb 6th, 2026
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.24 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using TMPro;
  4. using UnityEngine;
  5.  
  6.  
  7. public class CleaningInteraction : MonoBehaviour
  8. {
  9.     public int resolution = 512;
  10.     float brushSize = 3.5f;
  11.     public Texture2D[] brushes;
  12.     public int brushIndex = 0;
  13.     public GameObject sponge;
  14.     public TextMeshProUGUI debug;
  15.     private RaycastHit[] hits = new RaycastHit[3];
  16.     Cleanable ActiveCleanable;
  17.     Vector2 stored;
  18.     float brushRotateSpeed = 10f;
  19.  
  20.     public void SetBrushSize(float brushSizeButton)
  21.     {
  22.         brushSize = brushSizeButton;
  23.     }
  24.  
  25.     public void SetDebugView(bool status)
  26.     {
  27.         Shader.SetGlobalInt("_SHOWMASK", status ? 1 : 0);
  28.     }
  29.  
  30.     public void SwitchBrush(int brushIndexButton)
  31.     {
  32.         brushIndex = brushIndexButton;
  33.     }
  34.  
  35.     void Update()
  36.     {
  37.         if (Input.GetMouseButton(0))
  38.         {
  39.             RaycastHit hit;
  40.             int hitCount = Physics.SphereCastNonAlloc(Camera.main.ScreenPointToRay(Input.mousePosition), 0.5f, hits);// spherecast for the brush
  41.             for (int i = 0; i < hitCount; i++)
  42.             {
  43.                 Collider coll = hits[i].collider;
  44.                 if (coll != null)
  45.                 {
  46.  
  47.                     if (sponge != null)
  48.                     {
  49.                         sponge.transform.position = Vector3.Lerp(sponge.transform.position, hits[i].point, Time.deltaTime * brushRotateSpeed);
  50.                         Quaternion rot = Quaternion.Slerp(sponge.transform.rotation, Quaternion.LookRotation(hits[i].normal), Time.deltaTime * brushRotateSpeed);
  51.                         sponge.transform.rotation = rot;
  52.                     }
  53.                     if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit))
  54.                     {
  55.  
  56.                         if (hit.transform.gameObject.TryGetComponent(out Cleanable cleanable))
  57.                         {
  58.                             ActiveCleanable = cleanable;
  59.                             if (!cleanable.currentPaintMap) // if there is already paint on the material, add to that
  60.                             {
  61.                                 RenderTexture newMap = TexToRT(cleanable.GetReadTex());
  62.                                 cleanable.SetTex(newMap);
  63.  
  64.                             }
  65.                             if (stored != hit.lightmapCoord) // stop drawing on the same point
  66.                             {
  67.                                 if (debug != null)
  68.                                 {
  69.                                     debug.text = hit.lightmapCoord.ToString();
  70.                                 }
  71.  
  72.                                 Vector2 pixelUV = hit.lightmapCoord;
  73.                                 pixelUV.y *= resolution;
  74.                                 pixelUV.x *= resolution;
  75.  
  76.                                 DrawTexture(cleanable.currentPaintMap, pixelUV.x, pixelUV.y);
  77.                                 cleanable.SetTex(cleanable.currentPaintMap);
  78.                                 stored = hit.lightmapCoord;
  79.                             }
  80.                         }
  81.  
  82.                     }
  83.                 }
  84.             }
  85.         }
  86.     }
  87.  
  88.     void ResetThis()
  89.     {
  90.         ActiveCleanable.ResetTex(TexToRT(ActiveCleanable.GetUVCheckedTex()));
  91.     }
  92.  
  93.     void DrawTexture(RenderTexture rt, float posX, float posY)
  94.     {
  95.         RenderTexture.active = rt; // activate rendertexture for drawtexture;
  96.         GL.PushMatrix();                       // save matrixes
  97.         GL.LoadPixelMatrix(0, resolution, resolution, 0);      // setup matrix for correct size
  98.  
  99.         // draw brushtexture
  100.         Graphics.DrawTexture(new Rect(posX - brushes[brushIndex].width / brushSize, (rt.height - posY) - brushes[brushIndex].height / brushSize, brushes[brushIndex].width / (brushSize * 0.5f), brushes[brushIndex].height / (brushSize * 0.5f)), brushes[brushIndex]);
  101.         GL.PopMatrix();
  102.         RenderTexture.active = null;// turn off rendertexture
  103.     }
  104.  
  105.     RenderTexture TexToRT(Texture2D tex)
  106.     {
  107.         // blit the UV 2D texture onto a rendertexture so we can draw on it.
  108.         RenderTexture rt = new RenderTexture(resolution, resolution, 0, RenderTextureFormat.ARGB32);
  109.         Graphics.Blit(tex, rt);
  110.         return rt;
  111.     }
  112.  
  113.  
  114. }
Advertisement
Add Comment
Please, Sign In to add comment