Files
UnrealEngineUWP/Engine/Plugins/Editor/MeshEditor/Source/PolygonModeling/EditVertexOrEdgeSharpness.cpp

198 lines
7.5 KiB
C++
Raw Normal View History

// Copyright Epic Games, Inc. All Rights Reserved.
#include "EditVertexOrEdgeSharpness.h"
#include "IMeshEditorModeEditingContract.h"
#include "IMeshEditorModeUIContract.h"
#include "Framework/Commands/UICommandInfo.h"
#include "EditableMesh.h"
#include "StaticMeshAttributes.h"
#include "MeshElement.h"
#include "Framework/MultiBox/MultiBoxBuilder.h"
#include "Framework/Commands/UICommandList.h"
#include "ViewportInteractor.h"
#define LOCTEXT_NAMESPACE "MeshEditorMode"
namespace VertexOrEdgeSharpnessHelpers
{
/** Where the active interactor's impact point was when the "edit sharpness" action started */
FVector EditSharpnessStartLocation( FVector::ZeroVector );
// @todo mesheditor extensibility: Get rid of all of the static stuff ideally and CDOs with state. Have MeshEditorMode construct instances of commands. Don't use TObjectIterator except at startup.
/** Figures out how much we should change the sharpness amount by looking at the interactor aim delta */
static float ComputeSharpnessChangeDelta( IMeshEditorModeEditingContract& MeshEditorMode, UViewportInteractor* ViewportInteractor )
{
// @todo mesheditor subdiv: Hard coded tweakables; ideally should be sized in screen space
const float DragScaleFactor = 5.0f;
const float ProgressBarHeight = 1000.0f;
// Figure out how much to either increase or decrease sharpness based on how far the user has dragged up or down in world space.
float DragDeltaZ = 0.0f;
FVector LaserStart, LaserEnd;
const bool bLaserIsValid = ViewportInteractor->GetLaserPointer( /* Out */ LaserStart, /* Out */ LaserEnd );
FSphere GrabberSphere;
const bool bGrabberSphereIsValid = ViewportInteractor->GetGrabberSphere( /* Out */ GrabberSphere );
if( bLaserIsValid || bGrabberSphereIsValid )
{
const FVector ProgressBarStart( EditSharpnessStartLocation + FVector( 0.0f, 0.0f, -ProgressBarHeight * 0.5f ) );
const FVector ProgressBarEnd( EditSharpnessStartLocation + FVector( 0.0f, 0.0f, ProgressBarHeight * 0.5f ) );
// First check to see if we're within grabber sphere range. Otherwise, we'll use the laser.
bool bIsInRange = false;
FVector ClosestPointOnProgressBar;
if( bGrabberSphereIsValid )
{
if( FMath::PointDistToSegment( GrabberSphere.Center, ProgressBarStart, ProgressBarEnd ) <= GrabberSphere.W )
{
bIsInRange = true;
ClosestPointOnProgressBar = FMath::ClosestPointOnSegment(
GrabberSphere.Center,
ProgressBarStart, ProgressBarEnd );
}
}
if( !bIsInRange && ensure( bLaserIsValid ) )
{
bIsInRange = true;
FVector ClosestPointOnRay;
FMath::SegmentDistToSegment(
ProgressBarStart, ProgressBarEnd,
LaserStart, LaserEnd,
/* Out */ ClosestPointOnProgressBar,
/* Out */ ClosestPointOnRay );
}
if( bIsInRange )
{
// Generate a drag value between -1.0 and 1.0 based on the interaction of the ray and the progress bar line segment.
const float ProgressBarLength = ( ProgressBarEnd - ProgressBarStart ).Size();
DragDeltaZ = -1.0f + 2.0f * ( ( ClosestPointOnProgressBar - ProgressBarStart ).Size() / ProgressBarLength );
}
}
const float ScaledDragDelta = DragDeltaZ * DragScaleFactor;
return ScaledDragDelta;
}
}
void UEditVertexCornerSharpnessCommand::RegisterUICommand( FBindingContext* BindingContext )
{
UI_COMMAND_EXT( BindingContext, /* Out */ UICommandInfo, "EditVertexCornerSharpness", "Edit Corner Sharpness", "Change the subdivision vertex corner sharpness of a vertex by clicking and dragging up and down.", EUserInterfaceActionType::RadioButton, FInputChord() );
}
bool UEditVertexCornerSharpnessCommand::TryStartingToDrag( IMeshEditorModeEditingContract& MeshEditorMode, UViewportInteractor* ViewportInteractor )
{
VertexOrEdgeSharpnessHelpers::EditSharpnessStartLocation = ViewportInteractor->GetHoverLocation();
return true;
}
void UEditVertexCornerSharpnessCommand::ApplyDuringDrag( IMeshEditorModeEditingContract& MeshEditorMode, UViewportInteractor* ViewportInteractor )
{
static TMap< UEditableMesh*, TArray<FMeshElement> > MeshesWithSelectedVertices;
MeshEditorMode.GetSelectedMeshesAndVertices( /* Out */ MeshesWithSelectedVertices );
if( MeshesWithSelectedVertices.Num() > 0 )
{
const float ScaledDragDelta = VertexOrEdgeSharpnessHelpers::ComputeSharpnessChangeDelta( MeshEditorMode, ViewportInteractor );
for( auto& MeshAndSelectedVertices : MeshesWithSelectedVertices )
{
UEditableMesh* EditableMesh = MeshAndSelectedVertices.Key;
const TArray<FMeshElement>& VertexElements = MeshAndSelectedVertices.Value;
static TArray<FVertexID> VertexIDs;
VertexIDs.Reset();
static TArray<float> NewSharpnessValues;
NewSharpnessValues.Reset();
New attribute array API. Fixed some flaws in the original API, deprecated various methods, and introduced some new features. - Now attribute arrays are accessed via TAttributesRef or TAttributesView (and corresponding const versions). These value types hold references to attribute arrays, and individual elements can be accessed by their element ID and attribute index. Using a value type is safer than the previous method which required assignment to a const-ref (and not doing so would take a temporary copy of the attribute array). - The attribute set has been totally flattened, so all attributes of different types are added to the same container. This greatly improves compile times, prevents attributes from being created with the same name but different types, and permits the view feature. - The class hierarchy has changed to have generic base classes where possible with no particular ElementID type. This reduces the code footprint by no longer generating nearly identical copies of templated methods. - A TAttributesView allows the user to access an attribute array by the type of their choosing, regardless of its actual type. For example, the Normal attribute may be registered with type FPackedVector, but accessed as if it was an FVector. This allows us to move away from very strong typing, and instead transparently store attributes of a more efficient size, while the user is not affected. - A transient attribute flag has been added, to denote that a particular attribute should not be saved. #rb none [CL 4226081 by Richard TalbotWatkin in Dev-Editor branch]
2018-07-20 18:58:37 -04:00
const TVertexAttributesRef<float> VertexSharpnesses = EditableMesh->GetMeshDescription()->VertexAttributes().GetAttributesRef<float>( MeshAttribute::Vertex::CornerSharpness );
Total revamp of mesh element attribute model. Attributes now have a number of possible types (FVector, FVector4, FVector2D, float, int, bool, FName, UObject*) and are exposed as individual flat arrays, indexed by element ID. For example, vertex positions are essentially exposed as an array of FVector which can be directly accessed and modified. This has a number of advantages: - It is completely extensible: new attributes can be created (even by a third party) and added to a mesh description without requiring a serialization version bump, or any change to the parent structures. - This is more efficient in batch operations which deal with a number of mesh elements in one go. - These attribute buffers can potentially be passed directly to third-party libraries without requiring any kind of transformation. - The distinct types allow for a better representation of the attribute being specified, without invalid values being possible (cf representing a bool value in an FVector4). Attributes also have default values, and a flags field which confers use-specific properties to them. Editable Mesh currently uses this to determine whether an attribute's value can be automatically initialized by lerping the values of its neighbours, as well as for identifying auto-generated attributes such as tangents/normals. This is desirable as it means that even unknown / third-party attributes can potentially be handled transparently by Editable Mesh, without requiring the code to be extended. Certain higher-level operations in EditableMesh have been optimized to make full use of vertex instances where possible. The welding/splitting of identical vertex instances has been removed from here, as the aim is to unify this with mesh utility code elsewhere. Various bug fixes. #rb Alexis.Matte [CL 3794563 by Richard TalbotWatkin in Dev-Geometry branch]
2017-12-07 13:02:12 -05:00
for( const FMeshElement& VertexElement : VertexElements )
{
const FVertexID VertexID( VertexElement.ElementAddress.ElementID );
Total revamp of mesh element attribute model. Attributes now have a number of possible types (FVector, FVector4, FVector2D, float, int, bool, FName, UObject*) and are exposed as individual flat arrays, indexed by element ID. For example, vertex positions are essentially exposed as an array of FVector which can be directly accessed and modified. This has a number of advantages: - It is completely extensible: new attributes can be created (even by a third party) and added to a mesh description without requiring a serialization version bump, or any change to the parent structures. - This is more efficient in batch operations which deal with a number of mesh elements in one go. - These attribute buffers can potentially be passed directly to third-party libraries without requiring any kind of transformation. - The distinct types allow for a better representation of the attribute being specified, without invalid values being possible (cf representing a bool value in an FVector4). Attributes also have default values, and a flags field which confers use-specific properties to them. Editable Mesh currently uses this to determine whether an attribute's value can be automatically initialized by lerping the values of its neighbours, as well as for identifying auto-generated attributes such as tangents/normals. This is desirable as it means that even unknown / third-party attributes can potentially be handled transparently by Editable Mesh, without requiring the code to be extended. Certain higher-level operations in EditableMesh have been optimized to make full use of vertex instances where possible. The welding/splitting of identical vertex instances has been removed from here, as the aim is to unify this with mesh utility code elsewhere. Various bug fixes. #rb Alexis.Matte [CL 3794563 by Richard TalbotWatkin in Dev-Geometry branch]
2017-12-07 13:02:12 -05:00
const float CurrentSharpnessValue = VertexSharpnesses[ VertexID ];
const float NewSharpnessValue = FMath::Clamp( CurrentSharpnessValue + ScaledDragDelta, 0.0f, 1.0f );
VertexIDs.Add( VertexID );
NewSharpnessValues.Add( NewSharpnessValue );
}
verify( !EditableMesh->AnyChangesToUndo() );
EditableMesh->SetVerticesCornerSharpness( VertexIDs, NewSharpnessValues );
MeshEditorMode.TrackUndo( EditableMesh, EditableMesh->MakeUndo() );
}
}
}
void UEditEdgeCreaseSharpnessCommand::RegisterUICommand( FBindingContext* BindingContext )
{
UI_COMMAND_EXT( BindingContext, /* Out */ UICommandInfo, "EditEdgeCreaseSharpness", "Edit Crease Sharpness", "Change the subdivision edge crase sharpness of an edge by clicking and dragging up and down.", EUserInterfaceActionType::RadioButton, FInputChord() );
}
bool UEditEdgeCreaseSharpnessCommand::TryStartingToDrag( IMeshEditorModeEditingContract& MeshEditorMode, UViewportInteractor* ViewportInteractor )
{
VertexOrEdgeSharpnessHelpers::EditSharpnessStartLocation = ViewportInteractor->GetHoverLocation();
return true;
}
void UEditEdgeCreaseSharpnessCommand::ApplyDuringDrag( IMeshEditorModeEditingContract& MeshEditorMode, UViewportInteractor* ViewportInteractor )
{
static TMap< UEditableMesh*, TArray<FMeshElement> > MeshesWithSelectedEdges;
MeshEditorMode.GetSelectedMeshesAndEdges( /* Out */ MeshesWithSelectedEdges );
if( MeshesWithSelectedEdges.Num() > 0 )
{
const float ScaledDragDelta = VertexOrEdgeSharpnessHelpers::ComputeSharpnessChangeDelta( MeshEditorMode, ViewportInteractor );
for( auto& MeshAndSelectedEdges : MeshesWithSelectedEdges )
{
UEditableMesh* EditableMesh = MeshAndSelectedEdges.Key;
const TArray<FMeshElement>& EdgeElements = MeshAndSelectedEdges.Value;
New attribute array API. Fixed some flaws in the original API, deprecated various methods, and introduced some new features. - Now attribute arrays are accessed via TAttributesRef or TAttributesView (and corresponding const versions). These value types hold references to attribute arrays, and individual elements can be accessed by their element ID and attribute index. Using a value type is safer than the previous method which required assignment to a const-ref (and not doing so would take a temporary copy of the attribute array). - The attribute set has been totally flattened, so all attributes of different types are added to the same container. This greatly improves compile times, prevents attributes from being created with the same name but different types, and permits the view feature. - The class hierarchy has changed to have generic base classes where possible with no particular ElementID type. This reduces the code footprint by no longer generating nearly identical copies of templated methods. - A TAttributesView allows the user to access an attribute array by the type of their choosing, regardless of its actual type. For example, the Normal attribute may be registered with type FPackedVector, but accessed as if it was an FVector. This allows us to move away from very strong typing, and instead transparently store attributes of a more efficient size, while the user is not affected. - A transient attribute flag has been added, to denote that a particular attribute should not be saved. #rb none [CL 4226081 by Richard TalbotWatkin in Dev-Editor branch]
2018-07-20 18:58:37 -04:00
const TEdgeAttributesRef<float> EdgeSharpnesses = EditableMesh->GetMeshDescription()->EdgeAttributes().GetAttributesRef<float>( MeshAttribute::Edge::CreaseSharpness );
Total revamp of mesh element attribute model. Attributes now have a number of possible types (FVector, FVector4, FVector2D, float, int, bool, FName, UObject*) and are exposed as individual flat arrays, indexed by element ID. For example, vertex positions are essentially exposed as an array of FVector which can be directly accessed and modified. This has a number of advantages: - It is completely extensible: new attributes can be created (even by a third party) and added to a mesh description without requiring a serialization version bump, or any change to the parent structures. - This is more efficient in batch operations which deal with a number of mesh elements in one go. - These attribute buffers can potentially be passed directly to third-party libraries without requiring any kind of transformation. - The distinct types allow for a better representation of the attribute being specified, without invalid values being possible (cf representing a bool value in an FVector4). Attributes also have default values, and a flags field which confers use-specific properties to them. Editable Mesh currently uses this to determine whether an attribute's value can be automatically initialized by lerping the values of its neighbours, as well as for identifying auto-generated attributes such as tangents/normals. This is desirable as it means that even unknown / third-party attributes can potentially be handled transparently by Editable Mesh, without requiring the code to be extended. Certain higher-level operations in EditableMesh have been optimized to make full use of vertex instances where possible. The welding/splitting of identical vertex instances has been removed from here, as the aim is to unify this with mesh utility code elsewhere. Various bug fixes. #rb Alexis.Matte [CL 3794563 by Richard TalbotWatkin in Dev-Geometry branch]
2017-12-07 13:02:12 -05:00
static TArray<FEdgeID> EdgeIDs;
EdgeIDs.Reset();
static TArray<float> NewSharpnessValues;
NewSharpnessValues.Reset();
for( const FMeshElement& EdgeElement : EdgeElements )
{
const FEdgeID EdgeID( EdgeElement.ElementAddress.ElementID );
Total revamp of mesh element attribute model. Attributes now have a number of possible types (FVector, FVector4, FVector2D, float, int, bool, FName, UObject*) and are exposed as individual flat arrays, indexed by element ID. For example, vertex positions are essentially exposed as an array of FVector which can be directly accessed and modified. This has a number of advantages: - It is completely extensible: new attributes can be created (even by a third party) and added to a mesh description without requiring a serialization version bump, or any change to the parent structures. - This is more efficient in batch operations which deal with a number of mesh elements in one go. - These attribute buffers can potentially be passed directly to third-party libraries without requiring any kind of transformation. - The distinct types allow for a better representation of the attribute being specified, without invalid values being possible (cf representing a bool value in an FVector4). Attributes also have default values, and a flags field which confers use-specific properties to them. Editable Mesh currently uses this to determine whether an attribute's value can be automatically initialized by lerping the values of its neighbours, as well as for identifying auto-generated attributes such as tangents/normals. This is desirable as it means that even unknown / third-party attributes can potentially be handled transparently by Editable Mesh, without requiring the code to be extended. Certain higher-level operations in EditableMesh have been optimized to make full use of vertex instances where possible. The welding/splitting of identical vertex instances has been removed from here, as the aim is to unify this with mesh utility code elsewhere. Various bug fixes. #rb Alexis.Matte [CL 3794563 by Richard TalbotWatkin in Dev-Geometry branch]
2017-12-07 13:02:12 -05:00
const float CurrentSharpnessValue = EdgeSharpnesses[ EdgeID ];
const float NewSharpnessValue = FMath::Clamp( CurrentSharpnessValue + ScaledDragDelta, 0.0f, 1.0f );
EdgeIDs.Add( EdgeID );
NewSharpnessValues.Add( NewSharpnessValue );
}
verify( !EditableMesh->AnyChangesToUndo() );
EditableMesh->SetEdgesCreaseSharpness( EdgeIDs, NewSharpnessValues );
MeshEditorMode.TrackUndo( EditableMesh, EditableMesh->MakeUndo() );
}
}
}
#undef LOCTEXT_NAMESPACE