gg.easy.airship 0.1.1662 → 0.1.1663

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  using System;
2
2
  using UnityEngine;
3
- using System.Windows.Forms;
3
+ using System.Collections.Generic;
4
4
 
5
5
  #if UNITY_EDITOR
6
6
  using UnityEditor;
@@ -259,7 +259,7 @@ public class SelectionZone : MonoBehaviour
259
259
  #if UNITY_EDITOR
260
260
 
261
261
  [CustomEditor(typeof(SelectionZone))]
262
- public class SelectionZoneEditor : Editor {
262
+ public class SelectionZoneEditor : UnityEditor.Editor {
263
263
  private const float handleSize = 0.3f;
264
264
 
265
265
  private bool mouseDown = false;
@@ -298,10 +298,21 @@ public class SelectionZoneEditor : Editor {
298
298
  //Gui
299
299
  public override void OnInspectorGUI() {
300
300
  //draw default
301
- DrawDefaultInspector();
301
+ //DrawDefaultInspector();
302
302
 
303
- //Draw a reset button
303
+ //Add typeins for size x y and z
304
304
  SelectionZone cube = (SelectionZone)target;
305
+ Vector3Int oldSize = new Vector3Int((int)cube.size.x, (int)cube.size.y, (int)cube.size.z);
306
+ Vector3Int newSize = EditorGUILayout.Vector3IntField("Size", oldSize);
307
+
308
+ if (newSize != oldSize) {
309
+ cube.size = newSize;
310
+ SnapToGrid();
311
+ cube.BuildCube();
312
+ ResetHandles();
313
+ }
314
+
315
+ //Draw a reset button
305
316
  if (GUILayout.Button("Reset")) {
306
317
  handleOffset = new float[6] {
307
318
  1,1,1,1,1,1
@@ -313,6 +324,13 @@ public class SelectionZoneEditor : Editor {
313
324
  cube.size = new Vector3(1, 1, 1);
314
325
  cube.BuildCube();
315
326
  }
327
+ if (cube.voxelWorld == null) {
328
+ return;
329
+ }
330
+ if (cube.voxelWorld.voxelBlocks == null) {
331
+ return;
332
+ }
333
+ VoxelEditManager voxelEditManager = VoxelEditManager.Instance;
316
334
 
317
335
  //Add Copy Button
318
336
  if (GUILayout.Button("Fill")) {
@@ -323,20 +341,23 @@ public class SelectionZoneEditor : Editor {
323
341
  float px = cube.transform.localPosition.x;
324
342
  float py = cube.transform.localPosition.y;
325
343
  float pz = cube.transform.localPosition.z;
326
-
327
-
344
+
328
345
  if (cube.voxelWorld) {
329
-
346
+
347
+ List<VoxelEditAction.EditInfo> edits = new();
348
+
349
+ int selectedIndex = cube.voxelWorld.selectedBlockIndex;
330
350
  //Walk the current selection zone
331
- for (int x = (int)(px - dx); x < (int)(px+dx); x++) {
332
- for (int y = (int)(py - dy); y < (int)(py + dy); y++) {
333
- for (int z = (int)(pz - dz); z < (int)(pz + dz); z++) {
334
- cube.voxelWorld.WriteVoxelAt(new Vector3Int(x, y, z), 1, false);
351
+ for (int x = Mathf.FloorToInt(px - dx); x < Mathf.CeilToInt(px + dx); x++) {
352
+ for (int y = Mathf.FloorToInt(py - dy); y < Mathf.CeilToInt(py + dy); y++) {
353
+ for (int z = Mathf.FloorToInt(pz - dz); z < Mathf.CeilToInt(pz + dz); z++) {
354
+ UInt16 prevData = cube.voxelWorld.ReadVoxelAt(new Vector3Int(x, y, z));
355
+ edits.Add(new VoxelEditAction.EditInfo(new Vector3Int(x, y, z), prevData, (UInt16)selectedIndex));
335
356
  }
336
357
  }
337
358
  }
359
+ voxelEditManager.AddEdits(cube.voxelWorld, edits, "Fill Voxels");
338
360
  }
339
-
340
361
  }
341
362
 
342
363
  if (GUILayout.Button("Copy")) {
@@ -354,18 +375,17 @@ public class SelectionZoneEditor : Editor {
354
375
  copiedData = new UInt16[(int)cube.size.x * (int)cube.size.y * (int)cube.size.z];
355
376
 
356
377
  if (cube.voxelWorld) {
357
-
378
+
358
379
  int index = 0;
359
380
  //Walk the current selection zone
360
- for (int x = (int)(px - dx); x < (int)(px + dx); x++) {
361
- for (int y = (int)(py - dy); y < (int)(py + dy); y++) {
362
- for (int z = (int)(pz - dz); z < (int)(pz + dz); z++) {
381
+ for (int x = Mathf.FloorToInt(px - dx); x < Mathf.CeilToInt(px + dx); x++) {
382
+ for (int y = Mathf.FloorToInt(py - dy); y < Mathf.CeilToInt(py + dy); y++) {
383
+ for (int z = Mathf.FloorToInt(pz - dz); z < Mathf.CeilToInt(pz + dz); z++) {
363
384
  copiedData[index++] = cube.voxelWorld.ReadVoxelAt(new Vector3Int(x, y, z));
364
385
  }
365
386
  }
366
387
  }
367
388
  }
368
-
369
389
  }
370
390
 
371
391
  if (haveCopiedData == false) {
@@ -380,7 +400,7 @@ public class SelectionZoneEditor : Editor {
380
400
  }else {
381
401
  //Actual paste
382
402
  if (GUILayout.Button("Paste")) {
383
- //walk the bounds
403
+ //walk the bouns
384
404
  float dx = copiedSize.x / 2;
385
405
  float dy = copiedSize.y / 2;
386
406
  float dz = copiedSize.z / 2;
@@ -390,18 +410,27 @@ public class SelectionZoneEditor : Editor {
390
410
 
391
411
  if (cube.voxelWorld) {
392
412
  int index = 0;
413
+
414
+ List<VoxelEditAction.EditInfo> edits = new();
415
+
393
416
  //Walk the current selection zone
394
- for (int x = (int)(px - dx); x < (int)(px + dx); x++) {
395
- for (int y = (int)(py - dy); y < (int)(py + dy); y++) {
396
- for (int z = (int)(pz - dz); z < (int)(pz + dz); z++) {
397
- cube.voxelWorld.WriteVoxelAt(new Vector3Int(x, y, z), copiedData[index++], false);
417
+ for (int x = Mathf.FloorToInt(px - dx); x < Mathf.CeilToInt(px + dx); x++) {
418
+ for (int y = Mathf.FloorToInt(py - dy); y < Mathf.CeilToInt(py + dy); y++) {
419
+ for (int z = Mathf.FloorToInt(pz - dz); z < Mathf.CeilToInt(pz + dz); z++) {
420
+ //cube.voxelWorld.WriteVoxelAt(new Vector3Int(x, y, z), copiedData[index++], false);
421
+ UInt16 prevData = cube.voxelWorld.ReadVoxelAt(new Vector3Int(x, y, z));
422
+ edits.Add(new VoxelEditAction.EditInfo(new Vector3Int(x, y, z), prevData, copiedData[index++]));
398
423
  }
399
424
  }
400
425
  }
426
+ voxelEditManager.AddEdits(cube.voxelWorld, edits, "Paste Voxels");
427
+
428
+
401
429
  }
402
430
 
403
431
  //resize the box to whatever we pasted
404
432
  cube.size = new Vector3(copiedSize.x, copiedSize.y, copiedSize.z);
433
+ SnapToGrid();
405
434
  cube.BuildCube();
406
435
  ResetHandles();
407
436
  }
@@ -480,7 +509,6 @@ public class SelectionZoneEditor : Editor {
480
509
  }
481
510
  if (Event.current.type == EventType.MouseDown) {
482
511
  mouseDown = true;
483
-
484
512
  }
485
513
 
486
514
  Transform cubeTransform = cube.transform;
@@ -527,9 +555,7 @@ public class SelectionZoneEditor : Editor {
527
555
  if (moved == true) {
528
556
 
529
557
  float distance = localHandlePos.magnitude - trueHandleOffset[i];
530
-
531
558
  float steps = Mathf.Floor(distance);
532
- Debug.Log("Steps" + steps);
533
559
 
534
560
  handleOffset[i] = localHandlePos.magnitude;
535
561
 
@@ -120,7 +120,7 @@ namespace Code.Airship.Resources.VoxelRenderer.Editor {
120
120
 
121
121
 
122
122
  //See if we're in the selection mode
123
- if (VoxelWorldSelectionTool.buttonActive == true) {
123
+ if (VoxelWorldSelectionToolBase.buttonActive == true) {
124
124
  //Find or create the SelectionZone for this voxelWorld
125
125
 
126
126
  selection = world.GetComponentInChildren<SelectionZone>();
@@ -134,7 +134,7 @@ namespace Code.Airship.Resources.VoxelRenderer.Editor {
134
134
  //Select this
135
135
  Selection.activeGameObject = selection.gameObject;
136
136
  }
137
- if (VoxelWorldEditorTool.buttonActive == true) {
137
+ if (VoxelWorldEditorToolBase.buttonActive == true) {
138
138
 
139
139
  //If we're not in selection mode, destroy the selection zone
140
140
  selection = world.GetComponentInChildren<SelectionZone>();
@@ -164,13 +164,19 @@ namespace Code.Airship.Resources.VoxelRenderer.Editor {
164
164
 
165
165
  GUIStyle selectedStyle = new GUIStyle(GUI.skin.button);
166
166
  selectedStyle.normal.textColor = Color.green;
167
-
167
+ selectedStyle.hover.textColor = Color.green;
168
168
 
169
169
  foreach (var pair in world.voxelBlocks.loadedBlocks) {
170
+
171
+ string name = pair.Value.definition.name;
172
+ if (name == "") {
173
+ name = "Air";
174
+ }
175
+
170
176
  if (pair.Key == world.selectedBlockIndex) {
171
- GUILayout.Button(pair.Value.definition.name, selectedStyle);
177
+ GUILayout.Button(name, selectedStyle);
172
178
  } else {
173
- if (GUILayout.Button(pair.Value.definition.name)) {
179
+ if (GUILayout.Button(name)) {
174
180
  world.selectedBlockIndex = pair.Key;
175
181
  }
176
182
  }
@@ -7,17 +7,32 @@ using UnityEditor.EditorTools;
7
7
  using UnityEngine;
8
8
  using System;
9
9
  using static UnityEditor.PlayerSettings;
10
+ using static VoxelEditAction;
10
11
 
11
12
  public class VoxelEditAction {
12
- public Vector3Int position;
13
- public ushort oldValue;
14
- public ushort newValue;
13
+
14
+ public struct EditInfo{
15
+ public Vector3Int position;
16
+ public ushort oldValue;
17
+ public ushort newValue;
18
+ //constructor
19
+ public EditInfo(Vector3Int position, ushort oldValue, ushort newValue){
20
+ this.position = position;
21
+ this.oldValue = oldValue;
22
+ this.newValue = newValue;
23
+ }
24
+ }
25
+ public List<EditInfo> edits = new List<EditInfo>();
26
+
15
27
  [NonSerialized]
16
28
  public WeakReference<VoxelWorld> world;
17
- public void Initialize(VoxelWorld world, Vector3Int position, ushort oldValue, ushort newValue) {
18
- this.position = position;
19
- this.oldValue = oldValue;
20
- this.newValue = newValue;
29
+ public void CreateSingleEdit(VoxelWorld world, Vector3Int position, ushort oldValue, ushort newValue) {
30
+ edits.Add(new EditInfo(position, oldValue, newValue));
31
+ this.world = new(world);
32
+ }
33
+
34
+ public void CreateMultiEdit(VoxelWorld world, List<EditInfo> edits) {
35
+ this.edits = edits;
21
36
  this.world = new(world);
22
37
  }
23
38
  }
@@ -30,11 +45,10 @@ public class VoxelEditManager : Singleton<VoxelEditManager> {
30
45
  VoxelEditMarker undoObject;
31
46
  public List<VoxelEditAction> edits = new List<VoxelEditAction>();
32
47
  public List<VoxelEditAction> redos = new List<VoxelEditAction>();
33
-
34
-
48
+
35
49
  public void AddEdit(VoxelWorld world, Vector3Int position, ushort oldValue, ushort newValue, string name) {
36
50
  VoxelEditAction edit = new VoxelEditAction();
37
- edit.Initialize(world, position, oldValue, newValue);
51
+ edit.CreateSingleEdit(world, position, oldValue, newValue);
38
52
  edits.Add(edit);
39
53
 
40
54
  //If we're adding a new edit, clear the redos
@@ -52,9 +66,33 @@ public class VoxelEditManager : Singleton<VoxelEditManager> {
52
66
  world.DirtyNeighborMeshes(position);
53
67
 
54
68
  world.hasUnsavedChanges = true;
55
-
56
69
  }
57
70
 
71
+ public void AddEdits(VoxelWorld world, List<EditInfo> editInfos, string name) {
72
+ VoxelEditAction edit = new VoxelEditAction();
73
+ edit.CreateMultiEdit(world, editInfos);
74
+ edits.Add(edit);
75
+
76
+ //If we're adding a new edit, clear the redos
77
+ redos.Clear();
78
+
79
+ if (undoObject == null) {
80
+ undoObject = ScriptableObject.CreateInstance<VoxelEditMarker>();
81
+ }
82
+ undoObject.lastAction = edit;
83
+
84
+ //Save the state of this whole object into the undo system
85
+ Undo.RegisterCompleteObjectUndo(undoObject, name);
86
+
87
+ foreach (EditInfo editInfo in editInfos) {
88
+ world.WriteVoxelAtInternal(editInfo.position, editInfo.newValue);
89
+ world.DirtyNeighborMeshes(editInfo.position);
90
+ }
91
+
92
+ world.hasUnsavedChanges = true;
93
+ }
94
+
95
+
58
96
  //Constructor
59
97
  public VoxelEditManager() {
60
98
  Undo.undoRedoEvent += UndoRedoEvent;
@@ -69,8 +107,10 @@ public class VoxelEditManager : Singleton<VoxelEditManager> {
69
107
 
70
108
  edit.world.TryGetTarget(out VoxelWorld currentWorld);
71
109
  if (currentWorld){
72
- currentWorld.WriteVoxelAtInternal(edit.position, edit.oldValue);
73
- currentWorld.DirtyNeighborMeshes(edit.position);
110
+ foreach (var editInfo in edit.edits) {
111
+ currentWorld.WriteVoxelAtInternal(editInfo.position, editInfo.oldValue);
112
+ currentWorld.DirtyNeighborMeshes(editInfo.position);
113
+ }
74
114
  currentWorld.hasUnsavedChanges = true;
75
115
  }
76
116
 
@@ -83,8 +123,10 @@ public class VoxelEditManager : Singleton<VoxelEditManager> {
83
123
  redos.RemoveAt(redos.Count - 1);
84
124
  edit.world.TryGetTarget(out VoxelWorld currentWorld);
85
125
  if (currentWorld) {
86
- currentWorld.WriteVoxelAtInternal(edit.position, edit.newValue);
87
- currentWorld.DirtyNeighborMeshes(edit.position);
126
+ foreach (var editInfo in edit.edits) {
127
+ currentWorld.WriteVoxelAtInternal(editInfo.position, editInfo.newValue);
128
+ currentWorld.DirtyNeighborMeshes(editInfo.position);
129
+ }
88
130
  currentWorld.hasUnsavedChanges = true;
89
131
  }
90
132
 
@@ -380,9 +422,7 @@ public class VoxelWorldEditor : UnityEditor.Editor {
380
422
  handle.name = "_SelectionHandle";
381
423
  handle.hideFlags = HideFlags.HideAndDontSave;
382
424
  }
383
-
384
-
385
-
425
+
386
426
  if (faceHandle == null) {
387
427
  faceHandle = GameObject.CreatePrimitive(PrimitiveType.Quad);
388
428
  faceHandle.transform.localScale = new Vector3(1.01f, 1.01f, 1.01f);
@@ -396,13 +436,12 @@ public class VoxelWorldEditor : UnityEditor.Editor {
396
436
  if (Event.current.shift) { //Delete
397
437
  DestroyImmediate(faceHandle);
398
438
  faceHandle = null;
399
-
400
439
  }
401
-
402
440
 
403
441
  if (handle) {
404
442
  handle.transform.position = world.TransformPointToWorldSpace(lastPos + new Vector3(0.5f,0.5f,0.5f));
405
443
  handle.transform.localScale = new Vector3(1.01f, 1.01f, 1.01f);
444
+ handle.transform.localRotation = Quaternion.identity;
406
445
 
407
446
  WireCube wireCube = handle.GetComponent<WireCube>();
408
447
  if (wireCube) {
@@ -422,7 +461,7 @@ public class VoxelWorldEditor : UnityEditor.Editor {
422
461
 
423
462
  if (faceHandle) {
424
463
  faceHandle.transform.position = world.TransformPointToWorldSpace(lastPos + new Vector3(0.5f, 0.5f, 0.5f) + lastNormal * 0.51f);
425
- faceHandle.transform.rotation = Quaternion.LookRotation(lastNormal);
464
+ faceHandle.transform.rotation = Quaternion.LookRotation(world.TransformVectorToWorldSpace(lastNormal));
426
465
 
427
466
  MeshRenderer ren = faceHandle.GetComponent<MeshRenderer>();
428
467
  if (leftControlDown == true) {
@@ -447,7 +486,7 @@ public class VoxelWorldEditor : UnityEditor.Editor {
447
486
  Event e = Event.current;
448
487
 
449
488
  //Only allow editing if both the editor window is active and the gizmo toolbar is active
450
- bool enabled = VoxelBuilderEditorWindow.Enabled() && VoxelWorldEditorTool.buttonActive;
489
+ bool enabled = VoxelBuilderEditorWindow.Enabled() && VoxelWorldEditorToolBase.buttonActive;
451
490
 
452
491
  if (enabled != lastEnabled) {
453
492
  CleanupHandles();
@@ -575,13 +614,13 @@ public class VoxelWorldEditor : UnityEditor.Editor {
575
614
  SceneView.duringSceneGui -= GizmoRefreshEvent;
576
615
  }
577
616
 
578
- void OnSelectionChanged() {
617
+ void OnSelectionChanged() {
579
618
  if (target == null) {
580
619
  return;
581
620
  }
582
621
  //If we're seleceted
583
622
  if (Selection.activeGameObject == ((VoxelWorld)target).gameObject) {
584
- ToolManager.SetActiveTool<VoxelWorldEditorTool>();
623
+ ToolManager.SetActiveTool<VoxelWorldEditorToolBase>();
585
624
  }
586
625
 
587
626
 
@@ -591,8 +630,7 @@ public class VoxelWorldEditor : UnityEditor.Editor {
591
630
 
592
631
 
593
632
  //Create the spiffy toolbar addition
594
- [EditorTool("Edit Voxel World", typeof(VoxelWorld))]
595
- public class VoxelWorldEditorTool : EditorTool {
633
+ public class VoxelWorldEditorToolBase : EditorTool {
596
634
 
597
635
  public static bool buttonActive = false;
598
636
 
@@ -619,8 +657,8 @@ public class VoxelWorldEditorTool : EditorTool {
619
657
  }
620
658
 
621
659
 
622
- [EditorTool("Edit Voxel Selection", typeof(VoxelWorld))]
623
- public class VoxelWorldSelectionTool : EditorTool {
660
+
661
+ public class VoxelWorldSelectionToolBase : EditorTool {
624
662
 
625
663
  public static bool buttonActive = false;
626
664
 
@@ -645,4 +683,22 @@ public class VoxelWorldSelectionTool : EditorTool {
645
683
  }
646
684
  }
647
685
  }
686
+
687
+ [EditorTool("Edit Voxel World", typeof(VoxelWorld))]
688
+ public class VoxelWorldSelectionToolVW : VoxelWorldSelectionToolBase {
689
+ }
690
+
691
+ [EditorTool("Edit Voxel Selection", typeof(VoxelWorld))]
692
+ public class VoxelWorldEditorToolVW : VoxelWorldEditorToolBase {
693
+ }
694
+
695
+ //Same again for SelectionZone
696
+ [EditorTool("Edit Voxel World", typeof(SelectionZone))]
697
+ public class VoxelWorldSelectionToolSZ : VoxelWorldSelectionToolBase {
698
+ }
699
+
700
+ [EditorTool("Edit Voxel Selection", typeof(SelectionZone))]
701
+ public class VoxelWorldEditorToolSZ : VoxelWorldEditorToolBase {
702
+ }
703
+
648
704
  #endif
@@ -138,6 +138,11 @@ public partial class VoxelWorld : MonoBehaviour {
138
138
  return transform.localToWorldMatrix.MultiplyPoint(point);
139
139
  }
140
140
 
141
+ public Vector3 TransformVectorToWorldSpace(Vector3 vec) {
142
+ return transform.localToWorldMatrix.MultiplyVector(vec);
143
+ }
144
+
145
+
141
146
  public void InvokeOnFinishedReplicatingChunksFromServer() {
142
147
  this.finishedReplicatingChunksFromServer = true;
143
148
  this.OnFinishedReplicatingChunksFromServer?.Invoke();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gg.easy.airship",
3
- "version": "0.1.1662",
3
+ "version": "0.1.1663",
4
4
  "displayName": "Airship",
5
5
  "unity": "2021.3",
6
6
  "unityRelease": "12f1",