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

**URL:** <https://forums.unrealengine.com/t/ue5-material-nodes-array-loop-lookup-filter-method/1257269>\
**Category:** Programming & Scripting\
**Tags:** Graphics, unreal-engine, visual, editor, Rendering, Lighting, meshes, Shaders, Blueprint, question, CPP, UMG\
**Created:** [August 10, 2023, 11:35pm UTC](https://forums.unrealengine.com/t/ue5-material-nodes-array-loop-lookup-filter-method/1257269 "2023-08-10T23:35:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Roy\_Wierer.Seda145](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/roy_wierer.seda145/32/906551_2.png) [@Roy\_Wierer.Seda145](https://forums.unrealengine.com/u/Roy_Wierer.Seda145)\
**Post date:** [August 10, 2023, 11:35pm UTC](https://forums.unrealengine.com/t/ue5-material-nodes-array-loop-lookup-filter-method/1257269/1 "2023-08-10T23:35:12Z")

</div>

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? 🙂 🍕

---

<div class="post-metadata">

**Author:** ![NKRVVI](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@NKRVVI](https://forums.unrealengine.com/u/NKRVVI)\
**Post date:** [May 9, 2024, 11:36am UTC](https://forums.unrealengine.com/t/ue5-material-nodes-array-loop-lookup-filter-method/1257269/2 "2024-05-09T11:36:16Z")

</div>

Hi, did you find a solution for this?

---

<div class="post-metadata">

**Author:** ![Roy\_Wierer.Seda145](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/roy_wierer.seda145/32/906551_2.png) [@Roy\_Wierer.Seda145](https://forums.unrealengine.com/u/Roy_Wierer.Seda145)\
**Post date:** [May 9, 2024, 11:39pm UTC](https://forums.unrealengine.com/t/ue5-material-nodes-array-loop-lookup-filter-method/1257269/3 "2024-05-09T23:39:12Z")

</div>

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

 ![afbeelding](https://d3kjluh73b9h9o.cloudfront.net/original/4X/c/8/b/c8b1f9d4da24cd4567233588ec477099ccaf870f.png)

![afbeelding](https://d3kjluh73b9h9o.cloudfront.net/original/4X/3/c/4/3c4584e430fadc4970b633ab6f57620edba26390.png)

```auto
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;

```

---

<div class="post-metadata">

**Author:** ![system](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/1/3/713c9d3f58553f0de89543d76a8a3a2779dc9fa4.png) [@system](https://forums.unrealengine.com/u/system)\
**Post date:** [June 8, 2024, 11:39pm UTC](https://forums.unrealengine.com/t/ue5-material-nodes-array-loop-lookup-filter-method/1257269/4 "2024-06-08T23:39:20Z")

</div>

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