Search Unity

Question regarding functions inside shaders

Discussion in 'Shaders' started by b1gry4n, Sep 24, 2020.

  1. b1gry4n

    b1gry4n

    Joined:
    Sep 11, 2013
    Posts:
    146
    I am auto generating a cginc. If I were to generate the following...

    Code (CSharp):
    1. float3 Sphere_EXTENTS()
    2. {
    3.     return float3(0.5,0.5,0.5);
    4. }
    and then doing something like this...

    Sphere_EXTENTS().x, Sphere_EXTENTS().y ... etc


    Is this bad/wrong? Should I be generating something like...

    float3 sphereA = Sphere_EXTENTS();


    And then reference using
    sphere.x, sphere.y
    ?

    Would there be a difference between methods if I had hundreds of different "sphere" with different extents inside the shader? I know in C# its better to temporarily cache the value, but does it make a difference in shader?
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    No difference in a shader. But an easier method might be:
    Code (CSharp):
    1. #define Sphere_EXTENTS float3(0.5, 0.5, 0.5)
    2.  
    3. // use it like this
    4. Sphere_EXTENTS.x
    No difference from the other options, but a little more concise.
     
    Last edited: Sep 24, 2020
  3. b1gry4n

    b1gry4n

    Joined:
    Sep 11, 2013
    Posts:
    146
    That does look better, Ill do it that way. Thanks