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

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


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?

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?

1 Like

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

1 Like

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 :lion:?

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 :green_square:
So a float of 0.65 shift, would turn it into a reddish color :red_square:.
So what would be the corresponding input color and hue shift that would match the LinearRGBToHSV?
This is quite confusing for my adhd brain.

1 Like

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

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

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)


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

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.

1 Like

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

1 Like

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.

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

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;
}

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.

2 Likes

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.

1 Like

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