Material.SetFloat not working
I have some values that I need to pass to a material through my code:
var targetMat = Instantiate(meshMaterial);
...
targetMat.SetFloat(MaterialManager.TILE_SIZE_NAME,
MaterialManager.TILE_SIZE_PX);
targetMat.SetFloat(MaterialManager.TEXTURE_SIZE_NAME,
MaterialManager.TEXTURE_SIZE_PX);
targetMat.SetFloat(MaterialManager.SAFE_BORDER_NAME,
MaterialManager.SAFE_BORDER_PX);
targetMat.SetVector(SHADER_VALUES, shaderValues);
image.material = targetMat;
I have checked that all values are valid (non-null, etc.) multiple times through logging them before assigning the material and logging again through Material.GetXXX() after assigning the material, they are always correct. In my shader I am accessing the variables using
uniform float4 _Material;
uniform float _TileSize;
uniform float _TextureSize;
uniform float _SafeBorderSize;
Here's the thing: When I assign my material to a MeshRenderer, the shader works perfectly fine. However, when assigning the material to a UI element (I included _Stencil, etc. to make it work with the UI layer), all values I pass to the shader using Material.SetXXX are always zero. If I remove the variables and hard-code the values, the shader works perfectly fine.
Is there anything I'm doing wrong or need to pay attention to when using shaders as GUI elements? It'd be great if someone could help me out here!
For reference, here's the whole shader code:
Shader "Illuminated Games/Materials/Material Renderer UI"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_StencilComp ("Stencil Comparison", Float) = 8
_Stencil ("Stencil ID", Float) = 0
_StencilOp ("Stencil Operation", Float) = 0
_StencilWriteMask ("Stencil Write Mask", Float) = 255
_StencilReadMask ("Stencil Read Mask", Float) = 255
_ColorMask ("Color Mask", Float) = 15
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// make fog work
#pragma multi_compile_fog
#include "UnityCG.cginc"
#include "TileTerrain_Base.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
float4 _MainTex_ST;
uniform fixed4 _Material;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed2 finalUvCoords = getTileUVCoords(_Material, i.uv);
return tex2D(_MainTex, finalUvCoords);
}
ENDCG
}
}
}
as well as the TileTerrain_Base.cginc:
sampler2D _MainTex;
sampler2D _DataTex;
sampler2D _NeighbourTex;
sampler2D _TransTex;
struct Input {
float2 uv_MainTex;
float2 uv2_DataTex;
float4 color : COLOR;
};
uniform float4 _MaterialInfo[100];
uniform float _TileSize;
uniform float _TextureSize;
uniform float _SafeBorderSize;
float2 getTileUVCoords(float4 color, float2 uv) {
float tileSizePlusBorder = _TileSize + _SafeBorderSize * 2;
float tileCoordsX = color.r;
float tileCoordsY = color.g;
float2 finalUvCoords = float2(
tileCoordsX + ((uv.x * tileSizePlusBorder) / _TextureSize),
tileCoordsY +(((1 - uv.y) * tileSizePlusBorder) / _TextureSize));
finalUvCoords.y = 1 - finalUvCoords.y;
return finalUvCoords;
}
fixed4 getColorFromMaterialID(float index, float2 uv) {
float4 tilePosition = _MaterialInfo[index];
float2 uvCoords = getTileUVCoords(tilePosition, uv);
return tex2D(_MainTex, uvCoords);
}
fixed4 blendNeighbourTiles(fixed4 tileColor, fixed2 tileUvCoords, fixed2 dataCoords) {
// Calculate the x and y component of the tile.
float2 finalUvCoords = getTileUVCoords(tileColor, tileUvCoords);
// sample the texture
fixed4 col = tex2D(_MainTex, finalUvCoords);
fixed4 colData = tex2D(_DataTex, dataCoords);
fixed4 colNeighbours = tex2D(_NeighbourTex, dataCoords);
float centerIndex = colData.r * 255;
float northIndex = colNeighbours.r * 255;
float eastIndex = colNeighbours.g * 255;
float southIndex = colNeighbours.b * 255;
float westIndex = colNeighbours.a * 255;
fixed4 transitionCol = tex2D(_TransTex, tileUvCoords);
if (all(colData.g)) {
float4 northData = _MaterialInfo[northIndex];
float4 eastData = _MaterialInfo[eastIndex];
float4 southData = _MaterialInfo[southIndex];
float4 westData = _MaterialInfo[westIndex];
if (centerIndex != northIndex && all(northData.a)) {
col = lerp(col, getColorFromMaterialID(northIndex, tileUvCoords), transitionCol.r);
}
if (centerIndex != eastIndex && all(eastData.a)) {
col = lerp(col, getColorFromMaterialID(eastIndex, tileUvCoords), transitionCol.g);
}
if (centerIndex != southIndex && all(southData.a)) {
col = lerp(col, getColorFromMaterialID(southIndex, tileUvCoords), transitionCol.b);
}
if (centerIndex != westIndex && all(westData.a)) {
col = lerp(col, getColorFromMaterialID(westIndex, tileUvCoords), transitionCol.a);
}
}
return col;
}
Your answer
Follow this Question
Related Questions
How to change/fix Shader Graph node connection ports 3 Answers
Stencil Shaders and Other Shaders 0 Answers
Modifying shader requires restarting application 0 Answers
See-Through effect for top-down game (Performance issues) 0 Answers
Unity shaders/materials outside unity enviroment,Unity object export with shaders from shadergraph 0 Answers