private void SaveAsRaw(int size, int channels, string fileName, RenderTexture rtex) { ComputeBuffer buffer = new ComputeBuffer(size, sizeof(float) * channels); CBUtility.ReadFromRenderTexture(rtex, channels, buffer, m_readData); float[] data = new float[size * channels]; buffer.GetData(data); byte[] byteArray = new byte[size * 4 * channels]; System.Buffer.BlockCopy(data, 0, byteArray, 0, byteArray.Length); System.IO.File.WriteAllBytes(Application.dataPath + m_filePath + fileName + ".raw", byteArray); buffer.Release(); }
private void SaveAs8bit(int width, int height, int channels, string fileName, RenderTexture rtex, float scale = 1.0f) { //Only used to get a visible image for debugging. ComputeBuffer buffer = new ComputeBuffer(width * height, sizeof(float) * channels); CBUtility.ReadFromRenderTexture(rtex, channels, buffer, m_readData); float[] data = new float[width * height * channels]; buffer.GetData(data); Texture2D tex = new Texture2D(width, height); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { Color col = new Color(0, 0, 0, 1); col.r = data[(x + y * width) * channels + 0]; if (channels > 1) { col.g = data[(x + y * width) * channels + 1]; } if (channels > 2) { col.b = data[(x + y * width) * channels + 2]; } tex.SetPixel(x, y, col * scale); } } tex.Apply(); byte[] bytes = tex.EncodeToPNG(); System.IO.File.WriteAllBytes(Application.dataPath + m_filePath + fileName + ".png", bytes); buffer.Release(); }