Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by jjey225 · Sep 28, 2019 at 11:35 AM · shaderarrayfloat

Shader float4[]

I have to use array in the shader (float4[]) but I can't get it to work:

 float4[] spheres;
                 spheres[0] = float4(10, 1, 10, 1);
                 spheres[1] = float4(8, 1, 10, 1);
                 spheres[2] = float4(6, 1, 10, 1);
                 spheres[3] = float4(12, 1, 10, 1);
 
                 float Sphere1 = sdSphere(p - spheres[0].xyz, spheres[0].w);
                 float Sphere2 = sdSphere(p - spheres[1].xyz, spheres[1].w);
 
                 return opSmoothUnion(Sphere1, Sphere2, 1);

When I use separate variables, everything works perfectly, but there is a problem with array.
How should I define and use them in a shader?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by MichaI · Sep 28, 2019 at 12:55 PM

Declaring array in shader you have to specify its length like this

float4 spheres[4];

Also great tutorial about arrays in shader you can find here: https://www.alanzucconi.com/2016/10/24/arrays-shaders-unity-5-4/

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jjey225 · Sep 28, 2019 at 01:28 PM 0
Share

Still, it doesn't work as it should.
It works well in this way:
C#:

 public Vector4 _sphere1, _sphere2;
 
 material.SetVector("_sphere1", _sphere1);
 material.SetVector("_sphere2", _sphere2);
 

Shader:

 uniform float4 _sphere1, _sphere2;
 
 float distanceField(float3 p)
 {
     float Sphere1 = sdSphere(p - _sphere1.xyz, _sphere1.w);
     float Sphere2 = sdSphere(p - _sphere2.xyz, _sphere2.w);
     
     return opSmoothUnion(Sphere1, Sphere2, 1);
 }

And when I change it to array it looks like this:

C#:

 public Vector4[] _blobs;
 
 material.SetVectorArray("_blobs", _blobs);

Shader:

 uniform float4 _blobs[2];
 
 float distanceField(float3 p)
 {
     float Sphere1 = sdSphere(p - _blobs[0].xyz, _blobs[0].w);
     float Sphere2 = sdSphere(p - _blobs[1].xyz, _blobs[1].w);
     
     return opSmoothUnion(Sphere1, Sphere2, 1);
 }

And it doesn't matter how I define this array:
-uniform float4[] _blobs;
-uniform float4[2] _blobs;
-uniform float4 _blobs[];
-uniform float4 _blobs[2];
And I have already seen this tutorial.

avatar image MichaI · Sep 28, 2019 at 01:52 PM 1
Share

In C# when setting Vector array you have to set array that is already initialized to its desired length:

 public Vector4[] _blobs = new Vector4[2];
  
  material.SetVectorArray("_blobs", _blobs);

according to Step 3. Limitations in Alan Zucconi's tutorial

avatar image jjey225 MichaI · Sep 28, 2019 at 02:09 PM 0
Share

Nothing - no matter if I set Vector4 in the inspector or in the script itself. I have probably tried all possible combinations. Due to the fact that C# refers to a variable in Shader using a string, propably I'm gonna hard-encode 500 individual variables and use something like this:

 for(int i=0 ; i < _blobs.Length ; i++){
     material.SetVector("_sphere"+i, _blobs[i]);
 }

This is probably the worst way to do it, but I'm running out of patience and ideas on how to do it... I've never had such a problem with arrays...

avatar image MichaI MichaI · Sep 28, 2019 at 03:06 PM 1
Share

Sometimes when I set uninitialized array or wanted to make array longer, after changes in script I had to restart whole Unity for it to properly assigned my changes.

avatar image jjey225 MichaI · Sep 28, 2019 at 03:24 PM 0
Share

I restarted it few times and still nothing. It can also be a problem in my shader but I don't think so because if I manually set up to 20 vectors4 it works fine. As far as I know, debugging shader doesn't work the same way as in the case of an ordinary script - Debug.Log showed that Vector4[] which is sent to the shader is correct (regardless of whether the data is set in the script or inspector and whether I am in play/edit mod). I don't know if the shader receives these data correctly and what they actually look like. float4 works, and float4[] does not - the editor does not show any errors and I have no way of checking what the variables in the shader look like.

avatar image MichaI · Sep 28, 2019 at 03:35 PM 1
Share

Could you post your shader and c# script to see if there isn't any mistake?

avatar image jjey225 MichaI · Sep 28, 2019 at 03:55 PM 1
Share

Got it! This s**t causes all the problems:

 // Upgrade NOTE: excluded shader from DX11, OpenGL ES 2.0 because it uses unsized arrays
 #pragma exclude_renderers d3d11 gles

How did it appear in my code?
Now it is working!
$$anonymous$$ichaI, my master, thank you very much for your help :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

195 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Split a number? 1 Answer

Pick between two floats 2 Answers

How to use array in cg shader 1 Answer

NullRefenceException Using Arrays 2 Answers

Why cant I do this? Incorrect number of values(terrain and array related). 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges