• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by PhilippCh · Feb 13, 2017 at 10:18 PM · uishaderrenderingshadersmaterials

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;
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

155 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to change/fix Shader Graph node connection ports 2 Answers

stop materal instancing 1 Answer

Shader Graph Transparent Texture Problem HELP! 2 Answers

Shadows on Custom Lit Surface Shader 1 Answer

How to make a shader with multiple textures, one color for each texture? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges