阴阳师画符制作

(一)设计思路
(1)鼠标按下:存储设计鼠标点击位置(视图坐标)
(2)鼠标抬起:内存中生成图片Texture2D,遍历list,为防止点的离散效果,采用插值方法在两点中间。使用像素点制图。清空List
(3) 使用GL绘制图形,在OnRenderObject函数中在屏幕中制图。
(二)制作步骤

public class Yinyangshi : MonoBehaviour {

    List<Vector3> allPoints;
    static Material lineMaterial;
    Texture2D paintText;

    /// <summary>
    /// 采用视图坐标,将绘制图形绘制到图片上
    /// 关键:相似缩放原则
    /// </summary>
    public void GenerateText()
    {
        //1.生成图片
        paintText = new Texture2D(300, 400);
        //2.作画
        for(int i=1;i<allPoints.Count;i++)
        {
            Vector3 tmpFront = allPoints[i-1];
            Vector3 tmpBack = allPoints[i];
            float front_x =(paintText.width * tmpFront.x);
            float front_y = (paintText.height * tmpFront.y);
            float back_x = (paintText.width * tmpBack.x);
            float back_y = (paintText.height * tmpBack.y);
            //插值运算实现平滑,插值运算法则(a*t+(1-t)*b)
            int tmpcount = 50;
            for (int j = 0; j < tmpcount; j++)
            {
               int tmpx=(int) Mathf.Lerp(front_x, back_x,j/(float)tmpcount);
                int tmpy = (int)Mathf.Lerp(front_y, back_y, j / (float)tmpcount);
                paintText.SetPixel(tmpx, tmpy, Color.blue);
            }

        }
        //3.使用
        paintText.Apply();
        //4.修改材质球的贴图
        transform.GetComponent<Renderer>().material.mainTexture = paintText;
    }
    void Start () {
        allPoints = new List<Vector3>();

       }


       void Update () {
        if(Input.GetMouseButton(0))
        {
            Vector3 tmpView =  Camera.main.ScreenToViewportPoint(Input.mousePosition);
            allPoints.Add(tmpView);
        }
        if (Input.GetMouseButtonUp(0))
        {
            GenerateText();          
            allPoints.Clear();
        }
    }
    void OnPostRender()
    {
        // Set your materials
        GL.PushMatrix();
        // yourMaterial.SetPass( );
        // Draw your stuff
        GL.PopMatrix();
    }  
       /// <summary>
       /// 实例化材质球
       /// </summary>
    static void CreateLineMaterial()
    {
        if (!lineMaterial)
        {
            // Unity has a built-in shader that is useful for drawing
            // simple colored things.
            Shader shader = Shader.Find("Hidden/Internal-Colored");
            lineMaterial = new Material(shader);
            lineMaterial.hideFlags = HideFlags.HideAndDontSave;
            // Turn on alpha blending
            lineMaterial.SetInt("_SrcBlend",  (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
            lineMaterial.SetInt("_DstBlend",  (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
            // Turn backface culling off
            lineMaterial.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Off);
            // Turn off depth writes
            lineMaterial.SetInt("_ZWrite", 0);
        }
    }
    // 画物体是回调函数
    public void OnRenderObject()
    {
        //设置材质球
        CreateLineMaterial();
        // 划线材质球设置
        lineMaterial.SetPass(0);
        //转变为正交投影(坐标系统类似视图坐标系)
        GL.LoadOrtho();
        //枚举GL画的种类:划线
        GL.Begin(GL.LINES);
        GL.Color(Color.red);
        for(int i=1;i<allPoints.Count;i++)
        {
            Vector3 tmpFront = allPoints[i - 1];
            Vector3 tmpBack = allPoints[i];
            //设置起始点
            GL.Vertex3(tmpFront.x,tmpFront.y,tmpFront.z);
            GL.Vertex3(tmpBack.x, tmpBack.y, tmpBack.z);
        }      
        GL.End();

    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值