Created
April 15, 2020 15:03
-
-
Save BigETI/2e6a8188575156b385a00533b523c167 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Draw line | |
/// </summary> | |
/// <param name="from">From in texture coordinates</param> | |
/// <param name="to">To in texture coordinates</param> | |
/// <param name="brushSize">Brush size</param> | |
/// <param name="color">Color</param> | |
/// <param name="isForeignDrawCommand">Is foreign draw command</param> | |
public void DrawLine(Vector2 from, Vector2 to, float brushSize, Color32 color, bool isForeignDrawCommand) | |
{ | |
ILobby lobby = Lobby; | |
if (lobby != null) | |
{ | |
//if (lobby.IsPlayerDrawing || isForeignDrawCommand) | |
//{ | |
if (UseComputeShader) | |
{ | |
if ((renderTexture != null) && (drawComputeShader != null)) | |
{ | |
drawComputeShader.SetVector("from", from); | |
drawComputeShader.SetVector("to", to); | |
drawComputeShader.SetFloat("brushSize", brushSize); | |
drawComputeShader.SetVector("color", new Vector4(color.r / 255.0f, color.g / 255.0f, color.b / 255.0f, 1.0f)); | |
drawComputeShader.SetTexture(drawLineComputeShaderKernelIndex, "inputOutputRenderTexture", renderTexture); | |
drawComputeShader.Dispatch(drawLineComputeShaderKernelIndex, ((renderTexture.width % 8) == 0) ? (renderTexture.width / 8) : ((renderTexture.width / 8) + 1), ((renderTexture.height % 8) == 0) ? (renderTexture.height / 8) : ((renderTexture.height / 8) + 1), 1); | |
} | |
} | |
else | |
{ | |
if (texture != null) | |
{ | |
Color32[] bitmap = Bitmap; | |
float max_axial_distance = Mathf.Max(Mathf.Abs(to.x - from.x), Mathf.Abs(to.y - from.y)); | |
Vector2Int texture_size = new Vector2Int(texture.width, texture.height); | |
float half_brush_size = brushSize * 0.5f; | |
Vector2Int origin = new Vector2Int( | |
Mathf.Clamp(Mathf.FloorToInt(Mathf.Min(to.x, from.x) - half_brush_size), 0, texture_size.x), | |
Mathf.Clamp(Mathf.FloorToInt(Mathf.Min(to.y, from.y) - half_brush_size), 0, texture_size.y)); | |
Vector2Int size = new Vector2Int( | |
Mathf.Clamp(Mathf.CeilToInt(Mathf.Abs(to.x - from.x) + brushSize) + 1, 0, texture_size.x - origin.x), | |
Mathf.Clamp(Mathf.CeilToInt(Mathf.Abs(to.y - from.y) + brushSize) + 1, 0, texture_size.y - origin.y)); | |
Action<int> method = (i) => | |
{ | |
Vector2Int position = new Vector2Int(origin.x + (i % size.x), origin.y + (i / size.x)); | |
Vector2 va = to - from; | |
Vector2 vb = from - position; | |
float a_squared = (va.x * va.x) + (va.y * va.y); | |
float b_squared = (vb.x * vb.x) + (vb.y * vb.y); | |
float distance_squared; | |
if (a_squared > float.Epsilon) | |
{ | |
Vector2 vc = position - to; | |
float c_squared = (vc.x * vc.x) + (vc.y * vc.y); | |
float a = Mathf.Sqrt(a_squared); | |
float b = Mathf.Sqrt(b_squared); | |
float c = Mathf.Sqrt(c_squared); | |
float s = (a + b + c) * 0.5f; | |
float height_squared = Mathf.Abs((s * (s - a) * (s - b) * (s - c)) / (a_squared * 0.25f)); | |
if (c_squared > (a_squared + height_squared)) | |
{ | |
distance_squared = b_squared; | |
} | |
else if (b_squared > (a_squared + height_squared)) | |
{ | |
distance_squared = c_squared; | |
} | |
else | |
{ | |
distance_squared = height_squared; | |
} | |
} | |
else | |
{ | |
distance_squared = b_squared; | |
} | |
if (distance_squared <= (brushSize * brushSize * 0.25f)) | |
{ | |
bitmap[position.x + (texture_size.x * position.y)] = color; | |
} | |
}; | |
if (UseMultipleCores) | |
{ | |
Parallel.For(0, size.x * size.y, method); | |
} | |
else | |
{ | |
for (int i = 0, length = size.x * size.y; i < length; i++) | |
{ | |
method(i); | |
} | |
} | |
texture.SetPixels32(bitmap); | |
texture.Apply(); | |
} | |
} | |
//} | |
if (lobby.IsPlayerDrawing) | |
{ | |
lobby.SendDrawCommandAsync(EDrawCommandType.Line, from.x, from.y, to.x, to.y, System.Drawing.Color.FromArgb(color.a, color.r, color.b, color.b), brushSize); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment