Read and Write Mental Ray User Data
During the time I worked on the integration of Golaem Crowd with Mental Ray as a shader plugin, I had two recurrent thoughts :
- When I was on the geometry shader, it was something like : “damn this Developper Manual rocks: all is well documented, there are some inspiring samples, some tips… This is a real pleasure to develop stuff for this rendering engine…”
- Then, I had to work on the material shader and it was more something like : “#@%! how the hell am I suppose to do that !!? This is a real nightmare to developp stuff for this rendering engine...”
How to write/read userdata? How to dynamically create a material and call it (if like me, you would expect that mi_call_material would do that, do not even think about it)? Add some silent crashing function calls and weird behaviors to the whole and you almost got it. Now, I better understand why Shader Writer is a real job: it requires a lot of patience, tenacity, courage and puzzle solving skills. Those guys are heroes…
What about internet and forums? Well, too many often for my poor nerves, I felt in this classic situation:
Wisdom of the ancients
Thus, as I now consider myself as an ancient, I feel this need to share my knowledge with the next generations of Mental Ray sorcerer apprentices. Please sons, spead the good word around you and never let another soul wander in the darkness again…
In this post, I’ll answer my first question : How to store user data in an object and read/write it in a shader ? In mental ray, arbitrary user data can be attached to any element in the scene, or stand by itself, for usage by a shader. User data can be attached to instances, objects, lights, instance groups… to extend the scene entity with extra data or attributes.
Why would you use them? Well, it could be efficient for varying the look of large numbers of object instances, adding simulation data usable by parallel jobs running in mental ray or sharing huge data between several shaders (e.g. data which could not get through the shading network)…
In my example, I considered two plugins :
- myPluginOne writes data of type myStructData in the hit object (miObject);
- myPluginTwo reads this data and displays it.
As always, any question, improvement suggestion, bug report is welcome and will help me to access perfection and all-wisdom. Finally I’d like to thank the LAMRUG (Los Angeles Mental Ray User Group) which website is a bible and whose owners are prophets!