コード例 #1
0
ファイル: MapDrawer.cs プロジェクト: fengqk/Art
	Mesh CreateSubMesh(MapData data, int xOffset, int zOffset, int patchXCount, int patchZCount, float size, Vector3 offset)
	{
		Mesh mesh = new Mesh();

		int vCount = patchXCount * patchZCount * 4;
		Vector3[] pts = new Vector3[vCount];
		Color[] colors = new Color[vCount];
		int[] indices = new int[vCount * 2];


		size *= 0.5f;
		Vector3[] squareBias = new Vector3[4]{ new Vector3(-size, 0, -size)
											,new Vector3(-size, 0, size)
											,new Vector3(size, 0, size)
											 ,new Vector3(size, 0, -size)	};

		//Vector3 offset = GetOffset() + bia;

		int i = 0;
		for (int z = zOffset; z < zOffset + patchZCount; z++)
		{
			for (int x = xOffset; x < xOffset + patchXCount; x++)
			{
				MapData.MapNode node = data.GetNode(x, z);
				Vector3[] neighbours = data.GetNeighbourOffset(x, z);
				Vector3 center = node.Position;
				Color vertexColor = node.IsWayPoint ? wayPointColor : node.IsReachable ? reachableColor : notReachableColor;

				pts[i * 4] = center + squareBias[0] + offset + neighbours[0];
				pts[i * 4 + 1] = center + squareBias[1] + offset + neighbours[1];
				pts[i * 4 + 2] = center + squareBias[2] + offset + neighbours[2];
				pts[i * 4 + 3] = center + squareBias[3] + offset + neighbours[3];

				int start = i * 4;
				for (int j = 0; j < 4; j++)
				{
					colors[start + j] = vertexColor;
				}

				indices[i * 8] = start; indices[i * 8 + 1] = start + 1;
				indices[i * 8 + 2] = start + 1; indices[i * 8 + 3] = start + 2;
				indices[i * 8 + 4] = start + 2; indices[i * 8 + 5] = start + 3;
				indices[i * 8 + 6] = start + 3; indices[i * 8 + 7] = start;
				i++;
			}
		}

		mesh.vertices = pts;
		mesh.colors = colors;
		//mesh.SetIndices(indices, MeshTopology.Lines, 0);
		mesh.SetIndices(indices, MeshTopology.Quads, 0);

		return mesh;
	}