Skip to content

Commit 072f32e

Browse files
authored
A brush with an invalid shape keeps its handles (#66)
The editable outline only needs whole topology, not a valid shape: a brush dragged into a concave or flat shape keeps its handles, drawn in the invalid color, and can be dragged back. A brush's shape errors aren't logged any more (they come with every drag while the shape is invalid); the inspector shows them.
1 parent d9bf2d9 commit 072f32e

4 files changed

Lines changed: 63 additions & 3 deletions

File tree

‎Core/1.Input/Generators/Brush/ChiselBrushDefinition.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,10 @@ public bool Validate()
173173
if (brushOutline.planes == null || brushOutline.planes.Length != brushOutline.polygons.Length)
174174
brushOutline.CalculatePlanes();
175175

176-
// If the brush is concave, we set the generator to not be valid, so that when we commit, it will be reverted
176+
// If the brush is concave, we set the generator to not be valid, so that when we commit, it will be reverted.
177+
// Not logged: it happens while a shape is dragged, and the inspector shows why (GetMessages)
177178
if (!brushOutline.ValidateShape(out errorMessage))
178179
{
179-
Debug.LogError(errorMessage);
180180
validState = false;
181181
return false;
182182
}

‎Editor/ComponentEditors/Base/ChiselEditableOutline.cs‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,14 +508,20 @@ public void Rebuild()
508508
UpdateSelectionCenter();
509509
}
510510

511+
// Whether the outline can be edited: whole topology only, so a brush whose shape is invalid (concave, flat) keeps
512+
// its handles and can be dragged back
511513
bool IsValidBrush()
512514
{
513515
if (brushMesh == null ||
514516
brushMesh.vertices == null || brushMesh.vertices.Length == 0 ||
515517
brushMesh.halfEdges == null || brushMesh.halfEdges.Length == 0 ||
516518
brushMesh.polygons == null || brushMesh.polygons.Length == 0)
517519
return false;
518-
return brushMesh.Validate();
520+
if (!brushMesh.ValidateData(out _))
521+
return false;
522+
if (brushMesh.planes == null || brushMesh.planes.Length != brushMesh.polygons.Length)
523+
brushMesh.CalculatePlanes();
524+
return true;
519525
}
520526

521527

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using Chisel.Components;
2+
using Chisel.Core;
3+
using NUnit.Framework;
4+
using UnityEngine;
5+
6+
namespace Chisel.Editors.Tests
7+
{
8+
// A brush being edited keeps its handles when its shape is invalid (concave, flat), so it can be dragged back
9+
public sealed class ChiselEditableOutlineTests
10+
{
11+
GameObject root;
12+
13+
[TearDown]
14+
public void TearDown()
15+
{
16+
if (root)
17+
Object.DestroyImmediate(root);
18+
}
19+
20+
// A box with its top corner pushed in past its middle: concave
21+
static BrushMesh ConcaveBox()
22+
{
23+
BrushMeshFactory.CreateBox(new Vector3(2, 2, 2), 0, out var box);
24+
var corner = 0;
25+
for (int v = 1; v < box.vertices.Length; v++)
26+
{
27+
if (box.vertices[v].x + box.vertices[v].y + box.vertices[v].z > box.vertices[corner].x + box.vertices[corner].y + box.vertices[corner].z)
28+
corner = v;
29+
}
30+
box.vertices[corner] = new Unity.Mathematics.float3(-0.25f, -0.25f, -0.25f);
31+
box.CalculatePlanes();
32+
return box;
33+
}
34+
35+
[Test]
36+
public void AConcaveBrush_KeepsItsHandles()
37+
{
38+
var outline = ConcaveBox();
39+
Assert.That(outline.ValidateShape(out _), Is.False, "the brush isn't concave");
40+
41+
root = new GameObject("Brush");
42+
var brush = root.AddComponent<ChiselBrushComponent>();
43+
brush.definition.BrushOutline = outline;
44+
brush.OnValidate();
45+
Assert.That(brush.definition.IsValid, Is.False, "an invalid brush is drawn as it is");
46+
47+
var editable = new ChiselEditableOutline(brush);
48+
Assert.That(editable.polygonCenters, Is.Not.Null, "no handles");
49+
Assert.That(editable.polygonCenters.Length, Is.EqualTo(outline.polygons.Length));
50+
}
51+
}
52+
}

‎Editor/Tests/ChiselEditableOutlineTests.cs.meta‎

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)