x


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.

more ▼

asked Jun 09 '10 at 08:35 AM

Sejster gravatar image

Sejster
59 3 3 6

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.

Jun 09 '10 at 01:40 PM jonas echterhoff ♦♦
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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.

more ▼

answered Jun 10 '10 at 04:13 PM

jonas echterhoff gravatar image

jonas echterhoff ♦♦
9.8k 7 23 104

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.

Jun 11 '10 at 06:39 AM Sejster

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

Feb 27 at 03:15 PM Nullspace
(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered Jun 10 '10 at 05:14 PM

Dylan Fitterer gravatar image

Dylan Fitterer
33 1 1 8

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1739
x1395
x526
x90

asked: Jun 09 '10 at 08:35 AM

Seen: 3088 times

Last Updated: Feb 27 at 03:15 PM