# How do i convert this material function to C++?

**URL:** <https://forums.unrealengine.com/t/how-do-i-convert-this-material-function-to-c/2222076>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine, editor, Rendering, CPP, Blueprint, question\
**Created:** [December 15, 2024, 6:52am UTC](https://forums.unrealengine.com/t/how-do-i-convert-this-material-function-to-c/2222076 "2024-12-15T06:52:33Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![SophiaWolfie](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/sophiawolfie/32/1008648_2.png) [@SophiaWolfie](https://forums.unrealengine.com/u/SophiaWolfie)\
**Post date:** [December 15, 2024, 6:52am UTC](https://forums.unrealengine.com/t/how-do-i-convert-this-material-function-to-c/2222076/1 "2024-12-15T06:52:33Z")

</div>

So i need to convert this function to C++, Hue Shift of the material editor:

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/5/4/0/540021266553cbf7a61d32340a4d6c397d1aebd2.png)  
 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/4/4/6/4461468e140814372c44c6a55d905ea41a0b4cc9.png)

There seems to be no corresponding function in the engine for the HueShift, because its a material function that is made of other functions, of course…

So i tried looking for the RotateAboutAxis function and gives me this super weird and obscure function that seems to need a material:

UMaterialExpressionRotateAboutAxis()

What should be my course of action in this situation?

---

<div class="post-metadata">

**Author:** ![3dRaven](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/3draven/32/906552_2.png) [@3dRaven](https://forums.unrealengine.com/u/3dRaven)\
**Post date:** [December 15, 2024, 7:54am UTC](https://forums.unrealengine.com/t/how-do-i-convert-this-material-function-to-c/2222076/2 "2024-12-15T07:54:48Z")

</div>

What do you need the function to change in C++? Are you planning on hue shifting a UTexture or FVector? Are you tying to get the same functionality out of the material editor and into a game world ready function?

---

<div class="post-metadata">

**Author:** ![RecourseDesign](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/recoursedesign/32/1194503_2.png) [@RecourseDesign](https://forums.unrealengine.com/u/RecourseDesign)\
**Post date:** [December 15, 2024, 8:06am UTC](https://forums.unrealengine.com/t/how-do-i-convert-this-material-function-to-c/2222076/3 "2024-12-15T08:06:46Z")

</div>

If you’ve got an FLinearColor structure you can use the “LinearRGBToHSV” method then change the R value (Hue).

---

<div class="post-metadata">

**Author:** ![SophiaWolfie](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/sophiawolfie/32/1008648_2.png) [@SophiaWolfie](https://forums.unrealengine.com/u/SophiaWolfie)\
**Post date:** [December 15, 2024, 9:50pm UTC](https://forums.unrealengine.com/t/how-do-i-convert-this-material-function-to-c/2222076/4 "2024-12-15T21:50:06Z")

</div>

Thanks.  
Basically, i use the hue shift instead of a 3 Per Instance Custom Data to change the color of my Units.  
So i start with a color Green and hue shift with just 1 PICD. That seems to me that it is better in performance, 1 PICD vs 3 PICD.  
But the problem is that i need to calculate the hue shift outside so that i SetCustomDataValue() in C++ with the right colors. You get me 🦁?

---

<div class="post-metadata">

**Author:** ![SophiaWolfie](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/sophiawolfie/32/1008648_2.png) [@SophiaWolfie](https://forums.unrealengine.com/u/SophiaWolfie)\
**Post date:** [December 15, 2024, 9:55pm UTC](https://forums.unrealengine.com/t/how-do-i-convert-this-material-function-to-c/2222076/5 "2024-12-15T21:55:10Z")

</div>

I tried that but never get accurate values.  
The hue shift requires a color input and then the hue shift float value.  
What would the LinearRGBToHSV values correspond to?  
In my case my hue shift has a Green input: 0, 1, 0 🟩  
So a float of 0.65 shift, would turn it into a reddish color 🟥.  
So what would be the corresponding input color and hue shift that would match the LinearRGBToHSV?  
This is quite confusing for my adhd brain.

---

<div class="post-metadata">

**Author:** ![RecourseDesign](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/recoursedesign/32/1194503_2.png) [@RecourseDesign](https://forums.unrealengine.com/u/RecourseDesign)\
**Post date:** [December 15, 2024, 10:03pm UTC](https://forums.unrealengine.com/t/how-do-i-convert-this-material-function-to-c/2222076/6 "2024-12-15T22:03:05Z")

</div>

The Hue is represented as an angle between 0 and 360, so it’s 0.65x360 that you want to add:

```FLinearColor
	FLinearColor hsv = col.LinearRGBToHSV();
	hsv.R = fmod( hsv.R + ( 0.65 * 360.0f ) , 360.0f );
	FLinearColor rgb = hsv.HSVToLinearRGB();

```

---

<div class="post-metadata">

**Author:** ![SophiaWolfie](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/sophiawolfie/32/1008648_2.png) [@SophiaWolfie](https://forums.unrealengine.com/u/SophiaWolfie)\
**Post date:** [December 15, 2024, 10:18pm UTC](https://forums.unrealengine.com/t/how-do-i-convert-this-material-function-to-c/2222076/7 "2024-12-15T22:18:34Z")

</div>

> [@RecourseDesign](#):
>
> ```auto
> FLinearColor hsv = col.LinearRGBToHSV();
> hsv.R = fmod( hsv.R + ( 0.65 * 360.0f ) , 360.0f );
> FLinearColor rgb = hsv.HSVToLinearRGB();
> 
> ```

I tested this. ^Somethings not right.

So this is what happens in the material:  
Hue shift of green (0,1,0), by 0.65, becomes this reddish color, (0.99, 0, 0.064)

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/e/0/3/e03c3929e2e1058858b0bc878ef9c0dfe2e1d019.jpeg)  
 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/d/7/3/d7349ff41051f4640ebdc0d9a0ffc8a9ef518672.png)

But when i try to use your formula, I get these values (1, 0, 0.099)

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/e/9/6/e96d83567a457f8ba6fb7f27ec76335cbcaa0932.png)

---

<div class="post-metadata">

**Author:** ![RecourseDesign](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/recoursedesign/32/1194503_2.png) [@RecourseDesign](https://forums.unrealengine.com/u/RecourseDesign)\
**Post date:** [December 15, 2024, 10:48pm UTC](https://forums.unrealengine.com/t/how-do-i-convert-this-material-function-to-c/2222076/8 "2024-12-15T22:48:15Z")

</div>

Yeah they won’t be absolutely exact as they’re using different algorithms (and floating point units).

The FLinearColor one is more accurate - shifting the hue with photoshop gives almost identical results to that.

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/6/2/0/620b0cdfd0d908445e9f8633952cbe46bb332a15.png)

---

<div class="post-metadata">

**Author:** ![SophiaWolfie](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/sophiawolfie/32/1008648_2.png) [@SophiaWolfie](https://forums.unrealengine.com/u/SophiaWolfie)\
**Post date:** [December 15, 2024, 11:06pm UTC](https://forums.unrealengine.com/t/how-do-i-convert-this-material-function-to-c/2222076/9 "2024-12-15T23:06:18Z")

</div>

ahh i didnt noticed the difference was so small.  
So its solved, thanks so much 🙏

---

<div class="post-metadata">

**Author:** ![SophiaWolfie](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/sophiawolfie/32/1008648_2.png) [@SophiaWolfie](https://forums.unrealengine.com/u/SophiaWolfie)\
**Post date:** [December 16, 2024, 12:00am UTC](https://forums.unrealengine.com/t/how-do-i-convert-this-material-function-to-c/2222076/10 "2024-12-16T00:00:55Z")

</div>

Now sometimes in my project i need to reverse and instead of getting the Target Color after the hueshift, I need to get the hueshift between 2 colors.  
Idk if this is even possible. I tried a lot, but all i get is weird results.

```auto
FLinearColor ReferenceColor = FLinearColor::Green;
FLinearColor ResultColor = UCampaignBlueprintFunctionLibrary::GetColorFromHueShift(0.65, ReferenceColor);
//got the result color that in this case is a reddish color from the hueshift 0.65
float Hue2 = UCampaignBlueprintFunctionLibrary::GetHueShiftBetweenColors(ResultColor, ReferenceColor);
//now im trying to get that hueshift given only the 2 colors. here the output should be 0.65. Though im getting 0.35

```

```auto

FLinearColor UCampaignBlueprintFunctionLibrary::GetColorFromHueShift( float HueShift, FLinearColor ReferenceColor){

        FLinearColor hsv = ReferenceColor.LinearRGBToHSV();
        hsv.R = fmod(hsv.R + (HueShift * 360.0f), 360.0f);
        FLinearColor rgb = hsv.HSVToLinearRGB();
        return rgb;

}

float UCampaignBlueprintFunctionLibrary::GetHueShiftBetweenColors( FLinearColor ReferenceColor, FLinearColor TargetColor) {
    FLinearColor hsvReference = ReferenceColor.LinearRGBToHSV();
    FLinearColor hsvTarget = TargetColor.LinearRGBToHSV();

    // Extract hue values in degrees
    float hueReference = hsvReference.R; // Reference hue in degrees
    float hueTarget = hsvTarget.R; // Target hue in degrees

    // Calculate hue shift
    float hueShift = fmod((hueTarget - hueReference + 360.0f), 360.0f) / 360.0f;

    return hueShift;
}

```

---

<div class="post-metadata">

**Author:** ![RecourseDesign](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/recoursedesign/32/1194503_2.png) [@RecourseDesign](https://forums.unrealengine.com/u/RecourseDesign)\
**Post date:** [December 16, 2024, 12:20am UTC](https://forums.unrealengine.com/t/how-do-i-convert-this-material-function-to-c/2222076/11 "2024-12-16T00:20:51Z")

</div>

Try this:

`float hueShift = (( hueTarget > hueReference ) ? 360.0f-(hueTarget-hueReference) : (hueReference-hueTarget) ) / 360.0f;`

The results you will have been getting will probably be the negative amount.

---

<div class="post-metadata">

**Author:** ![SophiaWolfie](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/sophiawolfie/32/1008648_2.png) [@SophiaWolfie](https://forums.unrealengine.com/u/SophiaWolfie)\
**Post date:** [December 16, 2024, 12:48am UTC](https://forums.unrealengine.com/t/how-do-i-convert-this-material-function-to-c/2222076/12 "2024-12-16T00:48:49Z")

</div>

> [@RecourseDesign](#):
>
> float hueShift = (( hueTarget \> hueReference ) ? 360.0f-(hueTarget-hueReference) : (hueReference-hueTarget) ) / 360.0f;

Thanks a lot. This worked perfectly. i spent like almost an hour trying to figure this out by myself. Even chatgpt was not doing it right. Thanks.

---

<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:** [January 15, 2025, 12:49am UTC](https://forums.unrealengine.com/t/how-do-i-convert-this-material-function-to-c/2222076/13 "2025-01-15T00:49:16Z")

</div>

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