public static GaussFalloff ( float distance, float inRadius ) : float | ||
distance | float | |
inRadius | float | |
return | float |
public static Texture2D PaintLine(Vector2 from, Vector2 to, float rad, Color col, float hardness, Texture2D tex) { float width = rad * 2; float extent = rad; float stY = Mathf.Clamp(Mathf.Min(from.y, to.y) - extent, 0, tex.height); float stX = Mathf.Clamp(Mathf.Min(from.x, to.x) - extent, 0, tex.width); float endY = Mathf.Clamp(Mathf.Max(from.y, to.y) + extent, 0, tex.height); float endX = Mathf.Clamp(Mathf.Max(from.x, to.x) + extent, 0, tex.width); float lengthX = endX - stX; float lengthY = endY - stY; float sqrRad = rad * rad; float sqrRad2 = (rad + 1) * (rad + 1); Color[] pixels = tex.GetPixels((int)stX, (int)stY, (int)lengthX, (int)lengthY, 0); Vector2 start = new Vector2(stX, stY); //Debug.Log (widthX + " "+ widthY + " "+ widthX*widthY); for (int y = 0; y < lengthY; y++) { for (int x = 0; x < lengthX; x++) { Vector2 p = new Vector2(x, y) + start; Vector2 center = p + new Vector2(0.5f, 0.5f); float dist = (center - Mathfx.NearestPointStrict(from, to, center)).sqrMagnitude; if (dist > sqrRad2) { continue; } dist = Mathfx.GaussFalloff(Mathf.Sqrt(dist), rad) * hardness; //dist = (samples[i]-pos).sqrMagnitude; Color c = Color.white; int index = (int)(y * lengthX + x); //不能超过限度 index = Mathf.Min(pixels.Length - 1, index); if (dist > 0) { c = Color.Lerp(pixels[index], col, dist); } else { c = pixels[index]; } pixels[index] = c; } } tex.SetPixels((int)start.x, (int)start.y, (int)lengthX, (int)lengthY, pixels, 0); return(tex); }
public static Texture2D Paint(Vector2 pos, float rad, Color col, float hardness, Texture2D tex) { var start = new Vector2(Mathf.Clamp(pos.x - rad, 0, tex.width), Mathf.Clamp(pos.y - rad, 0, tex.height)); var width = rad * 2; var end = new Vector2(Mathf.Clamp(pos.x + rad, 0, tex.width), Mathf.Clamp(pos.y + rad, 0, tex.height)); var widthX = Mathf.Round(end.x - start.x); var widthY = Mathf.Round(end.y - start.y); var sqrRad = rad * rad; var sqrRad2 = (rad + 1) * (rad + 1); Color[] pixels = tex.GetPixels((int)start.x, (int)start.y, (int)widthX, (int)widthY, 0); for (var y = 0; y < widthY; y++) { for (var x = 0; x < widthX; x++) { var p = new Vector2(x, y) + start; var center = p + new Vector2(0.5f, 0.5f); float dist = (center - pos).sqrMagnitude; if (dist > sqrRad2) { continue; } var samples = Sample(p); var c = Color.black; for (var i = 0; i < samples.Length; i++) { dist = Mathfx.GaussFalloff(Vector2.Distance(samples[i], pos), rad) * hardness; if (dist > 0) { c += Color.Lerp(pixels[y * (int)widthX + x], col, dist); } else { c += pixels[y * (int)widthX + x]; } } c /= samples.Length; pixels[y * (int)widthX + x] = c; } } tex.SetPixels((int)start.x, (int)start.y, (int)widthX, (int)widthY, pixels, 0); return(tex); }
public static Texture2D PaintLine(Vector2 from, Vector2 to, float rad, Color col, float hardness, Texture2D tex) { float num2 = rad; float y = Mathf.Clamp(Mathf.Min(from.y, to.y) - num2, 0, tex.height); float x = Mathf.Clamp(Mathf.Min(from.x, to.x) - num2, 0, tex.width); float num5 = Mathf.Clamp(Mathf.Max(from.y, to.y) + num2, 0, tex.height); float num7 = Mathf.Clamp(Mathf.Max(from.x, to.x) + num2, 0, tex.width) - x; float num8 = num5 - y; float num10 = (rad + 1) * (rad + 1); Color[] colors = tex.GetPixels((int)x, (int)y, (int)num7, (int)num8, 0); Vector2 vector = new Vector2(x, y); for (int i = 0; i < num8; i++) { for (int j = 0; j < num7; j++) { Vector2 vector2 = new Vector2(j, i) + vector; Vector2 point = vector2 + new Vector2(0.5f, 0.5f); Vector2 vector4 = point - Mathfx.NearestPointStrict(from, to, point); float sqrMagnitude = vector4.sqrMagnitude; if (sqrMagnitude <= num10) { Color color; sqrMagnitude = Mathfx.GaussFalloff(Mathf.Sqrt(sqrMagnitude), rad) * hardness; var index0 = ((int)(i * num7)) + j; //print(index0); //print(colors.Length); if (sqrMagnitude > 0) { color = Color.Lerp(colors[index0], col, sqrMagnitude); } else { color = colors[index0]; } colors[index0] = color; } } } tex.SetPixels((int)vector.x, (int)vector.y, (int)num7, (int)num8, colors, 0); return(tex); }