转载请注明出处:http://www.cnblogs.com/shamoyuu/p/unity_minecraft_02.html

这一篇的内容比较简单,因为所有理论内容都在上一篇中讲到了。但有两点需要特别注意一下

第一点:我们在确定面的4个点的时候,一定要面朝它,跟它面对面。

第二点:你的四边面划分2个三边面,无论是从左上到右下划分,还是从左下到右上划分都可以。我是全部参照Unity3D中cube的各个面的划分方式来的。

贴代码↓

using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class Chunk : MonoBehaviour
{
private Mesh mesh; //面需要的点
private List<Vector3> vertices = new List<Vector3>();
//生成三边面时用到的vertices的index
private List<int> triangles = new List<int>(); void Start()
{
mesh = new Mesh(); //把所有面的点和面的索引添加进去
AddFrontFace();
AddBackFace();
AddRightFace();
AddLeftFace();
AddTopFace();
AddBottomFace(); //为点和index赋值
mesh.vertices = vertices.ToArray();
mesh.triangles = triangles.ToArray(); //重新计算顶点和法线
mesh.RecalculateBounds();
mesh.RecalculateNormals(); //将生成好的面赋值给组件
GetComponent<MeshFilter>().mesh = mesh;
} //前面
void AddFrontFace()
{
//第一个三角面
triangles.Add( + vertices.Count);
triangles.Add( + vertices.Count);
triangles.Add( + vertices.Count); //第二个三角面
triangles.Add( + vertices.Count);
triangles.Add( + vertices.Count);
triangles.Add( + vertices.Count); //添加4个点
vertices.Add(new Vector3(, , ));
vertices.Add(new Vector3(, , ));
vertices.Add(new Vector3(, , ));
vertices.Add(new Vector3(, , ));
} //背面
void AddBackFace()
{
//第一个三角面
triangles.Add( + vertices.Count);
triangles.Add( + vertices.Count);
triangles.Add( + vertices.Count); //第二个三角面
triangles.Add( + vertices.Count);
triangles.Add( + vertices.Count);
triangles.Add( + vertices.Count); //添加4个点
vertices.Add(new Vector3(-, , ));
vertices.Add(new Vector3(-, , ));
vertices.Add(new Vector3(-, , ));
vertices.Add(new Vector3(-, , ));
} //右面
void AddRightFace()
{
//第一个三角面
triangles.Add( + vertices.Count);
triangles.Add( + vertices.Count);
triangles.Add( + vertices.Count); //第二个三角面
triangles.Add( + vertices.Count);
triangles.Add( + vertices.Count);
triangles.Add( + vertices.Count); //添加4个点
vertices.Add(new Vector3(, , ));
vertices.Add(new Vector3(-, , ));
vertices.Add(new Vector3(-, , ));
vertices.Add(new Vector3(, , ));
} //左面
void AddLeftFace()
{
//第一个三角面
triangles.Add( + vertices.Count);
triangles.Add( + vertices.Count);
triangles.Add( + vertices.Count); //第二个三角面
triangles.Add( + vertices.Count);
triangles.Add( + vertices.Count);
triangles.Add( + vertices.Count); //添加4个点
vertices.Add(new Vector3(-, , ));
vertices.Add(new Vector3(, , ));
vertices.Add(new Vector3(, , ));
vertices.Add(new Vector3(-, , ));
} //上面
void AddTopFace()
{
//第一个三角面
triangles.Add( + vertices.Count);
triangles.Add( + vertices.Count);
triangles.Add( + vertices.Count); //第二个三角面
triangles.Add( + vertices.Count);
triangles.Add( + vertices.Count);
triangles.Add( + vertices.Count); //添加4个点
vertices.Add(new Vector3(, , ));
vertices.Add(new Vector3(, , ));
vertices.Add(new Vector3(-, , ));
vertices.Add(new Vector3(-, , ));
} //下面
void AddBottomFace()
{
//第一个三角面
triangles.Add( + vertices.Count);
triangles.Add( + vertices.Count);
triangles.Add( + vertices.Count); //第二个三角面
triangles.Add( + vertices.Count);
triangles.Add( + vertices.Count);
triangles.Add( + vertices.Count); //添加4个点
vertices.Add(new Vector3(-, , ));
vertices.Add(new Vector3(-, , ));
vertices.Add(new Vector3(, , ));
vertices.Add(new Vector3(, , ));
}
}

细心的童鞋应该发现了,我们改变了添加点和添加面的索引的顺序,是为了添加他们的Count,以便于下一个面的点都能一一对应。

比如我们第二个面的第一个点的索引,应该是1+前面所有面的点的Count。

这里面Top和Bottom这两个面要特别注意一下。

代码就是上面这么简单,但是你自己一定要在Unity3D里建一个Cube,并且把它每个点的坐标写一下试试看。

【Unity3D】Unity3D开发《我的世界》之二、创建一个立方体的更多相关文章

  1. Unity 2D游戏开发高速入门第1章创建一个简单的2D游戏

    Unity 2D游戏开发高速入门第1章创建一个简单的2D游戏 即使是如今,非常多初学游戏开发的同学.在谈到Unity的时候.依旧会觉得Unity仅仅能用于制作3D游戏的. 实际上.Unity在2013 ...

  2. .Net开发笔记(十九) 创建一个可以可视化设计的对象

    阅读本篇博客之前需要了解VS窗体设计器的工作原理,详细可参见本系列博客(十).(十一).(十二).必须需要知道的一条结论就是:处于窗体设计器(Form Designer)中的任何组件(包含控件,下同) ...

  3. 从开发到部署,使用django创建一个简单可用的个人博客

    本文参考于: 简书-Django搭建简易博客教程:http://www.jianshu.com/p/d15188a74104 自强学堂-Django基础教程:http://www.ziqiangxue ...

  4. Unity3D游戏开发从零单排(三) - 极速创建狂拽酷炫的游戏地形

    提要 在Unity工作流程内,地形是一个必不可少的重要元素.不论是游戏或虚拟现实都会使用到各种类型的地形效果,在这个教学中我们须要了解到地形的制作基本概念与,当中对于Unity的地形操作部分须要大量的 ...

  5. IOS开发之小实例--使用UIImagePickerController创建一个简单的相机应用程序

    前言:本篇博文是本人阅读国外的IOS Programming Tutorial的一篇入门文章的学习过程总结,难度不大,因为是入门.主要是入门UIImagePickerController这个控制器,那 ...

  6. android wear开发:为可穿戴设备创建一个通知 - Creating a Notification for Wearables

    注:本文内容来自:https://developer.android.com/training/wearables/notifications/creating.html 翻译水平有限,如有疏漏,欢迎 ...

  7. 翻译二--创建一个Web测试计划

    这里主要是翻译jmeter官方文档第4章:创建一个基本的测试计划来测试一个网站.你将创建5个用户来发送请求给两个页面,同时,你将告诉用户去执行两次测试.所以,请求的总和是5(users)*2(requ ...

  8. netty(二) 创建一个netty服务端和客户端

    服务端 NettyServer package com.zw.netty.config; import com.zw.netty.channel.ServerInitializer;import io ...

  9. maven(二)创建一个maven的web项目中解决Cannot change version of project facet Dynamic web module to 2.5

    我们用Eclipse创建Maven结构的web项目的时候选择了Artifact Id为maven-artchetype-webapp,由于这个catalog比较老,用的servlet还是2.3的,而一 ...

随机推荐

  1. curl与grep的使用

    [root@nhserver1 ~]# curl -s www.163.com | grep hot_pop_js.js | sed 's/www.[[:digit:]]*.com/111/'    ...

  2. Notice!

    之后的小车内容在这里更新,开源社区,新浪博客不再更新.

  3. [DeeplearningAI笔记]ML strategy_1_2开发测试集评价指标

    机器学习策略 ML strategy 觉得有用的话,欢迎一起讨论相互学习~Follow Me 1.4 满足和优化指标 Stisficing and optimizing metrics 有时候把你要考 ...

  4. Cypher查询语言--Neo4j 之高级篇 (六)

    目录 排序Order by 通过节点属性排序节点 通过多节点属性排序节点 倒序排列节点 空值排序 Skip 跳过前三个 返回中间两个 Limit 返回第一部分 函数Functions 判断 All A ...

  5. 我是如何让minio client上传速度提高几十倍的

    minio java client 使用okhttp作为底层的http实现,在产品包里面局域网上传文件的速度一直只有400~800KB/s,经过一天排查发现是-Djava.compile=none禁用 ...

  6. selenium打开chrome浏览器代码

    import os from selenium import webdriver chromedriver = "C:\Program Files (x86)\Google\Chrome\A ...

  7. JDBC为什么要使用PreparedStatement而不是Statement

    PreparedStatement是什么? PreparedStatement是java.sql包下面的一个接口,用来执行SQL语句查询,通过调用connection.preparedStatemen ...

  8. BZOJ 1483: [HNOI2009]梦幻布丁 [链表启发式合并]

    1483: [HNOI2009]梦幻布丁 题意:一个带颜色序列,一种颜色合并到另一种,询问有多少颜色段 一种颜色开一个链表,每次遍历小的合并到大的里,顺带维护答案 等等,合并方向有规定? 令col[x ...

  9. BZOJ 3998: [TJOI2015]弦论 [后缀自动机 DP]

    3998: [TJOI2015]弦论 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 2152  Solved: 716[Submit][Status] ...

  10. Linux知识体系之路径属性与目录

    最近在看鸟哥的Linux私房菜,我觉得这本书还是很不错的.这里进行相关的总结. 1.Linux目录权限概念   Linux一般讲目录可存取的方式分为三个类别,分别是owner/group/other, ...