Search Unity

Shader property from script

Discussion in 'Shaders' started by AverageCG, Oct 31, 2019.

  1. AverageCG

    AverageCG

    Joined:
    Nov 13, 2017
    Posts:
    19
    Hey community,

    Edit: nevermind me this is resolved and was just due to me not adhering to good practices and unknowingly having my script run on a different gameobject:rolleyes:...

    This probably just comes down to me not knowing how to use properties in my fragment shader but here's what i am struggling with:
    I am trying to set an float4 array in my shader via script. for debuggin I am currently doing so in the start() method via:

    Code (CSharp):
    1. m = gameObject.GetComponent<MeshRenderer>().material;
    2. shc1[0] = new Vector4(1f, 1f, 1f, 1f);
    3. m.SetVectorArray("shc", shc1);
    4.  
    5. Debug.Log(shc1[0]);
    6. Debug.Log(m.GetVectorArray("shc")[0]);
    I have also tried:
    Code (CSharp):
    1. var renderer = gameObject.GetComponent<Renderer>();
    2. renderer.material.SetVectorArray("_shc", shc1);
    3. renderer.material.SetVector("_justOne", shc1[0]);
    In theory eveything prints fine, the array is initialised at the same size as the property specified in the shader, i am just overwriting the first entry here to make debugging simpler. The output of both debug.logs is the same. which makes me think i am simply using properties the wrong way in my shader.

    However if i try to use the property in my fragment shader to just return the value of the first entry (which i manually set to (1,1,1,1) in line 1 of c# snippet) my gameobject is still rendered black.

    Here is some shader code:

    Code (cg/hlsl):
    1. CGPROGRAM
    2. #pragma vertex vert
    3. #pragma fragment frag
    4. #include "UnityCG.cginc"
    5.  
    6. float4 shc[9];
    7. ...
    8.  
    9. ...
    10. fixed4 frag(v2f i) : SV_Target
    11.             { return  shc[0];}

    What am I doing wrong?
    Am i supposed to declare the float4 shc[9] somewhere else than in between CGPROGRAM / ENDCG? Do i need to specify somehow in the shader that this property is going to change?
    I can't find any examples
    ´



    Thanks for your time
     
    Last edited: Oct 31, 2019
  2. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,546
    This should work, what specific Unity version are you using?

    Also can we see the whole shader?
     
    AverageCG likes this.
  3. AverageCG

    AverageCG

    Joined:
    Nov 13, 2017
    Posts:
    19
    Nevermind :oops:

    For debug purposes i started logging material.shader.name and was promptly returned a different shader, after trying to figure out why and in the end reassigning my shader to the material via script, i figured out that I apparently had this script running on a different gameobject with a different shader attached so that i didn't see anything happening on screen. :rolleyes:

    Lack of sleep is getting to me o_O

    Thanks regardless
     
    Invertex likes this.