Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for DrawShape tool not always respecting pivot location #575

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 44 additions & 14 deletions Editor/EditorCore/DrawShapeTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,28 @@ public PivotLocation pivotLocation
}
}

internal Vector3 m_LastNonDuplicateCenterToOrigin;
internal Quaternion m_LastNonDuplicateRotation;
internal Vector3 previewPivotPosition
{
get
{
if (pivotLocation == PivotLocation.FirstVertex && instance != null)
{
var lastCenterToOrigin = instance.m_LastNonDuplicateCenterToOrigin;
var lastCenterToOriginNorm = lastCenterToOrigin.normalized;

var deltaRot = Quaternion.Inverse(instance.m_LastNonDuplicateRotation) * instance.m_PlaneRotation;
lastCenterToOriginNorm = deltaRot * lastCenterToOriginNorm;

var pivotOffset = lastCenterToOriginNorm * lastCenterToOrigin.magnitude;
return pivotOffset + instance.m_Bounds.center;
}

return m_Bounds.center;
}
}

int m_ControlID;

internal float minSnapSize
Expand Down Expand Up @@ -656,16 +678,17 @@ internal void DoDuplicateShapePreviewHandle(Vector3 position)

var size = currentShapeInOverlay.size;
m_Bounds.size = size;

position = GetPoint(position);
var cornerPosition = position - size / 2f;
cornerPosition.y = position.y;

m_Bounds.center = cornerPosition + new Vector3(size.x / 2f, 0, size.z / 2f) + (size.y / 2f) * m_Plane.normal;
m_PlaneRotation = Quaternion.LookRotation(m_PlaneForward, m_Plane.normal);

m_BB_Origin = m_Bounds.center - m_PlaneRotation * (size / 2f);
m_BB_HeightCorner = m_Bounds.center + m_PlaneRotation * (size / 2f);
m_BB_OppositeCorner = m_BB_HeightCorner - m_PlaneRotation * new Vector3(0, size.y, 0);
var preview_BB_Origin = m_Bounds.center - m_PlaneRotation * (size / 2f);
var preview_BB_HeightCorner = m_Bounds.center + m_PlaneRotation * (size / 2f);
var preview_BB_OppositeCorner = preview_BB_HeightCorner - m_PlaneRotation * new Vector3(0, size.y, 0);


if (m_DuplicateGO == null)
{
Expand All @@ -677,7 +700,7 @@ internal void DoDuplicateShapePreviewHandle(Vector3 position)
shape.GetComponent<MeshRenderer>().sharedMaterial = BuiltinMaterials.ShapePreviewMaterial;

EditorShapeUtility.CopyLastParams(shape.shape, shape.shape.GetType());
shape.Rebuild(m_Bounds, m_PlaneRotation);
shape.Rebuild(previewPivotPosition, m_PlaneRotation, m_Bounds);
ProBuilderEditor.Refresh(false);
}
else if (m_DuplicateGO.TryGetComponent<MeshRenderer>(out var renderer) && !renderer.enabled)
Expand All @@ -687,16 +710,18 @@ internal void DoDuplicateShapePreviewHandle(Vector3 position)

ApplyPrefsSettings(shape);
EditorShapeUtility.CopyLastParams(shape.shape, shape.shape.GetType());
shape.Rebuild(m_Bounds, m_PlaneRotation);
shape.Rebuild(previewPivotPosition, m_PlaneRotation, m_Bounds);
ProBuilderEditor.Refresh(false);
}

var pivot = GetPoint(position);
pivot += .5f * size.y * m_Plane.normal;

if (pivotLocation == PivotLocation.Center)
pivot += .5f * size.y * m_Plane.normal;
else
pivot = previewPivotPosition;
m_DuplicateGO.transform.SetPositionAndRotation(pivot, Quaternion.LookRotation(m_PlaneForward, m_Plane.normal));

DrawBoundingBox(false);
DrawBoundingBox(preview_BB_Origin, preview_BB_HeightCorner, preview_BB_OppositeCorner, false);
}

/// <summary>
Expand Down Expand Up @@ -785,7 +810,7 @@ public override void OnToolGUI(EditorWindow window)
m_CurrentState = m_CurrentState.DoState(evt);
}

internal void DrawBoundingBox(bool drawCorners = true)
internal void DrawBoundingBox(Vector3 origin, Vector3 oppositeCorner, Vector3 heightCorner, bool drawCorners = true)
{
using (new Handles.DrawingScope(k_BoundsColor, Matrix4x4.TRS(m_Bounds.center, m_PlaneRotation.normalized, Vector3.one)))
{
Expand All @@ -797,14 +822,19 @@ internal void DrawBoundingBox(bool drawCorners = true)

using (new Handles.DrawingScope(Color.white))
{
Handles.DotHandleCap(-1, m_BB_Origin, Quaternion.identity, HandleUtility.GetHandleSize(m_BB_Origin) * 0.05f, EventType.Repaint);
Handles.DotHandleCap(-1, m_BB_OppositeCorner, Quaternion.identity, HandleUtility.GetHandleSize(m_BB_OppositeCorner) * 0.05f, EventType.Repaint);
Handles.DotHandleCap(-1, origin, Quaternion.identity, HandleUtility.GetHandleSize(origin) * 0.05f, EventType.Repaint);
Handles.DotHandleCap(-1, oppositeCorner, Quaternion.identity, HandleUtility.GetHandleSize(oppositeCorner) * 0.05f, EventType.Repaint);
}
using (new Handles.DrawingScope(EditorHandleDrawing.vertexSelectedColor))
{
Handles.DotHandleCap(-1, m_BB_HeightCorner, Quaternion.identity, HandleUtility.GetHandleSize(m_BB_HeightCorner) * 0.05f, EventType.Repaint);
Handles.DotHandleCap(-1, heightCorner, Quaternion.identity, HandleUtility.GetHandleSize(heightCorner) * 0.05f, EventType.Repaint);
}
}

internal void DrawBoundingBox(bool drawCorners = true)
{
DrawBoundingBox(m_BB_Origin, m_BB_OppositeCorner, m_BB_HeightCorner, drawCorners);
}

void OnOverlayGUI(UObject overlayTarget, SceneView view)
{
Expand Down
9 changes: 6 additions & 3 deletions Editor/StateMachines/ShapeState_DrawBaseShape.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,17 @@ public override ShapeState DoState(Event evt)
case EventType.MouseUp:
if(evt.button == 0)
{
if(!m_IsDragging && evt.shift)
if (!m_IsDragging && evt.shift)
{
CreateLastShape();
return ResetState();
}

if(Vector3.Distance(tool.m_BB_OppositeCorner, tool.m_BB_Origin) <= Mathf.Min(0.01f, tool.minSnapSize))
return ResetState();

tool.m_LastNonDuplicateCenterToOrigin = (tool.m_BB_Origin - tool.m_BB_OppositeCorner) * 0.5f;
tool.m_LastNonDuplicateRotation = tool.m_PlaneRotation;

return NextState();
}
Expand Down Expand Up @@ -96,9 +99,9 @@ public void CreateLastShape()
UndoUtility.RegisterCreatedObjectUndo(shape.gameObject, $"Create Shape");
EditorUtility.InitObject(shape.mesh);
tool.ApplyPrefsSettings(shape);

EditorShapeUtility.CopyLastParams(shape.shape, shape.shape.GetType());
shape.Rebuild(tool.m_Bounds, tool.m_PlaneRotation);
shape.Rebuild(tool.previewPivotPosition, tool.m_PlaneRotation, tool.m_Bounds);

//Finish initializing object and collider once it's completed
ProBuilderEditor.Refresh(false);
Expand Down
3 changes: 3 additions & 0 deletions Editor/StateMachines/ShapeState_DrawHeightShape.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ public override ShapeState DoState(Event evt)
break;

case EventType.MouseUp:
{
tool.m_LastNonDuplicateCenterToOrigin = (tool.m_BB_Origin - tool.m_BB_HeightCorner) * 0.5f;
return ValidateShape();
}
}
}

Expand Down