S
S
Stanislav Silin2014-10-15 18:44:04
Shaders
Stanislav Silin, 2014-10-15 18:44:04

How to pass a float array of length n to the shader?

Good day to all!
For a shader in Unity3D, I need to pass an array of floats of a given length.
I did not find anything in Google and decided to generate a texture and transfer it. Everything is fine, but not quite. The texture is generated, saved, passed to the shader, and then it fails :( ...
There is not enough accuracy (depth) of the texture to transfer the exact data, they are cut off and it turns out not quite what I wanted. I need 2 decimal places.
PS I pass the coordinates (r,g,b) and circle radius(a).

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Daniil Basmanov, 2014-10-16
@byme

How many decimal places?
Usually in such situations, they take a texture, turn off mipmaps, encode the necessary data and take it in the shader. UnityCG.cginc has a couple of useful methods for this:

// Encoding/decoding [0..1) floats into 8 bit/channel RGBA. Note that 1.0 will not be encoded properly.
inline float4 EncodeFloatRGBA( float v )
{
  float4 kEncodeMul = float4(1.0, 255.0, 65025.0, 16581375.0);
  float kEncodeBit = 1.0/255.0;
  float4 enc = kEncodeMul * v;
  enc = frac (enc);
  enc -= enc.yzww * kEncodeBit;
  return enc;
}
inline float DecodeFloatRGBA( float4 enc )
{
  float4 kDecodeDot = float4(1.0, 1/255.0, 1/65025.0, 1/16581375.0);
  return dot( enc, kDecodeDot );
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question