Unity实现瓦片地图tile map

Unity自定义mesh绘制  基于上篇的mesh修改,实现tile map

第一步,修改mesh 顶点和三角片信息,生成方格

顶点信息就是一系列正方形的组合,三角片,在这里实现的方法是一个矩形由2个三角形组成,注意,三角形的顺序依赖于顶点的顺序,三角形的三个点按照观察方向顺时针,三角形数量=顶点数量*6,6=2*3,2是三角形个数,3是一个三角形 由3个顶点组成,输入的三角数组的顺序是,对应顶点在输入的顶点数据中的index,贴代码:

1using UnityEngine; 2using System.Collections; 3 4 5public class ss5 : MonoBehaviour 6{ 7 void Start() 8 { 9 dooo(); 10 11 } 12 13 void dooo() 14 { 15 { 16 17 MeshFilter filter = this.GetComponent<MeshFilter>(); 18 MeshRenderer render = this.GetComponent<MeshRenderer>(); 19 var mesh = filter.mesh; 20 21 var ts = this.GetComponentsInChildren<Transform>(); 22 23 Vector3[] vers = new Vector3[ts.Length]; 24 25 for (int i = 0; i < ts.Length; i++) 26 { 27 vers[i] = ts[i].position; 28 } 29 30 const int MAX_X = 30; 31 const int MAX_Y = 30; 32 Vector3[] vertics = new Vector3[MAX_Y * MAX_X]; 33 34 for (int x = 0; x < MAX_X; x++) 35 { 36 for (int y = 0; y < MAX_Y; y++) 37 { 38 vertics[x + MAX_X * y] = new Vector3(x, 0f, y); 39 } 40 } 41 foreach (var p in vertics) 42 { 43 Ray ray = new Ray(p, new Vector3(-100f, -100f, -100f)); 44 Debug.DrawLine(ray.GetPoint(-0.1f), ray.GetPoint(0.1f), Color.red); 45 } 46 int[] triss = new int[(MAX_Y - 1) * (MAX_X - 1) * 6]; 47 48 for (int x = 0; x < MAX_X - 1; x++) 49 { 50 for (int y = 0; y < MAX_Y - 1; y++) 51 { 52 var p = vertics[x + MAX_X * y]; 53 int baseIdx = 0; 54 if (y >= 1) 55 { 56 baseIdx = (y * (MAX_X - 1) * 6) + x * 6; 57 } 58 else 59 { 60 baseIdx = x * 6; 61 } 62 Debug.LogError(x + " " + y + " " + triss.Length + " baseIdx=" + baseIdx); 63 triss[baseIdx + 0] = x + y * MAX_X; 64 triss[baseIdx + 1] = x + (y + 1) * MAX_X; 65 triss[baseIdx + 2] = (x + 1) + y * MAX_X; 66 triss[baseIdx + 3] = x + (y + 1) * MAX_X; 67 triss[baseIdx + 4] = (x + 1) + (y + 1) * MAX_X; 68 triss[baseIdx + 5] = (x + 1) + y * MAX_X; ; 69 70 /* triss[x + y * MAX_X + 0] = 0; 71 triss[x + y * MAX_X + 1] = 2; 72 triss[x + y * MAX_X + 2] = 1; 73 triss[x + y * MAX_X + 3] = 2; 74 triss[x + y * MAX_X + 4] = 3; 75 triss[x + y * MAX_X + 5] = 1; 76 */ 77 78 /* triss[(x * 2 + y * 9 * 2) + 0] = 5; 79 triss[(x * 2 + y * 9 * 2) + 1] = 5; 80 triss[(x * 2 + y * 9 * 2) + 2] = 5; 81 triss[(x * 2 + y * 9 * 2) + 3] = 5; 82 triss[(x * 2 + y * 9 * 2) + 4] = 5; 83 triss[(x * 2 + y * 9 * 2) + 5] = 5; 84 */ 85 } 86 } 87 88 89 mesh.vertices = vertics; 90 mesh.triangles = triss;// this.GetTris(vers); 91 mesh.RecalculateNormals(); 92 mesh.RecalculateBounds(); 93 this.GetComponent<MeshCollider>().sharedMesh = mesh; 94 filter.mesh = mesh; 95 return; 96 } 97 } 98 void Update() 99 { 100 101 } 102 103 104 105 void OnDrawGizmos() 106 { 107 return; 108 109 { 110 111 MeshFilter filter = this.GetComponent<MeshFilter>(); 112 MeshRenderer render = this.GetComponent<MeshRenderer>(); 113 var mesh = filter.mesh; 114 115 var ts = this.GetComponentsInChildren<Transform>(); 116 117 Vector3[] vers = new Vector3[ts.Length]; 118 119 for (int i = 0; i < ts.Length; i++) 120 { 121 vers[i] = ts[i].position; 122 } 123 124 const int MAX_X = 3; 125 const int MAX_Y = 3; 126 Vector3[] vertics = new Vector3[MAX_Y * MAX_X]; 127 128 for (int x = 0; x < MAX_X; x++) 129 { 130 for (int y = 0; y < MAX_Y; y++) 131 { 132 vertics[x + MAX_X * y] = new Vector3(x, 0f, y); 133 } 134 } 135 foreach (var p in vertics) 136 { 137 Ray ray = new Ray(p, new Vector3(-100f, -100f, -100f)); 138 Debug.DrawLine(ray.GetPoint(-0.1f), ray.GetPoint(0.1f), Color.red); 139 } 140 int[] triss = new int[(MAX_Y - 1) * (MAX_X - 1) * 6]; 141 142 for (int x = 0; x < MAX_X - 1; x++) 143 { 144 for (int y = 0; y < MAX_Y - 1; y++) 145 { 146 var p = vertics[x + MAX_X * y]; 147 int baseIdx = 0; 148 if (y >= 1) 149 { 150 baseIdx = (y * (MAX_X - 1) * 6) + x * 6; 151 } 152 else 153 { 154 baseIdx = x * 6; 155 } 156 Debug.LogError(x + " " + y + " " + triss.Length + " baseIdx=" + baseIdx); 157 triss[baseIdx + 0] = x + y * MAX_X; 158 triss[baseIdx + 1] = x + (y + 1) * MAX_X; 159 triss[baseIdx + 2] = (x + 1) + y * MAX_X; 160 triss[baseIdx + 3] = x + (y + 1) * MAX_X; 161 triss[baseIdx + 4] = (x + 1) + (y + 1) * MAX_X; 162 triss[baseIdx + 5] = (x + 1) + y * MAX_X; ; 163 164 /* triss[x + y * MAX_X + 0] = 0; 165 triss[x + y * MAX_X + 1] = 2; 166 triss[x + y * MAX_X + 2] = 1; 167 triss[x + y * MAX_X + 3] = 2; 168 triss[x + y * MAX_X + 4] = 3; 169 triss[x + y * MAX_X + 5] = 1; 170 */ 171 /* triss[(x * 2 + y * 9 * 2) + 0] = 5; 172 triss[(x * 2 + y * 9 * 2) + 1] = 5; 173 triss[(x * 2 + y * 9 * 2) + 2] = 5; 174 triss[(x * 2 + y * 9 * 2) + 3] = 5; 175 triss[(x * 2 + y * 9 * 2) + 4] = 5; 176 triss[(x * 2 + y * 9 * 2) + 5] = 5; 177 */ 178 } 179 } 180 181 182 mesh.vertices = vertics; 183 mesh.triangles = triss;// this.GetTris(vers); 184 mesh.RecalculateNormals(); 185 mesh.RecalculateBounds(); 186 this.GetComponent<MeshCollider>().sharedMesh = mesh; 187 filter.mesh = mesh; 188 return; 189 } 190 } 191 192 193}

第二步:修改UV信息,U是x轴,V是y轴,矩形4个顶点 对应UV4个坐标点,在这里是逆时针,UV也是逆时针。因此只需要一张大贴图 附上对应的UV信息 就可以实现tile map了

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

java——20171121

!(http://a.51jsoft.com/uploads/default/original/1X/c542896b094a42a5653fb75adf6cdacd6e35d12e.png)!(https://static.oschina.net/uploads/space/2017/1121/210719_G80Z_3715033.png)