From 8cfeecd8e8e1a9fdb31708d6b1235f9a525361b8 Mon Sep 17 00:00:00 2001 From: shenjianxing <”315615051@qq.com“> Date: Mon, 24 Feb 2025 11:11:14 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E7=9F=A9=E5=BD=A2=E5=92=8C?= =?UTF-8?q?=E5=9C=86=E5=BD=A2=E7=BB=98=E7=94=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/Art/UIPrefab/UIDraw.prefab | 12 +- Assets/Draw/Scripts/ScreenShotPainter.cs | 210 ++++++++++++++++++----- Assets/Scripts/UI/UIDraw.Designer.cs | 22 +-- Assets/Scripts/UI/UIDraw.cs | 47 ++++- 4 files changed, 223 insertions(+), 68 deletions(-) diff --git a/Assets/Art/UIPrefab/UIDraw.prefab b/Assets/Art/UIPrefab/UIDraw.prefab index ec90502c..c6a18998 100644 --- a/Assets/Art/UIPrefab/UIDraw.prefab +++ b/Assets/Art/UIPrefab/UIDraw.prefab @@ -185,6 +185,8 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: ec04d319ffe11bf449631785ba9df2ca, type: 3} m_Name: m_EditorClassIdentifier: + captureBGImg: {fileID: 1613795782999759057} + RawImg: {fileID: 3350589312635769069} Content: {fileID: 6174875567737795578} ClearBtn: {fileID: 2761790485595999517} BackBtn: {fileID: 8433557736356431636} @@ -200,8 +202,8 @@ MonoBehaviour: Pen: {fileID: 4450100727786627695} Eraser: {fileID: 2941908847590949937} Line: {fileID: 1266115251622546497} - Fang: {fileID: 5657928476508508069} - Yuan: {fileID: 1768255187776321352} + Rect: {fileID: 5657928476508508069} + Circle: {fileID: 1768255187776321352} Arrow: {fileID: 626467036928040670} Colors: {fileID: 2726755476368017795} Black: {fileID: 8105826465705320577} @@ -213,8 +215,6 @@ MonoBehaviour: Purple: {fileID: 944934755680367845} White: {fileID: 8867501838096098682} More: {fileID: 5620078665499499056} - captureBGImg: {fileID: 1613795782999759057} - RawImg: {fileID: 3350589312635769069} --- !u!1 &572013391981846340 GameObject: m_ObjectHideFlags: 0 @@ -5177,7 +5177,7 @@ GameObject: - component: {fileID: 815582870047914654} - component: {fileID: 1768255187776321352} m_Layer: 5 - m_Name: Yuan + m_Name: Circle m_TagString: Untagged m_Icon: {fileID: 0} m_NavMeshLayer: 0 @@ -8097,7 +8097,7 @@ GameObject: - component: {fileID: 8770634451895496709} - component: {fileID: 5657928476508508069} m_Layer: 5 - m_Name: Fang + m_Name: Rect m_TagString: Untagged m_Icon: {fileID: 0} m_NavMeshLayer: 0 diff --git a/Assets/Draw/Scripts/ScreenShotPainter.cs b/Assets/Draw/Scripts/ScreenShotPainter.cs index 04039520..dd7dc443 100644 --- a/Assets/Draw/Scripts/ScreenShotPainter.cs +++ b/Assets/Draw/Scripts/ScreenShotPainter.cs @@ -2,10 +2,12 @@ using System; using System.Collections; using System.Collections.Generic; +using System.Net; using UnityEngine; using UnityEngine.Events; using UnityEngine.EventSystems; using UnityEngine.UI; +using static UnityEditor.Progress; [RequireComponent(typeof(Camera))] public class ScreenShotPainter : MonoBehaviour @@ -210,7 +212,8 @@ public class ScreenShotPainter : MonoBehaviour Pen, Eraser, Line, - + Rect, + Circle, } public EnterCaptureModeEvent EnterCaptureModeEvent { @@ -605,12 +608,12 @@ public class ScreenShotPainter : MonoBehaviour { if (_enabled && _haveRegion) { - Debug.LogError("OnPaintBeginDrag"); switch (status) { case Status.Pen: break; case Status.Line: + case Status.Rect: if (Input.GetMouseButton(0)) { @@ -622,6 +625,20 @@ public class ScreenShotPainter : MonoBehaviour LerpPaint(Input.mousePosition, _eraserFlag); } + break; + + case Status.Circle: + + if (_currentLine == null) + { + if (_currentLine == null) + { + _currentLine = new LineSegment(_paintBrushMat.GetColor("_Color"), _brushSize, _eraserFlag); + } + _lastPoint = Input.mousePosition; + } + + break; } } @@ -638,24 +655,44 @@ public class ScreenShotPainter : MonoBehaviour { if (_enabled && _haveRegion) { - switch (status) + if (Input.GetMouseButton(0)) { - case Status.Pen: - case Status.Eraser: + switch (status) + { + case Status.Pen: + case Status.Eraser: + - if (Input.GetMouseButton(0)) - { if (_currentLine == null) { _currentLine = new LineSegment(_paintBrushMat.GetColor("_Color"), _brushSize, _eraserFlag); } _currentLine.Points.Add(Input.mousePosition); LerpPaint(Input.mousePosition, _eraserFlag); - } - break; - case Status.Line: - break; + + break; + //case Status.Line: + // if (_currentLine != null) + // { + // if (_currentLine.Points.Count <= 0) + // { + // _currentLine.Points.Add(Input.mousePosition); + // LerpPaint(Input.mousePosition, false); + // } + // } + // break; + //case Status.Rect: + // if (_currentLine != null) + // { + // if (_currentLine.Points.Count <= 0) + // { + // _currentLine.Points.Add(Input.mousePosition); + // LerpPaint(Input.mousePosition, false); + // } + // } + // break; + } } } } @@ -666,24 +703,134 @@ public class ScreenShotPainter : MonoBehaviour switch (status) { case Status.Pen: - case Status.Line: case Status.Eraser: if (Input.GetMouseButtonUp(0)) { - if (_currentLine != null) - { - _lineStack.Push(_currentLine); - _currentLine = null; - } - _lastPoint = Vector2.zero; + FinishedRaw(); } + break; + case Status.Line: + if (_currentLine != null) + { + _currentLine.Points.Add(Input.mousePosition); + LerpPaint(Input.mousePosition, false); + FinishedRaw(); + + } + + break; + case Status.Rect: + { + if (_currentLine != null) + { + var points = GenerateRectanglePoints(_lastPoint, Input.mousePosition); + + + + _currentLine.Points.Add(points[1]); + LerpPaint(points[1], false); + _currentLine.Points.Add(Input.mousePosition); + LerpPaint(Input.mousePosition, false); + _currentLine.Points.Add(points[2]); + LerpPaint(points[2], false); + + _currentLine.Points.Add(_lastPoint); + LerpPaint(points[0], false); + + FinishedRaw(); + + } + } + break; + case Status.Circle: + { + float radius = Vector2.Distance(_lastPoint, Input.mousePosition); + var points = GenerateCirclePoints(_lastPoint, radius); + _lastPoint = default; + foreach (var item in points) + { + _currentLine.Points.Add(item); + LerpPaint(item, false); + } + _currentLine.Points.Add(points[0]); + LerpPaint(points[0], false); + FinishedRaw(); + } break; default: break; } } } + List GenerateCirclePoints(Vector2 center, float r, int numPoints = 36) + { + List points = new List(); + for (int i = 0; i < numPoints; i++) + { + // 计算每个点的角度 + float angle = 2 * Mathf.PI * i / numPoints; + // 根据三角函数计算点的坐标 + float x = center.x + r * Mathf.Cos(angle); + float y = center.y + r * Mathf.Sin(angle); + points.Add(new Vector2(x, y)); + } + return points; + } + Vector2[] GenerateRectanglePoints(Vector2 start, Vector2 end) + { + Vector2[] points = new Vector2[4]; + points[0] = start; + points[3] = end; + + if (start.x < end.x) + { + if (start.y < end.y) + { + // 从左下方到右上方 + points[1] = new Vector2(end.x, start.y); + points[2] = new Vector2(start.x, end.y); + } + else + { + // 从左上方到右下方 + points[1] = new Vector2(end.x, start.y); + points[2] = new Vector2(start.x, end.y); + } + } + else + { + if (start.y < end.y) + { + // 从右下方到左上方 + points[1] = new Vector2(start.x, end.y); + points[2] = new Vector2(end.x, start.y); + } + else + { + // 从右上方到左下方 + points[1] = new Vector2(start.x, end.y); + points[2] = new Vector2(end.x, start.y); + } + } + + return points; + } + + + public void FinishedRaw() + { + + if (_currentLine != null) + { + _lineStack.Push(_currentLine); + _currentLine = null; + } + _lastPoint = Vector2.zero; + } + + + public void Undo() { if (_lineStack.Count > 0) @@ -712,35 +859,10 @@ public class ScreenShotPainter : MonoBehaviour _tempRenderTexture = RenderTexture.GetTemporary(_screenWidth, _screenHeight, 24); _paintCanvasImg.texture = _blitRenderTexture; - + _lineStack.Clear(); } - private void DrawLineSegment(LineSegment line) - { - if (line.Points.Count < 2) - return; - for (int i = 1; i < line.Points.Count; i++) - { - Vector2 startPoint = line.Points[i - 1]; - Vector2 endPoint = line.Points[i]; - - if (line.IsEraser) - { - _eraserBrushMat.SetVector("_UV", new Vector2(startPoint.x / _screenWidth, startPoint.y / _screenHeight)); - Graphics.Blit(_blitRenderTexture, _tempRenderTexture, _eraserBrushMat); - Graphics.Blit(_tempRenderTexture, _blitRenderTexture); - } - else - { - _paintBrushMat.SetColor("_Color", line.Color); - _paintBrushMat.SetFloat("_Size", line.Width); - _paintBrushMat.SetVector("_UV", new Vector2(startPoint.x / _screenWidth, startPoint.y / _screenHeight)); - Graphics.Blit(_blitRenderTexture, _tempRenderTexture, _paintBrushMat); - Graphics.Blit(_tempRenderTexture, _blitRenderTexture); - } - } - } /// /// 绘画进行插值 /// diff --git a/Assets/Scripts/UI/UIDraw.Designer.cs b/Assets/Scripts/UI/UIDraw.Designer.cs index be304ef5..19a40dba 100644 --- a/Assets/Scripts/UI/UIDraw.Designer.cs +++ b/Assets/Scripts/UI/UIDraw.Designer.cs @@ -5,11 +5,15 @@ using QFramework; namespace QFramework.Example { - // Generate Id:fa83cbd2-4e57-43de-97e7-b9a5b8e8da19 + // Generate Id:4d840482-3ece-436e-9579-de5573072430 public partial class UIDraw { public const string Name = "UIDraw"; + [SerializeField] + public UnityEngine.UI.RawImage captureBGImg; + [SerializeField] + public UnityEngine.UI.RawImage RawImg; [SerializeField] public RectTransform Content; [SerializeField] @@ -41,9 +45,9 @@ namespace QFramework.Example [SerializeField] public UnityEngine.UI.Toggle Line; [SerializeField] - public UnityEngine.UI.Toggle Fang; + public UnityEngine.UI.Toggle Rect; [SerializeField] - public UnityEngine.UI.Toggle Yuan; + public UnityEngine.UI.Toggle Circle; [SerializeField] public UnityEngine.UI.Toggle Arrow; [SerializeField] @@ -66,15 +70,13 @@ namespace QFramework.Example public UnityEngine.UI.Toggle White; [SerializeField] public UnityEngine.UI.Image More; - [SerializeField] - public UnityEngine.UI.RawImage captureBGImg; - [SerializeField] - public UnityEngine.UI.RawImage RawImg; private UIDrawData mPrivateData = null; protected override void ClearUIComponents() { + captureBGImg = null; + RawImg = null; Content = null; ClearBtn = null; BackBtn = null; @@ -90,8 +92,8 @@ namespace QFramework.Example Pen = null; Eraser = null; Line = null; - Fang = null; - Yuan = null; + Rect = null; + Circle = null; Arrow = null; Colors = null; Black = null; @@ -103,8 +105,6 @@ namespace QFramework.Example Purple = null; White = null; More = null; - captureBGImg = null; - RawImg = null; mData = null; } diff --git a/Assets/Scripts/UI/UIDraw.cs b/Assets/Scripts/UI/UIDraw.cs index 36cdad98..719b7dd3 100644 --- a/Assets/Scripts/UI/UIDraw.cs +++ b/Assets/Scripts/UI/UIDraw.cs @@ -96,6 +96,7 @@ namespace QFramework.Example if (isOn) { ScreenShotPainter.instance.SetStatus(ScreenShotPainter.Status.Pen); + RefreshPenSize(); } }); Eraser.onValueChanged.AddListener(isOn => @@ -103,6 +104,7 @@ namespace QFramework.Example if (isOn) { ScreenShotPainter.instance.SetStatus(ScreenShotPainter.Status.Eraser); + RefreshPenSize(); } }); Line.onValueChanged.AddListener(isOn => @@ -114,6 +116,21 @@ namespace QFramework.Example }); + Rect.onValueChanged.AddListener(isOn => + { + if (isOn) + { + ScreenShotPainter.instance.SetStatus(ScreenShotPainter.Status.Rect); + } + }); + + Circle.onValueChanged.AddListener(isOn => + { + if (isOn) + { + ScreenShotPainter.instance.SetStatus(ScreenShotPainter.Status.Circle); + } + }); } public void ChangeColor(Color color) @@ -124,21 +141,37 @@ namespace QFramework.Example public void RefreshPenSize() { + var normalValue = PenSizeSlider.value / PenSizeSlider.maxValue; - float size = PaintingParams.BrushSizeMaxValue + (1 - normalValue) * PaintingParams.BrushSizeFactor; - //ScreenShotPainter.instance.SetPaintingSize(size); + if (Eraser.isOn) + { - float sizeY = ((float)Screen.height / (float)Screen.width) * size; + //С + float size = PaintingParams.EraserSizeMaxValue + (1 - normalValue) * PaintingParams.EraserSizeFactor; + float sizeY = ((float)Screen.height / (float)Screen.width) * size; + ScreenShotPainter.instance.SetEraserSize(size, sizeY); - ScreenShotPainter.instance.SetPaintingSize(size, sizeY); + float width = Screen.width / size; + float height = Screen.width / size;//y + Icon.transform.localScale = new Vector3(width / Icon.rectTransform.rect.width, height / Icon.rectTransform.rect.height, 1); + } + else + { - float width = Screen.width / size; - float height = Screen.width / size;//y - Icon.transform.localScale = new Vector3(width / Icon.rectTransform.rect.width, height / Icon.rectTransform.rect.height, 1); + float size = PaintingParams.BrushSizeMaxValue + (1 - normalValue) * PaintingParams.BrushSizeFactor; + float sizeY = ((float)Screen.height / (float)Screen.width) * size; + ScreenShotPainter.instance.SetPaintingSize(size, sizeY); + float width = Screen.width / size; + float height = Screen.width / size;//y + Icon.transform.localScale = new Vector3(width / Icon.rectTransform.rect.width, height / Icon.rectTransform.rect.height, 1); + } Penvalue.text = PenSizeSlider.value.ToString(); + + + } public void RefreshAlphaSet()