880 lines
26 KiB
C#
880 lines
26 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class RealWaterNormalMap : MonoBehaviour, RealWaterInterface
|
|
{
|
|
[Tooltip("The texture on which to draw the simulation).")]
|
|
public Texture2D normalMap;
|
|
|
|
public SimulationSettings simulationSettings;
|
|
|
|
public FadeSettings fadeSettings;
|
|
|
|
public InputSmoothingSettings inputSmoothingSettings;
|
|
|
|
public ObstructionSettings obstructionSettings;
|
|
|
|
public ObstructionTextureSettings obstructionTextureSettings;
|
|
|
|
[Tooltip("Wave dissipation multiplier (Higher values-> Less dissipation).")]
|
|
[Range(0.7f, 0.999f)]
|
|
public float Damping = 0.97f;
|
|
|
|
private int[] buffer1;
|
|
|
|
private int[] buffer2;
|
|
|
|
private int[] bufferCurrent;
|
|
|
|
private TextureScanner textureScanner;
|
|
|
|
private int columns;
|
|
|
|
private int rows;
|
|
|
|
private int baseForce = 18000000;
|
|
|
|
private bool buffer1Current = true;
|
|
|
|
private bool buffer1CurrentAmb = true;
|
|
|
|
private int arrayLength;
|
|
|
|
private RealWaterMouseInput inputManager;
|
|
|
|
private Vector3[] vertices;
|
|
|
|
private bool allowInput = true;
|
|
|
|
private float simulationSpeed;
|
|
|
|
private bool visible = true;
|
|
|
|
private bool ready;
|
|
|
|
private Renderer rend;
|
|
|
|
private bool paused = true;
|
|
|
|
private int obstructionCounter;
|
|
|
|
private int[] obstructionTextureColumns;
|
|
|
|
private int[] obstructionTextureRows;
|
|
|
|
private int[] obstructedTexturePositions;
|
|
|
|
private int[] obstructedPositions;
|
|
|
|
private int obstructionTextureCounter;
|
|
|
|
private bool obstructionsScanned;
|
|
|
|
private bool obstructionTextuReScanned;
|
|
|
|
private bool obstructionTexturesEnabledCurrent;
|
|
|
|
private bool obstructionsEnabledCurrent;
|
|
|
|
private int rowStart;
|
|
|
|
private int rowEnd;
|
|
|
|
private int colStart;
|
|
|
|
private int colEnd;
|
|
|
|
private Vector2 inputPosition;
|
|
|
|
private Vector2 inputPositionPrev;
|
|
|
|
private bool up;
|
|
|
|
private bool down;
|
|
|
|
private bool left;
|
|
|
|
private bool right;
|
|
|
|
private void Start()
|
|
{
|
|
initializeVariables();
|
|
if (obstructionSettings.ObstructionsEnabled)
|
|
{
|
|
ObstructionDetection();
|
|
}
|
|
if (obstructionTextureSettings.ObstructionTexturesEnabled)
|
|
{
|
|
ProcessObstructionTexture();
|
|
}
|
|
InvokeRepeating("VariableChangeCheck", 0.001f, 1f / 30f);
|
|
InvokeRepeating("Fade", 0.0001f, 1f / fadeSettings.fadeRate);
|
|
if (simulationSettings.fullPlaneSimulation)
|
|
{
|
|
initFullPlaneSim();
|
|
}
|
|
InvokeRepeating("VisibilityCheck", 0.001f, 0.2f);
|
|
}
|
|
|
|
private void initializeVariables()
|
|
{
|
|
inputManager = Object.FindObjectOfType(typeof(RealWaterMouseInput)) as RealWaterMouseInput;
|
|
rend = GetComponent<Renderer>();
|
|
rowStart = 1;
|
|
rowEnd = 1;
|
|
colStart = 1;
|
|
colEnd = 1;
|
|
if (normalMap != null)
|
|
{
|
|
try
|
|
{
|
|
normalMap.GetPixel(0, 0);
|
|
}
|
|
catch (UnityException ex)
|
|
{
|
|
if (ex.Message.StartsWith("Texture '" + normalMap.name + "' is not readable"))
|
|
{
|
|
Debug.LogError("Please enable read/write for Normal Map: [" + normalMap.name + "]");
|
|
paused = true;
|
|
simulationSettings.collisionCulling = false;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError("No normal map assigned!");
|
|
paused = true;
|
|
simulationSettings.collisionCulling = false;
|
|
}
|
|
arrayLength = normalMap.GetPixels().Length;
|
|
buffer1 = new int[arrayLength];
|
|
buffer2 = new int[arrayLength];
|
|
Color32[] array = new Color32[arrayLength];
|
|
columns = normalMap.width;
|
|
rows = normalMap.height;
|
|
bufferCurrent = buffer1;
|
|
obstructionTexturesEnabledCurrent = obstructionTextureSettings.ObstructionTexturesEnabled;
|
|
visible = true;
|
|
for (int i = 0; i < arrayLength; i++)
|
|
{
|
|
array[i] = new Color32(127, 127, 127, 127);
|
|
}
|
|
normalMap.SetPixels32(array);
|
|
normalMap.Apply();
|
|
textureScanner = new TextureScanner(rows, columns);
|
|
}
|
|
|
|
private void initFullPlaneSim()
|
|
{
|
|
inputPosition = new Vector2(columns / 2, rows / 2);
|
|
simulationSettings.adaptiveSimulationSettings.useAdaptivePosition = false;
|
|
if (rows < columns)
|
|
{
|
|
simulationSettings.simSize = columns;
|
|
}
|
|
else
|
|
{
|
|
simulationSettings.simSize = rows;
|
|
}
|
|
}
|
|
|
|
public void Pause()
|
|
{
|
|
CancelInvoke("MainLoop");
|
|
CancelInvoke("Fade");
|
|
CancelInvoke("VariableChangeCheck");
|
|
CancelInvoke("SetObstructionTexture");
|
|
CancelInvoke("SetObstructions");
|
|
Color32[] array = new Color32[(colEnd - colStart) * (rowEnd - rowStart)];
|
|
for (int i = colStart; i < colEnd; i++)
|
|
{
|
|
for (int j = rowStart; j < rowEnd; j++)
|
|
{
|
|
int num = (j - 1) * columns + i - 1;
|
|
int num2 = (j - rowStart) * (colEnd - colStart) + i - colStart;
|
|
buffer1[num] = 0;
|
|
buffer2[num] = 0;
|
|
bufferCurrent[num] = 0;
|
|
array[num2] = new Color32(127, 127, 127, 127);
|
|
}
|
|
}
|
|
normalMap.SetPixels32(colStart, rowStart, colEnd - colStart, rowEnd - rowStart, array);
|
|
Fade();
|
|
normalMap.Apply(updateMipmaps: false);
|
|
paused = true;
|
|
}
|
|
|
|
public void Play()
|
|
{
|
|
if (paused)
|
|
{
|
|
InvokeRepeating("MainLoop", 0.001f, 1f / simulationSpeed);
|
|
InvokeRepeating("Fade", 0.0001f, 1f / fadeSettings.fadeRate);
|
|
if (obstructionTextureSettings.ObstructionTexturesEnabled && obstructionTextureSettings.SetRate == ObstructionTextureSettings.setRate.Half)
|
|
{
|
|
InvokeRepeating("SetObstructionTexture", 0.008f, 1f / (simulationSpeed * 0.55f));
|
|
}
|
|
else if (obstructionTextureSettings.ObstructionTexturesEnabled && obstructionTextureSettings.SetRate == ObstructionTextureSettings.setRate.Full)
|
|
{
|
|
InvokeRepeating("SetObstructionTexture", 0.008f, 1f / simulationSpeed);
|
|
}
|
|
if (obstructionSettings.ObstructionsEnabled && obstructionSettings.SetRate == ObstructionSettings.setRate.Half)
|
|
{
|
|
InvokeRepeating("SetObstructions", 0.003f, 1f / (simulationSpeed * 0.55f));
|
|
}
|
|
else if (obstructionSettings.ObstructionsEnabled && obstructionSettings.SetRate == ObstructionSettings.setRate.Full)
|
|
{
|
|
InvokeRepeating("SetObstructions", 0.003f, 1f / simulationSpeed);
|
|
}
|
|
obstructionsEnabledCurrent = obstructionSettings.ObstructionsEnabled;
|
|
obstructionTexturesEnabledCurrent = obstructionTextureSettings.ObstructionTexturesEnabled;
|
|
InvokeRepeating("VariableChangeCheck", 0.001f, 1f / 30f);
|
|
paused = false;
|
|
visible = true;
|
|
}
|
|
}
|
|
|
|
public void ObstructionDetection()
|
|
{
|
|
List<int> list = new List<int>();
|
|
Vector3 center = rend.bounds.center;
|
|
float num = rend.bounds.size.x / 2f;
|
|
float num2 = rend.bounds.size.z / 2f;
|
|
float num3 = center[0] - num;
|
|
float num4 = center[0] + num;
|
|
float num5 = center[2] + num2;
|
|
float num6 = center[2] - num2;
|
|
float num7 = (num4 - num3) / (float)(columns + 1);
|
|
float num8 = (num5 - num6) / (float)(rows + 1);
|
|
float y = center[1];
|
|
for (float num9 = num3; num9 < num4; num9 += num7)
|
|
{
|
|
for (float num10 = num6; num10 < num5; num10 += num8)
|
|
{
|
|
Vector3 vector = new Vector3(num9, y, num10);
|
|
if (Physics.Raycast(vector + Vector3.up * obstructionSettings.heightCuttoff, Vector3.down, out var hitInfo, obstructionSettings.heightCuttoff) && hitInfo.collider.gameObject.name != "RealWater Plane" && hitInfo.collider.gameObject.GetComponent<RealWaterCollider>() == null && !hitInfo.collider.gameObject.name.Contains("RW-NonOb") && hitInfo.collider.gameObject.GetComponent<Camera>() == null && Physics.Raycast(vector + Vector3.up * 0.01f, Vector3.down, out var hitInfo2, 0.01f) && hitInfo2.collider.gameObject.name == "RealWater Plane")
|
|
{
|
|
int num11 = (int)(hitInfo2.textureCoord.x * (float)columns);
|
|
int item = (int)(hitInfo2.textureCoord.y * (float)rows) * columns + num11;
|
|
list.Add(item);
|
|
obstructionCounter++;
|
|
}
|
|
}
|
|
}
|
|
obstructedPositions = list.ToArray();
|
|
list.Clear();
|
|
list.TrimExcess();
|
|
obstructionsScanned = true;
|
|
}
|
|
|
|
public void ProcessObstructionTexture()
|
|
{
|
|
bool flag = true;
|
|
if (obstructionTextureSettings.ObstructionTexture != null)
|
|
{
|
|
try
|
|
{
|
|
obstructionTextureSettings.ObstructionTexture.GetPixel(0, 0);
|
|
}
|
|
catch (UnityException ex)
|
|
{
|
|
if (ex.Message.StartsWith("Texture '" + obstructionTextureSettings.ObstructionTexture.name + "' is not readable"))
|
|
{
|
|
Debug.LogError("Please enable read/write for Obstruction Texture: [" + obstructionTextureSettings.ObstructionTexture.name + "]");
|
|
flag = false;
|
|
}
|
|
}
|
|
if (flag)
|
|
{
|
|
textureScanner.setTexture(obstructionTextureSettings.ObstructionTexture);
|
|
textureScanner.setGSCuttOff(obstructionTextureSettings.GrayScaleCuttoff);
|
|
obstructionTextuReScanned = textureScanner.ProcessTexture();
|
|
obstructionTextureColumns = textureScanner.getCols();
|
|
obstructionTextureRows = textureScanner.getRows();
|
|
obstructionTextureCounter = textureScanner.getCount();
|
|
obstructedTexturePositions = new int[obstructionTextureCounter];
|
|
for (int i = 0; i < obstructionTextureCounter; i++)
|
|
{
|
|
obstructedTexturePositions[i] = obstructionTextureRows[i] * columns + obstructionTextureColumns[i];
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
obstructionTextuReScanned = true;
|
|
Debug.LogError("No Obstruction Texture Assigned");
|
|
}
|
|
}
|
|
|
|
private void VisibilityCheck()
|
|
{
|
|
if (!GetComponent<Renderer>().isVisible && ready)
|
|
{
|
|
CancelInvoke("MainLoop");
|
|
CancelInvoke("VariableChangeCheck");
|
|
CancelInvoke("SetObstructionTexture");
|
|
CancelInvoke("SetObstructions");
|
|
CancelInvoke("Fade");
|
|
visible = false;
|
|
}
|
|
else if (!visible && GetComponent<Renderer>().isVisible && ready && !paused)
|
|
{
|
|
InvokeRepeating("MainLoop", 0.001f, 1f / simulationSpeed);
|
|
InvokeRepeating("Fade", 0.0001f, 1f / fadeSettings.fadeRate);
|
|
if (obstructionTextureSettings.ObstructionTexturesEnabled && obstructionTextureSettings.SetRate == ObstructionTextureSettings.setRate.Half)
|
|
{
|
|
InvokeRepeating("SetObstructionTexture", 0.008f, 1f / (simulationSpeed * 0.55f));
|
|
}
|
|
else if (obstructionTextureSettings.ObstructionTexturesEnabled && obstructionTextureSettings.SetRate == ObstructionTextureSettings.setRate.Full)
|
|
{
|
|
InvokeRepeating("SetObstructionTexture", 0.008f, 1f / simulationSpeed);
|
|
}
|
|
if (obstructionSettings.ObstructionsEnabled && obstructionSettings.SetRate == ObstructionSettings.setRate.Half)
|
|
{
|
|
InvokeRepeating("SetObstructions", 0.003f, 1f / (simulationSpeed * 0.55f));
|
|
}
|
|
else if (obstructionSettings.ObstructionsEnabled && obstructionSettings.SetRate == ObstructionSettings.setRate.Full)
|
|
{
|
|
InvokeRepeating("SetObstructions", 0.003f, 1f / simulationSpeed);
|
|
}
|
|
obstructionsEnabledCurrent = obstructionSettings.ObstructionsEnabled;
|
|
obstructionTexturesEnabledCurrent = obstructionTextureSettings.ObstructionTexturesEnabled;
|
|
InvokeRepeating("VariableChangeCheck", 0.001f, 1f / 30f);
|
|
visible = true;
|
|
}
|
|
}
|
|
|
|
public void MainLoop()
|
|
{
|
|
if (buffer1Current)
|
|
{
|
|
HugoEliasFilter(buffer2, buffer1);
|
|
bufferCurrent = buffer1;
|
|
}
|
|
else
|
|
{
|
|
HugoEliasFilter(buffer1, buffer2);
|
|
bufferCurrent = buffer2;
|
|
}
|
|
Color32[] array = new Color32[(colEnd - colStart) * (rowEnd - rowStart)];
|
|
for (int i = colStart; i < colEnd; i++)
|
|
{
|
|
for (int j = rowStart; j < rowEnd; j++)
|
|
{
|
|
int num = (j - 1) * columns + i - 1;
|
|
int num2 = (j - rowStart) * (colEnd - colStart) + i - colStart;
|
|
if (inputSmoothingSettings.ClampBuffer)
|
|
{
|
|
bufferCurrent[num] = (int)Mathf.Clamp(bufferCurrent[num], (float)(-baseForce) * inputSmoothingSettings.Clamping, (float)baseForce * inputSmoothingSettings.Clamping);
|
|
}
|
|
byte b = (byte)(((float)(bufferCurrent[num] / 2) / ((float)baseForce * inputSmoothingSettings.Clamping) + 0.5f) * 255f);
|
|
array[num2] = new Color32(b, b, b, b);
|
|
}
|
|
}
|
|
normalMap.SetPixels32(colStart, rowStart, colEnd - colStart, rowEnd - rowStart, array);
|
|
normalMap.Apply(updateMipmaps: false);
|
|
buffer1Current = !buffer1Current;
|
|
}
|
|
|
|
private void Fade()
|
|
{
|
|
int num = simulationSettings.simSize / 2;
|
|
int num2 = (int)inputPosition[1];
|
|
int num3 = (int)inputPosition[0];
|
|
rowStart = Mathf.Clamp(num3 - num, 2, rows - 1);
|
|
rowEnd = Mathf.Clamp(num3 + num, 2, rows - 1);
|
|
colStart = Mathf.Clamp(num2 - num, 2, columns - 1);
|
|
colEnd = Mathf.Clamp(num2 + num, 2, columns - 1);
|
|
int num4 = Mathf.Clamp(colStart - fadeSettings.fadeSize, 1, columns);
|
|
int num5 = Mathf.Clamp(colEnd + fadeSettings.fadeSize, 1, columns);
|
|
int num6 = Mathf.Clamp(rowStart - fadeSettings.fadeSize, 1, rows);
|
|
int num7 = Mathf.Clamp(rowEnd + fadeSettings.fadeSize, 1, rows);
|
|
int num8 = (num5 - num4) * (rowStart - num6);
|
|
int num9 = (num5 - num4) * (num7 - rowEnd);
|
|
int num10 = (colStart - num4) * (num7 - num6);
|
|
int num11 = (num5 - colEnd) * (num7 - num6);
|
|
Color32[] array = new Color32[num8];
|
|
Color32[] array2 = new Color32[num9];
|
|
Color32[] array3 = new Color32[num10];
|
|
Color32[] array4 = new Color32[num11];
|
|
for (int i = num4; i < num5; i++)
|
|
{
|
|
for (int j = num6; j < rowStart; j++)
|
|
{
|
|
int num12 = (j - 1) * columns + i - 1;
|
|
int num13 = (j - num6) * (num5 - num4) + i - num4;
|
|
buffer1[num12] = 0;
|
|
buffer2[num12] = 0;
|
|
bufferCurrent[num12] = 0;
|
|
array[num13] = new Color32(127, 127, 127, 127);
|
|
}
|
|
for (int k = rowEnd; k < num7; k++)
|
|
{
|
|
int num14 = (k - 1) * columns + i - 1;
|
|
int num15 = (k - rowEnd) * (num5 - num4) + i - num4;
|
|
buffer1[num14] = 0;
|
|
buffer2[num14] = 0;
|
|
bufferCurrent[num14] = 0;
|
|
array2[num15] = new Color32(127, 127, 127, 127);
|
|
}
|
|
}
|
|
for (int l = num6; l < num7; l++)
|
|
{
|
|
for (int m = num4; m < colStart; m++)
|
|
{
|
|
int num16 = (l - 1) * columns + m - 1;
|
|
int num17 = (l - num6) * (colStart - num4) + m - num4;
|
|
buffer1[num16] = 0;
|
|
buffer2[num16] = 0;
|
|
bufferCurrent[num16] = 0;
|
|
array3[num17] = new Color32(127, 127, 127, 127);
|
|
}
|
|
for (int n = colEnd; n < num5; n++)
|
|
{
|
|
int num18 = (l - 1) * columns + n - 1;
|
|
int num19 = (l - num6) * (num5 - colEnd) + n - colEnd;
|
|
buffer1[num18] = 0;
|
|
buffer2[num18] = 0;
|
|
bufferCurrent[num18] = 0;
|
|
array4[num19] = new Color32(127, 127, 127, 127);
|
|
}
|
|
}
|
|
normalMap.SetPixels32(num4, num6, num5 - num4, rowStart - num6, array);
|
|
normalMap.SetPixels32(num4, rowEnd, num5 - num4, num7 - rowEnd, array2);
|
|
normalMap.SetPixels32(num4, num6, colStart - num4, num7 - num6, array3);
|
|
normalMap.SetPixels32(colEnd, num6, num5 - colEnd, num7 - num6, array4);
|
|
}
|
|
|
|
private void VariableChangeCheck()
|
|
{
|
|
if (obstructionTexturesEnabledCurrent != obstructionTextureSettings.ObstructionTexturesEnabled)
|
|
{
|
|
if (obstructionTextureSettings.ObstructionTexturesEnabled && obstructionTextureSettings.SetRate == ObstructionTextureSettings.setRate.Half)
|
|
{
|
|
InvokeRepeating("SetObstructionTexture", 0.003f, 1f / (simulationSpeed * 0.55f));
|
|
}
|
|
else if (obstructionTextureSettings.ObstructionTexturesEnabled && obstructionTextureSettings.SetRate == ObstructionTextureSettings.setRate.Full)
|
|
{
|
|
InvokeRepeating("SetObstructionTexture", 0.003f, 1f / simulationSpeed);
|
|
}
|
|
if (!obstructionTextureSettings.ObstructionTexturesEnabled)
|
|
{
|
|
CancelInvoke("SetObstructionTexture");
|
|
}
|
|
obstructionTexturesEnabledCurrent = obstructionTextureSettings.ObstructionTexturesEnabled;
|
|
}
|
|
if (obstructionsEnabledCurrent != obstructionSettings.ObstructionsEnabled)
|
|
{
|
|
if (obstructionSettings.ObstructionsEnabled && obstructionSettings.SetRate == ObstructionSettings.setRate.Half)
|
|
{
|
|
InvokeRepeating("SetObstructions", 0.003f, 1f / (simulationSpeed * 0.55f));
|
|
}
|
|
else if (obstructionSettings.ObstructionsEnabled && obstructionSettings.SetRate == ObstructionSettings.setRate.Full)
|
|
{
|
|
InvokeRepeating("SetObstructions", 0.003f, 1f / simulationSpeed);
|
|
}
|
|
else if (!obstructionSettings.ObstructionsEnabled)
|
|
{
|
|
CancelInvoke("SetObstructions");
|
|
}
|
|
obstructionsEnabledCurrent = obstructionSettings.ObstructionsEnabled;
|
|
}
|
|
}
|
|
|
|
public void DisturbBufferAt(int row, int column, int force, bool ambient)
|
|
{
|
|
if (!allowInput)
|
|
{
|
|
return;
|
|
}
|
|
int num = row * columns + column;
|
|
bufferCurrent[num] = force;
|
|
if (!inputSmoothingSettings.SmoothingEnabled)
|
|
{
|
|
return;
|
|
}
|
|
for (int i = 0; i < inputSmoothingSettings.SmoothingLevel; i++)
|
|
{
|
|
for (int j = row - inputSmoothingSettings.BlurRadius; j <= row + inputSmoothingSettings.BlurRadius; j++)
|
|
{
|
|
for (int k = column - inputSmoothingSettings.BlurRadius; k <= column + inputSmoothingSettings.BlurRadius; k++)
|
|
{
|
|
bufferCurrent[(j - 1) * columns + k - 1] = (bufferCurrent[(j - 3) * columns + (k - 2) - 1] + 4 * bufferCurrent[(j - 3) * columns + (k - 1) - 1] + 7 * bufferCurrent[(j - 3) * columns + k - 1] + 4 * bufferCurrent[(j - 3) * columns + (k + 1) - 1] + bufferCurrent[(j - 3) * columns + (k + 2) - 1] + 4 * bufferCurrent[(j - 2) * columns + (k - 2) - 1] + 16 * bufferCurrent[(j - 2) * columns + (k - 1) - 1] + 26 * bufferCurrent[(j - 2) * columns + k - 1] + 16 * bufferCurrent[(j - 2) * columns + (k + 1) - 1] + 4 * bufferCurrent[(j - 2) * columns + (k + 2) - 1] + 7 * bufferCurrent[(j - 1) * columns + (k - 2) - 1] + 26 * bufferCurrent[(j - 1) * columns + (k - 1) - 1] + 41 * bufferCurrent[(j - 1) * columns + k - 1] + 26 * bufferCurrent[(j - 1) * columns + (k + 1) - 1] + 7 * bufferCurrent[(j - 1) * columns + (k + 2) - 1] + 4 * bufferCurrent[j * columns + (k - 2) - 1] + 16 * bufferCurrent[j * columns + (k - 1) - 1] + 26 * bufferCurrent[j * columns + k - 1] + 16 * bufferCurrent[j * columns + (k + 1) - 1] + 4 * bufferCurrent[j * columns + (k + 2) - 1] + bufferCurrent[(j + 1) * columns + (k - 2) - 1] + 4 * bufferCurrent[(j + 1) * columns + (k - 1) - 1] + 7 * bufferCurrent[(j + 1) * columns + k - 1] + 4 * bufferCurrent[(j + 1) * columns + (k + 1) - 1] + bufferCurrent[(j + 1) * columns + (k + 2) - 1]) / 273;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void HugoEliasFilter(int[] previousBuffer, int[] currentBuffer)
|
|
{
|
|
int num = simulationSettings.simSize / 2;
|
|
int num2 = (int)inputPosition[1];
|
|
int num3 = (int)inputPosition[0];
|
|
if (simulationSettings.adaptiveSimulationSettings.useAdaptivePosition)
|
|
{
|
|
if ((int)inputPosition[1] < (int)inputPositionPrev[1] - simulationSettings.adaptiveSimulationSettings.adaptiveTollerance)
|
|
{
|
|
num2 = (int)Mathf.Clamp(inputPosition[1] + (float)simulationSettings.adaptiveSimulationSettings.adaptiveOffset, 2f, columns - 1);
|
|
right = true;
|
|
left = false;
|
|
}
|
|
else if ((int)inputPosition[1] <= (int)inputPositionPrev[1] + simulationSettings.adaptiveSimulationSettings.adaptiveTollerance)
|
|
{
|
|
num2 = (right ? ((int)Mathf.Clamp(inputPosition[1] + (float)simulationSettings.adaptiveSimulationSettings.adaptiveOffset, 2f, columns - 1)) : ((!left) ? ((int)inputPosition[1]) : ((int)Mathf.Clamp(inputPosition[1] - (float)simulationSettings.adaptiveSimulationSettings.adaptiveOffset, 2f, columns - 1))));
|
|
}
|
|
else
|
|
{
|
|
num2 = (int)Mathf.Clamp(inputPosition[1] - (float)simulationSettings.adaptiveSimulationSettings.adaptiveOffset, 2f, columns - 1);
|
|
left = true;
|
|
right = false;
|
|
}
|
|
if ((int)inputPosition[0] < (int)inputPositionPrev[0] - simulationSettings.adaptiveSimulationSettings.adaptiveTollerance)
|
|
{
|
|
num3 = (int)Mathf.Clamp(inputPosition[0] + (float)simulationSettings.adaptiveSimulationSettings.adaptiveOffset, 2f, rows - 1);
|
|
up = true;
|
|
down = false;
|
|
}
|
|
else if ((int)inputPosition[0] <= (int)inputPositionPrev[0] + simulationSettings.adaptiveSimulationSettings.adaptiveTollerance)
|
|
{
|
|
num3 = (up ? ((int)Mathf.Clamp(inputPosition[0] + (float)simulationSettings.adaptiveSimulationSettings.adaptiveOffset, 2f, rows - 1)) : ((!down) ? ((int)inputPosition[0]) : ((int)Mathf.Clamp(inputPosition[0] - (float)simulationSettings.adaptiveSimulationSettings.adaptiveOffset, 2f, rows - 1))));
|
|
}
|
|
else
|
|
{
|
|
num3 = (int)Mathf.Clamp(inputPosition[0] - (float)simulationSettings.adaptiveSimulationSettings.adaptiveOffset, 2f, rows - 1);
|
|
down = true;
|
|
up = false;
|
|
}
|
|
}
|
|
rowStart = Mathf.Clamp(num3 - num, 2, rows - 1);
|
|
rowEnd = Mathf.Clamp(num3 + num, 2, rows - 1);
|
|
colStart = Mathf.Clamp(num2 - num, 2, columns - 1);
|
|
colEnd = Mathf.Clamp(num2 + num, 2, columns - 1);
|
|
for (int i = colStart; i < colEnd; i++)
|
|
{
|
|
for (int j = rowStart; j < rowEnd; j++)
|
|
{
|
|
int num4 = (j - 1) * columns + i - 1;
|
|
currentBuffer[num4] = (previousBuffer[num4 - 1] + previousBuffer[num4 + 1] + previousBuffer[num4 - columns] + previousBuffer[num4 + columns]) / 2 - currentBuffer[num4];
|
|
currentBuffer[num4] = (int)((float)currentBuffer[num4] * Damping);
|
|
}
|
|
}
|
|
inputPositionPrev = inputPosition;
|
|
}
|
|
|
|
public void SetObstructions()
|
|
{
|
|
if (!obstructionsScanned)
|
|
{
|
|
ObstructionDetection();
|
|
}
|
|
int num = rowStart * columns + colStart;
|
|
int num2 = rowEnd * columns + colEnd;
|
|
for (int i = 0; i < obstructionCounter; i++)
|
|
{
|
|
int num3 = obstructedPositions[i];
|
|
if (num3 <= num2 && num3 >= num)
|
|
{
|
|
bufferCurrent[num3] = 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void SetObstructionTexture()
|
|
{
|
|
if (!obstructionTextuReScanned)
|
|
{
|
|
ProcessObstructionTexture();
|
|
}
|
|
if (obstructionTextureSettings.ReScan)
|
|
{
|
|
obstructionTextureCounter = 0;
|
|
ProcessObstructionTexture();
|
|
}
|
|
obstructionTextureSettings.ReScan = false;
|
|
int num = rowStart * columns + colStart;
|
|
int num2 = rowEnd * columns + colEnd;
|
|
if (obstructionTextureSettings.useAppearanceModifiers)
|
|
{
|
|
for (int i = 0; i < obstructionTextureCounter; i++)
|
|
{
|
|
int num3;
|
|
int num4;
|
|
if (obstructionTextureSettings.Appearance.FlipImage)
|
|
{
|
|
num3 = rows - (int)((float)obstructionTextureRows[i] * obstructionTextureSettings.Appearance.RowScale) + obstructionTextureSettings.Appearance.RowOffset;
|
|
num4 = columns - (int)((float)obstructionTextureColumns[i] * obstructionTextureSettings.Appearance.ColScale) + obstructionTextureSettings.Appearance.ColOffset;
|
|
}
|
|
else
|
|
{
|
|
num3 = (int)((float)obstructionTextureRows[i] * obstructionTextureSettings.Appearance.RowScale) + obstructionTextureSettings.Appearance.RowOffset;
|
|
num4 = (int)((float)obstructionTextureColumns[i] * obstructionTextureSettings.Appearance.ColScale) + obstructionTextureSettings.Appearance.ColOffset;
|
|
}
|
|
if (num4 > 0 && num4 < columns && num3 > 0 && num3 < rows)
|
|
{
|
|
int num5 = num3 * columns + num4;
|
|
if (num5 <= num2 && num5 >= num)
|
|
{
|
|
bufferCurrent[num5] = 0;
|
|
}
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
for (int j = 0; j < obstructionTextureCounter; j++)
|
|
{
|
|
int num6 = obstructedTexturePositions[j];
|
|
if (num6 <= num2 && num6 >= num)
|
|
{
|
|
bufferCurrent[num6] = 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void Reset()
|
|
{
|
|
RealWaterCollider.reseting = true;
|
|
CancelInvoke("MainLoop");
|
|
CancelInvoke("SetObstructions");
|
|
CancelInvoke("SetObstructionTexture");
|
|
CancelInvoke("VariableChangeCheck");
|
|
CancelInvoke("Fade");
|
|
obstructionCounter = 0;
|
|
obstructionTextureCounter = 0;
|
|
obstructionsScanned = false;
|
|
obstructionTextuReScanned = false;
|
|
initializeVariables();
|
|
if (simulationSettings.fullPlaneSimulation)
|
|
{
|
|
initFullPlaneSim();
|
|
}
|
|
if (obstructionSettings.ObstructionsEnabled)
|
|
{
|
|
ObstructionDetection();
|
|
if (!simulationSettings.collisionCulling)
|
|
{
|
|
if (obstructionSettings.ObstructionsEnabled && obstructionSettings.SetRate == ObstructionSettings.setRate.Half)
|
|
{
|
|
InvokeRepeating("SetObstructions", 0.003f, 1f / (simulationSpeed * 0.55f));
|
|
}
|
|
if (obstructionSettings.ObstructionsEnabled && obstructionSettings.SetRate == ObstructionSettings.setRate.Full)
|
|
{
|
|
InvokeRepeating("SetObstructions", 0.003f, 1f / simulationSpeed);
|
|
}
|
|
}
|
|
}
|
|
if (obstructionTextureSettings.ObstructionTexturesEnabled)
|
|
{
|
|
ProcessObstructionTexture();
|
|
if (!simulationSettings.collisionCulling)
|
|
{
|
|
if (obstructionTextureSettings.ObstructionTexturesEnabled && obstructionTextureSettings.SetRate == ObstructionTextureSettings.setRate.Half)
|
|
{
|
|
InvokeRepeating("SetObstructionTexture", 0.003f, 1f / (simulationSpeed * 0.55f));
|
|
}
|
|
if (obstructionTextureSettings.ObstructionTexturesEnabled && obstructionTextureSettings.SetRate == ObstructionTextureSettings.setRate.Full)
|
|
{
|
|
InvokeRepeating("SetObstructionTexture", 0.003f, 1f / simulationSpeed);
|
|
}
|
|
}
|
|
}
|
|
if (inputManager != null)
|
|
{
|
|
inputManager.Reset();
|
|
}
|
|
if (!simulationSettings.collisionCulling)
|
|
{
|
|
InvokeRepeating("MainLoop", 0.001f, 1f / simulationSpeed);
|
|
InvokeRepeating("VariableChangeCheck", 0.001f, 1f / 30f);
|
|
InvokeRepeating("Fade", 0.0001f, 1f / fadeSettings.fadeRate);
|
|
}
|
|
else
|
|
{
|
|
paused = true;
|
|
}
|
|
StartCoroutine("ColliderResetDelay");
|
|
}
|
|
|
|
private IEnumerator ColliderResetDelay()
|
|
{
|
|
yield return new WaitForSeconds(0.1f);
|
|
RealWaterCollider.reseting = false;
|
|
}
|
|
|
|
public void SetSpeed(float s)
|
|
{
|
|
simulationSpeed = s;
|
|
}
|
|
|
|
public float GetSpeed()
|
|
{
|
|
return simulationSpeed;
|
|
}
|
|
|
|
public int GetCols()
|
|
{
|
|
return columns;
|
|
}
|
|
|
|
public int GetRows()
|
|
{
|
|
return rows;
|
|
}
|
|
|
|
public bool GetVisibility()
|
|
{
|
|
return visible;
|
|
}
|
|
|
|
public void SetReady(bool val)
|
|
{
|
|
ready = val;
|
|
}
|
|
|
|
public void SetInputPos(Vector2 pos)
|
|
{
|
|
inputPosition = pos;
|
|
}
|
|
|
|
public ObstructionTextureSettings GetObstructionTextureSettings()
|
|
{
|
|
return obstructionTextureSettings;
|
|
}
|
|
|
|
public ObstructionSettings GetObstructionSettings()
|
|
{
|
|
return obstructionSettings;
|
|
}
|
|
|
|
public bool GetPaused()
|
|
{
|
|
return paused;
|
|
}
|
|
|
|
public bool getIsLarge()
|
|
{
|
|
return true;
|
|
}
|
|
|
|
public float GetExitPauseDelay()
|
|
{
|
|
return simulationSettings.exitPauseDelay;
|
|
}
|
|
|
|
public float GetInterPlanePauseDelay()
|
|
{
|
|
return simulationSettings.interPlanePauseDelay;
|
|
}
|
|
|
|
public void SetExitPauseDelay(float val)
|
|
{
|
|
simulationSettings.exitPauseDelay = val;
|
|
}
|
|
|
|
public void SetInterPlanePauseDelay(float val)
|
|
{
|
|
simulationSettings.interPlanePauseDelay = val;
|
|
}
|
|
|
|
public bool IsFullPlaneSim()
|
|
{
|
|
return simulationSettings.fullPlaneSimulation;
|
|
}
|
|
|
|
public void SetPaused(bool val)
|
|
{
|
|
paused = val;
|
|
}
|
|
|
|
public bool GetCollisionCulling()
|
|
{
|
|
return simulationSettings.collisionCulling;
|
|
}
|
|
|
|
public void SetCollisionCulling(bool val)
|
|
{
|
|
simulationSettings.collisionCulling = val;
|
|
}
|
|
|
|
public bool GetAmbientMode()
|
|
{
|
|
return false;
|
|
}
|
|
|
|
public bool GetInteractiveAmbientMode()
|
|
{
|
|
return false;
|
|
}
|
|
|
|
public void EnableObstructions(bool val)
|
|
{
|
|
obstructionSettings.ObstructionsEnabled = val;
|
|
}
|
|
|
|
public ObstructionSettings.setRate GetObstructionSetRate()
|
|
{
|
|
return obstructionSettings.SetRate;
|
|
}
|
|
|
|
public void SetObstructionSetRate(ObstructionSettings.setRate rate)
|
|
{
|
|
obstructionSettings.SetRate = rate;
|
|
}
|
|
|
|
public void EnableObstructionTexture(bool val)
|
|
{
|
|
obstructionTextureSettings.ObstructionTexturesEnabled = val;
|
|
}
|
|
|
|
public void SetObstructionTextureTo(Texture2D img)
|
|
{
|
|
obstructionTextureSettings.ObstructionTexture = img;
|
|
obstructionTextureSettings.ReScan = true;
|
|
}
|
|
|
|
public ObstructionTextureSettings.setRate GetObstructionTextureSetRate()
|
|
{
|
|
return obstructionTextureSettings.SetRate;
|
|
}
|
|
|
|
public void SetObstructionTextureSetRate(ObstructionTextureSettings.setRate rate)
|
|
{
|
|
obstructionTextureSettings.SetRate = rate;
|
|
}
|
|
|
|
public void SetObstructionTextureAppearance(Appearance settings)
|
|
{
|
|
obstructionTextureSettings.Appearance = settings;
|
|
}
|
|
|
|
public int GetSmoothingLevel()
|
|
{
|
|
return inputSmoothingSettings.SmoothingLevel;
|
|
}
|
|
|
|
public void SetSmoothingLevel(int val)
|
|
{
|
|
inputSmoothingSettings.SmoothingLevel = val;
|
|
}
|
|
|
|
public int GetBR()
|
|
{
|
|
return inputSmoothingSettings.BlurRadius;
|
|
}
|
|
|
|
public void SetBR(int val)
|
|
{
|
|
inputSmoothingSettings.BlurRadius = val;
|
|
}
|
|
|
|
public float GetDamping()
|
|
{
|
|
return Damping;
|
|
}
|
|
|
|
public void SetDamping(float val)
|
|
{
|
|
Damping = val;
|
|
}
|
|
}
|