Unity 模型涂鸦

首先,要获取鼠标点击处对应的UV坐标,详见API:

https://docs.unity3d.com/ScriptReference/RaycastHit-textureCoord.html

API中其实已经重画了图片,但只是一个像素,下面给线设置宽度:

    /// 
    /// 画线
    /// 
    /// 点
    /// 贴图
    /// 颜色
    /// 宽度
    public void DrawLine(Vector2 point, Texture2D texture, Color color, float width)
    {
        Rect rext = new Rect(0, 0, texture.width, texture.height);

        point -= new Vector2(width / 2, width / 2);

        int x = Mathf.FloorToInt(point.x);
        int y = Mathf.FloorToInt(point.y);

        for (int i = 0; i < width; i++)
        {
            for (int j = 0; j < width; j++)
            {
                Vector2 position = new Vector2(x + i, y + j);
                if (rext.Contains(position))
                {
                    texture.SetPixel(x + i, y + j, color);
                }
            }
        }

        texture.Apply();
    }

脚本下载链接:https://download.csdn.net/download/n_moling/10625147

无积分可联系,私发。

你可能感兴趣的:(Unity3D)