Unity+Code Learning

【Unity x Mesh】网格生成笔记①-基础

2019-08-03  本文已影响0人  zitaoye

参考Brackeys视频 https://www.youtube.com/watch?v=eJEpeUH1EMg

步骤

一个空GameObject 叫做Mesh Generator
创建Mesh Filter:存储mesh自身,存储所有关于shape的信息
以及Mesh Renderer:向filter拿去data并且渲染让我们可见

概念

注意顶点vertex,position
存储成一个array,vertex array,
用triangle填充成为triangle array;

顶点数组与三角形数组.jpg

创建script

using UnityEnginel

[RequireComponent(typeof(MeshFilter))]
public class MeshGenerator: MonoBehaviour{

Mesh mesh;

Vector3[] vertices;
int[] triangles;

void Start(){
  mesh = new Mesh();
  GetComponent<MeshFilter>().mesh = mesh;

  CreateShape();
}


void CreateShape()
{
  vertices = new Vector3[]
  {
    new Vector3(0,0,0),
    new Vector3(0,0,1),
    new Vector3(1,0,0),
    new Vector3(1,0,1),
  }
  traiangles = new int[]
  {
    0,1,2
    1,3,2
  }
}

void UpdateMesh()
{
  mesh.Clear();
  mesh.vertices = vertices;
  mesh.triangles = triangles;

  mesh.ReCalculateNormal();
}

}

上一篇下一篇

猜你喜欢

热点阅读