check whether `trace()` collide a face at back or front side? How to check whether trace(ray,direction,...) collides the nearest face at front OR back side?
According to OSL: Anomalous normals returned by getmessage(&quot;trace&quot;), these below codes won't work because it always return value as front-face (automatically flipped) from ray's perspective.
getmessage (&quot;trace&quot;, &quot;N&quot;, tN);
getmessage (&quot;trace&quot;, &quot;Ng&quot;, 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 &quot;stdosl.h&quot;
shader tN_anomaly
[[ string help = &quot;&quot; ]]
(      
    output vector hitBackfacing = vector(0,0,0)
){
    hitBackfacing=vector(0,0,0);  
    if (trace(P,vector(0,0,1))){
        string tObj;
        getmessage (&quot;trace&quot;, &quot;geom:name&quot;,tObj);
        if(tObj==&quot;CubeB&quot;){
            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 my blend file.

Here is another blend file.  It is very similar, but I stretch Cube2 in x&amp;y axis to cover the whole Cube1.  It also prints log z which &gt; 1.
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 (&quot;trace&quot;, ...) 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 the cross-product of two edges can be taken to find the direction of the triangle's normal, as defined by the mesh.
#include &quot;stdosl.h&quot;
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(&quot;trace&quot;,&quot;geom:trianglevertices&quot;,triVerts);
        triNormal = cross(triVerts[1]-triVerts[0],triVerts[2]-triVerts[0]); 
        
        Hit_Backfacing = dot(triNormal,rayDir) &gt; 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.