Passing vector and texture parameters to UE materials
Hey guys,
I'm working with UE5.1 and Noesis 3.2 and I want to implement a health bar using an UE material. In my material, I want to process three parameters:
- A health ratio value passed as a scalar parameter
- A texture atlas containing the health bar image as a texture2D parameter
- The uv coords for the texture within the atlas as a vector parameter
In my xaml resource dictionary, the texture is defined as a bitmap and the uv coords are defined as a color.
The health value is taken from a data context binding.
Here's the relevant snippet:
And the resources used:
When importing all of this, I get the following warnings:
The warnings for the color parameter will go away, if I use the RGBA properties instead of ScA, ScR and so on, but I need floats of course.
Needless to say, the texture won't show up in the game and the vector parameter values are also incorrect. The health ratio value is the only thing that works.
I searched the forums and the web but couldn't find any related solution. The documentation doesn't seem to mention anything about parameters (correct me if I'm wrong) and I couldn't find anything in the samples either.
I'm aware there are other ways to implement a progress bar, but this is really not about progress bars. We are going to make plenty of use of materials so this is a general issue we would need to solve.
Thanks in advance.
I'm working with UE5.1 and Noesis 3.2 and I want to implement a health bar using an UE material. In my material, I want to process three parameters:
- A health ratio value passed as a scalar parameter
- A texture atlas containing the health bar image as a texture2D parameter
- The uv coords for the texture within the atlas as a vector parameter
In my xaml resource dictionary, the texture is defined as a bitmap and the uv coords are defined as a color.
The health value is taken from a data context binding.
Here's the relevant snippet:
Code: Select all
<noesis:Brush.Shader>
<mat:M_TexturedProgressBar Ratio="{Binding HealthRatio}" RectForeground="{StaticResource R_HealthBar}" Texture="{StaticResource T_SpriteSheet_HUD}" />
</noesis:Brush.Shader>
Code: Select all
<BitmapImage x:Key="T_SpriteSheet_HUD" UriSource="T_SpriteSheet_HUD.png"/>
<Color x:Key="R_HealthBar" ScA="0" ScR="0.1123046875" ScG="0.9765625" ScB="0.1123046875" />
Code: Select all
Cannot assign property to abstract class 'Color'
Cannot set value for 'Materials.UI.M_TexturedProgressBar.RectForeground', invalid value 'null' for property type 'Color'
Unknown property 'Materials.UI.M_TexturedProgressBar.Texture', or object to assign is incompatible
Needless to say, the texture won't show up in the game and the vector parameter values are also incorrect. The health ratio value is the only thing that works.
I searched the forums and the web but couldn't find any related solution. The documentation doesn't seem to mention anything about parameters (correct me if I'm wrong) and I couldn't find anything in the samples either.
I'm aware there are other ways to implement a progress bar, but this is really not about progress bars. We are going to make plenty of use of materials so this is a general issue we would need to solve.
Thanks in advance.
-
-
sfernandez
Site Admin
- Posts: 2794
- Joined:
Re: Passing vector and texture parameters to UE materials
Hi, if you need to define a vector (x,y,z,w) as a resource in xaml you can use the following:
The problem with the Color is a known issue (#1752) that happens when defining a Color resource with properties, instead you can use the string conversion:
Could you please try any of these approaches?
Code: Select all
<Point4D x:Key="R_HealthBar">0.0, 0.1123046875, 0.9765625, 0.1123046875</Point4D>
Code: Select all
<Color x:Key="R_HealthBar">sc#0.0, 0.1123046875, 0.9765625, 0.1123046875</Color>
Re: Passing vector and texture parameters to UE materials
As for material parameters in Unreal, currently we only expose scalar parameters as float properties, and vector parameters as color properties.
I can look into adding support for texture parameters, but on a first look it looks like the UMaterialInstanceDynamic interface for setting textures uses high level UTexture objects, which currently we don't keep.
For the time being you could have the texture set in a MaterialInstance in Unreal, and use that instead of the base Material.
I can look into adding support for texture parameters, but on a first look it looks like the UMaterialInstanceDynamic interface for setting textures uses high level UTexture objects, which currently we don't keep.
For the time being you could have the texture set in a MaterialInstance in Unreal, and use that instead of the base Material.
Re: Passing vector and texture parameters to UE materials
Thanks a lot for the Replies.
@sfernandez:
I tried both approaches.
Defining the color as you suggested works, but the values passed to the material are incorrect, as they get converted to sRGB. To fix that, I would need to convert them back to linear in my material, which is not ideal. Is there a way to force the conversion off?
Using Point4D still produces a warning in UE and no values get passed to the material
@hcpizzi: Yeah that's ok too, as we can set the texture atlas directly in UE and use vectors (once we know how) to "crop" the atlas
@sfernandez:
I tried both approaches.
Defining the color as you suggested works, but the values passed to the material are incorrect, as they get converted to sRGB. To fix that, I would need to convert them back to linear in my material, which is not ideal. Is there a way to force the conversion off?
Using Point4D still produces a warning in UE and no values get passed to the material
Code: Select all
Cannot set value for 'Materials.UI.M_TexturedProgressBar.RectForeground', invalid value '0,0.112305,0.976563,0.112305' for property type 'Color'
@hcpizzi: Yeah that's ok too, as we can set the texture atlas directly in UE and use vectors (once we know how) to "crop" the atlas
Re: Passing vector and texture parameters to UE materials
Hi,
I could expose two different properties for each vector parameter: The current one, and another one with the "_Linear" suffix that does the sRGB->linear conversion before passing the values to the Unreal material. So all you would need to do would be to set the RectForeground_Linear property instead of RectForeground.
Could this work for you?
I could expose two different properties for each vector parameter: The current one, and another one with the "_Linear" suffix that does the sRGB->linear conversion before passing the values to the Unreal material. So all you would need to do would be to set the RectForeground_Linear property instead of RectForeground.
Could this work for you?
Re: Passing vector and texture parameters to UE materials
Hi hcpizzi, while I totally agree that doing the conversion in code once would be better than in the material, it still sounds more like a workaround. LinearToSRGB and SRGBToLinear are a bit expensive, and there's also unnecessary loss of precision involved. There shouldn't be a conversion in the first place, if we intend to use the values as they are. I think there should be proper support for Point4D. I saw a definition of Point4D in the Noesis SDK code, but it seems there is no implementation in the core library. Apart from that, the function NoesisCreateTypeClassForUMaterial(UMaterialInterface* Material), where material parameters are actually set, doesn't handle the case if the type is a Point4D.
Re: Passing vector and texture parameters to UE materials
We could make that second property be of type Point4D instead of Color to avoid converting back and forth.
If you like the idea in principle I can send you a patch once I have it working.
If you like the idea in principle I can send you a patch once I have it working.
Re: Passing vector and texture parameters to UE materials
Hey, sounds good to me. I'd be happy to give it a try! Thanks.
Re: Passing vector and texture parameters to UE materials
Here it is. Let us know if this works for you.
- Attachments
-
- LinearColorMaterialParams.zip
- (750 Bytes) Downloaded 10 times
Re: Passing vector and texture parameters to UE materials
Hi hcpizzi, it works perfectly. Thanks for your help!
Who is online
Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 2 guests