How to check whether trace(ray,direction,...)
collides the nearest face at front OR back side?
According to OSL: Anomalous normals returned by getmessage("trace"), these below codes won't work because it always return value as front-face (automatically flipped) from ray's perspective.
getmessage ("trace", "N", tN);
getmessage ("trace", "Ng", tNg);
The same link suggests that backfacing()
is the solution.
However, it doesn't work. I will show a test case below.
I have 2 cubes.
Here is my OSL shader of CubeA
.
For every point of CubeA
, shoot +z ray, and check whether the ray touches CubeB
at CubeB
's back-face (return green) or front-face (return red).
#include "stdosl.h"
shader tN_anomaly
[[ string help = "" ]]
(
output vector hitBackfacing = vector(0,0,0)
){
hitBackfacing=vector(0,0,0);
if (trace(P,vector(0,0,1))){
string tObj;
getmessage ("trace", "geom:name",tObj);
if(tObj=="CubeB"){
hitBackfacing=vector(1,0,0);
if(backfacing()){
hitBackfacing=vector(0,1,0); //: never reach
};
}
}
}
The below image is the result. It suggests that backfacing()
always return false
.
It looks like to me that the backfacing()
just cares about whether it is back-face from eye perspective, not from the most recent trace()
's perspective.
Left image = they are overlapping each other. (The correct result should be green.)
Right image = CubeB
is above CubeA
(not touching)
Here is another blend file. It is very similar, but I stretch Cube2
in x&y axis to cover the whole Cube1
. It also prints log z which > 1.
backfacing
, as determined by the clockwise / anticlockwise order of vertices around the polygon containing the shading point, from the ray's POV. The only way I can find of accessing this information is via thebackfacing()
function, but that can only be called on the shaded P, not on a P we hit ourselves, with a probing ray. I've tried vertex locations, dPdu,dPdv.. .... all sorts. $\endgroup$point ps[3]; getmessage("trace","geom:trianglevertices",ps); vector trueNormal= cross(ps[1]-ps[0],ps[2]-ps[0]);
I need to test more. $\endgroup$