Ejemplo n.º 1
0
 public CellVertInfo(HexXY coords)
 {
     this.Coords  = coords;
     WallVertices = new WallVerticesInfo[6];
     for (int i = 0; i < 6; i++)
     {
         WallVertices[i] = new WallVerticesInfo();
     }
 }
Ejemplo n.º 2
0
    public void GenerateWalls(MeshData wallMesh)
    {
        int nextIdx = 0;

        int cw = patchSize + 1;
        int ch = 2 * patchSize / 3 + 1;

        for (int i = 0; i < ch; i++)
        {
            for (int j = 0; j < cw; j++)
            {
                CellVertInfo cvi = cellVertInfos[i * cw + j];
                if (cvi.WallCount > 1)
                {
                    for (int k = 0; k < 6; k++)
                    {
                        WallVerticesInfo wis = cvi.WallVertices[k];

                        if (wis.High.Count < 2)
                        {
                            continue;
                        }

                        for (int l = 0; l < wis.High.Count; l++)
                        {
                            //Verts
                            Vector3 highVert = wis.High[l];
                            Vector3 lowVert  = wis.Low[l];

                            wallMesh.vertices.Add(highVert);
                            wallMesh.vertices.Add(lowVert);

                            //UVs
                            Vector2Int gridCoords = wis.GridCoords[l];

                            gridCoords.x += patchOffsetX * patchSize * subDivs;
                            gridCoords.y += patchOffsetY * patchSize * subDivs;

                            float u = WallUForGridCoords(gridCoords);
                            wallMesh.uvs.Add(new Vector2(u, 1));
                            wallMesh.uvs.Add(new Vector2(u, 0));

                            //Normals
                            //TODO: just neigbours for now, can smooth later if needed
                            Vector3 highNeigh;
                            if (l == 0)
                            {
                                highNeigh = wis.High[l + 1];
                            }
                            else
                            {
                                highNeigh = wis.High[l - 1];
                            }

                            Vector3 normal = Vector3.Cross(highNeigh - highVert, lowVert - highVert);
                            if (l > 0)
                            {
                                normal = -normal;
                            }
                            if (!(k >= 1 && k <= 3))
                            {
                                normal = -normal;
                            }

                            normal = normal.normalized;
                            wallMesh.normals.Add(normal);
                            wallMesh.normals.Add(normal);
                        }

                        for (int l = 0; l < wis.High.Count - 1; l++)
                        {
                            //Cut 0,0 outermost walls (duplicate walls in different patches cause z-fighting
                            Vector2Int gridCoords0 = wis.GridCoords[l];
                            Vector2Int gridCoords1 = wis.GridCoords[l + 1];
                            if ((gridCoords0.x == 0 && gridCoords1.x == 0) || (gridCoords0.y == 0 && gridCoords1.y == 0))
                            {
                                nextIdx += 2;
                                continue;
                            }

                            //Different winding order due to wall vertices
                            //filling order in main mesh generation above
                            if (k >= 1 && k <= 3)
                            {
                                wallMesh.triangles.Add(nextIdx);
                                wallMesh.triangles.Add(nextIdx + 3);
                                wallMesh.triangles.Add(nextIdx + 1);

                                wallMesh.triangles.Add(nextIdx);
                                wallMesh.triangles.Add(nextIdx + 2);
                                wallMesh.triangles.Add(nextIdx + 3);
                            }
                            else
                            {
                                wallMesh.triangles.Add(nextIdx);
                                wallMesh.triangles.Add(nextIdx + 1);
                                wallMesh.triangles.Add(nextIdx + 3);

                                wallMesh.triangles.Add(nextIdx);
                                wallMesh.triangles.Add(nextIdx + 3);
                                wallMesh.triangles.Add(nextIdx + 2);
                            }

                            nextIdx += 2;
                        }

                        nextIdx = wallMesh.vertices.Count;
                    }
                }
            }
        }
    }