Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
0
Question by Zamaroth · Feb 18 at 12:14 PM · shaderarray

Set specific value in shader array

So, I have an array in shader:

 float3 _PointPositions [10];

I'm tryng to loop through it and set each element with:

 material.SetVector("_PointPositions"+i.ToString(), value);

It doesn't seem to work tho and only mentions of this method I found are ~6 years old.

Note: I know of SetVectorArray, but the input array isn't always the same size, so it cannot use it as it wants to restart editor every time different size of array is passed into it.

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
0
Best Answer

Answer by Shrimpey · Feb 18 at 12:54 PM

If you want to use arrays, they have to be static size. So you need to use SetVectorArray and fill rest with zeros. If you want to only fill specific parts of the array, you should use buffers instead and then SetBuffer method.

You would need something like this for buffers:

 StructuredBuffer<float3> _PointPositions;

and in C# you would use this method:

 SetData(Array data, int managedBufferStartIndex, int computeBufferStartIndex, int count);

it should look like this:

 var dataArray = new Vector3[numberOfPoints];
 positionBuffer = new ComputeBuffer(numberOfPoints, sizeof(float) * 3, ComputeBufferType.Default);
 positionBuffer.SetData(dataArray, dataArrayOffset, bufferOffset, numberOfValuesToFill);
 material.SetBuffer("_PointPositions", positionBuffer);

So dataArray is your array of Vec3s, dataArrayOffset is integer that determines the start and numberOfValuesToFill determines the end of vector values from array to pass. bufferOffset should be the same as dataArrayOffset if buffer in shader and array in C# are the same size.

It's a rough code, but this should give you some links to look for buffer stuff. There are StructuredBuffers, RWStructuredBuffers for different Read/Write purposes, so you can read about them as well.

Comment
Add comment · Show 5 · 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 Zamaroth · Feb 18 at 04:26 PM 0
Share

that works, thanks! one more question tho... Unity says the buffer should be manually released, but if I call it right after the SetBuffer method, nothing gets set, so I suppose the buffer didn't manage to set the values yet when it is released. is there a way to wait until its set so I can release it?

avatar image Shrimpey Zamaroth · Feb 18 at 05:31 PM 0
Share

Release is just used after the buffer is no longer needed - so usually while quitting the game you should free up it's memory to make sure everything is fine. Also while changing scenes etc. I usually do it in OnDestroy() function.

avatar image Zamaroth Shrimpey · Feb 18 at 06:17 PM 0
Share

well what im trying to do is set the points only once, after a model is loaded. so when buffer is set once, it can be released as its no longer needed and wont be used again.

just dont know where to call it.

Show more comments

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

216 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 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

Implementing multi-texture shading with the marching cube algorithm (voxels) 1 Answer

Texture2D Array Appears Grainy 0 Answers

Shader float4[] 1 Answer

Possible shader interference when updating matrix 1 Answer

How to use array in cg shader 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