Getting and Setting BaseColor from C++

You don’t set light colors with material instances. You’ll want to use the SetLightColor() function instead.

Hope this helps, be sure to accept the answer if the problem is solved. Thanks!

Instead of reading the base color, why don’t you just store the result in a variable?

`float Rand = FMath::RandRange(0.f, 1.f);

LightColoringMaterialInstanceDynamic->SetScalarParameterValue(FName(“GradientValue”), Rand));

 u->SetLightColor(Rand)

`

This way you don’t have to read from the base colour.

If you’re referring to a Texture2D object, someone breaks down how to sample pixels from that here: https://answers.unrealengine.com/questions/25594/accessing-pixel-values-of-texture2d.html

Another method to read pixels from your material could be to write your material to a render target, and then read from the render target since it is fairly easy to read values from a render target (There is a tutorial on this on the UE4 wiki)

If you’re referring to a Texture2D object, someone breaks down how to sample pixels from that here: https://answers.unrealengine.com/questions/25594/accessing-pixel-values-of-texture2d.html

Another method to read pixels from your material could be to write your material to a render target, and then read from the render target since it is fairly easy to read values from a render target (There is a tutorial on this on the UE4 wiki)

Hey, I made the comment into an answer so you can mark as correct, thanks dude!

I have the following code:

LightColoringMaterialInstanceDynamic = UMaterialInstanceDynamic::Create(LightColoringMaterial, this);
for (ULightComponent* u : LightComponents)
{
	LightColoringMaterialInstanceDynamic->SetScalarParameterValue(FName("GradientValue"), FMath::RandRange(0.f, 1.f));
	u->SetLightColor(LightColoringMaterialInstanceDynamic->GetMaterial()->BaseColor.Constant);
}

That I was hoping I could use to procedurally set some light colours.
I can see that the material instance is getting the color results that I want, but I’m having trouble setting the light colors to be equal to the material instance’s colors.
How do I get the result base color of a material instance?

Thanks!

I’m using SetLightColor in the code above. Problem is I want to get the base colour of the material and set the light colour to that colour.

I don’t think that would work, since it’s setting the light colour to a float, isn’t it?
The gradient material looks like this and I’m trying to set the light colour to its result.

You wont be able to set the light to anything other than a standard Linear Color as far as i know. You wont be able to apply a material to the lights color. You can use a Light Function to give the light a gradient or pattern, but Light Functions cannot change the color, only the shape. If you want the light to be the same color as the material, you can just use the code I showed (You can pass in a float for the light color, just use the FLinearColor constructor to make a color)

Ok, so let’s say I have a texture object and I want to choose a color from the texture object. How would I construct an FLinearColor based on a sample pixel from that texture object?

yup, that seems like the best way to do it! any way to promote the comment to an answer?