Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
2
Question by Sejster · Jun 09, 2010 at 08:35 AM · shaderarraycolorcg

Is there a way to pass an array to a Cg shader?

I'm trying to write a Cg shader that determines each vertex's color by looking up some values in a 3-dim array that is updated regularly in the game loop. Currently, everytime the array changes, I update the vertex colors in the actual meshes with a script and use a standard vertex color shader to show them. That's a lot of work for the cpu, so I would rather pass the whole color value array to a custom shader and let it do the work. Is there a way to do so? If my array of colors would be twodimensional, I could pass it to the shader as a texture. Since there are no 3d textures in Unity, how do I get my 3-dim array into my shader? Is there a better way than breaking my array down to several 2d textures or combining these into a single large 2d texture?

Edit: Additional Information - What exactly I'm trying to do

In my script, I have got a 3-dimensional array that stores colors. Let's assume it's 16x16x16 in size. In my shader I want to determine the color of each vertex by its position in the world space by looking up and interpolating the corresponding values in my 3-dim color array. For example, if the vertex position vector is x = 2, y = 4, z = 3 then it should get the color in my colorArray at [2,4,3]. If its x coordinate would be 2.5 instead, its color would get interpolated from [2,4,3] and [3,4,3].

That's about it, I'm just searching the best way to pass the values in my array to the shader. The best I came up with yet is writing them into a single 2D texture with SetPixel. For my example of a 16x16x16 array, this texture would be 256x16 in size (or 64x64). That's all a workaround because Unity doesn't support 3D textures (yet).

So, is there a way to do this that is simpler than mine? The more I think about it, the more I doubt it.

Comment
Add comment · Show 1
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 jonas-echterhoff ♦♦ · Jun 09, 2010 at 01:40 PM 0
Share

While I'm not sure if there is an efficient way to do this, it might help to describe what the actual problem you need to solve is - maybe someone might be able to suggest a different solution.

2 Replies

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

Answer by jonas-echterhoff · Jun 10, 2010 at 04:13 PM

I cannot think of any better way of doing this, unless the color values can easily be mathematically computed in the shader. The 2D Texture workaround should work, you just have to do the interpolation on one coordinate yourself (and be sure to disable mipmaps, as that might mix up your 2D planes among each other).

Have you actually check that this is a performance bottleneck? If not, setting the mesh color values in a script might actually be the nicest solution.

Comment
Add comment · Show 2 · 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 Sejster · Jun 11, 2010 at 06:39 AM 0
Share

I just implemented the 2d texture workaround, works like a charm. I will do some testing how frequently I can update the texture without decreasing the performance too much.

avatar image Nullspace · Feb 27, 2013 at 03:15 PM 0
Share

@Sejster Hi, I want to pass a depth map to Unity shader. How did you do this by using a Texture2D? Thanks.

avatar image
0

Answer by Dylan Fitterer · Jun 10, 2010 at 05:14 PM

To read a texture in the vertex shader you would need to use Vertex Texture Fetch, which isn't supported in Unity (2.6) yet: http://forum.unity3d.com/viewtopic.php?t=33137

Comment
Add comment · 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

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

1 Person is following this question.

avatar image

Related Questions

How to force the compilation of a shader in Unity? 5 Answers

Shader: get nearest point light's color, direction and general custom lighting questions 0 Answers

Material doesn't have a color property '_Color' 4 Answers

Font Color changing shader [color changes based on what background color is] 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