# OSL script node : detect if two object collides and output a bool

I would like to have a script node that would output a boolean depending of if two objects are intersecting or not.

The goals is to use it in a mix shader and to hide / show my object depending of the intersection (kind of a mask).

The available inputs of my script node should be two objects ("active" object and "mask" object); and the output should be a boolean (0 or 1) for the FAC of a mix shader.

After having read this, I think it is possible, but don't know Blender scripting good enough yet to achieve it.

Can anyone help ?

Thanks !

• Does this actually need to be a script node or could it be achieved with other means - such as Dynamic Paint? – Rich Sedman Apr 9 '18 at 22:29
• Hi Rich, I don't know about Dynamic Paint. I added an image to improve my question. What do you think ? – gordie Apr 10 '18 at 8:39
• Knowing that I would like to be able to "switch" the material of the inside voxels too, not only the "surface" of the pile. – gordie Apr 10 '18 at 8:40
• Do you intend for the whole object (cube) to switch as soon as any of it intersected with the ‘mask’ or would only the intersecting bit be affected? – Rich Sedman Apr 10 '18 at 11:50
• it's better to this with python and hide the geometry if it intersects with another object... specially because there are too many voxels, doing this in OSL is not a good ideia. – Secrop Apr 10 '18 at 13:36

I've been working on something that goes somewhere towards answering your problem.. don't take it too seriously, I'm only getting used to some of the quirks of OSL in Blender/Cycles: some of these conditionals may look unnecessary, but for example, if I don't use the 'hit' local variable, it won't work, even though I don't need to.

#include "stdosl.h"

string Container = "",
output int IsInside = 0
)

{
if  ( raytype("refraction") != 1 ) IsInside = 0;
else{

int ray_depth;
getattribute("path:ray_depth", ray_depth);

if  ( ray_depth % 2  != 0){

string source_name;
int hit;
hit = trace (P,I);
getmessage ("trace", "geom:name", source_name);

if (hit==1){
if (source_name == Container ){
IsInside =1;
}
}
}
}
}


You enter the name of your container object in the 'Container' input,(unquoted).

I'm also pretty sure you could make the logic of this shader fail, without too much effort.

• Awesome !!! I'll try that. Just to be sure : OSL does not work on GPU ? – gordie Apr 12 '18 at 19:51
• @gordie .. at the moment, you are not offered the option of OSL if you select GPU as your rendering device in Blender. – Robin Betts Apr 12 '18 at 22:06
• You could adapt this old script of mine. It works with materialsIDs, so you get a better control of what objects work as a mask and what objects should be rendered as normal. blenderartists.org/forum/… – Secrop Apr 13 '18 at 6:16
• Seems nice too. Also, what I wanted to achieve is to "toggle" the material change (eg. when the origin of the mesh is inside the "domain"); not to have a transition. Would it be possible ? – gordie Apr 13 '18 at 7:20
• @Robin Betts, the script is old.. from a time I was still exploring OSL. just posted here to show that you can also probe materialsIDs, when tracing. For the full list of attributes you can retrieve from the message system, check the osl_services.cpp: git.blender.org/gitweb/gitweb.cgi/cycles.git/blob/HEAD:/src/… – Secrop Apr 13 '18 at 11:14