[UE5] material nodes, array loop / lookup / filter method?

I spend most of my time programming, not on the nodes in Unreal’s material system.
I’m looking for a node which let me do something like:

“Given an array of vectors (RGB), look up RGB value X and return the closest color from given array”

This idea is quite a simple distance comparison over the elements of an array, a function, yet I don’t see such thing in the massive list of material nodes. I could do this in HLSL probably but documentation states this will not be optimized.

As a bonus, this “array” needs to be a parameter of RGB values somehow. A texture sample is acceptable in this case as long as I can pixel loop to exact values.

What do I do? :slightly_smiling_face: :pizza:

1 Like

Hi, did you find a solution for this?

I don’t call this finalized but it seems to do the job, made it a long time ago:

afbeelding

float3 Result = float3(0,0,0);

float2 PaletteDimensions;
PaletteTexture.GetDimensions(PaletteDimensions.x, PaletteDimensions.y);

float2 ClosestColorUV = float2(0, 0);
float ClosestDistance = 99999999;

for (int x = 0; x <= PaletteDimensions.x; x++) {
	for (int y = 0; y <= PaletteDimensions.y; y++) {
			const float2 CurrentPaletteUV = float2(x / PaletteDimensions.x, y / PaletteDimensions.y);
		const float3 PaletteSample = Texture2DSample(PaletteTexture, PaletteTextureSampler, CurrentPaletteUV);

		const float Distance = length(Image - PaletteSample);
		if (Distance < ClosestDistance) {
			ClosestDistance = Distance;
			ClosestColorUV = CurrentPaletteUV;
		}
	}
}

Result = Texture2DSample(PaletteTexture, PaletteTextureSampler, ClosestColorUV);

return Result;
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.