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.
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.
answered Jun 10 '10 at 04:13 PM
jonas echterhoff ♦♦