Skip to content

Instantly share code, notes, and snippets.

@glebov21
Created March 15, 2018 09:25
Show Gist options
  • Save glebov21/a7c11f2efebef561dbe377a1f89ed4f5 to your computer and use it in GitHub Desktop.
Save glebov21/a7c11f2efebef561dbe377a1f89ed4f5 to your computer and use it in GitHub Desktop.
Draw line on texture Unity3d
void DrawLine(Texture2D tex, int x1, int y1, int x2, int y2, Color col)
{
int dy = (int)(y1-y0);
int dx = (int)(x1-x0);
int stepx, stepy;
if (dy < 0) {dy = -dy; stepy = -1;}
else {stepy = 1;}
if (dx < 0) {dx = -dx; stepx = -1;}
else {stepx = 1;}
dy <<= 1;
dx <<= 1;
float fraction = 0;
tex.SetPixel(x0, y0, col);
if (dx > dy) {
fraction = dy - (dx >> 1);
while (Mathf.Abs(x0 - x1) > 1) {
if (fraction >= 0) {
y0 += stepy;
fraction -= dx;
}
x0 += stepx;
fraction += dy;
tex.SetPixel(x0, y0, col);
}
}
else {
fraction = dx - (dy >> 1);
while (Mathf.Abs(y0 - y1) > 1) {
if (fraction >= 0) {
x0 += stepx;
fraction -= dy;
}
y0 += stepy;
fraction += dx;
tex.SetPixel(x0, y0, col);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment