# Modify material instance for individual instanced meshes

**URL:** <https://forums.unrealengine.com/t/modify-material-instance-for-individual-instanced-meshes/211860>\
**Category:** UDK Programming and UnrealScript\
**Tags:** unreal-engine\
**Created:** [April 8, 2021, 12:13am UTC](https://forums.unrealengine.com/t/modify-material-instance-for-individual-instanced-meshes/211860 "2021-04-08T00:13:28Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Coldscooter](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Coldscooter](https://forums.unrealengine.com/u/Coldscooter)\
**Post date:** [April 8, 2021, 12:13am UTC](https://forums.unrealengine.com/t/modify-material-instance-for-individual-instanced-meshes/211860/1 "2021-04-08T00:13:28Z")

</div>

I use instanced meshes for the individual building pieces in my game (I use the foliage system to do this).

I’m wondering if there’s any way to apply modifications to the material for the individual instanced meshes. For example if i wanted to allow the player to paint individual pieces different colors.

There is a material node called PerInstanceRandom, which gets a random value between 0.0 to 1.0 on a per instance basis. So it would appear the material editor can identify one instance from another.

You can see in this example (where the shapes are a single InstancedStaticMeshComponent painted with the foliage tool) the material is treating the individual instances separately (not world pixel position):  
 ![](https://content.screencast.com/users/coldgames/folders/Capture/media/a57e0668-a53a-4083-b109-4d4385152926/LWR_Recording.png)  
 ![](https://content.screencast.com/users/coldgames/folders/Capture/media/7ca91513-2630-4754-99fa-ae0405fd51bd/LWR_Recording.png)  
I can store extra data in each instance in InstancedStaticMeshComponent, by setting a float in the PerInstanceSMData[i].Transform.WPlane.W field (which doesn’t seem to be used by anything). If I could just access this data in the material editor, this would unlock huge potential…

Wondering if I could write some HLSL code in the material editor to get a value from the instanced component.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_f7db3d16](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_f7db3d16](https://forums.unrealengine.com/u/anonymous_user_f7db3d16)\
**Post date:** [April 8, 2021, 3:07pm UTC](https://forums.unrealengine.com/t/modify-material-instance-for-individual-instanced-meshes/211860/2 "2021-04-08T15:07:12Z")

</div>

Here is code to edit your foilage. We used this to apply snow or rain to our grass. 🙂 just use it to edit the Scalar Params of what you are after.

local InstancedFoliageActor FoliageActors;  
local MaterialInstanceConstant CurMat, CurMat2, CurMat3;  
local int j;  
local float GetFloat;

```
foreach AllActors(Class'InstancedFoliageActor', FoliageActors)
{
    for(j = 0; j < FoliageActors.InstancedStaticMeshComponents.length; j++)
    {
       CurMat = MaterialInstanceConstant(FoliageActors.InstancedStaticMeshComponents[j].GetMaterial(0));
       CurMat2 = MaterialInstanceConstant(FoliageActors.InstancedStaticMeshComponents[j].GetMaterial(1));            
       CurMat3 = MaterialInstanceConstant(FoliageActors.InstancedStaticMeshComponents[j].GetMaterial(2));

        if (GRI.GetWeatherSetting() == 3)
        {
            if(CurMat != none)
            {
              CurMat.GetScalarParameterValue('Rain',GetFloat);                   
              if(GetFloat != 1)
                CurMat.SetScalarParameterValue('Rain', 1);
            }
            if(CurMat2 != none)
            {
              CurMat2.GetScalarParameterValue('Rain',GetFloat);  
              if(GetFloat != 1)
                CurMat2.SetScalarParameterValue('Rain', 1);
            }
            if(CurMat3 != none)
            {
              CurMat3.GetScalarParameterValue('Rain',GetFloat);                    
              if(GetFloat != 1)
                CurMat3.SetScalarParameterValue('Rain', 1);
            }
        }
        else if (GRI.GetWeatherSetting() == 4)
        {
            if(CurMat != none)
            {
              CurMat.GetScalarParameterValue('Snow',GetFloat); 
              if(GetFloat != 2)
                CurMat.SetScalarParameterValue('Snow', 2);
            }
            if(CurMat2 != none)
            {
              CurMat2.GetScalarParameterValue('Snow',GetFloat);  
              if(GetFloat != 2)
                CurMat2.SetScalarParameterValue('Snow', 2);
            }
            if(CurMat3 != none)
            { 
              CurMat3.GetScalarParameterValue('Snow',GetFloat);                 
              if(GetFloat != 2)
                CurMat3.SetScalarParameterValue('Snow', 2);
            }
        }
    }
}

```

That should work for what your after just edit it as you need.

---

<div class="post-metadata">

**Author:** ![Coldscooter](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Coldscooter](https://forums.unrealengine.com/u/Coldscooter)\
**Post date:** [April 8, 2021, 5:51pm UTC](https://forums.unrealengine.com/t/modify-material-instance-for-individual-instanced-meshes/211860/3 "2021-04-08T17:51:17Z")

</div>

Hey . I think you misunderstood what I am trying to do.

In your example, you are applying material changes that will affect every instanced mesh in each InstancedStaticMeshComponent.

I am trying to apply material changes to individual instanced meshes.

Each InstancedStaticMeshComponent has an array PerInstanceSMData. This array holds transform data for each instanced mesh to display.

In my example above, the shapes are all from one InstancedStaticMeshComponent. The mesh is rendered once, then instanced six times (six entries in its PerInstanceSMData array).

In the material editor, the PerInstanceRandom is able to differentiate between the individual instanced meshes, so I was hoping there might be a way to access data on each instanced mesh within the material. If I was able to do this, then I could potentially store some extra data in each PerInstanceSMData entry, and use that in the material to show different colors per instanced mesh, etc.

This way I could control rendering different colored shapes, from a single instanced static mesh.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_f7db3d16](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_f7db3d16](https://forums.unrealengine.com/u/anonymous_user_f7db3d16)\
**Post date:** [April 8, 2021, 7:28pm UTC](https://forums.unrealengine.com/t/modify-material-instance-for-individual-instanced-meshes/211860/4 "2021-04-08T19:28:32Z")

</div>

> [@](#):
>
> ` for(j = 0; j < FoliageActors.InstancedStaticMeshComponents.length; j++)`

what if you go like this  
for(j = 0; j \< FoliageActors.InstancedStaticMeshComponents.PerInstanceSMData.length; j++)

will that get your 6 instances?

---

<div class="post-metadata">

**Author:** ![Coldscooter](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Coldscooter](https://forums.unrealengine.com/u/Coldscooter)\
**Post date:** [April 8, 2021, 7:53pm UTC](https://forums.unrealengine.com/t/modify-material-instance-for-individual-instanced-meshes/211860/5 "2021-04-08T19:53:58Z")

</div>

Yes, but these are just array entries (they just dictate where instances of the mesh should be rendered).

I’m trying to find a way to identify these in the material editor.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_f7db3d16](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_f7db3d16](https://forums.unrealengine.com/u/anonymous_user_f7db3d16)\
**Post date:** [April 9, 2021, 12:37pm UTC](https://forums.unrealengine.com/t/modify-material-instance-for-individual-instanced-meshes/211860/6 "2021-04-09T12:37:52Z")

</div>

If you were to set up one of these in your materials. then i think you could scan it with the below line and find the texture you are after.

GetParameterDesc(name ParameterName, out String OutDesc);

MaterialInstanceConstant(FoliageActors.InstancedStaticMeshComponents[j].Materials[i].GetParameterDesc(name ParameterName, out String OutDesc);

Just give each texture a same ParameterName name. but a different OutDesc and then you should be able to identify the ones you need and do what your after.

---

<div class="post-metadata">

**Author:** ![Coldscooter](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Coldscooter](https://forums.unrealengine.com/u/Coldscooter)\
**Post date:** [April 9, 2021, 5:19pm UTC](https://forums.unrealengine.com/t/modify-material-instance-for-individual-instanced-meshes/211860/7 "2021-04-09T17:19:22Z")

</div>

What you are describing here again would affect all instances of each InstancedStaticMeshComponent.

An InstancedStaticMeshComponent extends from StaticMeshComponent, but can render multiples of its static mesh. So a single InstancedStaticMeshComponent could be rendering 1000’s of meshes (which are all copies of a single mesh).

All of the individual instanced are defined by the PerInstanceSMData array (which includes a matrix of transform data, which holds data of the location and rotation for each instance).

What I am trying to figure out, is if I can somehow access the data in the PerInstanceSMData array from the material itself (not from unrealscript). This way I could render different colors via the material itself based on data I’ve set in the PerInstanceSMData.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_f7db3d16](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_f7db3d16](https://forums.unrealengine.com/u/anonymous_user_f7db3d16)\
**Post date:** [April 9, 2021, 5:35pm UTC](https://forums.unrealengine.com/t/modify-material-instance-for-individual-instanced-meshes/211860/8 "2021-04-09T17:35:13Z")

</div>

the only way i know of getting a hold of what is inside a material is going thru a param of its instance to get access into it. To change what ever it is your after.

---

<div class="post-metadata">

**Author:** ![Coldscooter](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Coldscooter](https://forums.unrealengine.com/u/Coldscooter)\
**Post date:** [April 9, 2021, 7:16pm UTC](https://forums.unrealengine.com/t/modify-material-instance-for-individual-instanced-meshes/211860/9 "2021-04-09T19:16:44Z")

</div>

Just to clarify, the issue is not getting parameters from a material, it’s the other way around (accessing US data from within the material expression).

---

<div class="post-metadata">

**Author:** ![Chosker](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/chosker/32/710746_2.png) [@Chosker](https://forums.unrealengine.com/u/Chosker)\
**Post date:** [April 10, 2021, 7:43am UTC](https://forums.unrealengine.com/t/modify-material-instance-for-individual-instanced-meshes/211860/10 "2021-04-10T07:43:18Z")

</div>

using the same material is a hard requirement for meshes to be instanced, which is why the material is always applied per mesh-batch and not per instance. this is true even in the editor.  
if you want a different material for an instance you have to de-instance it. you can either delete the instance and create a new non-instanced version (regular static mesh), or you’ll need to somehow create a new mesh entry in your list of instanced meshes, assign a different material, and initialize an instance of it.

regarding PerInstanceRandom: under the hood the engine is capable of passing a number of per-instance-data to the shader so that it can use data that will be unique per instance, but:

- this is just data passed to the same shader used with instancing rendering. it does not decouple the instancing (i.e. it does not apply a different shader/material)
- these are just a per-instance buffer, i.e. one float/vector for the entire instance (you can’t pass one per vertex/pixel to do painting), and no passing of textures
- in UE3 it’s not customizable, there’s only the hardcoded PerInstanceRandom (I believe UE4 has a bit more access to perinstancedata now but I could be wrong)

so while you could technically pass a solid color for each instance to color it uniformly like in your PerInstanceRandom example, UDK doesn’t really expose any of this functionality (and IIRC, even if you had UE3 source access it’s not really developed in an extensible way at all)

unfortunately I can’t suggest a solution with perinstance data in mind

---

<div class="post-metadata">

**Author:** ![Coldscooter](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Coldscooter](https://forums.unrealengine.com/u/Coldscooter)\
**Post date:** [April 10, 2021, 7:17pm UTC](https://forums.unrealengine.com/t/modify-material-instance-for-individual-instanced-meshes/211860/11 "2021-04-10T19:17:42Z")

</div>

Thanks for your input @Chosker. Yes I don’t think it’s going to be possible without updating the engine source. Which is a shame as it seems very possible to be done (based on the example with PerInstanceRandom). Oh well. Thanks anyway.

---

<div class="post-metadata">

**Author:** ![Coldscooter](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Coldscooter](https://forums.unrealengine.com/u/Coldscooter)\
**Post date:** [February 21, 2024, 8:59pm UTC](https://forums.unrealengine.com/t/modify-material-instance-for-individual-instanced-meshes/211860/12 "2024-02-21T20:59:59Z")

</div>

@Chosker . I thought I would give this another shot…

I’m still trying to find a way to access any properties of a single instance (from an instanced static mesh) within the material editor.

The structure within InstancedStaticMeshComponent.uc for each instance is:

```auto
struct immutablewhencooked native InstancedStaticMeshInstanceData
{
	var matrix Transform;
	var vector2d LightmapUVBias;
	var vector2d ShadowmapUVBias;

	structcpptext
	{
		// Serialization
		friend FArchive& operator<<(FArchive& Ar, FInstancedStaticMeshInstanceData& InstanceData)
		{
			// @warning BulkSerialize: FInstancedStaticMeshInstanceData is serialized as memory dump
			// See TArray::BulkSerialize for detailed description of implied limitations.
			Ar << InstanceData.Transform << InstanceData.LightmapUVBias << InstanceData.ShadowmapUVBias;
			return Ar;
		}
	}
};
```

I create/add/remove these structs from unrealscript (the the PerInstanceSMData array) to add/remove instances to/from the ISMC.

If you look in the HLSL code in the material editor, here is the implementation of PerInstanceRandom:

```auto
float GetPerInstanceRandom(FMaterialPixelParameters Parameters)
{
#if USE_INSTANCING
	return Parameters.PerInstanceParams.x;
#else
	return 0.f;
#endif
}
```

I’m not really familiar with using HLSL code, but I’m curious if by using it i could access more info on the individual instances.

I’m wondering what else is stored in this Parameters.PerInstanceParams structure. Perhaps some details of the instance transform matrix, or anything really…

If there was anyway to access any data from InstancedStaticMeshInstanceData from within the material editor, then i could encode/hide data in the struct to use in the material editor.

Just bumping this thread in case anyone has any thoughts on whether HLSL could be used for this purpose.

---

<div class="post-metadata">

**Author:** ![Frenetic](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/frenetic/32/1142542_2.png) [@Frenetic](https://forums.unrealengine.com/u/Frenetic)\
**Post date:** [February 21, 2024, 9:10pm UTC](https://forums.unrealengine.com/t/modify-material-instance-for-individual-instanced-meshes/211860/13 "2024-02-21T21:10:25Z")

</div>

If you want to get into the instance of a foliage instanced mesh, look into what is going on here:

[![](https://d3kjluh73b9h9o.cloudfront.net/original/4X/4/0/e/40ed82fd814691bb5f4b28089c170e8c5fecc3d0.jpeg "Individual Foliage Logic in UE5 [Harvesting, Damage, Particles]") ](https://www.youtube.com/watch?v=6sjc4dqoCF0)

tl;dr - make a child of instanced-foliage-mesh and on-damage/on-whatever, set custom-primative-data values which are then read in by the material shader.

EDIT: I didn’t see the forum, my bad! ☹

---

<div class="post-metadata">

**Author:** ![Coldscooter](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Coldscooter](https://forums.unrealengine.com/u/Coldscooter)\
**Post date:** [February 21, 2024, 9:13pm UTC](https://forums.unrealengine.com/t/modify-material-instance-for-individual-instanced-meshes/211860/14 "2024-02-21T21:13:08Z")

</div>

This is the UDK (UE3) forum. This functionality is not available in UE3.

---

<div class="post-metadata">

**Author:** ![OnyeNacho](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/onyenacho/32/1122220_2.png) [@OnyeNacho](https://forums.unrealengine.com/u/OnyeNacho)\
**Post date:** [March 14, 2026, 11:12pm UTC](https://forums.unrealengine.com/t/modify-material-instance-for-individual-instanced-meshes/211860/15 "2026-03-14T23:12:00Z")

</div>

Thank you mate!

I swear too many people keep posting UE4 and UE5 spam on our only safe space for UDK content… Its getting insane now having to browse through that slop…
