Esempio n. 1
0
    public static void SaveAsRaw(int size, CBUtility.Channels channels, string fileName, string filePath, RenderTexture rtex, ComputeShader readDataComputeShader)
    {
        var channelsSize = (byte)channels;
        var buffer       = new ComputeBuffer(size, sizeof(float) * channelsSize);

        CBUtility.ReadFromRenderTexture(rtex, channels, buffer, readDataComputeShader);

        var data = new float[size * channelsSize];

        buffer.GetData(data);

        var byteArray = new byte[size * 4 * channelsSize];

        Buffer.BlockCopy(data, 0, byteArray, 0, byteArray.Length);
        File.WriteAllBytes(Application.dataPath + filePath + fileName + ".raw", byteArray);

        buffer.Release();
    }
Esempio n. 2
0
    public static void SaveAs8bit(int width, int height, CBUtility.Channels channels, string fileName, string filePath, RenderTexture rtex, ComputeShader readDataComputeShader, float scale = 1.0f)
    {
        var channelsSize = (byte)channels;
        var buffer       = new ComputeBuffer(width * height, sizeof(float) * channelsSize);

        CBUtility.ReadFromRenderTexture(rtex, channels, buffer, readDataComputeShader);

        var data = new float[width * height * channelsSize];

        buffer.GetData(data);

        var texture = new Texture2D(width, height);

        for (int x = 0; x < width; x++)
        {
            for (int y = 0; y < height; y++)
            {
                var color = new Color(0, 0, 0, 1);

                color.r = data[(x + y * width) * channelsSize + 0];

                if (channelsSize > 1)
                {
                    color.g = data[(x + y * width) * channelsSize + 1];
                }

                if (channelsSize > 2)
                {
                    color.b = data[(x + y * width) * channelsSize + 2];
                }

                texture.SetPixel(x, y, color * scale);
            }
        }

        texture.Apply();

        var bytes = texture.EncodeToPNG();

        File.WriteAllBytes(Application.dataPath + filePath + fileName + ".png", bytes);

        buffer.Release();
    }