@@ -93,7 +93,7 @@ float4 main(PixelInputType input) : SV_TARGET
93
93
float4 finalColor = ambientComponent + diffuseComponent + specularComponent ;
94
94
95
95
// ------------ TEXTURE START ----------------
96
- float2 texelSize = float2 (1.0 / (grid_dim_x + 1 ), 1.0 / (grid_dim_y + 1 ));
96
+ float2 texelSize = float2 (1.0 / (grid_dim_x ), 1.0 / (grid_dim_y ));
97
97
98
98
// Calculate the tile index
99
99
float2 tileIndex = floor (input .tex_coords0 / texelSize );
@@ -127,13 +127,26 @@ float4 main(PixelInputType input) : SV_TARGET
127
127
// Calculate the UV coordinates for each texture in the textureAtlas
128
128
uint indices [4 ] = { topLeftTexIdx , topRightTexIdx , bottomLeftTexIdx , bottomRightTexIdx };
129
129
float atlasTileSize = 1.0 / 8.0 ;
130
+ float innerRegionScale = 0.5 ; // This is used to extract the inner 50% of the texture
130
131
float2 atlasCoords [4 ];
131
132
for (int i = 0 ; i < 4 ; ++ i ) {
132
133
uint index = indices [i ];
133
134
float x = (index % 8 ) * atlasTileSize ;
134
135
float y = (index / 8 ) * atlasTileSize ;
135
136
float2 relativeUV = (input .tex_coords0 - topLeftTexCoord ) / texelSize ;
136
- atlasCoords [i ] = float2 (x , y ) + float2 (relativeUV .x , relativeUV .y ) * atlasTileSize ;
137
+
138
+ // Apply mirroring on every other tile
139
+ if (uint (tileIndex .x ) % 2 == 1 ) {
140
+ relativeUV .x = 1.0 - relativeUV .x ;
141
+ }
142
+ if (uint (tileIndex .y ) % 2 == 1 ) {
143
+ relativeUV .y = 1.0 - relativeUV .y ;
144
+ }
145
+
146
+ // Map the relativeUV coordinates to the inner 50% of the atlas texture
147
+ float2 innerOffset = atlasTileSize * (1.0 - innerRegionScale ) * 0.5 ;
148
+ float2 innerSize = atlasTileSize * innerRegionScale ;
149
+ atlasCoords [i ] = float2 (x , y ) + innerOffset + float2 (relativeUV .x , relativeUV .y ) * innerSize ;
137
150
}
138
151
139
152
// Sample the textureAtlas using the calculated UV coordinates
@@ -142,19 +155,20 @@ float4 main(PixelInputType input) : SV_TARGET
142
155
sampledColors [i ] = textureAtlas .Sample (ss , atlasCoords [i ]);
143
156
}
144
157
145
- // Calculate the weights for bilinear interpolation
146
- float2 weights = frac ( input .tex_coords0 / texelSize );
147
- float4 blendWeights = float4 (( 1 - weights . x ) * ( 1 - weights . y ), weights . x * ( 1 - weights . y ), ( 1 - weights . x ) * weights . y , weights . x * weights . y );
148
- float4 alphas = { topLeftAlpha , topRightAlpha , bottomLeftAlpha , bottomRightAlpha };
149
- float alphas_total = alphas [ 0 ] + alphas [ 1 ] + alphas [ 2 ] + alphas [ 3 ] ;
158
+ // Sample the terrain_texture_weights using input.tex_coords0
159
+ float4 weightColors = terrain_texture_weights . Sample ( ss , input .tex_coords0 );
160
+
161
+ // Calculate the total weight for normalization
162
+ float totalWeight = weightColors . r + weightColors . g + weightColors . b + weightColors . a ;
150
163
151
- // Blend the sampled colors based on the blend weights
152
- float4 sampledTextureColor = sampledColors [0 ];
153
- sampledTextureColor .a = 1 ;
154
- //for (int j = 1; j < 4; ++j) {
155
- // sampledTextureColor.rgb += sampledColors[j].rgb * alphas[j] / alphas_total;
156
- //}
164
+ // Normalize the weights
165
+ float4 normalizedWeights = weightColors / totalWeight ;
157
166
167
+ // Perform texture splatting using the normalized weights
168
+ float4 splattedTextureColor = float4 (0.0 , 0.0 , 0.0 , 1.0 );
169
+ for (int i = 0 ; i < 4 ; ++ i ) {
170
+ splattedTextureColor .rgb += sampledColors [i ].rgb * normalizedWeights [i ];
171
+ }
158
172
// ------------ TEXTURE END ----------------
159
173
160
174
@@ -163,10 +177,10 @@ float4 main(PixelInputType input) : SV_TARGET
163
177
// Multiply the sampled color with the finalColor
164
178
if (input .terrain_height <= water_level ) {
165
179
float4 blue_color = float4 (0.11 , 0.65 , 0.81 , 1.0 ); // Water color
166
- outputColor = finalColor * sampledTextureColor * blue_color ;
180
+ outputColor = finalColor * splattedTextureColor * blue_color ;
167
181
}
168
182
else {
169
- outputColor = finalColor * sampledTextureColor ;
183
+ outputColor = finalColor * splattedTextureColor ;
170
184
}
171
185
172
186
// Return the result
0 commit comments