转载请注明出处: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. GitHub For Beginners: Commit, Push And Go

    In Part 1 of this two-part GitHub tutorial, we examined the main uses for GitHub and bega5n the proc ...

  2. 【django之orm小练习】

    作业1 创建单表Book表,要求字段: 1 主键 nid 2 书名 title 3 价格 price 4 出版日期 pubDate 5 出版社 publisher(普通字符串字段) class Boo ...

  3. WPF 自定义图表(柱状图,曲线图)

    1.功能 实现图表的数据绑定,动态绑定,属性更改绑定. 本文提供了一种思路. 2.原理 使用canvas绘制,使用反射来确定属性的绑定. 3.效果: 柱状图效果: 曲线图效果: 感谢阅读. 源码地址: ...

  4. 使用mongoose和bcrypt实现用户密码加密

    前面的话 最近在做的个人项目中,需要对密码进行加密保存,对该操作的详细步骤记录如下 介绍 关于mongoose已经写过博客就不再赘述,下面主要介绍bcrypt bcrypt是一个由两个外国人根据Blo ...

  5. fiddler2请求参数乱码

    win7 1.windows按钮+R 2.输入regedit +回车+是 3.HKEY_CURRENT_USER\Software\Microsoft\Fiddler2 4.右键新建,选字符串值 加上 ...

  6. bzoj 4546: codechef XRQRS [可持久化Trie]

    4546: codechef XRQRS 可持久化Trie codechef上过了,bzoj上蜜汁re,看别人说要开5.2e5才行. #include <iostream> #includ ...

  7. 输入url到渲染出页面的过程

    输入地址 浏览器查找域名的 IP 地址 这一步包括 DNS 具体的查找过程,包括:浏览器缓存->系统缓存->路由器缓存... 浏览器向 web 服务器发送一个 HTTP 请求 服务器的永久 ...

  8. (python基础)时间辍time、时间元组localtime、时间格式化strftime

    可以直接将下方代码运行查看结果:#!/usr/bin/python# coding=utf-8import time # 引入time模块# 时间戳:# 每个时间戳都以自从1970年1月1日午夜(历元 ...

  9. SPSS 批量添加标签

  10. stderr,stdout,a.txt缓冲区别

    #include<stdlib.h>#include<stdio.h>#include<string.h>#include<error.h>#inclu ...