#distance from camera in shaders

30 messages · Page 1 of 1 (latest)

lean harness
#

How to get distance from camera z position to curerrent pixel uv.y like on image?

nova jacinth
#

Hi.

lean harness
#

i think it is z-depth or smth 🙂

#

hi

nova jacinth
#

It's pleasure to helping you.

#

To calculate the distance from the camera to an object along the Z-axis in WebGL, you need to consider the camera's position and the object's position in the 3D space.

#

const distanceZ = cameraPosition.z - objectPosition.z;

#

make a sense?

lean harness
#

for now yes

#

but i need distance to object uv.y

#

is any attribute in vertex shader that gives me camera position to current uv pixel?

nova jacinth
#

I see.

#

Just a minutes.

#

hello/

#

hi friend.

#

The UV.y coordinate can be obtained from the texture coordinates of the object.

#

hello? If you want, plz send me DM.

#

okay?

lean harness
#

ok

#

i will try it

nova jacinth
#

thank you.

#

I am waiting for you now.

lean harness
#
varying float distToCamera;

void main() {
  gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4( position, 1.0 );
  vUv = uv;
  distToCamera = gl_Position.z;
}```
#

this one works

#

simpler than i thought

nova jacinth
#

that's okay.

#

If you have a question, you may contact me at anytime. okay?

lean harness
#

ok, thx

nova jacinth
#

But how can we contact?

lean harness
#

discord pw i guess