Advertisement

How to Fix the Reflection in My DXR Setup?

Started by March 01, 2025 06:30 AM
1 comment, last by isu diss 4 days ago

Hi fellas,

Good day!

I'm trying to create reflections in DXR. I have setup the code. The problem is that I couldnt find any issue regarding the hlsl code. c++ side is ok. any thoughts??

 


//============================================================= REFLECT SHADERS =============================================

[shader("miss")]
void MyMissReflectShader(inout ReflectPayload payload)
{
    float4 skycolor = SkyTx.SampleLevel(ssAnisotropy, (DispatchRaysIndex().xy / float2(1920, 1080)), 0);
  
    float4 cloudscolor = CloudTx.SampleLevel(ssAnisotropy, (DispatchRaysIndex().xy / float2(1920, 1080)), 0);
 
    payload.color = lerp(skycolor, cloudscolor, cloudscolor.a);

}

float4 shootReflectRay(in float3 origin, in float3 dir, in float minT, in float maxT)
{
    RayDesc ray = { origin, minT, dir, maxT };
    ReflectPayload pay = { float4(0, 0, 0, 0) };
    
    uint flags = RAY_FLAG_ACCEPT_FIRST_HIT_AND_END_SEARCH | RAY_FLAG_SKIP_CLOSEST_HIT_SHADER;
    
    TraceRay(SceneBVH, flags, 0xFF, 2, 3, 2, ray, pay);
    
    return pay.color;
}

[shader("closesthit")]
void MyClosestHitShader(inout RayPayload payload, in MyAttributes attr)
{
    float3 hitPosition = HitWorldPosition();
    uint baseIndex = 3 * PrimitiveIndex();
    uint instanceId = InstanceID();
    
    float4 diffuseColor = 0;
    
    if (instanceId == 0) // terrain
    {
        // Retrieve corresponding vertex normals for the triangle vertices.
        float3 vertexNormals[3] =
        {
            Vertices_Terrain[baseIndex].Normal,
            Vertices_Terrain[baseIndex + 1].Normal,
            Vertices_Terrain[baseIndex + 2].Normal 
        };

        float2 vertexTextureCoordinates[3] =
        {
            Vertices_Terrain[baseIndex].TextureCoordinate,
            Vertices_Terrain[baseIndex + 1].TextureCoordinate,
            Vertices_Terrain[baseIndex + 2].TextureCoordinate 
        };
    
        float3 triangleNormal = HitAttribute1(vertexNormals, attr);
    
        float2 triangleTexcoord = HitAttribute2(vertexTextureCoordinates, attr);
    
        diffuseColor += CalculateDiffuseLighting(hitPosition, triangleNormal, triangleTexcoord);
    }
    else if (instanceId == 1) // ocean
    {
        float3 waterColor = float3(0.3, 0.7, 0.6);

        float3 vertexNormals[3] =
        {
            Vertices_Ocean[baseIndex].Normal,
            Vertices_Ocean[baseIndex + 1].Normal,
            Vertices_Ocean[baseIndex + 2].Normal 
        };
        float3 triangleNormal = normalize(HitAttribute1(vertexNormals, attr));
        
        float3 reflectDir = normalize(reflect(WorldRayDirection(), triangleNormal));

        float4 reflectColor = shootReflectRay(hitPosition, reflectDir, 0.001f, 100000.0f);
        diffuseColor += float4(waterColor * reflectColor.rgb, 1);

    }

    payload.color = diffuseColor;
}

if i move my camera below the terrain

I still dont see the clouds in the water

Advertisement