How to check the distance from the camera to the object in the shader?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Densevoid

How to check the distance from the camera to the object in the shader?

I suppose to do this through the uniform and sending the value through the processing script. But maybe there is a way to do this completely with the shader code?

Maybe you ought to remove that gdscript tag since this isn’t really concerning gdscript. Also when you say distance from object to camera do you also mean distance between camera and fragment, or one constant value for the object.

SIsilicon | 2018-08-06 02:18

Constant value.

Densevoid | 2018-08-10 06:52

:bust_in_silhouette: Reply From: PixiePal

You could use a math statement with the Camera Object to the Shader Object on a grid.

Calculate the distance of the Camera Object from the Shader Object.

Something like(not c#):

public class tryThis
    
    public static main void(String [] args)
    {
    Object camera;
    Object shader;
    double total;
    
    public double checkLength
    {
    Camera-Shader=Total;
    System..out.println(Total);
    
    }
    }

You could have this appear once in the console, or generate every-so-often as an alert everytime the shader or camera objects are moved for diagnostic purposes.