26.1 C
New York
Sunday, June 29, 2025

Infinite flooring grid shader


Ask: How can I venture the fragment place on the world flooring aircraft with respect to the interpretation, rotation and zoom of the cameras in the present day?

I’m making an attempt to implement a grid of infinity grid on the planet, much like This query.

My venture is to make use of metallic shaded language, however the ideas are comparable between GLSL/HLSL.

Web has supplied Numerous reference factors To study to Implement Ranking Shaw. What I’m preventing is the required ideas to convert between views To attain my aim.

I discover notably mystical shadows as a result of difficulties in purification visually, so I’ve interrupted what I perceive which might be the required steps;

  • 1.) Within the scene:

    • Create a aircraft that extends alongside the xy axis within the vary (-1, -1) -> (1, 1)
  • 2.) Within the vertex shader:

    • Set up the fragment place in order that the aircraft takes the view
    • Return the vertex not remodeled
  • 3.) Within the fragment shader:

    • Calculate the digital camera place within the clip house
    • Calculate the aircraft place within the clip house
    • Place a digital camera ray to the ground aircraft to seek out the purpose of the intersection
    • Render the world axis grid utilizing the intersection level

Community Illustration

This methodology determines the ultimate output coloration for the world grid for a given level alongside the aircraft.

inline float4 grid(float2 uv,
                   float scale,
                   float4 gridColor,
                   float4 backgroundColor) {
    uv /= scale;
    
    float2 fractional  = abs(fract(uv + 0.5f));
    float2 partial = fwidth(uv);
    
    float2 level = smoothstep(-partial, partial, fractional);
    
    float saturation = 1.f - saturate(level.x * level.y);
    
    return combine(backgroundColor, gridColor, saturation);
}

Vertices shador

The vertex shador passes the place of the vertex alongside the fragments shador with none transformation. Vértice positions are within the vary (-1, -1) -> (1, 1) that fills the graphic window within the clip house.

struct SurfaceFragment {
    
    float4 place ((place));
    float2 uv;
};

vertex SurfaceFragment surface_vertex(SimpleVertex v (( stage_in )),
                                      fixed SceneBuffer& scn_frame (( buffer(0) ))) {
    
    return {    .place = float4(v.place, 1.f),
                .uv = v.place.xy };
}

Fragment shador

fragment float4 surface_fragment(SurfaceFragment f ((stage_in)),
                                 fixed SceneBuffer& scn_frame (( buffer(0) ))) {
    
    //place is in clip house (z = 1.f (far aircraft))
    float3 place = float3(f.uv, 1.f);
    
    //convert eye from digital camera house to clip house
    float4 pov = scn_frame.projectionTransform * float4(0.f, 0.f, 0.f, 1.f);
    
    //convert aircraft from world house to clip house
    float4 origin = scn_frame.viewProjectionTransform * float4(0.f, 0.f, 0.f, 1.f);
    
    //decide course of ray from eye
    float3 course = normalize(place - pov.xyz);
    
    //aircraft regular
    float3 up = float3(0.f, 1.f, 0.f);

    float magnitude = dot(up, course);
    float size = dot((origin.xyz - pov.xyz), up) / magnitude;
    
    if (size <= epsilon) { return backgroundColor; }

    //decide hit location
    float3 hit = pov.xyz + (course * size);
    
    //draw grid
    return grid(hit.xz,
                1.f,
                gridColor,
                backgroundColor);
}

Issues

That is the ultimate exit of the scene the place we are able to see some issues:

  • Rotating the biased digital camera the strains of the grid incorrectly.
  • After translating the digital camera, the land aircraft is not aligned

This appears to be near what I wish to obtain, however I am unsure the place to go right here.

I suppose that Raycasting’s returned place is inaccurate as a result of additionally:

  • Incorrect transformations between the clip house and the house of the world.
  • Incorrect placement of the aircraft on the planet house.
  • Incorrectly launching the POV ray or incorrect course.

How can I venture the place of the fragment on the ground of the ground of the world with respect to the digital camera?

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles