@@ -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}
0 commit comments