Unity3D 实现阴阳师 画符

 

 

 

 

OnRenderObject: 在完成所有常规场景渲染后调用此函数。此时,可使用 GL 类或 Graphics.DrawMeshNow 绘制自定义几何图形。

 

C# 代码部分:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class huafu : MonoBehaviour
{
    List<Vector3> PointList;
    public Image image;
    Texture2D texture;
    // Start is called before the first frame update
    void Start()
    {
        PointList = new List<Vector3>();
        texture = new Texture2D(300, 400);// image.sprite.texture;
        image.GetComponent<Image>().material.mainTexture = texture;
    }
    static Material lineMaterial;
    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");//Custom/OutLine");
            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();
        // Apply the line material
        lineMaterial.SetPass(0);


        //正交投影
        GL.LoadOrtho();
        // Draw lines
        if (PointList.Count > 2)
        {
            GL.Begin(GL.LINES);
            for (int i = 0; i < PointList.Count-1; ++i)
            {
                GL.Color(Color.red);
             

                GL.Vertex(PointList[i]);
              
                GL.Vertex(PointList[i+1]);
            }
            GL.End();
        }
      
        // GLTrianGles2(myPoint2);
       
    }

    void GenerateTexture()
    {
       // texture = new Texture2D(300, 400);
        for (int i = 0; i < PointList.Count-1; i++)
        {
            int XX = (int)(PointList[i].x * texture.width);
            int YY = (int)(PointList[i].y * texture.height);

            int XX1 = (int)(PointList[i+1].x * texture.width);
            int YY1 = (int)(PointList[i+1].y * texture.height);
            // 两点之间 做lerp插值
            for (int j = 0; j < 80; j++)
            {
                int tmpXX =(int) Mathf.Lerp(XX, XX1, j / 80.0f);
                int tmpYY = (int)Mathf.Lerp(YY, YY1, j / 80.0f);
                texture.SetPixel(tmpXX, tmpYY, Color.red);
            }
           
            

        }
        texture.Apply();
        image.GetComponent<Image>().material.mainTexture = texture;
        //  GetComponent<Renderer>().material.mainTexture = texture;
    }
    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButton(0))
        {
            Debug.Log(Input.mousePosition+" "+ Screen.width);

            Vector2 viewPos = Camera.main.ScreenToViewportPoint(Input.mousePosition);
            if (PointList.Count == 0)
            {
                PointList.Add(viewPos);
            }
            else if (Input.mousePosition != PointList[PointList.Count - 1])
                PointList.Add(viewPos);

           
        }
        if (Input.GetMouseButtonUp(0))
        {
            GenerateTexture();
            PointList.Clear();
           
        }

       
    }
}
 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值