Skip to content

Commit b688f95

Browse files
authored
Fast texture tool fixes (#3901)
* Fix uv's going off screen when using existing uv's * Make world scale better * Remove the rect box, only show uv, block all actions related to scaling * Block more stuff in world scale * Don't keep flipping each time doing transform * Fix alignment and flipping not working in world scale
1 parent dc024e3 commit b688f95

File tree

2 files changed

+163
-21
lines changed

2 files changed

+163
-21
lines changed

game/addons/tools/Code/Editor/RectEditor/MeshRectangle.cs

Lines changed: 103 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ public MeshRectangle( Window window, MeshFace[] meshFaces ) : base( window )
6060

6161
public override void OnPaint( RectView view )
6262
{
63+
var settings = Session?.Settings?.FastTextureSettings;
64+
6365
var originalPen = Paint.Pen;
6466
Paint.SetPen( Color.White.WithAlpha( 0.8f ), 2 );
6567
var transformedPositions = GetRectangleRelativePositions();
@@ -83,6 +85,9 @@ public override void OnPaint( RectView view )
8385
}
8486
}
8587

88+
if ( settings != null && settings.ScaleMode == ScaleMode.WorldScale )
89+
return;
90+
8691
DrawIndexedLine( view, HoveredEdge.vertexA, HoveredEdge.vertexB, transformedPositions );
8792
DrawIndexedLine( view, AlignEdgeVertexA, AlignEdgeVertexB, transformedPositions, 0.5f );
8893
Paint.SetPen( originalPen );
@@ -189,12 +194,12 @@ public void ApplyMapping( FastTextureSettings settings, bool resetBoundsFromUseE
189194
ApplyEdgeAlignment( settings.Alignment );
190195
}
191196

192-
ApplyFastTextureTransforms( settings, hasEdgeAlignment );
193-
if ( currentMapping == MappingMode.UseExisting )
197+
if ( currentMapping != MappingMode.UseExisting )
194198
{
195-
CalculateUVBounds();
199+
ApplyFastTextureTransforms( settings, hasEdgeAlignment );
196200
}
197-
else if ( !resetBoundsFromUseExisting )
201+
202+
if ( currentMapping != MappingMode.UseExisting && !resetBoundsFromUseExisting )
198203
{
199204
Min = previousBounds.Min;
200205
Max = previousBounds.Max;
@@ -221,10 +226,21 @@ private void ApplyFastTextureTransforms( FastTextureSettings settings, bool hasE
221226
var rotated = new Vector2( -relative.y, relative.x );
222227
UnwrappedVertexPositions[i] = center + rotated;
223228
}
229+
230+
if ( UnwrappedVertexPositionsWorldSpace.Count == UnwrappedVertexPositions.Count )
231+
{
232+
var worldCenter = GetWorldSpaceMeshCenter();
233+
for ( int i = 0; i < UnwrappedVertexPositionsWorldSpace.Count; i++ )
234+
{
235+
var pos = UnwrappedVertexPositionsWorldSpace[i];
236+
var relative = pos - worldCenter;
237+
var rotated = new Vector2( -relative.y, relative.x );
238+
UnwrappedVertexPositionsWorldSpace[i] = worldCenter + rotated;
239+
}
240+
}
224241
}
225242

226243
var flipHorizontal = settings.IsFlippedHorizontal;
227-
if ( settings.Mapping == MappingMode.UnwrapSquare ) flipHorizontal = !flipHorizontal;
228244

229245
if ( flipHorizontal )
230246
{
@@ -235,6 +251,17 @@ private void ApplyFastTextureTransforms( FastTextureSettings settings, bool hasE
235251
pos.x = bounds.min.x + bounds.max.x - pos.x;
236252
UnwrappedVertexPositions[i] = pos;
237253
}
254+
255+
if ( UnwrappedVertexPositionsWorldSpace.Count == UnwrappedVertexPositions.Count )
256+
{
257+
var worldBounds = GetWorldSpaceMeshBounds();
258+
for ( int i = 0; i < UnwrappedVertexPositionsWorldSpace.Count; i++ )
259+
{
260+
var pos = UnwrappedVertexPositionsWorldSpace[i];
261+
pos.x = worldBounds.min.x + worldBounds.max.x - pos.x;
262+
UnwrappedVertexPositionsWorldSpace[i] = pos;
263+
}
264+
}
238265
}
239266

240267
if ( settings.IsFlippedVertical )
@@ -246,7 +273,47 @@ private void ApplyFastTextureTransforms( FastTextureSettings settings, bool hasE
246273
pos.y = bounds.min.y + bounds.max.y - pos.y;
247274
UnwrappedVertexPositions[i] = pos;
248275
}
276+
277+
if ( UnwrappedVertexPositionsWorldSpace.Count == UnwrappedVertexPositions.Count )
278+
{
279+
var worldBounds = GetWorldSpaceMeshBounds();
280+
for ( int i = 0; i < UnwrappedVertexPositionsWorldSpace.Count; i++ )
281+
{
282+
var pos = UnwrappedVertexPositionsWorldSpace[i];
283+
pos.y = worldBounds.min.y + worldBounds.max.y - pos.y;
284+
UnwrappedVertexPositionsWorldSpace[i] = pos;
285+
}
286+
}
287+
}
288+
}
289+
private Vector2 GetWorldSpaceMeshCenter()
290+
{
291+
if ( UnwrappedVertexPositionsWorldSpace.Count == 0 )
292+
return Vector2.Zero;
293+
294+
var sum = Vector2.Zero;
295+
foreach ( var pos in UnwrappedVertexPositionsWorldSpace )
296+
{
297+
sum += pos;
249298
}
299+
return sum / UnwrappedVertexPositionsWorldSpace.Count;
300+
}
301+
302+
private (Vector2 min, Vector2 max) GetWorldSpaceMeshBounds()
303+
{
304+
if ( UnwrappedVertexPositionsWorldSpace.Count == 0 )
305+
return (Vector2.Zero, Vector2.Zero);
306+
307+
var min = UnwrappedVertexPositionsWorldSpace[0];
308+
var max = UnwrappedVertexPositionsWorldSpace[0];
309+
310+
foreach ( var pos in UnwrappedVertexPositionsWorldSpace )
311+
{
312+
min = Vector2.Min( min, pos );
313+
max = Vector2.Max( max, pos );
314+
}
315+
316+
return (min, max);
250317
}
251318

252319
private Vector2 GetUnwrappedMeshCenter()
@@ -402,8 +469,29 @@ private void BuildUnwrappedMeshFromExistingUVs()
402469
FaceVertexIndices.Add( faceIndices );
403470
}
404471

472+
OffsetUVsToVisibleRange();
405473
CalculateUVBounds();
406474
}
475+
private void OffsetUVsToVisibleRange()
476+
{
477+
if ( UnwrappedVertexPositions.Count == 0 )
478+
return;
479+
480+
Vector2 sum = Vector2.Zero;
481+
foreach ( var pos in UnwrappedVertexPositions )
482+
sum += pos;
483+
Vector2 center = sum / UnwrappedVertexPositions.Count;
484+
485+
Vector2 offset = Vector2.Zero;
486+
offset.x = center.x >= 0 ? -(int)(center.x) : -(int)(center.x - 1);
487+
offset.y = center.y >= 0 ? -(int)(center.y) : -(int)(center.y - 1);
488+
489+
for ( int i = 0; i < UnwrappedVertexPositions.Count; i++ )
490+
UnwrappedVertexPositions[i] += offset;
491+
492+
Min += offset;
493+
Max += offset;
494+
}
407495

408496
private void BuildUnwrappedMeshWithPlanarMapping( Vector3 cameraLeft, Vector3 cameraUp )
409497
{
@@ -804,6 +892,16 @@ public void ApplyEdgeAlignment( AlignmentMode alignmentMode )
804892
var toVertex = UnwrappedVertexPositions[i] - uvA;
805893
UnwrappedVertexPositions[i] = new Vector2( axisU.Dot( toVertex ), axisV.Dot( toVertex ) );
806894
}
895+
896+
if ( UnwrappedVertexPositionsWorldSpace.Count == UnwrappedVertexPositions.Count )
897+
{
898+
var worldUvA = UnwrappedVertexPositionsWorldSpace[AlignEdgeVertexA];
899+
for ( int i = 0; i < UnwrappedVertexPositionsWorldSpace.Count; i++ )
900+
{
901+
var toVertex = UnwrappedVertexPositionsWorldSpace[i] - worldUvA;
902+
UnwrappedVertexPositionsWorldSpace[i] = new Vector2( axisU.Dot( toVertex ), axisV.Dot( toVertex ) );
903+
}
904+
}
807905
}
808906
}
809907
}

game/addons/tools/Code/Editor/RectEditor/RectView.cs

Lines changed: 60 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ private Vector2 PixelToUV_OnGrid( Vector2 vPixel )
130130

131131
private void DragCreateRect( Vector2 mousePos )
132132
{
133+
if ( Session.Settings.FastTextureSettings.ScaleMode == ScaleMode.WorldScale )
134+
return;
135+
133136
var minStart = PixelToUV_OnGrid( DragStartPos );
134137
var maxStart = PixelToUV_OnGrid( DragStartPos );
135138
var current = PixelToUV_OnGrid( mousePos );
@@ -143,6 +146,7 @@ private void DragCreateRect( Vector2 mousePos )
143146
var meshRect = Document.Rectangles.OfType<Document.MeshRectangle>().FirstOrDefault();
144147
if ( meshRect != null )
145148
{
149+
146150
meshRect.Min = NewRect.TopLeft;
147151
meshRect.Max = NewRect.BottomRight;
148152

@@ -179,6 +183,9 @@ private void DragMoveRect( Vector2 mousePos )
179183

180184
private void DragResizeRect( Vector2 mousePos )
181185
{
186+
if ( Session.Settings.FastTextureSettings.ScaleMode == ScaleMode.WorldScale )
187+
return;
188+
182189
var currentUV = PixelToUV_OnGrid( mousePos );
183190

184191
foreach ( var rectangle in DraggingRectangles )
@@ -359,6 +366,17 @@ protected override void OnMousePress( MouseEvent e )
359366
DragStartPos = e.LocalPosition;
360367
DraggingRectangles.Clear();
361368

369+
if ( Session.Settings.IsFastTextureTool && Session.Settings.FastTextureSettings.ScaleMode == ScaleMode.WorldScale )
370+
{
371+
var meshRect = Document.Rectangles.OfType<Document.MeshRectangle>().FirstOrDefault();
372+
if ( meshRect != null )
373+
{
374+
DraggingRectangles = [meshRect];
375+
Document.SelectRectangle( meshRect, SelectionOperation.Set );
376+
}
377+
return;
378+
}
379+
362380
var rectUnderCursor = GetFirstRectangleUnderCursor();
363381
if ( rectUnderCursor is not null )
364382
{
@@ -708,7 +726,7 @@ void SetCursorFromState()
708726
bool canResize = Document.SelectedRectangles.Count < 2 && HoveredCorner != 0;
709727
var rectUnderCursor = GetFirstRectangleUnderCursor();
710728

711-
if ( canResize )
729+
if ( canResize && Session.Settings.FastTextureSettings.ScaleMode != ScaleMode.WorldScale )
712730
{
713731
if ( HoveredCorner.x != 0 && HoveredCorner.y != 0 )
714732
{
@@ -759,6 +777,11 @@ public Vector2 GetHoveredCornerForRectangle( Document.Rectangle rectangle, Vecto
759777
var vec = Vector2.Zero;
760778
var tolerance = 0.02f;
761779

780+
if ( rectangle is Document.MeshRectangle && Session.Settings.FastTextureSettings.ScaleMode == ScaleMode.WorldScale )
781+
{
782+
return Vector2.Zero;
783+
}
784+
762785
if ( MathF.Abs( position.x - rectangle.Min.x ) < tolerance )
763786
{
764787
vec += new Vector2( -1, 0 );
@@ -790,26 +813,34 @@ private void DrawRectangleSet( IEnumerable<Document.Rectangle> rectangles )
790813
rectanglesItem?.OnPaint( this );
791814
}
792815

793-
var rectangleUnderCursor = GetFirstRectangleUnderCursor();
794-
foreach ( var rectangle in rectangles.Where( x => !Document.IsRectangleSelected( x ) && x != rectangleUnderCursor ) )
816+
if ( !Session.Settings.IsFastTextureTool || Session.Settings.FastTextureSettings.ScaleMode != ScaleMode.WorldScale )
795817
{
796-
Paint.SetBrush( rectangle.Color.WithAlpha( 0.2f ) );
797-
Paint.SetPen( Color.Black.WithAlpha( 192 / 255.0f ), 2 );
798-
DrawRectangle( rectangle );
799-
}
818+
var rectangleUnderCursor = GetFirstRectangleUnderCursor();
819+
foreach ( var rectangle in rectangles.Where( x => !Document.IsRectangleSelected( x ) && x != rectangleUnderCursor ) )
820+
{
821+
Paint.SetBrush( rectangle.Color.WithAlpha( 0.2f ) );
822+
Paint.SetPen( Color.Black.WithAlpha( 192 / 255.0f ), 2 );
823+
DrawRectangle( rectangle );
824+
}
800825

801-
foreach ( var rectangle in Document.SelectedRectangles )
802-
{
803-
Paint.SetBrush( Color.White.WithAlpha( 0.1f ) );
804-
Paint.SetPen( new Color32( 255, 255, 0 ), 3 );
805-
DrawRectangle( rectangle, corner: (rectangle == rectangleUnderCursor && Document.SelectedRectangles.Count < 2) ? HoveredCorner : 0 );
826+
foreach ( var rectangle in Document.SelectedRectangles )
827+
{
828+
Paint.SetBrush( Color.White.WithAlpha( 0.1f ) );
829+
Paint.SetPen( new Color32( 255, 255, 0 ), 3 );
830+
DrawRectangle( rectangle, corner: (rectangle == rectangleUnderCursor && Document.SelectedRectangles.Count < 2) ? HoveredCorner : 0 );
831+
}
832+
833+
if ( rectangleUnderCursor is not null && !Document.IsRectangleSelected( rectangleUnderCursor ) )
834+
{
835+
Paint.SetBrush( Color.Yellow.WithAlpha( 0.1f ) );
836+
Paint.SetPen( Color.Yellow, 2 );
837+
DrawRectangle( rectangleUnderCursor, corner: HoveredCorner );
838+
}
806839
}
807840

808-
if ( rectangleUnderCursor is not null && !Document.IsRectangleSelected( rectangleUnderCursor ) )
841+
if ( Session.Settings.IsFastTextureTool && Session.Settings.FastTextureSettings.ScaleMode == ScaleMode.WorldScale )
809842
{
810-
Paint.SetBrush( Color.Yellow.WithAlpha( 0.1f ) );
811-
Paint.SetPen( Color.Yellow, 2 );
812-
DrawRectangle( rectangleUnderCursor, corner: HoveredCorner );
843+
DrawWorldScaleIndicators();
813844
}
814845
}
815846

@@ -959,6 +990,19 @@ private void DrawTiledGrid( Rect baseRect )
959990
}
960991
}
961992

993+
private void DrawWorldScaleIndicators()
994+
{
995+
var meshRect = Document.Rectangles.OfType<Document.MeshRectangle>().FirstOrDefault();
996+
if ( meshRect == null || Session.Settings.FastTextureSettings.ScaleMode != ScaleMode.WorldScale )
997+
return;
998+
999+
var originPixel = UVToPixel( meshRect.Min );
1000+
1001+
Paint.SetPen( Color.Cyan, 2 );
1002+
Paint.DrawLine( originPixel, originPixel + new Vector2( 16, 0 ) );
1003+
Paint.DrawLine( originPixel, originPixel + new Vector2( 0, 16 ) );
1004+
}
1005+
9621006
protected override void OnDoubleClick( MouseEvent e )
9631007
{
9641008
base.OnDoubleClick( e );

0 commit comments

Comments
 (0)