Common material between geometry instances

Hello,

For each objects (geometry instances) in my scene I want to set two materials. One specific for each object, and an other common to all (or a group of) objects.

For the one specific to each object, there is no problem.

But for the other, I’ve got some trouble.
First I create the material from the context, and I set to it the closest hit program. Then, where I create each geometry instance, I set the material count to 2, and I add the common material to the current geometry instance. Afterward, I set a variable, “matVal”, to 1 to the material.
My problem is, the value set to matVal is only correct for the first object to which I linked the material. All other objects have matVal equal to 0.

How can I attach a material to several geometry instances?

For information, I’m under CentOS 7, with Optix 6.0.0 and cuda 10.2.

Thanks,
Arnaud

If this is about OptiX 6 built-in triangles please read this chapter of the OptiX 6.5.0 programming guide:
https://raytracing-docs.nvidia.com/optix6/guide_6_5/index.html#host#multimaterials

There is a variable scope order in OptiX. If you declared the same variable name and type at different scopes (GeometryInstance and Material) only one of them is evaluated and the order is this:
https://raytracing-docs.nvidia.com/optix6/guide_6_5/index.html#programs#program-variable-scoping

For custom geometric primitives the selection which material of multiple is used is decided inside the intersection program by the rtReportIntersection() function which picks the material index. If you look at the table, the intersection program has no access to the material’s variable scope. That variable would need to be declared at the GeometryInstance to be able to read it inside the intersection program.

If you suspect an OptiX error, please try OptiX 6.5.0 as well.

Depending on how that material system itself is working, there might not even be a need to have multiple material nodes and different shader programs.
My OptiX 5 based introduction examples use only a single closest hit program for all different material BSDFs and wouldn’t need to do any of the special cases for built-in triangles.
https://github.com/nvpro-samples/optix_advanced_samples/tree/master/src/optixIntroduction

In that renderer architecture, which material kind and its parameters is selected, can simply be driven by a single index, normally on the geometry instance.
In your case that would require two indices then, one for the per geometry instance material, one for the per group material. You could then drive which one to take with whatever information you have available. That could even happen per pixel with an attribute.

There exist OptiX 7 versions of that renderer architecture in these examples now: https://github.com/NVIDIA/OptiX_Apps