Terrain Picking using vertex shader on GPU

Started by
1 comment, last by Enzio599 1 year, 8 months ago

Hi guys,

I am trying to get a picked point under mouse cursor using vertex shader…

my cpu implementation is just fine and working …

this is my code for GPU picking in vertex shader … whcih is not giving me desired result …

    float r = 0.5f; //imaginary sphere around a vertex of terrain radius 
    float4 VertexPos = mul(output.WorldPosition, worldMatrix);
    float3 PosToRayOrigin = VertexPos.xyz - RayPosition.xyz;
    float ProjectionOnRay = dot(PosToRayOrigin, RayDirection.xyz);
    float PosRayOriginLenSq = dot(PosToRayOrigin, PosToRayOrigin);
    float r2 = r * r;
    float m2 = PosRayOriginLenSq - ProjetionOnRay * ProjetionOnRay;
    
    bool RayOriginoutSideSphere = PosRayOriginLenSq > r2;
    bool sphereBehindRayOrigin = ProjectionOnRay  < 0.0f;
    bool noIntersection = RayOriginoutSideSphere && sphereBehindRayOrigin;
    bool RayMisSphere = m2 > r2;
    
    if (!noIntersection && !RayMisSphere)
        output.PickedPointGpu = VertexPos;  

r = 0.5f because terrain vertex are 1.0f units apart on xz plane.

kindly help as i am not getting desired result, and dont know where I am making mistake …

Advertisement

Ok resolved …. idont know why but whne i am writing to output from vertex shader it not working, but when i created a RWTexture2D of 1x1 size , and wrote in to that , it worked …

THIS WORKED

    if (!noIntersection && !RayMisSpehere)
        PickedVertexData[int2(0, 0)] = VertexPos;

This topic is closed to new replies.

Advertisement