Skip to content

Commit 1348db9

Browse files
committed
Used triplanar mapping to reduce stretched trunk texture.
1 parent 5698b28 commit 1348db9

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

Samples/Desktop/D3D12Raytracing/src/D3D12RaytracingSakuraForestSER/D3D12RaytracingSakuraForestSER.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,7 +1029,6 @@ void D3D12RaytracingSakuraForestSER::BuildAccelerationStructures()
10291029
int objectsPerRow = 20; // Object per row along Z and X axis
10301030
float largerCubeSpacing = 4.0f; // Spacing between larger cubes
10311031
float randomCubeSpacing = 0.1f; // Spacing between random smaller cubes
1032-
float spacingGap = 0.0f; // Gap between two groups of forests
10331032

10341033
// Create random number generator for random offsets
10351034
std::random_device rd;
@@ -1038,7 +1037,7 @@ void D3D12RaytracingSakuraForestSER::BuildAccelerationStructures()
10381037

10391038
std::random_device rdBush;
10401039
std::mt19937 genBush(rdBush());
1041-
std::uniform_real_distribution<float> randomOffsetBush(0.0f, 0.4f);
1040+
std::uniform_real_distribution<float> randomOffsetBush(0.7f, 0.75f);
10421041

10431042
// Larger cubes for the floor
10441043
for (int x = -objectsPerRow / 2; x <= objectsPerRow / 2; ++x)
@@ -1098,7 +1097,7 @@ void D3D12RaytracingSakuraForestSER::BuildAccelerationStructures()
10981097
}
10991098

11001099
int treesPerRow = 30;
1101-
float spacingBetweenTrees = 1.7f;
1100+
float spacingBetweenTrees = 1.9f;
11021101

11031102
// Trunk and leaves
11041103
// Store random positions for trunks
@@ -1166,11 +1165,12 @@ void D3D12RaytracingSakuraForestSER::BuildAccelerationStructures()
11661165
{
11671166
for (int z = -bushesPerRow / 2; z <= bushesPerRow / 2; ++z)
11681167
{
1168+
float randomXOffset = randomOffset(gen);
11691169
float randomYOffset = randomOffsetBush(genBush);
11701170

1171-
float posX = x * 0.07;
1172-
float posY = 0.80f + randomYOffset;
1173-
float posZ = z * 0.07;
1171+
float posX = x * 0.2 + randomXOffset;
1172+
float posY = 1.2f + randomYOffset;
1173+
float posZ = z * 0.2;
11741174

11751175
D3D12_RAYTRACING_INSTANCE_DESC desc = {};
11761176
float scale = 2.1f; // Bush scale
@@ -1179,7 +1179,7 @@ void D3D12RaytracingSakuraForestSER::BuildAccelerationStructures()
11791179
desc.Transform[2][2] = scale;
11801180

11811181
desc.Transform[0][3] = posX; // X position with offset
1182-
desc.Transform[1][3] = posY + 0.8f; // Y position with offset
1182+
desc.Transform[1][3] = posY; // Y position with offset
11831183
desc.Transform[2][3] = posZ; // Z position
11841184

11851185
desc.InstanceMask = 1;

Samples/Desktop/D3D12Raytracing/src/D3D12RaytracingSakuraForestSER/Raytracing.hlsl

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -277,17 +277,21 @@ void TrunkClosestHitShader(inout RayPayload payload, in MyAttributes attr)
277277
vertexNormals[2] = VerticesTrunk[indices.z].normal;
278278
triangleNormal = HitAttribute(vertexNormals, attr);
279279

280-
float3 vertexTexCoords[3];
281-
vertexTexCoords[0] = VerticesTrunk[indices.x].uv;
282-
vertexTexCoords[1] = VerticesTrunk[indices.y].uv;
283-
vertexTexCoords[2] = VerticesTrunk[indices.z].uv;
284-
float2 interpolatedTexCoord = HitAttribute(vertexTexCoords, attr).xy;
285-
sampled.rgb = TrunkTexture.SampleLevel(TrunkSampler, interpolatedTexCoord, 0).rgb;
286-
287-
float3 baseColor = albedo * sampled.rgb;
288-
float3 finalColor;
280+
// Sample texture from 3 planes (triplanar mapping)
281+
float2 uvX = hitPosition.yz * 0.5;
282+
float2 uvY = hitPosition.xz * 0.5;
283+
float2 uvZ = hitPosition.xy * 0.5;
284+
285+
float3 texX = TrunkTexture.SampleLevel(TrunkSampler, uvX, 0).rgb;
286+
float3 texY = TrunkTexture.SampleLevel(TrunkSampler, uvY, 0).rgb;
287+
float3 texZ = TrunkTexture.SampleLevel(TrunkSampler, uvZ, 0).rgb;
288+
289+
// Blend based on normal
290+
float3 blendWeights = abs(triangleNormal);
291+
blendWeights = normalize(max(blendWeights, 0.00001));
292+
sampled.rgb = texX * blendWeights.x + texY * blendWeights.y + texZ * blendWeights.z;
289293

290-
finalColor = albedo * sampled.rgb * 0.7f;
294+
float3 finalColor = albedo * sampled.rgb * 0.5f;
291295
payload.color = float4(finalColor, g_cubeCB.albedo.w);
292296
}
293297

0 commit comments

Comments
 (0)