Announcement

Collapse
No announcement yet.

Instance Index for Instanced Static Mesh in the Material Editor

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

    [FEATURE REQUEST] Instance Index for Instanced Static Mesh in the Material Editor

    Hey,

    I would like to have access for the instance index for instanced static meshes in the material editor.
    I think it would provide more information about the instanced static mesh data, we can still generate a random number in a shader from the instance index on the fly. So i dont see why we actually need a Random Number Over the instanced static mesh index.
    My current problem with the Per instance random is that it generates a new random number everytime i remove or add a new instance. (So lets say my first instance got random number 56, then i add a new instance, the first instance gets a new random number 77 and this information is not reliable enough.
    Also an Instance To World Location node would be also helpful.

    It would be really good to use this for audio visualization, I am looking forward to have better access and use custom buffers in the material editor for instanced meshes.

    Thanks

    #2
    I was just looking for a way to get the instance number, or if there's even a way to set custom data per instance, such as a UV offset.

    Is the instance index, or custom data, accessible in custom shader code?

    Definitely pushing for this feature as well :-)

    / Patric

    Comment


      #3
      This would be very neat indeed. One user packed index into the scale of the instance but this won’t work if scale is variable per instance.
      Youtube Channel

      Comment


        #4
        +1 - I have been trying to figure out how-to reference a specific instance when foliage is laid out using the foliage paint tool and wanting to 'do something' with an OnOverlappedBegin node.

        Comment


          #5
          Yes! Please!

          I hacked the per-instance random to achieve this. UInstancedStaticMeshComponent::CreateSceneProxy is a good place to override the PerInstanceRenderData->InstancedBuffer data to reset the W component of each instances position. The W component stores the per-instance random.

          So, something like this:

          Code:
          FPrimitiveSceneProxy* UMappedInstancedStaticMesh::CreateSceneProxy()
          {
              auto proxy = Super::CreateSceneProxy();
          
              if(!this->PerInstanceRenderData.IsValid())
                  return proxy;
          
              // some custom code goes here
          
              // access the FInstanceStream16/32, where we can set the W component of the instances positions. This stores the per-instance random.
              if(bSupportsVertexHalfFloat)
              {
                  auto constRawData = static_cast<const FInstanceStream16*>(this->PerInstanceRenderData->InstanceBuffer.GetRawData());
          
                  FInstanceStream16 * RESTRICT instanceData = const_cast<FInstanceStream16*>(constRawData);
          
                  // rewrite the instance randoms ids to instance ids
                  for(int32 instance = 0; instance < n; ++instance)
                  {
                      instanceData[instance].InstanceOrigin.W = float( instance ) / scale;
                  }
              }
              else
              {
                  auto constRawData = static_cast<const FInstanceStream32*>(this->PerInstanceRenderData->InstanceBuffer.GetRawData());
          
                  FInstanceStream32 * RESTRICT instanceData = const_cast<FInstanceStream32*>(constRawData);
          
                  // rewrite the instance randoms ids to instance ids
                  for(int32 instance = 0; instance < n; ++instance)
                  {
                      instanceData[instance].InstanceOrigin.W = float( instance ) / scale;
                  }
              }
          }
          UMappedInstancedStaticMesh is the name of my ISMC subclass.
          Last edited by timtimmy; 04-16-2018, 01:14 PM.

          Comment


            #6
            Originally posted by timtimmy View Post
            Yes! Please!

            I hacked the per-instance random to achieve this. UInstancedStaticMeshComponent::CreateSceneProxy is a good place to override the PerInstanceRenderData->InstancedBuffer data to reset the W component of each instances position. The W component stores the per-instance random.

            So, something like this:

            Code:
            ....
            UMappedInstancedStaticMesh is the name of my ISMC subclass.
            What is "scale" in your code?
            Youtube Channel

            Comment


              #7
              Oops, that was declared in some of the code I cut out. That line could read:

              Code:
                  instanceData[instance].InstanceOrigin.W = instance;
              In addition to the per-instance id, I think that it would also be convenient to have access to the number of instances in a material node.

              I've been using this per-instance id hack along with a dynamic texture, that's updated each frame, to pass any number of parameters that I want to each instance. I encode the instance count in the texture (it's needed for indexing into the texture), but it would be better if Unreal passed it as a constant to the shader.

              Comment

              Working...
              X