Does Raytracing support instancing in HDRP 12.1?

Hi Guys

I does raytracing support instancing?

I’m asking as I can’t figure out why instanced prefabs do not work with raytraced shadows or raytraced reflections. It also doesn not seem to work AT ALL with pathtracing.

Even if I apply regular HDRP/Lit shaders to the plants and refresh everything, it still does not work.

However, if I directly drag the prefabs into the scene and manually duplicate them, then everything is fine.

This makes me believe there is some kinf of issue with either instances, or Vegetation Studio Pro

Hey, If by instancing you mean using this function at runtime, it is indeed working and supposed to. (i.e you’re supposed to see your instanced object in reflections and they should have raytraced shadows… etc)

Maybe your setup have something more to it.
If you can’t what it is, please make a report with a simple repro, drop the case number here, and we’ll look into it.
Thanks !

Hi @chap-unity sorry for the late reply. Here is a reply I received from Lennart from Vegetation Studio Pro:

“Apparently Unity has not made any API to add meshes rendered with the instancing API to the raytracing structure”

Is that statement correct? If not, could you please point me to the correct information?

Apparently that is the reason why I can’t get my spawned objects to work with Raytracing. (Objects spawned with Vegetation Studio Pro)

Some further info:

Raytracing does not suppor the 1st one in that list. But what about the other 2?

From the sounds of it, DrawMesh uses CPU, but DrawMeshInstanced uses GPU…

Hi!

Graphics.Draw* functions don’t make sense in ray tracing since ray tracing only works using a RayTracingAccelerationStructure and the source geometry in GPU memory. You can’t “draw” into an acceleration structure. If a geometry is not in the acceleration structure then rays can’t hit it. There’s a plan to add similar functions to RayTracingAccelerationStructure where we could add instances of a Mesh using a RayTracingAccelerationStructure.AddInstances function. For example a very possible function signature could be:

RayTracingAccelerationStructure.AddInstances(Mesh mesh, int submeshIndex, Material material, Matrix4x4[ ] matrices, int count, MaterialPropertyBlock properties);

This function should be a cheap alternative to create many many ray tracing instances of the same Mesh into the acceleration structure where only the transformation matrix is different between instances. The shader setup is the same for all instances and will happen only once (in DXR, there will be only one hit group setup for all instances).

2 Likes

Thanks @INedelcu
Do you know a rough ETA of when such a feature could become available?

Since its also mostly for foilage, as I understand the wind animation is mostly handled by the shaders, at the same time, could we get something that works with the instancing to also support the shader wind animations?

Currently this is a major hurdle that prevent us from using DXR/Pathtracing for exterior Film sets

I’ll be working on that function in the near future. I don’t have a general solution for where you change vertex positions individually in a vertex shader (ie wind animation). The requirement for building an acceleration is that the source vertex buffer to be in main GPU memory. If you have a vertex shader for that you use in rasterization where you animate vertices individually then that data is kept in GPU registers, then the acceleration structure build process has no idea about that. I’ll think about it but I don’t see a cheap solution.

Yeah since its for film and offline rendering with DXR, we’re not overly worried about realtime performance, but of course if it ends up being so bad that frames take longer than traditional DCC rendering, then there’s no point of course…

This sounds great! I’m looking foward to it. (my main project is a racing game with a lot of instances of the same wheels, cones, fences, guard rails, etc)

1 Like

I really need this function! I’m using DrawMeshInstancedIndirect but I cannot add the instanced meshes to the ray tracing acceleration structure because it requires non-existant Mesh Renderers.

I’m thinking about creating a “hidden” gameobject with a renderer for each instances in a layer not visible to the camera but I really don’t like this workaround.

1 Like

I started working on this today!

7 Likes

Hi @INedelcu
This is the thread I meant to have posted in.
How is work coming along for this?

Hi @newguy123

I figured out that we first need the equivalent of Graphics.RenderMesh in ray tracing.

There will be a new function in 2023.1 RayTracingAccelerationStructure.AddInstance which will use Mesh instead of MeshRenderer. This will add some nice flexibility regarding what can be added to an acceleration structure.

2 Likes

Also figured out that prefabs do not cast raytraced shadows for now.
Please “fix” that soon, thanks!

We also desperately need foilage to be able to blow in the wind, while using raytracing/pathtracing, and make LOOOOOADS of duplicates of foilage all over our scenes, using non terrain imported meshes

2 Likes

Hey, I was worried about this one and just tried to instantiate a few prefabs with multiple lights (dir and punctals) and it seems to work as expected on latest version.
Meaning, instantiated prefabs at runtime are indeed added automatically to the ray tracing acceleration structure and casts ray traced shadows.

Do you care to explain a bit more about your setup ? Or have a repro project by any chance ?
Thanks.

I copied some torusknots in 3dsmax as instances and imported the fbx in Unity.
All knots share the same original mesh, so 3dsmax instances work fine in Unity.
But the knots do not cast raytraced shadows.

I did not instantiate anything at runtime.

I’d be happy to help if you can manage to give a repro and/or report a bug !
Thanks :slight_smile:

any updates on this topic? running into a similar problem…

I finished RayTracingAccelerationStructure.AddInstances (the equivalent of Graphics.RenderMeshInstanced). It will be available in one of the future 2023.1 alpha versions.


9 Likes