void Start() { instance = this; // Init test node InitTestNode(); // Init grid SpawnAround(CreateGrid(startX, startZ, Vector3.zero, testNode)); }
public void UpdateMesh(PiratesOnlineNode node) { // Fast path if we only have one atlas if (node.Atlases.Length == 1) { // Use 0 submeshes mesh.subMeshCount = 0; // Set uv and triangles for the whole mesh mesh.uv = gridTexcoords; mesh.triangles = gridTriangles; // Set renderer material renderer.materials = new Material[1] { PiratesOnlineHexagonWorld.GetAtlasMaterial(node.Atlases[0].Atlas) }; // TODO: Calculate UV coords } // Slower path for two or more atlases else { // First, create triangle arrays for all sub atlases for (int i = 0; i < node.Atlases.Length; ++i) { node.Atlases[i].AssignedTiles = 0; node.Atlases[i].Triangles = new int[node.Atlases[i].Tiles * PiratesOnlineConstants.IndicesPerHexagon]; } // Secondly, setup triangle indices and uv coords for (int i = 0; i < PiratesOnlineConstants.GridTiles; ++i) { byte subAtlas = node.Tiles[i].SubAtlas; PiratesOnlineNode.SubAtlas atlas = node.Atlases[subAtlas]; // Copy indices into the triangle list of this sub-mesh for (int t = 0; t < PiratesOnlineConstants.IndicesPerHexagon; ++t) { atlas.Triangles[(atlas.AssignedTiles * PiratesOnlineConstants.IndicesPerHexagon) + t] = (i * PiratesOnlineConstants.VerticesPerHexagon) + PiratesOnlineHexagon.Indices[t]; } // TODO: Calculate UV coords // Increase amount of assigned tiles by 1 node.Atlases[subAtlas].AssignedTiles += (byte)1; } // Third, set uv coordinates on mesh and assign submesh count mesh.uv = uv; mesh.subMeshCount = node.Atlases.Length; // Also create materials array Material[] materials = new Material[node.Atlases.Length]; // Fourth, set sub mesh triangles and create shared materials array for (int i = 0; i < node.Atlases.Length; ++i) { // Set sub mesh triangles mesh.SetTriangles(node.Atlases[i].Triangles, i); // Clear triangles array node.Atlases[i].Triangles = null; // Set material in materials array materials[i] = PiratesOnlineHexagonWorld.GetAtlasMaterial(node.Atlases[i].Atlas); } // Last, assign shared materials to renderer renderer.sharedMaterials = materials; } }