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

how to initialize struct element in one line?

Discussion in 'Shaders' started by KILEYI, Apr 18, 2015.

  1. KILEYI

    KILEYI

    Joined:
    Mar 7, 2015
    Posts:
    52
    in shader i can init struct element one by one,such as

    Code (CSharp):
    1.  
    2. struct testStruct
    3. {
    4.     float a;
    5.     float b;
    6. };
    7. testStruct myarrays[9];
    8. myarrays[0].a = 0;
    9. myarrays[0].b = 0;
    10.  
    for few element this is ok,but if there is many element, how to init all of them in one line?
    something like this,not working though
    Code (CSharp):
    1.  
    2. struct Sphere
    3. {
    4.     float r;
    5.     vec3 p;
    6.     vec3 e;
    7.     vec3 c;
    8.     int refl;
    9. };
    10. spheres[0] = Sphere(1e5, vec3(-1e5+1., 40.8, 81.6), vec3(0,0,0), vec3(.75, .25, .25), DIFF);
    11.  
    is there a way to simplify the code.
     
  2. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,878
    Code (CSharp):
    1.             static const float BlurWeights[11] =
    2.             {
    3.                 0.008764, 0.026995, 0.064759, 0.120985, 0.176033,
    4.                 0.199471,
    5.                 0.176033, 0.120985, 0.064759, 0.026995, 0.008764
    6.             };
    for a float2 you should do:

    Code (CSharp):
    1.             static const float2 PixelKernelH[11] =
    2.             {
    3.                 { -5, 0 }, { -4, 0 }, { -3, 0 }, { -2, 0 }, { -1, 0 },
    4.                 { 0,  0 },
    5.                 { 1,  0 }, { 2,  0 }, { 3,  0 }, { 4,  0 }, { 5,  0 }
    6.             };
     
  3. KILEYI

    KILEYI

    Joined:
    Mar 7, 2015
    Posts:
    52
    can i delay initialize after declaration?
    maybe i will chage element in runtime?