-
Notifications
You must be signed in to change notification settings - Fork 0
/
BrushAndGUI.cs
319 lines (272 loc) · 12.6 KB
/
BrushAndGUI.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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
namespace VoxelTerrain
{
// Example script
public class BrushAndGUI : MonoBehaviour
{
public static BrushAndGUI Instance;
public RectTransform DropdownMenu;
public RectTransform SaveSuccessPanel;
public RectTransform ResetPanel;
public RectTransform PolyCountPanel;
public Text BrushSizeSlider;
public Transform HighlighterSphere;
public Transform HighlighterCube;
public Transform HighlighterPlane;
public Transform BoundsVisualizer;
// set default values
private float _distance = 100F;
private Vector3 _position = Vector3.zero;
public float BrushSize = 13F;
private bool _paintEnable = false;
public VoxTerrain.OBJ BrushObject = VoxTerrain.OBJ.SPHERE; //default brush object
private VoxTerrain.EFFECT BrushEffect = VoxTerrain.EFFECT.ADD; //default brush effect
static short grass = 0; //texture 1
static short stone = 1; //texture 2
static short sand = 2; //texture 3
static short white = 3; //White or no texture
private short paintBrush = 1; // current brush/texture
private Ray _ray;
private RaycastHit _hit;
private float _lastClick = 0;
private bool doPaint = true;
private bool doSculpt = true;
private bool Continuous = false;
private bool mouseDown = false;
private float dragCameraSpeed = 0.1f;
private float rotateCameraSpeed = 5f;
private float scrollCameraSpeed = 15f;
private float flyCameraSpeed = 20f;
private void Awake()
{
Instance = this;
}
void Start()
{
//BrushSizeSlider.text = "Brush Size: " + BrushSize;
BoundsVisualizer.gameObject.SetActive(false);
}
void OnGUI()
{
// switch brushs on the keyboard buttons
if (Input.GetKeyDown("1"))
{
paintBrush = grass;
}
if (Input.GetKeyDown("2"))
{
paintBrush = stone;
}
if (Input.GetKeyDown("3"))
{
paintBrush = sand;
}
if (Input.GetKeyDown("4"))
{
paintBrush = white;
}
if(Input.GetKeyDown("5")){
OnSaveConfirmPressed();
}
}
void Update()
{
// comparation with screen height is only because of down GUI
#if UNITY_EDITOR || UNITY_STANDALONE
if (Input.GetMouseButtonDown(0) && !EventSystem.current.IsPointerOverGameObject() && Input.GetKey(KeyCode.LeftControl)) { mouseDown = true; }
#else // Used to test drawing on mobile...
if (Input.GetMouseButtonDown(0)&&!EventSystem.current.IsPointerOverGameObject(0)) {mouseDown=true;}
#endif
if (Input.GetMouseButtonUp(0)) { mouseDown = false; }
//*/
// Get paintPosition
//*
_ray = Camera.main.ScreenPointToRay(Input.mousePosition);
// Find point to paint, if point find
if (Physics.Raycast(_ray, out _hit))
{
// Get distance
_distance = _hit.distance;
}
// Get Contact position
_position = _ray.GetPoint(_distance);
//*/
// Display mesh in paint point
UpdateHighlighter();
// if in sculpt mode and mouse down
if (mouseDown)
{
// left click, if you decreese 2F time to lower you ger more precision
if (mouseDown) { if (_lastClick < Time.time - 2F) { _paintEnable = true; } }
else { _paintEnable = false; }
// Paint
if (!_paintEnable)
VoxTerrain.Instance.ReBuildCollider();
VoxTerrain.Instance.ReBuildColliderClient();
VoxTerrain.Instance.Draw3D(_position, new Vector3(BrushSize, BrushSize, BrushSize), BrushObject, BrushEffect, paintBrush, doSculpt, doPaint, true, null, false);
// for continous brush enable this
if (Continuous)
{
VoxTerrain.Instance.ReBuildCollider();
VoxTerrain.Instance.ReBuildColliderClient();
}
}
else
{
VoxTerrain.Instance.ReBuildCollider();
VoxTerrain.Instance.ReBuildColliderClient();
}
// camera movement section
if (Input.GetKey(KeyCode.UpArrow)) { Camera.main.transform.Rotate(new Vector3(-1, 0, 0), flyCameraSpeed * Time.deltaTime); }
if (Input.GetKey(KeyCode.DownArrow)) { Camera.main.transform.Rotate(new Vector3(1, 0, 0), flyCameraSpeed * Time.deltaTime); }
if (Input.GetKey(KeyCode.LeftArrow)) { Camera.main.transform.Rotate(new Vector3(0, -1, 0), flyCameraSpeed * Time.deltaTime); }
if (Input.GetKey(KeyCode.RightArrow)) { Camera.main.transform.Rotate(new Vector3(0, 1, 0), flyCameraSpeed * Time.deltaTime); }
if (Input.GetKey(KeyCode.W)) { Camera.main.transform.position += Camera.main.transform.transform.TransformDirection(Vector3.forward * flyCameraSpeed * Time.deltaTime); }
if (Input.GetKey(KeyCode.S)) { Camera.main.transform.position -= Camera.main.transform.transform.TransformDirection(Vector3.forward * flyCameraSpeed * Time.deltaTime); }
if (Input.GetKey(KeyCode.A)) { Camera.main.transform.position += Camera.main.transform.transform.TransformDirection(Vector3.left * flyCameraSpeed * Time.deltaTime); }
if (Input.GetKey(KeyCode.D)) { Camera.main.transform.position += Camera.main.transform.transform.TransformDirection(Vector3.right * flyCameraSpeed * Time.deltaTime); }
#if UNITY_EDITOR || UNITY_STANDALONE
if (!Input.GetKey(KeyCode.LeftControl) && !EventSystem.current.IsPointerOverGameObject())
{
if (Input.GetMouseButton(0))
{
Vector3 newPos = new Vector3(-Input.GetAxis("Mouse X") * dragCameraSpeed, -Input.GetAxis("Mouse Y") * dragCameraSpeed, 0) * Camera.main.transform.position.magnitude;
Camera.main.transform.Translate(newPos, Space.Self);
}
if (Input.GetMouseButton(1))
{
Camera.main.transform.Rotate(0, -Input.GetAxis("Mouse X") * rotateCameraSpeed, 0, Space.World);
Camera.main.transform.Rotate(Input.GetAxis("Mouse Y") * rotateCameraSpeed, 0, 0);
}
Camera.main.transform.Translate(Camera.main.transform.forward * Input.GetAxis("Mouse ScrollWheel") * scrollCameraSpeed, Space.World);
}
if(Input.GetKey(KeyCode.T))
{
float height = 5;
Vector3 pos = new Vector3(VoxTerrain.Instance.width, VoxTerrain.Instance.height, VoxTerrain.Instance.depth) / 2;
pos.y = height;
VoxTerrain.Instance.Draw3D(pos, new Vector3(VoxTerrain.Instance.width, height, VoxTerrain.Instance.depth), VoxTerrain.OBJ.CUBE, VoxTerrain.EFFECT.ADD, 0, true, true, true, null, false);
}
#endif
//*/
}
private void UpdateHighlighter()
{
HighlighterSphere.gameObject.SetActive(false);
HighlighterCube.gameObject.SetActive(false);
HighlighterPlane.gameObject.SetActive(false);
Transform highligher = null;
float sizeX = BrushSize;
float sizeY = BrushSize;
float sizeZ = BrushSize;
if (BrushObject == VoxTerrain.OBJ.SPHERE)
{
highligher = HighlighterSphere;
}
else if (BrushObject == VoxTerrain.OBJ.CUBE)
{
highligher = HighlighterCube;
}
else if (BrushObject == VoxTerrain.OBJ.RANDOM)
{
highligher = HighlighterSphere;
sizeY = HighlighterPlane.transform.localScale.y;
}
else if (BrushObject == VoxTerrain.OBJ.PLANE)
{
highligher = HighlighterPlane;
sizeY = HighlighterPlane.transform.localScale.y;
}
if (highligher != null)
{
highligher.gameObject.SetActive(true);
highligher.localScale = new Vector3(sizeX, sizeY, sizeZ);
highligher.position = _position;
}
}
public void ChangeTexture(int textureIndex)
{
if (textureIndex == grass) paintBrush = grass;
if (textureIndex == stone) paintBrush = stone;
if (textureIndex == sand) paintBrush = sand;
if (textureIndex == white) paintBrush = white;
}
public void SetMode(int modeIndex)
{
if (modeIndex == 0) { doSculpt = true; doPaint = false; } // Sculpt Mode
if (modeIndex == 1) { doSculpt = false; doPaint = true; } // Paint Mode
if (modeIndex == 2) { doSculpt = true; doPaint = true; } // Combined Mode
}
public void SetContinuousMode(int isOn)
{
Continuous = (isOn == 1);
}
public void SetDrawingShape(int shapeIndex)
{
if (shapeIndex == 0) { BrushObject = VoxTerrain.OBJ.CUBE; }
if (shapeIndex == 1) { BrushObject = VoxTerrain.OBJ.SPHERE; }
if (shapeIndex == 2) { BrushObject = VoxTerrain.OBJ.RANDOM; }
if (shapeIndex == 3) { BrushObject = VoxTerrain.OBJ.PLANE; }
}
public void SetBrushSize(float sliderSize)
{
if (sliderSize > -1) { BrushEffect = VoxTerrain.EFFECT.ADD; BrushSize = sliderSize; }
else { BrushEffect = VoxTerrain.EFFECT.SUB; BrushSize = -sliderSize; }
BrushSizeSlider.text = "Brush Size: " + sliderSize;
}
public void OnMenuPressed()
{
DropdownMenu.gameObject.SetActive(true);
}
public void OnMenuItemSelected(int itemIndex)
{
if (itemIndex == 0) OnSaveConfirmPressed(); // On Save button pressed
if (itemIndex == 1) ResetPanel.gameObject.SetActive(true); // On Reset button pressed
if (itemIndex == 2) OnShowPolyCountPressed(); // On Show Poly Count button pressed
OnItemSelectionDone();
}
public void OnItemSelectionDone()
{
DropdownMenu.gameObject.SetActive(false);
}
public void OnResetConfirmed()
{
VoxTerrain.Instance.ResetMap(); // SceneManager.LoadScene("VoxelTerrainScene");
VoxTerrain.Instance.ReBuildCollider();
VoxTerrain.Instance.ReBuildColliderClient();
}
public void OnSaveConfirmPressed()
{
print("Saving " + VoxTerrain.Instance.TerrainName);
VoxTerrain.SaveToFile(VoxTerrain.Instance, VoxTerrain.Instance.TerrainName);
SaveSuccessPanel.gameObject.SetActive(true);
}
public void OnLoadTerrainPressed(Dropdown selectTerrainDropdown)
{
print("Loading");
VoxTerrain.LoadFromFile(selectTerrainDropdown.captionText.text);
}
public void OnShowPolyCountPressed()
{
PolyCountPanel.gameObject.SetActive(true);
int polyCount = 0;
foreach (VoxelTerrain.Cube c in VoxTerrain.Instance._cubes)
{
polyCount += c.mesh.triangles.Length / 3;
}
PolyCountPanel.Find("Text PolyCount").GetComponent<Text>().text = "Poly Count: " + polyCount;
}
public void OnShowHideBoundsPressed()
{
BoundsVisualizer.gameObject.SetActive(!BoundsVisualizer.gameObject.activeSelf);
if (BoundsVisualizer.gameObject.activeSelf)
{
Vector3 boundsSize = new Vector3(VoxTerrain.Instance.width, VoxTerrain.Instance.height, VoxTerrain.Instance.depth);
BoundsVisualizer.transform.position = boundsSize / 2;
BoundsVisualizer.transform.localScale = boundsSize;
}
}
}
}