scene depth from 2D screen

Hello all !

Is there a way with unity to grab the Z value from 2D screen coordinate ?
This question seems pretty stoopid ( as in OpenGL, this was done with 2
code lines ) but with unity i didn’t find how to achiere this.

thanks.

Most of that stuff is done via the Camera in Unity:

Hum.
lemme take this with humor :wink:
some answer like:
“this is done with Unity” —> http://www.unity3D.com

would have been as usefull…

As a deduction, i understand it’s possible, but how is it possible ?
The camera class dont have the member function: grab_Z_at(int x,int y)

so, beside ppl knowing unity3D doc links by heart, did anyone play with
Z scene info ? and if yes, is there some example on how to do this ?

thankies and have a nice day :slight_smile:

ScreenPointToRay() Unity - Scripting API: Camera.ScreenPointToRay
Will take a 2d screen position and create a ray that goes from the camera through that point in 3d space, so you need to raycast that ray through the scene, see if it hits something, then check the distance of the hit.

//ScreenPointToRay takes Vector3's and ignores the Z for some reason, instead of just taking Vector2's
var my2dPos = Vector3(100, 100, 0);
var ray = Camera.main.ScreenPointToRay(my2dPos);
var hit : RaycastHit;
if(Physics.Raycast(ray, hit)){
	print(hit.distance); //distance from camera
}

nah legend :wink:

I wish this would work but… unfortunately, it work only with colliders and physics stuff.
I thought about casting a ray to see wether or not this ray meets some non infinite Z
but this silmply doesn’t work.
It would work only if the million polygons i use each have a collider. And i would have
to buy a million dollars silicon graphics station :cry:

no, what i need is just the Z value of a pixel on screen. this is SOOOO SIMPLE that i
really hope it’s possible.

thanks tho for your answer, and plz other ppl: HEEEELP !

Hmm, the only options I can think of are ScreenPointToRay(), which requires raycasting/colliders, and ScreentoWorldPoint(), but I’m pretty sure that requires you already know the z distance to use. To my knowledge there is no other way to do it :-\

OK certainly stupid reply but… why not having a look at the DOF effect script ?

This FX is supposed to grab Z values of every points on screen, isn’t it ?
and no physics stuff involved, there.

I actually thought about that, I don’t have pro though so I couldn’t say. I know you can render the depth of the scene to a texture, so you might be able to get that information out of that somehow, but you can only do it in pro so I have no experience doing so.

Perhaps if you explained what it was you need this for instead of the end result we can recommend a more appropriate solution.

I’m trying to fathom why this would be necessary without being able to use info from a game object transform or something. You could also try using trigger flagged collides and disable all collisions via the physics collision matrix. Ray casting against them should still work. Not sure what kind of overhead that will add though, in theory not much as it shouldn’t try to collide that geometry with anything but rays.

If you’re casting against a particle system then you could loop though all the particles and build bounding box rects using the camera to screen cood functions then test mouse x/y against those rects (you’ll need to sort them). It’ll be slow, but if you only need it for a one-off “pick” event you could wrap the rect building function in a co-coutine and yeild it over several frames from a cached copy of particles[ ].

Again without any real info of what you’re trying to “pick” out of the scene and how many objects you’re working with we’re just stabbing in the dark here.

true Cameron.
Atm, my ‘small’ scene is dealing with several hundreds thousands of tris. This means that i can’t expect usable things with mesh colliders.
What i want to do is making a flare that is NOT triggered by colliders ( colliders are not the right way imho ) but by abscence of objects.
Anyway, all ppl cool down :wink: i finally manage to achieve this. Thanks all for your help !

I’ll be back soon with some more info, but i 1st got to tydy a bit my package :slight_smile:

Have a nice day all.

Be sure to come back, I’d love to see your solution!