在Unity 3D中从RGBAFloat纹理读取浮点值 [英] Read float values from RGBAFloat texture in Unity 3D

查看:329
本文介绍了在Unity 3D中从RGBAFloat纹理读取浮点值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎人们对浮点纹理的讨论不多.我用它们进行了一些计算,然后将结果转发到另一个曲面着色器(以获得某些特定的变形),这很酷,如果我在着色器中消化结果,它总是对我有用,但是这次我需要获取这些值CPU这样,我就得到一个带有结果的float []数组(在调用Graphics.Blit填充浮点纹理之后).如何实现?

It seems people aren't discussing much around floating point textures. I used them to do some computations and then forward the result to another surface shader (to obtain some specific deformations) and that's cool, it always works for me if I digest the results in a shader but this time I need to get those values CPU side so I get a float[] array with the results (just after calling Graphics.Blit that fills the floating point texture). How can this be achieved?

顺便提一句:到目前为止,我看到的唯一使用此方法的人是Keijiro,例如在他的Kvant Wall中;如果您还有其他消息来源,请告诉我.

On a side note: the only guy that I saw using this method so far is Keijiro, for example in his Kvant Wall; if you have other sources I'd be grateful if you let me know.

偶然地,我知道有计算着色器以及OpenCL和CUDA.这是我现在需要的方法.

Incidentally, I know there are compute shaders and OpenCL and CUDA. This is the method I need now.

推荐答案

所以我想出了这个解决方案.

So I came up with this solution.

 float[] DecodeFloatTexture()
{
    Texture2D decTex = new Texture2D(resultBuffer.width, resultBuffer.height, TextureFormat.RGBAFloat, false);
    RenderTexture.active = resultBuffer;
    decTex.ReadPixels(new Rect(0, 0, resultBuffer.width, resultBuffer.height), 0, 0);
    decTex.Apply();
    RenderTexture.active = null;
    Color[] colors = decTex.GetPixels();
    // HERE YOU CAN GET ALL 4 FLOATS OUT OR JUST THOSE YOU NEED.
    // IN MY CASE ALL 4 VALUES HAVE A MEANING SO I'M GETTING THEM ALL.
    float[] results = new float[colors.Length*4];
    for(int i=0; i<colors.Length; i++)
    {
        results[i * 4] = colors[i].r;
        results[i * 4 + 1] = colors[i].g;
        results[i * 4 + 2] = colors[i].b;
        results[i * 4 + 3] = colors[i].a;
    }
    return results;
}

或者,如果我们需要的不是浮点数,则可以使用GetRawTextureData通过System.BitConverter将字节转换为新类型,从而为从着色器传递的数据提供一些灵活性(例如,如果您的片段着色器正在输出half4).如果您需要 float ,尽管第一种方法更好.

Alternatively, if what we need is not a float, GetRawTextureData can be used to then convert the bytes to the new type with System.BitConverter which gives some flexibility on the data you are passing from the shader (for example if your fragment shader is outputting half4). If you need float though the first method is better.

 float[] DecodeFloatTexture()
{
    Texture2D decTex = new Texture2D(resultBuffer.width, resultBuffer.height, TextureFormat.RGBAFloat, false);
    RenderTexture.active = resultBuffer;
    decTex.ReadPixels(new Rect(0, 0, resultBuffer.width, resultBuffer.height), 0, 0);
    decTex.Apply();
    RenderTexture.active = null;
    byte[] bytes = decTex.GetRawTextureData();
    float[] results = new float[resultBuffer.width * resultBuffer.height];
    for (int i = 0; i < results.Length; i++)
    {
        int byteIndex = i * 4;
        byte[] localBytes = new byte[] { bytes[i], bytes[i + 1], bytes[i + 2], bytes[i + 3] }; // converts 4 bytes to a float
        results[i] = System.BitConverter.ToSingle(localBytes, 0);
    }
    return results;
}

这篇关于在Unity 3D中从RGBAFloat纹理读取浮点值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆