Skip to content

Commit

Permalink
Use the original algorithm when extruding a single face (#48)
Browse files Browse the repository at this point in the history
* Use the old algorithm when we're only extruding a single face

* Remove logging
  • Loading branch information
andybak authored Sep 6, 2024
1 parent ba81beb commit b69b239
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
15 changes: 7 additions & 8 deletions Assets/Scripts/tools/Extruder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@
using com.google.apps.peltzer.client.model.controller;
using com.google.apps.peltzer.client.model.core;
using com.google.apps.peltzer.client.model.main;
using com.google.apps.peltzer.client.model.util;
using com.google.apps.peltzer.client.model.render;
using com.google.apps.peltzer.client.tools.utils;

namespace com.google.apps.peltzer.client.tools
{
Expand All @@ -30,7 +28,7 @@ namespace com.google.apps.peltzer.client.tools
/// Update() loop.
/// An extrusion is composed of grabbing a face, (potentially) pulling out the face, and releasing. The face
/// itself is moved, and new faces are created to link the moved face back to the mesh.
///
///
/// </summary>
public class Extruder : MonoBehaviour
{
Expand Down Expand Up @@ -72,7 +70,7 @@ public class Extruder : MonoBehaviour

private WorldSpace worldSpace;

// Detection for trigger down & straight back up, vs trigger down and hold -- either of which
// Detection for trigger down & straight back up, vs trigger down and hold -- either of which
// begins an extrusion.
private bool triggerUpToRelease;
private float triggerDownTime;
Expand Down Expand Up @@ -108,7 +106,7 @@ public void Setup(Model model, ControllerMain controllerMain, PeltzerController
controllerMain.ControllerActionHandler += ControllerEventHandler;
}

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

Expand Down Expand Up @@ -182,7 +181,7 @@ private void Update()
var dragVector = peltzerController.LastPositionModel - extrusionBeginPosition;
towards = Vector3.Dot(faceVector, dragVector) > 0;
}
ExtrusionOperation.ExtrusionParams extrusionParams = BuildExtrusionParams(!towards);
ExtrusionOperation.ExtrusionParams extrusionParams = BuildExtrusionParams(!towards, extrusions.Count > 1);
foreach (ExtrusionOperation operation in extrusions)
{
operation.UpdateExtrudeGuide(extrusionParams);
Expand Down Expand Up @@ -415,7 +414,7 @@ private void ReleaseMesh()
var dragVector = peltzerController.LastPositionModel - extrusionBeginPosition;
towards = Vector3.Dot(faceVector, dragVector) > 0;
}
ExtrusionOperation.ExtrusionParams extrusionParams = BuildExtrusionParams(!towards);
ExtrusionOperation.ExtrusionParams extrusionParams = BuildExtrusionParams(!towards, extrusions.Count > 1);

foreach (ExtrusionOperation extrusion in extrusions)
{
Expand Down Expand Up @@ -464,7 +463,7 @@ private void ReleaseMesh()
}
}

// Expire the temporary held face commands, because the originally-held faces have now been removed
// Expire the temporary held face commands, because the originally-held faces have now been removed
// following a successful extrusion.
temporaryHeldFaceMaterialCommands.Clear();
model.ApplyCommand(new CompositeCommand(commands));
Expand Down
19 changes: 17 additions & 2 deletions Assets/Scripts/tools/ExtrusionOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public struct ExtrusionParams
// This is in MODEL space.
public Vector3 rotationPivotModel;
public bool flipped;
public bool multipleFaces;
}

/// <summary>
Expand Down Expand Up @@ -476,8 +477,22 @@ public static List<ExtrusionSideVertices> BuildExtrusionSides(
Vector3 projectedDelta;
if (extrusionParams.lockToNormal)
{
var extrudedPoint = GridUtils.SnapToGrid(extrusionParams.translationModel);
projectedDelta = mesh.rotation * face.normal * (extrudedPoint.magnitude * (extrusionParams.flipped ? -1 : 1));
if (extrusionParams.multipleFaces)
{
var extrudedPoint = GridUtils.SnapToGrid(extrusionParams.translationModel);
projectedDelta = mesh.rotation * face.normal * (extrudedPoint.magnitude * (extrusionParams.flipped ? -1 : 1));
}
else
{
List<Vector3> coplanar = new List<Vector3> {
mesh.VertexPositionInModelCoords(face.vertexIds[0]),
mesh.VertexPositionInModelCoords(face.vertexIds[1]),
mesh.VertexPositionInModelCoords(face.vertexIds[2])
};
Vector3 normal = MeshMath.CalculateNormal(coplanar);
projectedDelta =
Vector3.Project(GridUtils.SnapToGrid(extrusionParams.translationModel), normal);
}
}
else
{
Expand Down

0 comments on commit b69b239

Please sign in to comment.