• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Unity 网格画图形

武飞扬头像
LAAY
帮助1

鹅肉网格画图形就是画三角形

画一个正方形

建一个空物体加一些组件Mesh Filter,Mesh Renderer.

学新通

 学新通

 找到对应顶点(AddVert添加顶点),就可以绘制了(顶点是可以变得),我就绘制了正面。(顺指针绘制可以看到正面,逆时针看到后面,想要正反都看到,就正反都绘制)

  1.  
    using System.Collections;
  2.  
    using System.Collections.Generic;
  3.  
    using UnityEngine;
  4.  
    using UnityEngine.UI;
  5.  
     
  6.  
    public class Creatsquare : MonoBehaviour
  7.  
    {
  8.  
    // Start is called before the first frame update
  9.  
    void Start()
  10.  
    {
  11.  
    Mesh mesh = new Mesh();
  12.  
    VertexHelper vh = new VertexHelper();
  13.  
     
  14.  
    vh.AddVert(new Vector3(0, 0, 0), Color.white, new Vector2(0, 0));
  15.  
    vh.AddVert(new Vector3(0, 1, 0), Color.white, new Vector2(0, 1));
  16.  
    vh.AddVert(new Vector3(1, 0, 0), Color.white, new Vector2(1, 0));
  17.  
    vh.AddVert(new Vector3(1, 1, 0), Color.white, new Vector2(1, 1));
  18.  
     
  19.  
    vh.AddTriangle(0, 1, 3);
  20.  
    vh.AddTriangle(0, 3, 2);
  21.  
     
  22.  
    vh.FillMesh(mesh);
  23.  
    GetComponent<MeshFilter>().mesh = mesh;
  24.  
     
  25.  
    }
  26.  
     
  27.  
    // Update is called once per frame
  28.  
    void Update()
  29.  
    {
  30.  
     
  31.  
    }
  32.  
    }
学新通

运行后

学新通

然后画正方体

学新通

想要看到下面就要逆着画

  1.  
    using System.Collections;
  2.  
    using System.Collections.Generic;
  3.  
    using UnityEngine;
  4.  
    using UnityEngine.UI;
  5.  
     
  6.  
    public class Creatcube : MonoBehaviour
  7.  
    {
  8.  
    // Start is called before the first frame update
  9.  
    void Start()
  10.  
    {
  11.  
    Mesh mesh = new Mesh();
  12.  
    VertexHelper vh = new VertexHelper();
  13.  
    //正面
  14.  
    vh.AddVert(new Vector3(0, 0, 0), Color.white, new Vector2(0, 0));
  15.  
    vh.AddVert(new Vector3(0, 1, 0), Color.white, new Vector2(1, 0));
  16.  
    vh.AddVert(new Vector3(1, 1, 0), Color.white, new Vector2(1, 1));
  17.  
    vh.AddVert(new Vector3(1, 0, 0), Color.white, new Vector2(0, 1));
  18.  
    //右面
  19.  
    vh.AddVert(new Vector3(1, 0, 0), Color.white, new Vector2(0, 0));
  20.  
    vh.AddVert(new Vector3(1, 1, 0), Color.white, new Vector2(1, 0));
  21.  
    vh.AddVert(new Vector3(1, 1, 1), Color.white, new Vector2(1, 1));
  22.  
    vh.AddVert(new Vector3(1, 0, 1), Color.white, new Vector2(0, 1));
  23.  
    //后面
  24.  
    vh.AddVert(new Vector3(1, 0, 1), Color.white, new Vector2(0, 0));
  25.  
    vh.AddVert(new Vector3(1, 1, 1), Color.white, new Vector2(1, 0));
  26.  
    vh.AddVert(new Vector3(0, 1, 1), Color.white, new Vector2(1, 1));
  27.  
    vh.AddVert(new Vector3(0, 0, 1), Color.white, new Vector2(0, 1));
  28.  
    //左面
  29.  
    vh.AddVert(new Vector3(0, 0, 1), Color.white, new Vector2(0, 0));
  30.  
    vh.AddVert(new Vector3(0, 1, 1), Color.white, new Vector2(1, 0));
  31.  
    vh.AddVert(new Vector3(0, 1, 0), Color.white, new Vector2(1, 1));
  32.  
    vh.AddVert(new Vector3(0, 0, 0), Color.white, new Vector2(0, 1));
  33.  
    //上面
  34.  
    vh.AddVert(new Vector3(0, 1, 0), Color.white, new Vector2(0, 0));
  35.  
    vh.AddVert(new Vector3(0, 1, 1), Color.white, new Vector2(1, 0));
  36.  
    vh.AddVert(new Vector3(1, 1, 1), Color.white, new Vector2(1, 1));
  37.  
    vh.AddVert(new Vector3(1, 1, 0), Color.white, new Vector2(0, 1));
  38.  
    //下面
  39.  
    vh.AddVert(new Vector3(0, 0, 0), Color.white, new Vector2(0, 0));
  40.  
    vh.AddVert(new Vector3(0, 0, 1), Color.white, new Vector2(1, 0));
  41.  
    vh.AddVert(new Vector3(1, 0, 1), Color.white, new Vector2(1, 1));
  42.  
    vh.AddVert(new Vector3(1, 0, 0), Color.white, new Vector2(0, 1));
  43.  
    //正面
  44.  
    vh.AddTriangle(0, 1, 2);
  45.  
    vh.AddTriangle(0, 2, 3);
  46.  
    //右面
  47.  
    vh.AddTriangle(4, 5, 6);
  48.  
    vh.AddTriangle(4, 6, 7);
  49.  
    //后面
  50.  
    vh.AddTriangle(8,9,10);
  51.  
    vh.AddTriangle(8,10,11);
  52.  
    //左面
  53.  
    vh.AddTriangle(12,13,14);
  54.  
    vh.AddTriangle(12,14,15);
  55.  
    //上面
  56.  
    vh.AddTriangle(16, 17, 18);
  57.  
    vh.AddTriangle(16, 18, 19);
  58.  
    //下面
  59.  
    vh.AddTriangle(20, 23, 22);
  60.  
    vh.AddTriangle(20, 22, 21);
  61.  
     
  62.  
    vh.FillMesh(mesh);
  63.  
    GetComponent<MeshFilter>().mesh = mesh;
  64.  
    }
  65.  
     
  66.  
    // Update is called once per frame
  67.  
    void Update()
  68.  
    {
  69.  
     
  70.  
    }
  71.  
    }
学新通

最后画个圆

学新通

找到对应三角形顶点

学新通

找到渲染图片位置 

  1.  
    using System.Collections;
  2.  
    using System.Collections.Generic;
  3.  
    using UnityEngine;
  4.  
    using UnityEngine.UI;
  5.  
     
  6.  
    public class Creatcircle : MonoBehaviour
  7.  
    {
  8.  
    int num = 100;//三角形个数
  9.  
    int r = 1;//半径
  10.  
    // Start is called before the first frame update
  11.  
    void Start()
  12.  
    {
  13.  
    Mesh mesh = new Mesh();
  14.  
    VertexHelper vh = new VertexHelper();
  15.  
     
  16.  
    //圆
  17.  
    float ang = (2 * Mathf.PI) / num;//弧度
  18.  
    vh.AddVert(new Vector3(0, 0, 0), Color.red, new Vector2(0.5f, 0.5f));//原点
  19.  
    for (int i = 0; i < num; i )
  20.  
    {
  21.  
    float x = Mathf.Sin(ang * i) * r;
  22.  
    float y = Mathf.Cos(ang * i) * r;
  23.  
    float uvx = (x r) / (2 * r);
  24.  
    float uvy = (y r) / (2 * r);
  25.  
    vh.AddVert(new Vector3(x, y, 0), Color.red, new Vector2(uvx, uvy));
  26.  
    if (i == 0)
  27.  
    {
  28.  
    vh.AddTriangle(0, num, 1);
  29.  
    }
  30.  
    else
  31.  
    {
  32.  
    vh.AddTriangle(0, i, i 1);
  33.  
    }
  34.  
    }
  35.  
     
  36.  
    vh.FillMesh(mesh);
  37.  
    GetComponent<MeshFilter>().mesh = mesh;
  38.  
    }
  39.  
     
  40.  
    // Update is called once per frame
  41.  
    void Update()
  42.  
    {
  43.  
     
  44.  
    }
  45.  
    }
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhficeci
系列文章
更多 icon
同类精品
更多 icon
继续加载