using System.Collections; using System.Collections.Generic; using UnityEditor; using UnityEngine; using UnityEngine.TextCore; using UnityEngine.UIElements; [CustomEditor(typeof(DrawShadowMask))] public class DrawShadowMaskEditor : Editor { DrawShadowMask inst; Ray ray; RaycastHit hit; float brushSize; Vector2 centerPosRecord; private void OnEnable() { inst = (DrawShadowMask)target; } public override void OnInspectorGUI() { base.OnInspectorGUI(); // GUILayout.Space(100); GUILayout.BeginHorizontal(); if (GUILayout.Button("Clear", GUILayout.Height(50))) { Clear(); } if (GUILayout.Button("Fill", GUILayout.Height(50))) { Fill(); } GUILayout.EndHorizontal(); } void Clear() { for (int y = 0; y <= inst.shadowMaskTex.height; ++y) { for (int x = 0; x <= inst.shadowMaskTex.width; ++x) { inst.shadowMaskTex.SetPixel(x, y, Color.black); } } inst.shadowMaskTex.Apply(); } void Fill() { for (int y = 0; y <= inst.shadowMaskTex.height; ++y) { for (int x = 0; x <= inst.shadowMaskTex.width; ++x) { inst.shadowMaskTex.SetPixel(x, y, Color.white); } } inst.shadowMaskTex.Apply(); } private void OnSceneGUI() { if(!inst.shadowMaskTex && !inst.mat) { return; } if(Selection.activeObject == inst.gameObject) { HandleUtility.AddDefaultControl(GUIUtility.GetControlID(FocusType.Passive)); // ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition); if (Physics.Raycast(ray, out hit, 2000, 1 << LayerMask.NameToLayer("DrawHelp"))) { if (Event.current.type == EventType.MouseDown && Event.current.button == 0) { inst.isDrawing = true; } brushSize = inst.brushSize; if (inst.isDrawing) { Handles.color = Color.green; // Vector2Int centerPos = new Vector2Int((int)(hit.textureCoord.x * inst.shadowMaskTex.width), (int)(hit.textureCoord.y * inst.shadowMaskTex.height)); if(centerPosRecord != centerPos) { centerPosRecord = centerPos; // Handles.color = Color.green; Handles.Label(hit.point, centerPos.ToString()); for (int y = -(int)brushSize; y <= (int)brushSize; ++y) { for (int x = -(int)brushSize; x <= (int)brushSize; ++x) { Vector2Int rang = new Vector2Int(x, y) + centerPos; if (rang.x > 0 && rang.x < inst.shadowMaskTex.width || rang.y > 0 && rang.y < inst.shadowMaskTex.height) { Color col = inst.shadowMaskTex.GetPixel(rang.x, rang.y); float dis = 1 - 1 / Vector2.Distance(centerPos, rang); inst.shadowMaskTex.SetPixel(rang.x, rang.y, Color.Lerp(inst.color, col, dis * (1 - inst.color.a))); } } } inst.shadowMaskTex.Apply(); } } if (Event.current.type == EventType.MouseUp && Event.current.button == 0) { inst.isDrawing = false; Handles.color = Color.red; } Handles.DrawWireDisc(hit.point, Vector3.up, brushSize * 0.1f); SceneView.RepaintAll(); } } } }