-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicImageManipulation.cs
More file actions
168 lines (132 loc) · 6.71 KB
/
BasicImageManipulation.cs
File metadata and controls
168 lines (132 loc) · 6.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using ArashGh.Pixelator.Runtime.DataStructures;
using ArashGh.Pixelator.Runtime.Oulines;
using ArashGh.Pixelator.Runtime.Primitives;
using System;
using UnityEngine;
namespace ArashGh.Pixelator.Samples
{
public class BasicImageManipulation : MonoBehaviour
{
Image image;
Vector2Int dPos;
Layer movableLayer;
Vector3 lastMousePos;
bool movingLayer = false;
private void Start()
{
Test1();
// Test2();
}
void Test1()
{
image = new Image(32, 32);
// image["Base"].Fill(Color.black);
movableLayer = image["Base"];
// You can set and manipulate pixels on a layer in any way you want
for (int i = 0; i < 32 * 32; i++)
{
if (i % 4 == 0)
movableLayer.SetPixelColor(i % 32, i / 32, Color.blue);
}
movableLayer.SetPixelColor(0, 0, Color.red);
movableLayer.SetPixelColor(10, 5, Color.red);
Primitive2D.DrawCircle(movableLayer, new Vector2Int(20, 35), 10, Color.white, true, Color.grey);
movableLayer.MagicSelect(new Vector2Int(20, 30));
movableLayer.FillSelection(Color.red);
// MoveSelection will move the selected pixels by the specified amount
movableLayer.MoveSelection(-20, -12);
// ============ Using the Outline2D class you can draw outlines around your layers or selections
// Outline2D.OutlineLayer(movableLayer, Color.magenta, 1);
// Outline2D.OutlineLayerContent(movableLayer, Color.magenta, true, 3);
//Outline2D.OutlineSelection(movableLayer.GetSelections(), Color.magenta, false, false, 1);
Outline2D.OutlineSelection(movableLayer.GetSelections(), Color.cyan, false, SelectionOutlineType2D.Shape, 3);
image.Render();
}
void Test2()
{
// ============ Uncomment to time the operations
//Stopwatch sw = new Stopwatch();
//sw.Start();
// Creating a new Image
image = new Image(64, 64);
// ============ Each Image starts with a layer called "Base"
// You can access different layers of an Image using the [] (bracket) operator and passing the layer name
// SetPixelColor can be used on any layer to set individual pixel color
image["Base"].SetPixelColor(63, 0, Color.cyan);
image["Base"].SetPixelColor(0, 63, Color.cyan);
// ============ You can add new layers to the image
// There are several methods to help you do that
// => InsertLayerOnTop, InsertLayerOnBottom, InsertLayerUnder, InsertLayerAbove
image.InsertLayerOnTop("Top");
image["Top"].SetPixelColor(63, 0, Color.white);
image["Top"].SetPixelColor(0, 63, Color.white);
image.InsertLayerOnBottom("Background");
image["Background"].Fill(Color.black);
image.InsertLayerUnder("HiddenUnder", "Background");
image["HiddenUnder"].Fill(Color.red);
movableLayer = image.InsertLayerAbove("Pattern", "Background");
// You can set and manipulate pixels on a layer in any way you want
for (int i = 0; i < 64 * 64; i++)
{
if (i % 4 == 0)
movableLayer.SetPixelColor(i % 64, i / 64, Color.blue);
}
// ============ The Primitive2D class helps you draw simple shapes such as Lines and Circles
Primitive2D.DrawLine(image["Top"], new Vector2Int(0, 0), new Vector2Int(63, 42), Color.white, false);
Primitive2D.DrawCircle(image["Top"], new Vector2Int(20, 35), 10, Color.white, true, Color.grey);
// ============ There is a Selection system with basic tools for now. (WIP)
// RectangleSelect lets you select a rectangular area
movableLayer.RectangleSelect(new Vector2Int(35, 15), new Vector2Int(63, 63));
// You can then fill the selected pixels with the color you like
movableLayer.FillSelection(Color.red);
// MagicSelect lets you select the connected pixels with the same color starting from the position you specify (You know what magic wand does, right?)
movableLayer.MagicSelect(new Vector2Int(5, 0));
movableLayer.FillSelection(Color.magenta);
// ============ Calling Deselect will apply the changes done to the selection and apply them to the corrosponding layer
//movableLayer.Deselect();
// ============ The Render method on Image or Layer object will render the pixel color buffer to a Texture2D inside the object
image.Render();
//============Uncomment to time the operations
//sw.Stop();
//UnityEngine.Debug.Log($"{sw.ElapsedMilliseconds}");
//sw.Restart();
// ============ The ExportRenderedImage on the Image objects or Layer objects lets you export the corrosponding Image or Layer's content to a png file at the location you specify
//image.ExportRenderedImage(Path.Combine(Application.dataPath, "test.png"));
//============Uncomment to time the operations
//sw.Stop();
//UnityEngine.Debug.Log($"{sw.ElapsedMilliseconds}");
}
private void Update()
{
if (movableLayer == null)
return;
if (Input.GetMouseButtonDown(0))
{
movingLayer = true;
lastMousePos = Input.mousePosition;
}
if (Input.GetMouseButtonUp(0) || Input.GetMouseButtonUp(1))
{
movingLayer = false;
}
if (movingLayer)
{
dPos = new Vector2Int((int)(Input.mousePosition.x - lastMousePos.x), (int)(Input.mousePosition.y - lastMousePos.y));
dPos /= 4;
if (Mathf.Abs(dPos.x) > 0 || Mathf.Abs(dPos.y) > 0)
{
lastMousePos = Input.mousePosition;
// ============ The Move Layer on the Layer objects moves the whole layer by the amount you provide
movableLayer.MoveSelection(dPos.x, dPos.y);
}
}
image.Render();
}
private void OnGUI()
{
// ============ This is just for demonstration purposes
// You can Access the final Image or Layer rendered texture using the GetRenderedTexture method
GUI.DrawTexture(new Rect((Screen.width - image.Width * 4) / 2, (Screen.height - image.Height * 4) / 2, image.Width * 4, image.Height * 4), image.GetRenderedTexture());
}
}
}