This is really @cppBeginner's answer to his own post.. (see commentary on OP).
The backfacing()
function can be called on the implicit shading point in an OSL script, but is not available as an attribute returned in a message from a script-cast ray.
But many mesh and object attributes can be returned by OSL's trace()
. See getmessage ("trace", ...)
in the OSL specification.. this page of Blender source might help with types, etc.. (but that's only a grep, I don't strictly know how it ties in. Maybe someone can edit to flesh that out.)
These attributes include geom:trianglevertices
, which is an array of three 3D points: the coordinates in World space of the corners of the render-triangle containing the shading point. They appear to be in order, so theirthe cross-product of two edges can be taken to find the direction of the triangle's normal, as defined by the mesh.
#include "stdosl.h"
shader Hit_Backfacing
(
vector RayDir = vector(0.0),
output int Hit = 0,
output int Hit_Backfacing = 0
){
if (trace(P,rayDir)){
Hit = 1;
vector triNormal = vector(0.0);
point triVerts[3];
getmessage("trace","geom:trianglevertices",triVerts);
triNormal = cross(triVerts[1]-triVerts[0],triVerts[2]-triVerts[0]);
Hit_Backfacing = dot(triNormal,rayDir) > 0;
}
}
In this example, the sign of dot-product of that normal and the cast ray direction tells us whether the triangle we hit faces us, or not...
The large plane carries the illustrated material.