public static void WriteIntoRenderTexture2D(RenderTexture tex, int channels, float[] data) { if (tex == null) { Debug.Log("EncodeFloat::WriteIntoRenderTexture2D - RenderTexture is null"); return; } if (data == null) { Debug.Log("EncodeFloat::WriteIntoRenderTexture2D - Data is null"); return; } if (channels < 1 || channels > 4) { Debug.Log("EncodeFloat::WriteIntoRenderTexture2D - Channels must be 1, 2, 3, or 4"); return; } int width = tex.width; int height = tex.height; int num = width * height * channels; Color[] map = new Color[num]; float max = 1f; float min = 0f; EncodeFloat.LoadData(data, map, num, ref min, ref max); EncodeFloat.DecodeFloat2D(width, height, channels, min, max, tex, map); }
void GenerateWavesSpectrum() { float[] spectrum01 = new float[m_size * m_size * 4]; float[] spectrum23 = new float[m_size * m_size * 4]; int idx; float i; float j; Vector2 sample12XY; Vector2 sample12ZW; Vector2 sample34XY; Vector2 sample34ZW; Random.InitState(0); for (int x = 0; x < m_size; x++) { for (int y = 0; y < m_size; y++) { idx = x + y * m_size; i = (x >= m_size / 2) ? (float)(x - m_size) : (float)x; j = (y >= m_size / 2) ? (float)(y - m_size) : (float)y; sample12XY = GetSpectrumSample(i, j, m_gridSizes.x, Mathf.PI / m_gridSizes.x); sample12ZW = GetSpectrumSample(i, j, m_gridSizes.y, Mathf.PI * m_fsize / m_gridSizes.x); sample34XY = GetSpectrumSample(i, j, m_gridSizes.z, Mathf.PI * m_fsize / m_gridSizes.y); sample34ZW = GetSpectrumSample(i, j, m_gridSizes.w, Mathf.PI * m_fsize / m_gridSizes.z); spectrum01[idx * 4 + 0] = sample12XY.x; spectrum01[idx * 4 + 1] = sample12XY.y; spectrum01[idx * 4 + 2] = sample12ZW.x; spectrum01[idx * 4 + 3] = sample12ZW.y; spectrum23[idx * 4 + 0] = sample34XY.x; spectrum23[idx * 4 + 1] = sample34XY.y; spectrum23[idx * 4 + 2] = sample34ZW.x; spectrum23[idx * 4 + 3] = sample34ZW.y; } } //Write floating point data into render texture EncodeFloat.WriteIntoRenderTexture(m_spectrum01, 4, spectrum01); EncodeFloat.WriteIntoRenderTexture(m_spectrum23, 4, spectrum23); }
private static void LoadData(float[] data, Color[] map, int size, ref float min, ref float max) { for (int i = 0; i < size; i++) { if (data[i] > max) { max = data[i]; } if (data[i] < min) { min = data[i]; } } min = Mathf.Abs(min); max += min; for (int j = 0; j < size; j++) { float val = (data[j] + min) / max; float[] array = EncodeFloat.EncodeFloatRGBA(val); map[j] = new Color(array[0], array[1], array[2], array[3]); } }
void CreateWTable() { //Some values need for the InitWaveSpectrum function can be precomputed Vector2 uv, st; float k1, k2, k3, k4, w1, w2, w3, w4; float[] table = new float[m_size * m_size * 4]; for (int x = 0; x < m_size; x++) { for (int y = 0; y < m_size; y++) { uv = new Vector2(x, y) / m_fsize; st.x = uv.x > 0.5f ? uv.x - 1.0f : uv.x; st.y = uv.y > 0.5f ? uv.y - 1.0f : uv.y; k1 = (st * m_inverseGridSizes.x).magnitude; k2 = (st * m_inverseGridSizes.y).magnitude; k3 = (st * m_inverseGridSizes.z).magnitude; k4 = (st * m_inverseGridSizes.w).magnitude; w1 = Mathf.Sqrt(9.81f * k1 * (1.0f + k1 * k1 / (WAVE_KM * WAVE_KM))); w2 = Mathf.Sqrt(9.81f * k2 * (1.0f + k2 * k2 / (WAVE_KM * WAVE_KM))); w3 = Mathf.Sqrt(9.81f * k3 * (1.0f + k3 * k3 / (WAVE_KM * WAVE_KM))); w4 = Mathf.Sqrt(9.81f * k4 * (1.0f + k4 * k4 / (WAVE_KM * WAVE_KM))); table[(x + y * m_size) * 4 + 0] = w1; table[(x + y * m_size) * 4 + 1] = w2; table[(x + y * m_size) * 4 + 2] = w3; table[(x + y * m_size) * 4 + 3] = w4; } } //Write floating point data into render texture EncodeFloat.WriteIntoRenderTexture(m_WTable, 4, table); }