- Home /
DrawPixel with smooth edges
I am using DrawPixels on a Texture2D in order to create a painting style application. However, it draws squares which will give a very rough edge. Is it possible to have a more rounded look? Here is my code:
using UnityEngine;
using System.Collections;
public class PaintControl : MonoBehaviour
{
public Transform plane;
public Color color;
public int Size;
private RaycastHit hit;
private Color[] paintColors;
// Use this for initialization
void Start ()
{
// init the texture to white
Texture2D texToWhite = plane.renderer.material.mainTexture as Texture2D;
Color[] fillColorArray = texToWhite.GetPixels();
for (int i = 0; i < fillColorArray.Length; ++i)
fillColorArray[i] = Color.white;
texToWhite.SetPixels(fillColorArray);
texToWhite.Apply();
SetColor(this.color);
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButton(0))
{
Ray theRay = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(theRay, out hit, 50.0f))
{
GameObject go = hit.collider.gameObject;
if (go.tag == "DrawCanvas")
{
Debug.Log(Input.mousePosition);
Renderer render= hit.collider.renderer;
MeshCollider meshColl = hit.collider as MeshCollider;
Texture2D tex = render.material.mainTexture as Texture2D;
Vector2 pixelUV = hit.textureCoord;
pixelUV.x *= tex.width;
pixelUV.y *= tex.height;
//tex.SetPixel((int)pixelUV.x, (int)pixelUV.y, Color.black);
tex.SetPixels((int)pixelUV.x, (int)pixelUV.y, this.Size, this.Size, paintColors);
tex.Apply();
}
}
}
}
public void SetColor(Color tempColor)
{
this.color = tempColor;
SetSize(this.Size);
}
public void SetSize(int tempSize)
{
this.Size = tempSize;
paintColors = new Color[tempSize * tempSize];
for (int i = 0; i < paintColors.Length; i++)
paintColors[i] = this.color;
}
}
Comment
Answer by DaveA · May 09, 2012 at 03:49 PM
You could sample the surrounding pixels before you set it. Often top, bottom, left, right, average them into the color you are setting. You'll want to play with the math to achieve the look you want. Google antialiasing.
Your answer
Follow this Question
Related Questions
The non-trivial problem with image processing. 1 Answer
painting program not drawing at cursor point on texture 0 Answers
RaycastHit2D hit to texture coordinate 0 Answers
Why is my terrain so dark? 1 Answer
Coloring textures in Unity3d 1 Answer