首页 > 其他分享 >Unity中mesh 绘制正方体

Unity中mesh 绘制正方体

时间:2023-02-22 20:11:44浏览次数:46  
标签:正方体 vh Vector2 Vector3 Unity mesh black AddVert new

绘制正方体:

用mesh自带的函数绘制:


//创建一个mesh网格
Mesh mesh = new Mesh();
//添加网格过滤器
gameObject.AddComponent<MeshFilter>();
//给网格添加顶点
mesh.vertices = new Vector3[]
{
new Vector3(0,0,0),new Vector3(0,1,0),new Vector3(1,0,0),new Vector3(1,1,0),
new Vector3(0,0,1),new Vector3(0,1,1),new Vector3(1,0,1),new Vector3(1,1,1),
};
//为网格添加绘制顺序 3个顶点绘制1个三角形
mesh.triangles = new int[]
{
0,1,2, 3,2,1,//正面
2,3,6, 3,7,6,//右侧
6,7,4, 7,5,4,//后面
4,5,0, 5,1,0,//左侧
1,5,3, 5,7,3,//顶部
4,0,2, 2,6,4 //底部
};
//自动计算法线
mesh.RecalculateNormals();
//网格过滤器赋值
GetComponent<MeshFilter>().mesh = mesh;

 

上面就是简单的创建了Cube

 下面的算是完整的Cube

 

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class cube : MonoBehaviour
{
public Texture texture;//纹理
// Start is called before the first frame update
void Start()
{
//添加网格碰撞器
gameObject.AddComponent<MeshCollider>();
//添加网格渲染器
gameObject.AddComponent<MeshRenderer>();
//添加网格过滤器
gameObject.AddComponent<MeshFilter>();
//创建一个mesh网格
Mesh mesh = new Mesh();

VertexHelper vh = new VertexHelper();

//前面
vh.AddVert(new Vector3(0, 0, 0), Color.black, Vector2.zero);
vh.AddVert(new Vector3(0, 1, 0), Color.black, Vector2.up);
vh.AddVert(new Vector3(1, 0, 0), Color.black, Vector2.right);
vh.AddVert(new Vector3(1, 1, 0), Color.black, Vector2.one);
//右侧面
vh.AddVert(new Vector3(1, 0, 0), Color.black, Vector2.zero);
vh.AddVert(new Vector3(1, 1, 0), Color.black, Vector2.up);
vh.AddVert(new Vector3(1, 0, 1), Color.black, Vector2.right);
vh.AddVert(new Vector3(1, 1, 1), Color.black, Vector2.one);
//后面
vh.AddVert(new Vector3(1, 0, 1), Color.black, Vector2.zero);
vh.AddVert(new Vector3(1, 1, 1), Color.black, Vector2.up);
vh.AddVert(new Vector3(0, 0, 1), Color.black, Vector2.right);
vh.AddVert(new Vector3(0, 1, 1), Color.black, Vector2.one);
//左侧面
vh.AddVert(new Vector3(0, 0, 1), Color.black, Vector2.zero);
vh.AddVert(new Vector3(0, 1, 1), Color.black, Vector2.up);
vh.AddVert(new Vector3(0, 0, 0), Color.black, Vector2.right);
vh.AddVert(new Vector3(0, 1, 0), Color.black, Vector2.one);
//上面
vh.AddVert(new Vector3(0, 1, 0), Color.black, Vector2.zero);
vh.AddVert(new Vector3(0, 1, 1), Color.black, Vector2.up);
vh.AddVert(new Vector3(1, 1, 0), Color.black, Vector2.right);
vh.AddVert(new Vector3(1, 1, 1), Color.black, Vector2.one);

//下面
vh.AddVert(new Vector3(0, 0, 1), Color.black, Vector2.zero);
vh.AddVert(new Vector3(0, 0, 0), Color.black, Vector2.up);
vh.AddVert(new Vector3(1, 0, 1), Color.black, Vector2.right);
vh.AddVert(new Vector3(1, 0, 0), Color.black, Vector2.one);


//从哪里开始渲染
//vh.AddTriangle(0, 1, 3);
//vh.AddTriangle(0, 3, 2);
//添加顶点
for (int i = 0; i < 6; i++)
{
vh.AddTriangle(0 + i * 4, 1 + i * 4, 3 + i * 4);
vh.AddTriangle(0 + i * 4, 3 + i * 4, 2 + i * 4);
}
//填充网格
vh.FillMesh(mesh);
//绘制法线
mesh.RecalculateBounds();
//赋值给网格过滤器
GetComponent<MeshFilter>().mesh = mesh;
//创建一个材质球
Material material = new Material(Shader.Find("Standard"));
//赋纹理
material.mainTexture = texture;

GetComponent<MeshCollider>().sharedMesh = mesh;
//将材质赋给渲染器
GetComponent<MeshRenderer>().material = material;
}

// Update is called once per frame
void Update()
{

}
}

 

标签:正方体,vh,Vector2,Vector3,Unity,mesh,black,AddVert,new
From: https://www.cnblogs.com/z816/p/17145531.html

相关文章

  • 230222 Radiated Immunity Pre-compliance Test
    Helloeveryone,welcometoMach1DesignEMCchannel.Lasttimewetalkedabouthowtosetuparadiatedimmunitytestusingaverylowcostantennalikethis.......
  • Unity下简易字符串指令调试
    Unity下简易字符串指令调试输入相应的字符串命令即可调用特定的方法,比如让角色等级提升,生成特定数量的Boss等usingSystem;usingUnityEngine;usingSystem.Collectio......
  • Unity Shader标准光照模型的实现
    1Shader"Custom/P_Illumination"2{3Properties{4_BasicColor("基础颜色",Color)=(1.0,1.0,1.0,1.0)5_SpecularColor("高光颜色",Colo......
  • Unity手机移动设备重力感应
    一、引入大家对重力感应应该都不陌生,之前玩过的王者荣耀的资源更新界面就是使用了重力感应的概念,根据手机的晃动来给实体进行晃动。下图的王者荣耀刚开始的界面其实就是使用......
  • 虚拟机VMware安装Fedora15/ubuntu 11,要想使用gnome3/unity 要怎么办呢?
    SelecttheAccelerate3DgraphicsAcceleratedgraphicscapabilitiesapplytoWindowsXP,WindowsVista,andWindows7guests,onhostsrunningWindowsorLinux......
  • Unity2D游戏开发Update与FixedUpdate间的区别
    Update方法是每一帧执行一次,FixeUpdate方法是每0.02秒执行一次,即我们常说的50帧的刷新率,我们在进行物理模拟动作的编写中,常常会写在FixeUpdate中,由于是每0.02秒执行一次,不......
  • Unity客户端框架收集
    转载:https://blog.csdn.net/t163361/article/details/106499225LoxodonFrameworkhttps://github.com/cocowolf/loxodon-frameworkMVVM框架;支持XLua,可以完全使用Lua脚本......
  • Unity3D----行为操作
    Unity3D----行为操作1.导入资源          2.将小人模型放置到摄像机下           3.设置移动3.1创建 AnimatorContro......
  • 【Unity AR】复刻<PokemonGo - AR精灵>部分学习记录
    一、实验摘要   《宝可梦GO》(pokemonGo)是一款能对现实世界中出现的宝可梦进行探索捕捉、战斗以及交换的游戏。玩家可以通过智能手机在现实世界里发现宝可梦,进行抓捕和......
  • Unity Coroutine和async、await对比
    此篇文章,不谈原理,只聊用法,对比着看更容易理解  上述代码执行的逻辑顺序是一样的定义异步方法,协程用IEnumerator,线程用 async具体异步执行的内容,协程用yield,线程......