待填
using System.Collections.Generic; using UnityEngine; public class QPainter { private QBrush brush; private QPen pen; private Texture2D device; private Color[] data; private int width; private int height; public QPen Pen { get { return pen; } set { pen = value; } } public QBrush Brush { get { return brush; } set { brush = value; } } public QPainter() { pen = new QPen(QPen.Style.SolidLine); brush = new QBrush(Color.white); } public void Begin(Texture2D device) { this.device = device; data = device.GetPixels(); width = device.width; height = device.height; } public void End(Texture2D device=null) { if(device == null) { this.device.SetPixels(data); this.device.Apply(); } else { device.SetPixels(data); device.Apply(); } this.device = null; data = null; width = height = 0; } public void DrawLine(Vector2Int start, Vector2Int end) { start = Check(start); end = Check(end); float angle = (start.y-end.y) / (start.x-end.x); var Mat2d = QGlobalFunction.Rotate2D(new Matrix4x4(), angle); switch (pen.PenStyle) { case QPen.Style.SolidLine: var pos = GetBLinePoints(start, end); for(int i = 0; i < pos.Length; i++) { data[pos[i].y * width + pos[i].x] = pen.Color; } break; } } private Vector2Int[] GetBLinePoints(Vector2Int point0, Vector2Int point1) { var points = new List<Vector2Int>(); int x0 = point0.x; int y0 = point0.y; int x1 = point1.x; int y1 = point1.y; var dx = Mathf.Abs(x1 - x0); var dy = Mathf.Abs(y1 - y0); var sx = (x0 < x1) ? 1 : -1; var sy = (y0 < y1) ? 1 : -1; var err = dx - dy; int err2; while (true) { points.Add(new Vector2Int(x0, y0)); if ((x0 == x1) && (y0 == y1)) { break; } err2 = 2 * err; if (err2 + dy > 0) { x0 += sx; err -= dy; } if (err2 - dx < 0) { y0 += sy; err += dx; } } return points.ToArray(); } private Vector2Int Check(Vector2Int pos) { pos.x = Mathf.Abs(pos.x); pos.y = Mathf.Abs(pos.y); if (pos.x > width) pos.x = width-1; if (pos.y >= height) pos.y = height - 1; return pos; } } public class QPen { private Color color=Color.white; private int width; private Style style; private CapStyle capStyle; private JoinStyle joinStyle; public Color Color { get { return color; }set { color = value; } } public int Width { get { return width; }set { width = value; } } public Style PenStyle { get { return style; }set { style = value; } } public CapStyle PenCapStyle { get { return capStyle; } set { capStyle = value; } } public JoinStyle PenJoinStyle { get { return joinStyle; } set { joinStyle = value; } } public QPen(Style style) { this.style = style; capStyle = CapStyle.FlatCap; joinStyle = JoinStyle.BevelJoin; } public QPen(Color color) { this.color = color; style = Style.SolidLine; capStyle = CapStyle.FlatCap; joinStyle = JoinStyle.BevelJoin; } public QPen(Color color,int width, Style style, CapStyle capStyle, JoinStyle joinStyle) { this.color = color; this.width = width; this.style = style; this.capStyle = capStyle; this.joinStyle = joinStyle; } public enum Style { NoPen, SolidLine, DashLine, DotLine, DashDotLine, DashDotDotLine, CustomDashLine } public enum CapStyle { FlatCap, SquareCap, RoundCap } public enum JoinStyle { BevelJoin, MiterJoin, RoundJoin } } public class QBrush { private Color color; private Style brushStyle; private Texture2D texture; public Color Color { get { return color; }set { color = value; } } public Style BrushStyle { get { return brushStyle; }set { brushStyle = value; } } public Texture2D Texture { get { return texture; }set { texture = value; } } public QBrush(Color color, Style brushStyle = Style.SolidPattern) { this.color = color; this.brushStyle = brushStyle; } public QBrush(Color color, Texture2D texture) { this.color = color; this.texture = texture; } public QBrush(Texture2D texture) { this.texture = texture; } public QBrush(Style brushStyle = Style.SolidPattern) { this.brushStyle = brushStyle; } public enum Style { NoBrush, SolidPattern, Dense1Pattern, Dense2Pattern, Dense3Pattern, Dense4Pattern, Dense5Pattern, Dense6Pattern, Dense7Pattern, HorPattern, VerPattern, CrossPattern, BDiagPattern, Fdiagpattern, DiagCrossPattern, LinearGradientPattern, RadialGradientPattern, ConicalGradientPattern, TexturePattern } }