Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using TMPro;
- using UnityEngine;
- public class CleaningInteraction : MonoBehaviour
- {
- public int resolution = 512;
- float brushSize = 3.5f;
- public Texture2D[] brushes;
- public int brushIndex = 0;
- public GameObject sponge;
- public TextMeshProUGUI debug;
- private RaycastHit[] hits = new RaycastHit[3];
- Cleanable ActiveCleanable;
- Vector2 stored;
- float brushRotateSpeed = 10f;
- public void SetBrushSize(float brushSizeButton)
- {
- brushSize = brushSizeButton;
- }
- public void SetDebugView(bool status)
- {
- Shader.SetGlobalInt("_SHOWMASK", status ? 1 : 0);
- }
- public void SwitchBrush(int brushIndexButton)
- {
- brushIndex = brushIndexButton;
- }
- void Update()
- {
- if (Input.GetMouseButton(0))
- {
- RaycastHit hit;
- int hitCount = Physics.SphereCastNonAlloc(Camera.main.ScreenPointToRay(Input.mousePosition), 0.5f, hits);// spherecast for the brush
- for (int i = 0; i < hitCount; i++)
- {
- Collider coll = hits[i].collider;
- if (coll != null)
- {
- if (sponge != null)
- {
- sponge.transform.position = Vector3.Lerp(sponge.transform.position, hits[i].point, Time.deltaTime * brushRotateSpeed);
- Quaternion rot = Quaternion.Slerp(sponge.transform.rotation, Quaternion.LookRotation(hits[i].normal), Time.deltaTime * brushRotateSpeed);
- sponge.transform.rotation = rot;
- }
- if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit))
- {
- if (hit.transform.gameObject.TryGetComponent(out Cleanable cleanable))
- {
- ActiveCleanable = cleanable;
- if (!cleanable.currentPaintMap) // if there is already paint on the material, add to that
- {
- RenderTexture newMap = TexToRT(cleanable.GetReadTex());
- cleanable.SetTex(newMap);
- }
- if (stored != hit.lightmapCoord) // stop drawing on the same point
- {
- if (debug != null)
- {
- debug.text = hit.lightmapCoord.ToString();
- }
- Vector2 pixelUV = hit.lightmapCoord;
- pixelUV.y *= resolution;
- pixelUV.x *= resolution;
- DrawTexture(cleanable.currentPaintMap, pixelUV.x, pixelUV.y);
- cleanable.SetTex(cleanable.currentPaintMap);
- stored = hit.lightmapCoord;
- }
- }
- }
- }
- }
- }
- }
- void ResetThis()
- {
- ActiveCleanable.ResetTex(TexToRT(ActiveCleanable.GetUVCheckedTex()));
- }
- void DrawTexture(RenderTexture rt, float posX, float posY)
- {
- RenderTexture.active = rt; // activate rendertexture for drawtexture;
- GL.PushMatrix(); // save matrixes
- GL.LoadPixelMatrix(0, resolution, resolution, 0); // setup matrix for correct size
- // draw brushtexture
- 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]);
- GL.PopMatrix();
- RenderTexture.active = null;// turn off rendertexture
- }
- RenderTexture TexToRT(Texture2D tex)
- {
- // blit the UV 2D texture onto a rendertexture so we can draw on it.
- RenderTexture rt = new RenderTexture(resolution, resolution, 0, RenderTextureFormat.ARGB32);
- Graphics.Blit(tex, rt);
- return rt;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment