264 lines
7.3 KiB
C#
264 lines
7.3 KiB
C#
using System;
|
|
using System.IO;
|
|
using UnityEngine;
|
|
using UnityEngine.Rendering;
|
|
|
|
namespace Artngame.SKYMASTER.PlanetCreator
|
|
{
|
|
public static class CBUtility
|
|
{
|
|
public static void ReadFromRenderTexture(RenderTexture tex, int channels, ComputeBuffer buffer, ComputeShader readData)
|
|
{
|
|
if (tex == null)
|
|
{
|
|
Debug.Log("CBUtility::ReadFromRenderTexture - RenderTexture is null");
|
|
return;
|
|
}
|
|
if (buffer == null)
|
|
{
|
|
Debug.Log("CBUtility::ReadFromRenderTexture - buffer is null");
|
|
return;
|
|
}
|
|
if (readData == null)
|
|
{
|
|
Debug.Log("CBUtility::ReadFromRenderTexture - Computer shader is null");
|
|
return;
|
|
}
|
|
if (channels < 1 || channels > 4)
|
|
{
|
|
Debug.Log("CBUtility::ReadFromRenderTexture - Channels must be 1, 2, 3, or 4");
|
|
return;
|
|
}
|
|
int num = -1;
|
|
int num2 = 1;
|
|
string text = "2D";
|
|
string text2 = "C" + channels;
|
|
if (tex.dimension == TextureDimension.Tex3D)
|
|
{
|
|
num2 = tex.volumeDepth;
|
|
text = "3D";
|
|
}
|
|
num = readData.FindKernel("read" + text + text2);
|
|
if (num == -1)
|
|
{
|
|
Debug.Log("CBUtility::ReadFromRenderTexture - could not find kernel read" + text + text2);
|
|
return;
|
|
}
|
|
int width = tex.width;
|
|
int height = tex.height;
|
|
readData.SetTexture(num, "_Tex" + text, tex);
|
|
readData.SetInt("_Width", width);
|
|
readData.SetInt("_Height", height);
|
|
readData.SetBuffer(num, "_Buffer" + text + text2, buffer);
|
|
readData.Dispatch(num, Mathf.Max(1, width / 8), Mathf.Max(1, height / 8), Mathf.Max(1, num2 / 8));
|
|
}
|
|
|
|
public static void WriteIntoRenderTexture(RenderTexture tex, int channels, ComputeBuffer buffer, ComputeShader writeData)
|
|
{
|
|
if (tex == null)
|
|
{
|
|
Debug.Log("CBUtility::WriteIntoRenderTexture - RenderTexture is null");
|
|
return;
|
|
}
|
|
if (buffer == null)
|
|
{
|
|
Debug.Log("CBUtility::WriteIntoRenderTexture - buffer is null");
|
|
return;
|
|
}
|
|
if (writeData == null)
|
|
{
|
|
Debug.Log("CBUtility::WriteIntoRenderTexture - Computer shader is null");
|
|
return;
|
|
}
|
|
if (channels < 1 || channels > 4)
|
|
{
|
|
Debug.Log("CBUtility::WriteIntoRenderTexture - Channels must be 1, 2, 3, or 4");
|
|
return;
|
|
}
|
|
if (!tex.enableRandomWrite)
|
|
{
|
|
Debug.Log("CBUtility::WriteIntoRenderTexture - you must enable random write on render texture");
|
|
return;
|
|
}
|
|
int num = -1;
|
|
int num2 = 1;
|
|
string text = "2D";
|
|
string text2 = "C" + channels;
|
|
if (tex.dimension == TextureDimension.Tex3D)
|
|
{
|
|
num2 = tex.volumeDepth;
|
|
text = "3D";
|
|
}
|
|
num = writeData.FindKernel("write" + text + text2);
|
|
if (num == -1)
|
|
{
|
|
Debug.Log("CBUtility::WriteIntoRenderTexture - could not find kernel write" + text + text2);
|
|
return;
|
|
}
|
|
int width = tex.width;
|
|
int height = tex.height;
|
|
writeData.SetTexture(num, "_Des" + text + text2, tex);
|
|
writeData.SetInt("_Width", width);
|
|
writeData.SetInt("_Height", height);
|
|
writeData.SetBuffer(num, "_Buffer" + text + text2, buffer);
|
|
writeData.Dispatch(num, Mathf.Max(1, width / 8), Mathf.Max(1, height / 8), Mathf.Max(1, num2 / 8));
|
|
}
|
|
|
|
public static void WriteIntoRenderTexture(RenderTexture tex, int channels, string path, ComputeBuffer buffer, ComputeShader writeData)
|
|
{
|
|
if (tex == null)
|
|
{
|
|
Debug.Log("CBUtility::WriteIntoRenderTexture - RenderTexture is null");
|
|
return;
|
|
}
|
|
if (buffer == null)
|
|
{
|
|
Debug.Log("CBUtility::WriteIntoRenderTexture - buffer is null");
|
|
return;
|
|
}
|
|
if (writeData == null)
|
|
{
|
|
Debug.Log("CBUtility::WriteIntoRenderTexture - Computer shader is null");
|
|
return;
|
|
}
|
|
if (channels < 1 || channels > 4)
|
|
{
|
|
Debug.Log("CBUtility::WriteIntoRenderTexture - Channels must be 1, 2, 3, or 4");
|
|
return;
|
|
}
|
|
if (!tex.enableRandomWrite)
|
|
{
|
|
Debug.Log("CBUtility::WriteIntoRenderTexture - you must enable random write on render texture");
|
|
return;
|
|
}
|
|
int num = -1;
|
|
int num2 = 1;
|
|
string text = "2D";
|
|
string text2 = "C" + channels;
|
|
if (tex.dimension == TextureDimension.Tex3D)
|
|
{
|
|
num2 = tex.volumeDepth;
|
|
text = "3D";
|
|
}
|
|
num = writeData.FindKernel("write" + text + text2);
|
|
if (num == -1)
|
|
{
|
|
Debug.Log("CBUtility::WriteIntoRenderTexture - could not find kernel write" + text + text2);
|
|
return;
|
|
}
|
|
int width = tex.width;
|
|
int height = tex.height;
|
|
int num3 = width * height * num2 * channels;
|
|
float[] array = new float[num3];
|
|
if (LoadRawFile(path, array, num3))
|
|
{
|
|
buffer.SetData(array);
|
|
writeData.SetTexture(num, "_Des" + text + text2, tex);
|
|
writeData.SetInt("_Width", width);
|
|
writeData.SetInt("_Height", height);
|
|
writeData.SetBuffer(num, "_Buffer" + text + text2, buffer);
|
|
writeData.Dispatch(num, Mathf.Max(1, width / 8), Mathf.Max(1, height / 8), Mathf.Max(1, num2 / 8));
|
|
}
|
|
}
|
|
|
|
public static void WriteIntoRenderTexture(RenderTexture tex, int channels, TextAsset raw, ComputeBuffer buffer, ComputeShader writeData)
|
|
{
|
|
if (tex == null)
|
|
{
|
|
Debug.Log("CBUtility::WriteIntoRenderTexture - RenderTexture is null");
|
|
return;
|
|
}
|
|
if (buffer == null)
|
|
{
|
|
Debug.Log("CBUtility::WriteIntoRenderTexture - buffer is null");
|
|
return;
|
|
}
|
|
if (writeData == null)
|
|
{
|
|
Debug.Log("CBUtility::WriteIntoRenderTexture - Computer shader is null");
|
|
return;
|
|
}
|
|
if (channels < 1 || channels > 4)
|
|
{
|
|
Debug.Log("CBUtility::WriteIntoRenderTexture - Channels must be 1, 2, 3, or 4");
|
|
return;
|
|
}
|
|
if (!tex.enableRandomWrite)
|
|
{
|
|
Debug.Log("CBUtility::WriteIntoRenderTexture - you must enable random write on render texture");
|
|
return;
|
|
}
|
|
int num = -1;
|
|
int num2 = 1;
|
|
string text = "2D";
|
|
string text2 = "C" + channels;
|
|
if (tex.dimension == TextureDimension.Tex3D)
|
|
{
|
|
num2 = tex.volumeDepth;
|
|
text = "3D";
|
|
}
|
|
num = writeData.FindKernel("write" + text + text2);
|
|
if (num == -1)
|
|
{
|
|
Debug.Log("CBUtility::WriteIntoRenderTexture - could not find kernel write" + text + text2);
|
|
return;
|
|
}
|
|
int width = tex.width;
|
|
int height = tex.height;
|
|
int num3 = width * height * num2 * channels;
|
|
float[] array = new float[num3];
|
|
if (LoadRawFile(raw, array, num3))
|
|
{
|
|
buffer.SetData(array);
|
|
writeData.SetTexture(num, "_Des" + text + text2, tex);
|
|
writeData.SetInt("_Width", width);
|
|
writeData.SetInt("_Height", height);
|
|
writeData.SetBuffer(num, "_Buffer" + text + text2, buffer);
|
|
writeData.Dispatch(num, Mathf.Max(1, width / 8), Mathf.Max(1, height / 8), Mathf.Max(1, num2 / 8));
|
|
}
|
|
}
|
|
|
|
private static bool LoadRawFile(string path, float[] map, int size)
|
|
{
|
|
FileInfo fileInfo = new FileInfo(path);
|
|
if (fileInfo == null)
|
|
{
|
|
Debug.Log("CBUtility::LoadRawFile - Raw file not found");
|
|
return false;
|
|
}
|
|
FileStream fileStream = fileInfo.OpenRead();
|
|
byte[] array = new byte[fileInfo.Length];
|
|
fileStream.Read(array, 0, (int)fileInfo.Length);
|
|
fileStream.Close();
|
|
if (size > fileInfo.Length / 4)
|
|
{
|
|
Debug.Log("CBUtility::LoadRawFile - Raw file is not the required size");
|
|
return false;
|
|
}
|
|
int num = 0;
|
|
int num2 = 0;
|
|
while (num < size)
|
|
{
|
|
map[num] = BitConverter.ToSingle(array, num2);
|
|
num++;
|
|
num2 += 4;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
private static bool LoadRawFile(TextAsset raw, float[] map, int size)
|
|
{
|
|
byte[] bytes = raw.bytes;
|
|
int num = 0;
|
|
int num2 = 0;
|
|
while (num < size)
|
|
{
|
|
map[num] = BitConverter.ToSingle(bytes, num2);
|
|
num++;
|
|
num2 += 4;
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
}
|