Unity free - Floating point texture

Hey everyone,

Is there any way to create and read from floating point texture inside Unity free ?

Thanks a lot !

As far as I know only render textures have floating, half and int formats which makes it Pro only.

You can encode the 4 bytes from a float and store them in a ARGB32 texture and then decode them in the shader but its not very practical and bi-linear filtering and mip mapping of the texture will not work.

@scrawk you’re right, the Encode and Decode function provided in the cginc are very interesting but they give bad result when you use blending, any idea why ?

Maybe @Aras can give us some precisions about it as it seems he created those function like he explained here :slight_smile:

Encoding Floats to RGBA - The Final ?

I have seen you other post about blending but I am not 100% sure what your trying to do.

I got that you are using the camera to render to the screen and then grabbing the result into a texture.

The float has to be in the 0-0.9999 (cant be exactly 1) range to be encoded correctly so if its not thats the issue.

If the result you are rendering here is the encoded float and the shader uses blending thats the issue as you cant correctly blend a encoded value.

If your not using blend here but its the other shader where you read from the texture with the encoded value then thats fine as long as the result is decoded back to a float.

If the texture your reading from has bilinear filtering enabled then thats the issue as you cant filter a encoded value. If you want use filtering you will have to set the filter mode to point and write your own bilinear filter code in the shader.

If none of these are a problem then its not the encoding thats causing the steps but something else.

Thank @scrawk !

I am using additive blending in the pass that is doing the encoding so I think that this might be the problem. But I am quite confusing about blending order. Of course if it is blending encoding values that can’t work… but when does the blending intervene ?

Here is how it is done. Replacement Shader - Pass 1 with Blend one one with EncodeFloatRGBA

Is there a way to encore the result of the blending ? Maybe in another pass, but I think that there is no way to retrieve the result of the blending.

Ahh ok.

So your first pass encodes a float into 4 bytes and blends them one one which is additive so the encoded bytes get added to whatever is in the screen buffer. Correct?

The blending is applied to the final encoded result which will destroy the encoding, unless the screen is cleared to 0 which makes it point less to even have blending.

Theres no way to get the result of the blending.

Do you really need the blending? Do you need to render multiple passes and accumulate the results into the texture?

There is one pass in a shader replacement which is using additive blending (one one) and the fragment shader of this pass outputs the encoded float value into a texture which is used in a classic surface shader with no blending.

If I understand you well, there should not be any problem with the background because it is black and as I am doing additive blending it should not affect my shader.

:frowning:

I really need blending because I need to accumulate all the faces and render the result inside a texture. But I am not doing complicated things, I only need depth accumulated over overlapping faces, if you see what I mean.

I think this part is the issue. You are rendering multiple meshes and accumulate the encoded values in the screen buffer.

The screen buffer maybe cleared to black to start with but when a mesh triangle gets rendered it now contains a value and the next triangle to get rendered will be blended with the buffer and you will get blending of two encoded floats.

If this is correct I dont think there is a way around it.

Right. If there is no simple way to retrieve blended result before encoding it, it sounds like it’s not possible to achieve what I want…

The only way is Unity Pro but I would like to make my feature fully available for Unity free.

Another solution would be to add depth peeling but it might be a bit overkill for what I am trying to do.
Unfortunately, I think that I will need to truncate it. :frowning:

There is sort of a hack for Untiy Free which uses the Oculus rift sdk in Unity 4.5 or 4.6, that enables render textures in unity free for desktop targets.