Search Unity

Material.SetFloat not working

Discussion in 'Shaders' started by PhilippCh, Feb 13, 2017.

  1. PhilippCh

    PhilippCh

    Joined:
    Jun 14, 2013
    Posts:
    16
    Hi there!

    I've come across an issue using a custom shader with the Unity UI. I have some values that I need to pass to the material through my code:

    Code (CSharp):
    1.  
    2.     var targetMat = Instantiate(meshMaterial);
    3.    
    4.     ...
    5.    
    6.     targetMat.SetFloat(MaterialManager.TILE_SIZE_NAME,
    7.         MaterialManager.TILE_SIZE_PX);
    8.     targetMat.SetFloat(MaterialManager.TEXTURE_SIZE_NAME,
    9.         MaterialManager.TEXTURE_SIZE_PX);
    10.     targetMat.SetFloat(MaterialManager.SAFE_BORDER_NAME,
    11.         MaterialManager.SAFE_BORDER_PX);
    12.     targetMat.SetVector(SHADER_VALUES, shaderValues);
    13.    
    14.     image.material = targetMat;
    15.  
    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

    Code (CSharp):
    1.  
    2.     uniform float4 _Material;
    3.     uniform float  _TileSize;
    4.     uniform float  _TextureSize;
    5.     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:

    Code (CSharp):
    1.     Shader "Illuminated Games/Materials/Material Renderer UI"
    2.     {
    3.         Properties
    4.         {
    5.             _MainTex ("Texture", 2D) = "white" {}
    6.  
    7.             _StencilComp ("Stencil Comparison", Float) = 8
    8.             _Stencil ("Stencil ID", Float) = 0
    9.             _StencilOp ("Stencil Operation", Float) = 0
    10.             _StencilWriteMask ("Stencil Write Mask", Float) = 255
    11.             _StencilReadMask ("Stencil Read Mask", Float) = 255
    12.  
    13.             _ColorMask ("Color Mask", Float) = 15
    14.         }
    15.         SubShader
    16.         {
    17.             Tags { "RenderType"="Opaque" }
    18.             LOD 100
    19.  
    20.             Pass
    21.             {
    22.                 CGPROGRAM
    23.                 #pragma vertex vert
    24.                 #pragma fragment frag
    25.                 // make fog work
    26.                 #pragma multi_compile_fog
    27.              
    28.                 #include "UnityCG.cginc"
    29.  
    30.                 #include "TileTerrain_Base.cginc"
    31.  
    32.                 struct appdata
    33.                 {
    34.                     float4 vertex : POSITION;
    35.                     float2 uv : TEXCOORD0;
    36.                 };
    37.  
    38.                 struct v2f
    39.                 {
    40.                     float2 uv : TEXCOORD0;
    41.                     float4 vertex : SV_POSITION;
    42.                 };
    43.  
    44.                 float4 _MainTex_ST;
    45.                 uniform fixed4 _Material;
    46.  
    47.              
    48.                 v2f vert (appdata v)
    49.                 {
    50.                     v2f o;
    51.  
    52.                     o.vertex = UnityObjectToClipPos(v.vertex);
    53.                     o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    54.  
    55.                     return o;
    56.                 }
    57.              
    58.                 fixed4 frag (v2f i) : SV_Target
    59.                 {
    60.                     fixed2 finalUvCoords = getTileUVCoords(_Material, i.uv);
    61.                     return tex2D(_MainTex, finalUvCoords);
    62.                 }
    63.                 ENDCG
    64.             }
    65.         }
    66.     }
    67.  
    68. as well as the TileTerrain_Base.cginc:
    69.  
    70.     sampler2D _MainTex;
    71.     sampler2D _DataTex;
    72.     sampler2D _NeighbourTex;
    73.     sampler2D _TransTex;
    74.  
    75.     struct Input {
    76.         float2 uv_MainTex;
    77.         float2 uv2_DataTex;
    78.         float4 color : COLOR;
    79.     };
    80.  
    81.     uniform float4 _MaterialInfo[100];
    82.     uniform float  _TileSize;
    83.     uniform float  _TextureSize;
    84.     uniform float  _SafeBorderSize;
    85.  
    86.     float2 getTileUVCoords(float4 color, float2 uv) {
    87.         float tileSizePlusBorder = _TileSize + _SafeBorderSize * 2;
    88.         float tileCoordsX = color.r;
    89.         float tileCoordsY = color.g;
    90.  
    91.         float2 finalUvCoords = float2(
    92.            tileCoordsX + ((uv.x * tileSizePlusBorder) / _TextureSize),
    93.            tileCoordsY +(((1 - uv.y) * tileSizePlusBorder) / _TextureSize));
    94.  
    95.         finalUvCoords.y = 1 - finalUvCoords.y;
    96.  
    97.         return finalUvCoords;
    98.     }
    99.  
    100.     fixed4 getColorFromMaterialID(float index, float2 uv) {
    101.         float4 tilePosition = _MaterialInfo[index];
    102.         float2 uvCoords = getTileUVCoords(tilePosition, uv);
    103.  
    104.         return tex2D(_MainTex, uvCoords);
    105.     }
    106.  
    107.     fixed4 blendNeighbourTiles(fixed4 tileColor, fixed2 tileUvCoords, fixed2 dataCoords) {
    108.         // Calculate the x and y component of the tile.
    109.         float2 finalUvCoords = getTileUVCoords(tileColor, tileUvCoords);
    110.  
    111.         // sample the texture
    112.         fixed4 col = tex2D(_MainTex, finalUvCoords);
    113.         fixed4 colData = tex2D(_DataTex, dataCoords);
    114.         fixed4 colNeighbours = tex2D(_NeighbourTex, dataCoords);
    115.  
    116.         float centerIndex = colData.r * 255;
    117.         float northIndex = colNeighbours.r * 255;
    118.         float eastIndex = colNeighbours.g * 255;
    119.         float southIndex = colNeighbours.b * 255;
    120.         float westIndex = colNeighbours.a * 255;
    121.  
    122.         fixed4 transitionCol = tex2D(_TransTex, tileUvCoords);
    123.  
    124.         if (all(colData.g)) {
    125.             float4 northData = _MaterialInfo[northIndex];
    126.             float4 eastData = _MaterialInfo[eastIndex];
    127.             float4 southData = _MaterialInfo[southIndex];
    128.             float4 westData = _MaterialInfo[westIndex];
    129.  
    130.             if (centerIndex != northIndex && all(northData.a)) {
    131.                 col = lerp(col, getColorFromMaterialID(northIndex, tileUvCoords), transitionCol.r);
    132.             }
    133.  
    134.             if (centerIndex != eastIndex && all(eastData.a)) {
    135.                 col = lerp(col, getColorFromMaterialID(eastIndex, tileUvCoords), transitionCol.g);
    136.             }  
    137.  
    138.             if (centerIndex != southIndex && all(southData.a)) {
    139.                 col = lerp(col, getColorFromMaterialID(southIndex, tileUvCoords), transitionCol.b);
    140.             }  
    141.  
    142.             if (centerIndex != westIndex && all(westData.a)) {
    143.                 col = lerp(col, getColorFromMaterialID(westIndex, tileUvCoords), transitionCol.a);
    144.             }
    145.         }
    146.  
    147.         return col;
    148.     }