-
Notifications
You must be signed in to change notification settings - Fork 2
/
NativeGridPaint.cs
66 lines (59 loc) · 1.88 KB
/
NativeGridPaint.cs
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
using UnityEngine;
using Unity.Mathematics;
using NativeGridNamespace;
public class NativeGridPaint : MonoBehaviour
{
NativeGrid<ARGB32> GRID;
[SerializeField] int _width = 512, _height = 512;
[SerializeField] Color32 _color = Color.yellow;
Texture2D _texture = null;
int2 _prevCoord;
void OnEnable ()
{
_texture = new Texture2D( _width , _height , TextureFormat.ARGB32 , 0 , true );
GRID = new NativeGrid<ARGB32>( width:_width , height:_height , _texture.GetRawTextureData<ARGB32>() );
var fillJobHandle = GRID.Fill( new ARGB32{ A=0 , R=255 , G=255 , B=255 } , GRID.Dependency );
fillJobHandle.Complete();
_texture.Apply();
}
void OnDisable ()
{
Destroy( _texture );
GRID.Dispose();
}
void Update ()
{
int2 coord = (int2) math.round( Input.mousePosition / new Vector2{ x=Screen.width , y=Screen.height } * new Vector2{ x=_texture.width , y=_texture.height } );
if( math.any(coord!=_prevCoord) && math.all(new bool4{ x=coord.x>=0 , y=coord.y>=0 , z=coord.x<_texture.width , w=coord.y<_texture.height }) )
{
if( !Input.GetKey(KeyCode.LeftAlt) )
{
if( Input.GetMouseButtonDown(0) )
{
_prevCoord = coord;
GRID[coord] = _color;
_texture.Apply();
}
else if( Input.GetMouseButton(0) )
{
var fillLineJob = GRID.FillLine( _prevCoord , coord , _color );
fillLineJob.Complete();
_prevCoord = coord;
_texture.Apply();
}
}
else
{
if( GRID.IsCoordValid(coord) )
_color = GRID[coord];
}
}
}
void OnGUI () => Graphics.DrawTexture( new Rect{ width=Screen.width , height=Screen.height } , _texture );
public struct ARGB32
{
public byte A,R,G,B;
public static implicit operator ARGB32 ( Color32 rgba32 ) => new ARGB32{ A=rgba32.a , R=rgba32.r , G=rgba32.g , B=rgba32.b };
public static implicit operator Color32 ( ARGB32 argb32 ) => new Color32{ r=argb32.R , g=argb32.G , b=argb32.B , a=argb32.A };
}
}