Skip to content

Commit df96fde

Browse files
Using inner 50% of textures and mirroring hem on every other tile to make it slightly more seamless. Just a bandaid solution until I figure out how to properly render the textures.
1 parent 0bb79f3 commit df96fde

File tree

3 files changed

+59
-31
lines changed

3 files changed

+59
-31
lines changed

SourceFiles/AtexReader.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ std::vector<RGBA> ProcessDXT3(unsigned char* data, int xr, int yr)
136136
{
137137
image[x * 4 + a + (y * 4 + b) * xr] = ctbl[t & 3];
138138
t = t >> 2;
139-
image[x * 4 + a + (y * 4 + b) * xr].a = (unsigned char)((k & 15) << 4);
139+
image[x * 4 + a + (y * 4 + b) * xr].a = 255;
140140
k = k >> 4;
141141
}
142142

TerrainTexturedPixelShader.h

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ float4 main(PixelInputType input) : SV_TARGET
9393
float4 finalColor = ambientComponent + diffuseComponent + specularComponent;
9494

9595
// ------------ 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));
9797

9898
// Calculate the tile index
9999
float2 tileIndex = floor(input.tex_coords0 / texelSize);
@@ -127,13 +127,26 @@ float4 main(PixelInputType input) : SV_TARGET
127127
// Calculate the UV coordinates for each texture in the textureAtlas
128128
uint indices[4] = { topLeftTexIdx, topRightTexIdx, bottomLeftTexIdx, bottomRightTexIdx };
129129
float atlasTileSize = 1.0 / 8.0;
130+
float innerRegionScale = 0.5; // This is used to extract the inner 50% of the texture
130131
float2 atlasCoords[4];
131132
for (int i = 0; i < 4; ++i) {
132133
uint index = indices[i];
133134
float x = (index % 8) * atlasTileSize;
134135
float y = (index / 8) * atlasTileSize;
135136
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;
137150
}
138151

139152
// Sample the textureAtlas using the calculated UV coordinates
@@ -142,19 +155,20 @@ float4 main(PixelInputType input) : SV_TARGET
142155
sampledColors[i] = textureAtlas.Sample(ss, atlasCoords[i]);
143156
}
144157

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;
150163

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;
157166

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+
}
158172
// ------------ TEXTURE END ----------------
159173

160174

@@ -163,10 +177,10 @@ float4 main(PixelInputType input) : SV_TARGET
163177
// Multiply the sampled color with the finalColor
164178
if (input.terrain_height <= water_level) {
165179
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;
167181
}
168182
else {
169-
outputColor = finalColor * sampledTextureColor;
183+
outputColor = finalColor * splattedTextureColor;
170184
}
171185

172186
// Return the result

TerrainTexturedPixelShader.hlsl

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ float4 main(PixelInputType input) : SV_TARGET
8989
float4 finalColor = ambientComponent + diffuseComponent + specularComponent;
9090

9191
// ------------ TEXTURE START ----------------
92-
float2 texelSize = float2(1.0 / (grid_dim_x + 1), 1.0 / (grid_dim_y + 1));
92+
float2 texelSize = float2(1.0 / (grid_dim_x), 1.0 / (grid_dim_y));
9393

9494
// Calculate the tile index
9595
float2 tileIndex = floor(input.tex_coords0 / texelSize);
@@ -123,13 +123,26 @@ float4 main(PixelInputType input) : SV_TARGET
123123
// Calculate the UV coordinates for each texture in the textureAtlas
124124
uint indices[4] = { topLeftTexIdx, topRightTexIdx, bottomLeftTexIdx, bottomRightTexIdx };
125125
float atlasTileSize = 1.0 / 8.0;
126+
float innerRegionScale = 0.5; // This is used to extract the inner 50% of the texture
126127
float2 atlasCoords[4];
127128
for (int i = 0; i < 4; ++i) {
128129
uint index = indices[i];
129130
float x = (index % 8) * atlasTileSize;
130131
float y = (index / 8) * atlasTileSize;
131132
float2 relativeUV = (input.tex_coords0 - topLeftTexCoord) / texelSize;
132-
atlasCoords[i] = float2(x, y) + float2(relativeUV.x, relativeUV.y) * atlasTileSize;
133+
134+
// Apply mirroring on every other tile
135+
if (uint(tileIndex.x) % 2 == 1) {
136+
relativeUV.x = 1.0 - relativeUV.x;
137+
}
138+
if (uint(tileIndex.y) % 2 == 1) {
139+
relativeUV.y = 1.0 - relativeUV.y;
140+
}
141+
142+
// Map the relativeUV coordinates to the inner 50% of the atlas texture
143+
float2 innerOffset = atlasTileSize * (1.0 - innerRegionScale) * 0.5;
144+
float2 innerSize = atlasTileSize * innerRegionScale;
145+
atlasCoords[i] = float2(x, y) + innerOffset + float2(relativeUV.x, relativeUV.y) * innerSize;
133146
}
134147

135148
// Sample the textureAtlas using the calculated UV coordinates
@@ -138,19 +151,20 @@ float4 main(PixelInputType input) : SV_TARGET
138151
sampledColors[i] = textureAtlas.Sample(ss, atlasCoords[i]);
139152
}
140153

141-
// Calculate the weights for bilinear interpolation
142-
float2 weights = frac(input.tex_coords0 / texelSize);
143-
float4 blendWeights = float4((1 - weights.x) * (1 - weights.y), weights.x * (1 - weights.y), (1 - weights.x) * weights.y, weights.x * weights.y);
144-
float4 alphas = { topLeftAlpha, topRightAlpha, bottomLeftAlpha, bottomRightAlpha };
145-
float alphas_total = alphas[0] + alphas[1] + alphas[2] + alphas[3];
154+
// Sample the terrain_texture_weights using input.tex_coords0
155+
float4 weightColors = terrain_texture_weights.Sample(ss, input.tex_coords0);
156+
157+
// Calculate the total weight for normalization
158+
float totalWeight = weightColors.r + weightColors.g + weightColors.b + weightColors.a;
146159

147-
// Blend the sampled colors based on the blend weights
148-
float4 sampledTextureColor = sampledColors[0];
149-
sampledTextureColor.a = 1;
150-
//for (int j = 1; j < 4; ++j) {
151-
// sampledTextureColor.rgb += sampledColors[j].rgb * alphas[j] / alphas_total;
152-
//}
160+
// Normalize the weights
161+
float4 normalizedWeights = weightColors / totalWeight;
153162

163+
// Perform texture splatting using the normalized weights
164+
float4 splattedTextureColor = float4(0.0, 0.0, 0.0, 1.0);
165+
for (int i = 0; i < 4; ++i) {
166+
splattedTextureColor.rgb += sampledColors[i].rgb * normalizedWeights[i];
167+
}
154168
// ------------ TEXTURE END ----------------
155169

156170

@@ -159,10 +173,10 @@ float4 main(PixelInputType input) : SV_TARGET
159173
// Multiply the sampled color with the finalColor
160174
if (input.terrain_height <= water_level) {
161175
float4 blue_color = float4(0.11, 0.65, 0.81, 1.0); // Water color
162-
outputColor = finalColor * sampledTextureColor * blue_color;
176+
outputColor = finalColor * splattedTextureColor * blue_color;
163177
}
164178
else {
165-
outputColor = finalColor * sampledTextureColor;
179+
outputColor = finalColor * splattedTextureColor;
166180
}
167181

168182
// Return the result

0 commit comments

Comments
 (0)