I would like to create a osl shader that could show the visibility of the cube surface from the cursor pos (0,0,0). So I used the trace() function like this:

shader Visibility(    
    point target = (0.0, 0.0, 0.0),
    output int Hit = 0,
    output float dist=0, 
    vector rayDir = target - P; 
    float hit_dist = 0;
    float points_dist = distance(P, target);
    int ishit = trace(P,rayDir, "maxdist",points_dist); 
    Hit = ishit;

but the result is very strange. What should I do? enter image description here


1 Answer 1


You're firing a ray from P, the shading point, to your defined viewpoint, and you're checking for occlusion: whether anything is hit within the distance from P to the viewpoint, relying on "maxdist".

There's a possibility of floating-point error, here, if your viewpoint is close to a surface, or the ray hits P on the way out; but I've tried fixing that with "mindist", to no effect, which is a bit mysterious.

I do think, though, it might be more positive to fire from the viewpoint to P, where you know there is something to be hit, and check whether anything else has been hit on the way...

shader Is_Visible(
           point Viewpoint = (0),   
    output int   IsVisible = 0

    point hitpoint = (0);
    vector vp_to_P = (P - Viewpoint);
    trace (Viewpoint,vp_to_P);  
    getmessage ("trace","P",hitpoint);
    IsVisible = (distance (hitpoint, P) < 0.00001);            

which seems to work...

enter image description here

.. even then I feel an urge to wrap everything in an if hit {}


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .