Falcor
Falcor copied to clipboard
Store info per Mesh
Hi,
I need to store some information per Mesh. I put the information on a Texture which I can access but I need the index of the geometry to access the Texture.
Is there any way to do that?
Thank you
Raytracing or raster?
Raytracing. I tried the InstanceID() but I get the same id for any object.
InstanceID()
won't help, since we group geometries together into a single instance and there's no way to get the geometryID in DXR.
You can do something like:
RtProgramVars::SharedPtr pVars;
auto& hitVars = pVars->getHitVars(rayIndex);
for (auto& hit : hitVars)
{
// The hit-vars are per mesh, assuming you have a `PerMeshCB` in the hit-program you can do:
hit["PerMeshCB"]["MeshIndex"] = meshIndex;
}
The code basically leverages the local-root-signature to expose data to a specific geometry
Nice!!! But where does the "meshIndex" come from?
From you :)
This index should correlate to the order in which you created the texture.
There's also RtScene::getInstanceID()
which given the current mesh you are processing will return the hit-vars index.
Sorry, I am not sure if I got correctly.
In that case the "meshIndex" is an array of indices or single value?
single value per mesh
ok, but how do I know which one is being hit?
DXR tells you that. Do you know how local-root-signature works?
To make it clear, I am testing in a simple ray tracing algorithm which gets some information on the hit and show on screen.
for (auto pVars : mpRays->getHitVars(0)) {
pVars["gWsPos"] = wsPos;
pVars["gWsNorm"] = wsNorm;
pVars["gMatDif"] = matDif;
pVars["gMatSpec"] = matSpec;
pVars["gMatExtra"] = matExtra;
pVars["gMatEmissive"] = matEmit;
}
I am understanding that I am going to add the hit["PerMeshCB"]["MeshIndex"] = meshIndex; like this:
for (auto pVars : mpRays->getHitVars(0))
{
pVars["gWsPos"] = wsPos;
pVars["gWsNorm"] = wsNorm;
pVars["gMatDif"] = matDif;
pVars["gMatSpec"] = matSpec;
pVars["gMatExtra"] = matExtra;
pVars["gMatEmissive"] = matEmit;
pVars["MeshIndex"] = meshIndex;
}
And as happens with the other information, dxr should give me the meshIndex of the hit.
But how do I include the meshIndex on the object?
Where do you get wsPos
and friends from?
I assume that somewhere in your code you loop over all the meshes (you mentioned that you initialize a texture with a per-mesh value). If there is indeed a loop, the index there is your meshIndex
.
The new meshIndex
is a user defined value. You can make it a float2
and store the per-mesh coordinate into the texture. How you map mesh to coordinate is up to you, just apply the same logic to meshTextCrd
(or whatever you want to call it)
As each Mesh that I need the index have a different material id, I did a map between the gMaterial.id and the information that I want.
It worked. =)
About your question: I get the wsPos using the getTexture method and setting it to a constant value before starting the ray tracing.
By the way, I am storing new information on a shared texture, can I just create a array or do I have to use texture? Similar to this:
shared cbuffer PerFrameCB
{
float4x4 invView;
float4x4 invModel;
float2 viewportDims;
float tanHalfFovY;
};
And include an array on this cbuffer.
my solution is to create a new Scene class , it has the material like properties and GUI editor for each render object