iOS, RealityKit, Metal Shader APIs. Determine fragment's distance from the camera

221 views Asked by At

Using the new Metal Custom Shader API, I've created Grass models that move over time.

Is there a way to determine the distance between the model's fragment and the camera?

I've tried to search through surface_parameters.uniforms().

There are a few potential properties such as model_to_view, projection_to_view but I don't know what to do further.

Any tips appreciated.

Edit:: Here is the code for the grassGreenShader

[[visible]]
void grassGreenShader(realitykit::surface_parameters params)
{
    params.surface().set_base_color(half3(0.094, 0.697, 0.088));
    params.surface().set_opacity(1);
    respondToCameraDistanceShader(params);
}

Here is the code to change the base colour of the fragment. This part is wrong:

void respondToCameraDistanceShader(realitykit::surface_parameters params)
{
    float3 distanceToCamera = params.geometry().world_position();
    
    if (distanceToCamera.z < 0.3 && distanceToCamera.z > -0.3)
    {
        params.surface().set_opacity(distanceToCamera.z);
        params.surface().set_base_color(half3(0.934, 0.902, 0.355));
    }
}

Moving grass

0

There are 0 answers