Using a Dynamically Generated Texture to Send Data to a Shader
I'm trying to build a shader that will read the red channel values of a texture to use as an index. However, the color values I set in the data texture aren't the same values the shader reads when sampling the data texture.
For example, I build a data texture dynamically and set every pixel to Color32(15, 15, 15, 15) using setPixels32 (when I create the new Texture2D I set filter to point, wrap to clamp, disable mipmaps, and set linear to true).
Then when I sample the texture in the shader using tex2D, I would expect to get a float4 with values around 0.059 (~15/255) for x, y, z, and w.
However, I don't get the values I set, and the x,y,z, and w values are all different.
Is the problem something to do with going between linear and gamma color spaces?
I'm using this to generate a tile map Similar to this http://www.dfworkshop.net/improved-terrain-tiling/
The difference is that I'm using a dynamically generated index texture.
Edit:
Specific example. I set every pixel to Color32(1,1,1,1) and then when I sample a pixel of the texture in the shader I get the following value (examined with the VS graphical debugger).
x = 0.266666700, y = 0.537254900, z = 0.101960800, w = 1.000000000
Posted Apr 3, 2018, 8:27 PM UTC · edited
How are you checking the values that youre getting?
I'm using the Visual Studio graphical debugger. I also played around with returning the index values as the red channel of the fragment shader return value.
No idea really then. Never seen anything like that. Can you show the code?
Solved it.
I was using Graphics.Blit like this
So that the indexTexture gets set to the _MainTex property of lookupMaterial.
However, when I add an extra texture called _IndexTexture to the lookupShader and set it like this
before calling Graphics.Blit, it works fine. Something about Graphics.Blit setting the indexTexture to _MainTex on lookupMaterial messes it up.