Download

Get Color from a Gradient

Is there any functionality in Unreal right now for getting a color from a gradient?
In Unity, for example, you can make a gradient and choose a random color from this gradient or add a texture and get a color from the texture based on coordinates.
I don’t see anything in Unreal Engine 4 that lets me do this, making problems like heatmaps very complicated.

For my heatmap, I ended up writing a gradient in C++ that generated RGB values.
This is a really poor and inflexible solution.

Thanks!

Inside of the material editor, just hold down the 3 key and click the viewport. It will drop a float3 which if you click on, will have a color box that you can select a color with. From there, you’d just lerp between black(or a different color) and the main color; to obtain various blends. That will create a gradient.

There is one method to get pixel color value from texture with c++
textureResource->ReadPixels(ColorBuffer)

maybe this helps - https://wiki.unrealengine.com/Render_Target_Lookup

Thanks guys, I’m going to see if I can manage to make a general purpose gradient and color picker tool with these hints.

You can do it directly in the shader. You just place your gradient texture sample and then specify a single scalar for the UV position. Ie, if you specify the UVs to be float2(0.5,0.5) this would sample the single color in the middle of the gradient and then you could do whatever you want in the shader with it. It doesn’t really need a feature to describe the behavior since its just a simple lookup.

I think you just need to take your mask, plug it into a AppendMany, and put the RG output into the UVs of your gradient texture. You can also use a constant on the green channel to move where it’s sampling the gradient textures up and down, so you could have multiple gradients. Or you could even animate the gradients.

The gradient texture above the other texture sample is the same texture.

This ended up doing the trick.
I figured there was something stupid easy that would work.
Thanks, Ryan!
0f5e0ce18c278402c7de1b7f7f2cf27b34ee6bd6.png

This seemed to do the trick:

	FTexture2DMipMap* MipMap = &LightColoringTexture->PlatformData->Mips[0];
	FByteBulkData* RawImageData = &MipMap->BulkData;
	FColor* FormatedImageData = static_cast<FColor*>(RawImageData->Lock(LOCK_READ_ONLY));
	uint32 TextureWidth = MipMap->SizeX, TextureHeight = MipMap->SizeY;
	for (ULightComponent* u : LightComponents)
	{
		FColor PixelColor = FormatedImageData[FMath::RandRange(0, TextureWidth)]; // only getting from x
		u->SetLightColor(PixelColor);
	}
	RawImageData->Unlock();

Based on this: Accessing pixel values of Texture2D - UE4 AnswerHub