@@ -8,26 +8,33 @@ internal static class FezGeometryUtil
88 {
99 /// Recalculates texture coordinates to match what game does with them during loading process.
1010 public static void RecalculateCubemapTexCoords < VertexType > (
11- IndexedPrimitives < VertexInstance , VertexType > geometry , Vector3 geometryBounds
11+ IndexedPrimitives < VertexInstance , VertexType > geometry , Vector3 geometryBounds , bool widen
1212 ) {
13+ // keeping arithmetics vaguely similar to the one in game to prevent number mismatch after conversion
1314 foreach ( var vertex in geometry . Vertices )
1415 {
15- ( int textureOffset , Vector3 xAxis , Vector3 yAxis ) = vertex . NormalByte switch
16+ ( float textureOffset , Vector3 xAxis , Vector3 yAxis ) = vertex . NormalByte switch
1617 {
17- 5 => ( 0 , new Vector3 ( 1 , 0 , 0 ) , new Vector3 ( 0 , - 1 , 0 ) ) , // front
18- 3 => ( 1 , new Vector3 ( 0 , 0 , - 1 ) , new Vector3 ( 0 , - 1 , 0 ) ) , // right
19- 2 => ( 2 , new Vector3 ( - 1 , 0 , 0 ) , new Vector3 ( 0 , - 1 , 0 ) ) , // back
20- 0 => ( 3 , new Vector3 ( 0 , 0 , 1 ) , new Vector3 ( 0 , - 1 , 0 ) ) , // left
21- 4 => ( 4 , new Vector3 ( 1 , 0 , 0 ) , new Vector3 ( 0 , 0 , 1 ) ) , // top
22- 1 => ( 5 , new Vector3 ( 1 , 0 , 0 ) , new Vector3 ( 0 , 0 , - 1 ) ) , // bottom
18+ 5 => ( 0 , new Vector3 ( 1 , 0 , 0 ) , new Vector3 ( 0 , 1 , 0 ) ) , // front
19+ 3 => ( 0.25f , new Vector3 ( 0 , 0 , - 1 ) , new Vector3 ( 0 , 1 , 0 ) ) , // right
20+ 2 => ( 0.375f , new Vector3 ( - 1 , 0 , 0 ) , new Vector3 ( 0 , 1 , 0 ) ) , // back
21+ 0 => ( 0.375f , new Vector3 ( 0 , 0 , 1 ) , new Vector3 ( 0 , 1 , 0 ) ) , // left
22+ 4 => ( 0.5f , new Vector3 ( 1 , 0 , 0 ) , new Vector3 ( 0 , 0 , 1 ) ) , // top
23+ 1 => ( 0.625f , new Vector3 ( 1 , 0 , 0 ) , new Vector3 ( 0 , 0 , 1 ) ) , // bottom
2324 _ => ( 0 , new Vector3 ( ) , new Vector3 ( ) )
2425 } ;
2526
26- var texturePlanePosition = vertex . Position / geometryBounds ;
27- vertex . TextureCoordinate = new Vector2 (
28- ( Vector3 . Dot ( texturePlanePosition , xAxis ) + 0.5f + textureOffset ) / 6.0f ,
29- Vector3 . Dot ( texturePlanePosition , yAxis ) + 0.5f
30- ) ;
27+ var texturePlanePosition = ( Vector3 . One - vertex . Normal ) * ( vertex . Position / geometryBounds ) * 2f + vertex . Normal ;
28+ var texturePlanePositionNormalized = texturePlanePosition / 2f + new Vector3 ( 0.5f , 0.5f , 0.5f ) ;
29+ float uCoordinate = Vector3 . Dot ( xAxis , texturePlanePositionNormalized ) ;
30+ float vCoordinate = Vector3 . Dot ( yAxis , texturePlanePositionNormalized ) ;
31+ if ( vertex . NormalByte != 4 )
32+ {
33+ vCoordinate = 1.0f - vCoordinate ;
34+ }
35+
36+ float finalUCoordinate = ( textureOffset + uCoordinate / 8f ) * ( widen ? 1.3333334f : 1.0f ) ;
37+ vertex . TextureCoordinate = new Vector2 ( finalUCoordinate , vCoordinate ) ;
3138 }
3239 }
3340
0 commit comments