arbitrary camera_NDC space in shader?

   4463   5   1
User Avatar
Member
37 posts
Joined:
Offline
HI there,

Sometimes I find myself doing projects with lots and lots of prebaked geometry data that is a hassle to go back and recook. In these cases I really wish I could tell my surface shader to look up some coordinates from a projection camera ( OTHER than my rendering camera ) that I've set up without having to go back and bake in UV coordinates on the points or vertices. I see that you can do something in Light_NDC space, which is cool. I would like to do the same transform in a camera_NDC space.
User Avatar
Member
941 posts
Joined: July 2005
Offline
You could use a Null object (with “Output transform as render space” toggled on), instead of a camera. This null then becomes a transform space for the shader which, together with the parameters of the projection geometry, it can use to project the image (via custom NDC transform) as though it were the camera.

Given the same variables as a Houdini camera (res, aperture, aspect, focal), plus a transform space for the center of projection (null object), and assuming “perspective” projection, you can reconstruct the NDC space with something like this:

vector MyToNDC (
vector p; // pos to transform, e.g: global P
string obj; // path to null object, e.g: “/obj/null1”
float resx,resy; // projection: image resolution
float aperture; // projection: aperture
float aspect; // projection: device aspect ratio
float focal; // projection: focal length
)
{
vector pp = p * invert(otransform(obj));
float apx = aperture;
float apy = (resy*apx) / (resx*aspect);
float x = (focal*pp.x)/(apx*pp.z);
float y = (focal*pp.y)/(apy*pp.z);
return set( 0.5-x, 0.5-y, -pp.z);
}

… which obviously doesn't validate any of the inputs – it assumes the caller has already done that (check that obj is not an empty string, etc).

HTH.
Edited by - Aug. 3, 2010 11:59:25
Mario Marengo
Senior Developer at Folks VFX [folksvfx.com] in Toronto, Canada.
User Avatar
Member
37 posts
Joined:
Offline
Very nice. I can wrap this into an operator easily. Thank you.

Would the folks at Sidefx mind putting together a little Vex operator that does this very thing? It sounds like it would be pretty useful.
User Avatar
Member
37 posts
Joined:
Offline
This seems to work nicely, Mario. Thank you. What's even nicer is that I can easily parent the null object to the camera and use it's wiring to grab all the camera viewing parameters.

One question, though: device aspect is not the resx/resy ratio, but the pixel aspect? The reason I ask is that this little doohicky works when i set the aspect to 1 regardless of my resolution values.

Just wondering. Otherwise it's a charm. Thanks.
User Avatar
Member
941 posts
Joined: July 2005
Offline
freejonah
One question, though: device aspect is not the resx/resy ratio, but the pixel aspect? The reason I ask is that this little doohicky works when i set the aspect to 1 regardless of my resolution values.

Yes, sorry, it's what in the Houdini camera is called “Pixel Aspect ratio” – e.g: 0.9 for D1, 1.0 for HD, etc.

freejonah
Just wondering. Otherwise it's a charm. Thanks.

Glad to hear it. We use this little hack all the time here; especially for multiple projections, or any projection that's not aligned to the viewing camera… which is to say “just about every shot for just about every job”

Cheers.
Mario Marengo
Senior Developer at Folks VFX [folksvfx.com] in Toronto, Canada.
User Avatar
Member
37 posts
Joined:
Offline
that's what i figured. Pixel aspect.

I'm curious about this null RIB output thing, though? Why can't they put that into other objects like a camera or geometry? My operator is a little annoying in that it requires me to parent a null to a camera and then reference both of them in my shader. I would love to just give it the name of a camera or a light and let 'er fly.

I submitted an RFE to sidefx regarding this.

thanks so much for your help, Mario

Jonah Hall
Public Visual Effects
Venice, CA
  • Quick Links