Skip to content

Commit

Permalink
Keep track of the nearest face when extruding and use that to determi…
Browse files Browse the repository at this point in the history
…ne the extrude direction (#44)
  • Loading branch information
andybak authored Sep 2, 2024
1 parent 5a71ca3 commit 8b04f03
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
27 changes: 24 additions & 3 deletions Assets/Scripts/tools/Extruder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ public class Extruder : MonoBehaviour
/// </summary>
private bool isSnapping = false;

private Face nearestFace;

/// <summary>
/// Every tool is implemented as MonoBehaviour, which means it may do no work in its constructor.
/// As such, this setup method must be called before the tool is used for it to have a valid state.
Expand All @@ -106,7 +108,7 @@ public void Setup(Model model, ControllerMain controllerMain, PeltzerController
controllerMain.ControllerActionHandler += ControllerEventHandler;
}

private ExtrusionOperation.ExtrusionParams BuildExtrusionParams()
private ExtrusionOperation.ExtrusionParams BuildExtrusionParams(bool flipped)
{
// Note: ExtrusionParams is a struct, so this is stack allocated and doesn't generate garbage.
ExtrusionOperation.ExtrusionParams extrusionParams = new ExtrusionOperation.ExtrusionParams();
Expand All @@ -116,6 +118,7 @@ private ExtrusionOperation.ExtrusionParams BuildExtrusionParams()
extrusionParams.rotationPivotModel = peltzerController.LastPositionModel;
extrusionParams.rotationModel =
peltzerController.LastRotationModel * Quaternion.Inverse(extrusionBeginOrientation);
extrusionParams.flipped = flipped;
return extrusionParams;
}

Expand Down Expand Up @@ -172,7 +175,14 @@ private void Update()
triggerUpToRelease = true;
}

ExtrusionOperation.ExtrusionParams extrusionParams = BuildExtrusionParams();
bool towards = true;
if (nearestFace != null)
{
var faceVector = nearestFace.normal;
var dragVector = peltzerController.LastPositionModel - extrusionBeginPosition;
towards = Vector3.Dot(faceVector, dragVector) > 0;
}
ExtrusionOperation.ExtrusionParams extrusionParams = BuildExtrusionParams(!towards);
foreach (ExtrusionOperation operation in extrusions)
{
operation.UpdateExtrudeGuide(extrusionParams);
Expand Down Expand Up @@ -397,7 +407,15 @@ private void ReleaseMesh()
List<Command> commands = new List<Command>();
Dictionary<int, MMesh> modifiedMeshes = new Dictionary<int, MMesh>();
Dictionary<int, HashSet<Vertex>> newVerticesInModifiedMeshes = new Dictionary<int, HashSet<Vertex>>();
ExtrusionOperation.ExtrusionParams extrusionParams = BuildExtrusionParams();

bool towards = true;
if (nearestFace != null)
{
var faceVector = nearestFace.normal;
var dragVector = peltzerController.LastPositionModel - extrusionBeginPosition;
towards = Vector3.Dot(faceVector, dragVector) > 0;
}
ExtrusionOperation.ExtrusionParams extrusionParams = BuildExtrusionParams(!towards);

foreach (ExtrusionOperation extrusion in extrusions)
{
Expand Down Expand Up @@ -473,6 +491,9 @@ private void ControllerEventHandler(object sender, ControllerEventArgs args)
return;
}
}

nearestFace = selector.GetNearestFace(peltzerController.LastPositionModel);

triggerUpToRelease = false;
waitingToDetermineReleaseType = true;
triggerDownTime = Time.time;
Expand Down
3 changes: 2 additions & 1 deletion Assets/Scripts/tools/ExtrusionOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public struct ExtrusionParams
// Indicates the point about which the extruded face should be rotated, AFTER the translation.
// This is in MODEL space.
public Vector3 rotationPivotModel;
public bool flipped;
}

/// <summary>
Expand Down Expand Up @@ -476,7 +477,7 @@ public static List<ExtrusionSideVertices> BuildExtrusionSides(
if (extrusionParams.lockToNormal)
{
var extrudedPoint = GridUtils.SnapToGrid(extrusionParams.translationModel);
projectedDelta = mesh.rotation * face.normal * extrudedPoint.magnitude;
projectedDelta = mesh.rotation * face.normal * (extrudedPoint.magnitude * (extrusionParams.flipped ? -1 : 1));
}
else
{
Expand Down
15 changes: 15 additions & 0 deletions Assets/Scripts/tools/Selector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1563,5 +1563,20 @@ public void AddSelectedMeshForTest(int meshId)
{
selectedMeshes.Add(meshId);
}

public Face GetNearestFace(Vector3 pos)
{
float selectionThreshold = faceClosenessThreshold * INCREASED_SELECTION_FACTOR;
spatialIndex.FindFacesClosestTo(pos, selectionThreshold,
false, out List<DistancePair<FaceKey>> temp);
if (temp.Count == 0)
{
return null;
}
var facekey = temp[0].value;
var mesh = model.GetMesh(facekey.meshId);
var face = mesh.GetFace(facekey.faceId);
return face;
}
}
}

0 comments on commit 8b04f03

Please sign in to comment.