1. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    Dismiss Notice
  2. We're looking for feedback on Unity Starter Kits! Let us know what you’d like.
    Dismiss Notice
  3. We’re giving 2017.1 beta testers a chance to win t-shirts and a Nintendo Switch. Read more on the blog.
    Dismiss Notice
  4. We want to know how you learned Unity! Help us by taking this quick survey and have a chance at a $25 gift card
    Dismiss Notice
  5. Are you an artist or level designer going to Unite Europe? Join our roundtables there to discuss artist features.
    Dismiss Notice
  6. Unity 5.6 is now released.
    Dismiss Notice
  7. Check out all the fixes for 5.6 on the patch releases page.
    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:
    113
    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:
    848
    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:
    2,804
    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.