Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

cannot pass any data from script to shader

Discussion in 'Shaders' started by mrandmrsliamrussell, Mar 18, 2017.

  1. mrandmrsliamrussell

    mrandmrsliamrussell

    Joined:
    Oct 16, 2013
    Posts:
    1
    hi guys ive tried everything i can but my shader is just not reading any values i send to it. heres me shader below:

    Code (CSharp):
    1. Shader "Custom/final_shader"{
    2.     properties{
    3.         _MainTex("MainTexture", 2D) = "white" {}
    4.         _Tint("Tint", Color) = (1,1,1,1)
    5.     //    _uvposx ("uvposx", float) = 0
    6.         //_uvposy ("uvposy", float) = 0
    7.         _uvpos ("uvpos", Color ) = (0,0,0,0)
    8.     }
    9.         SubShader{
    10.            
    11.             Pass{
    12.             CGPROGRAM
    13.     #pragma vertex vert
    14.     #pragma fragment frag
    15.     #pragma multi_compile _DETAIL_MULX2 //_UVPOS ON
    16.  
    17.     #include "UnityCG.cginc"
    18.             float4 _Tint;
    19.             float _uvposx;
    20.             float _uvposy;
    21.             float4 _uvpos;
    22.  
    23.         float4 vert( float4 position : POSITION, out float3 localPosition : TEXCOORD0 ) : SV_POSITION {
    24.            
    25.         localPosition.xyz = position.xyz ;
    26.         _uvpos.x = _uvposx;
    27.         _uvpos.y = _uvposy;
    28.         return mul(UNITY_MATRIX_MVP, position);
    29.     }
    30.     float4 frag( float4 position : SV_POSITION, float3 localPosition : TEXCOORD0 ) : SV_TARGET {
    31.         return float4(localPosition.x  , localPosition.y, localPosition.z , 1)  * _Tint  ;
    32.     }
    33.  
    34.         ENDCG
    35.     }
    36.     }
    37. }

    and heres my c# code that sets the floats, ive tried messing around with keywords, global variables and floats vs vectors and i get nothing. please help.

    Code (CSharp):
    1.  
    2.         Texture2D tex = rend.material.mainTexture as Texture2D;
    3.         Texture2D texrep = rend.material.mainTexture as Texture2D;
    4.         Vector2 pixelUV = hit.textureCoord;
    5.        
    6.        pixelUV.x *= texrep.width;
    7.        pixelUV.y *= texrep.height;
    8.        
    9.         cube = GameObject.FindObjectOfType(typeof (lightcontroller)) as lightcontroller;
    10.  
    11.         // cube.mat.SetVector("_AltTex", new Vector2(pixelUV.x, pixelUV.y));
    12.         cube.mat.EnableKeyword("_uvpos_ON");
    13.         cube.mat.EnableKeyword("_UVPOSX");
    14.         cube.mat.EnableKeyword("_UVPOSY_ON");
    15.         Shader.SetGlobalFloat("_uvposx", pixelUV.x);
    16.         cube.mat.SetFloat("_uvposx", pixelUV.x);
    17.         cube.mat.SetFloat("_uvposy", pixelUV.y);
    18.         cube.mat.SetVector(" _uvpos", new Vector2(pixelUV.x , pixelUV.y));
    19.         cube.mat.SetVector(" _Tint", new Vector4(pixelUV.x, pixelUV.y, 0, 0));
    20.    
    21.         // tex.SetPixel((int)pixelUV.x, (int)pixelUV.y,Color.black);
    22.         // tex.Apply();
    23.        
    24.         Debug.Log(pixelUV.x);
    25.         Debug.Log(pixelUV.y);
    26.     }
    27.  
    28.    
    29.      
    30. }
     
  2. brownboot67

    brownboot67

    Joined:
    Jan 5, 2013
    Posts:
    375
    You need to use SetColor instead of set vector. And your vectors probably need to all be vector 4s. No idea if unity handles that, but safe to assume it doesn't.

    Why are you using keywords instead of toggles?
     
    brokenm likes this.
  3. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    There are implicit casts available from Vector2 and Vector3 to Vector4, so that's not the problem. And SetVector probably also works if the target is actually defined as a color. (The internal representation will still be a vector, but the UI will draw a color selector.)

    What doesn't work is trying to assign the value to " _uvpos", while it is "_uvpos". (Space in front of the name.)
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    Internally SetVector and SetColor are mostly the same, just SetColor takes a Color instead of a Vector4 and will do the appropriate sRGB conversion if needed, though using mat.SetVector on a material property that's defined as a Color in the shader I think will also do the sRGB conversion, which might be causing additional problems here. It is definitely recommended that you use the Vector material property type if the value is not a color, or if you don't want your color space settings to affect the value.

    https://docs.unity3d.com/Manual/SL-Properties.html
     
    brokenm likes this.