void GenerateTop(ref Chunk.MeshData meshData, Vector3 coordinateOffset, HexagonTypeData types, Hexagon[] neighbours) { // Get referential vertice. int verticesOffset = meshData.vertices.Count; for (int i = 0; i < positionsLookup.GetLength(0); i++) { Vector3 vertexRelativePosition; if (i != 0 && types[_typeID].SizeMultiplier < 1 - HexagonUtils.FloatEpsilon) { vertexRelativePosition = ComputeVertexRelativePosition(types, neighbours, i - 1, Height); } else { vertexRelativePosition = new Vector3(positionsLookup[i, 0], Height, positionsLookup[i, 1]); } AddVertex(ref meshData, vertexRelativePosition.Mult(Scale) + coordinateOffset, new Vector2(topUVsLookup[i, 0], topUVsLookup[i, 1])); } // triangles for (int i = 0; i < topTrianglesLookup.GetLength(0); i++) { meshData.triangles[types[_typeID].TopMaterialIndex].Add(verticesOffset + topTrianglesLookup[i, 0]); meshData.triangles[types[_typeID].TopMaterialIndex].Add(verticesOffset + topTrianglesLookup[i, 1]); meshData.triangles[types[_typeID].TopMaterialIndex].Add(verticesOffset + topTrianglesLookup[i, 2]); } }
static void AddSideQuad(ref Chunk.MeshData meshData, Vector3 coordinateOffset, Vector3[] topEdgePosition, Vector3[] bottomEdgePosition, int triangleIndex, float uvBottomPosition) { int verticesOffset = meshData.vertices.Count; AddVertex(ref meshData, topEdgePosition[0].Mult(Scale) + coordinateOffset, new Vector2(0, 1)); AddVertex(ref meshData, topEdgePosition[1].Mult(Scale) + coordinateOffset, new Vector2(1, 1)); AddVertex(ref meshData, bottomEdgePosition[0].Mult(Scale) + coordinateOffset, new Vector2(0, uvBottomPosition)); AddVertex(ref meshData, bottomEdgePosition[1].Mult(Scale) + coordinateOffset, new Vector2(1, uvBottomPosition)); // triangles meshData.triangles[triangleIndex].Add(verticesOffset + sideTrianglesLookup[0, 0]); meshData.triangles[triangleIndex].Add(verticesOffset + sideTrianglesLookup[0, 1]); meshData.triangles[triangleIndex].Add(verticesOffset + sideTrianglesLookup[0, 2]); meshData.triangles[triangleIndex].Add(verticesOffset + sideTrianglesLookup[1, 0]); meshData.triangles[triangleIndex].Add(verticesOffset + sideTrianglesLookup[1, 1]); meshData.triangles[triangleIndex].Add(verticesOffset + sideTrianglesLookup[1, 2]); }
static void AddVertex(ref Chunk.MeshData meshData, Vector3 position, Vector2 uvs) { meshData.vertices.Add(position); meshData.uvs.Add(uvs); //meshData.normals.Add(new Vector3(0, 1, 0)); // now use recalculateNormal function meshData.colors.Add(Color.white); }
void GenerateSide(ref Chunk.MeshData meshData, Vector3 coordinateOffset, int faceIndex, Hexagon[] neighbours, HexagonTypeData types) { float bottomEdgeHeight = Height - types[_typeID].EdgeHeight; if (neighbours[faceIndex] != null && bottomEdgeHeight < neighbours[faceIndex].Height) { // If the bottomEdge go lower than the neighbour hexagon, make them match. //bottomEdgeHeight = neighbours[faceIndex].Height; } // Make Edge if (types[_typeID].EdgeHeight > HexagonUtils.FloatEpsilon || types[_typeID].SizeMultiplier < 1 - HexagonUtils.FloatEpsilon) { Vector3[] topEdgePosition = ComputeEdgeRelativePosition(types, neighbours, faceIndex, Height); Vector3[] bottomEdgePosition = ComputeEdgeRelativePositionBase(faceIndex, bottomEdgeHeight); AddSideQuad(ref meshData, coordinateOffset, topEdgePosition, bottomEdgePosition, types[_typeID].EdgeMaterialIndex, 0f); } // Make Side float baseLevel = neighbours[faceIndex] == null ? -10 : neighbours[faceIndex].Height - types[neighbours[faceIndex].TypeID].EdgeHeight; Vector3[] topSidePosition = ComputeEdgeRelativePositionBase(faceIndex, bottomEdgeHeight); Vector3[] baseEdgePosition = ComputeEdgeRelativePositionBase(faceIndex, baseLevel); // TODO: remove unneeded underneath geometry when a corner don't make the side visible. if (baseLevel < bottomEdgeHeight - HexagonUtils.FloatEpsilon) { AddSideQuad(ref meshData, coordinateOffset, topSidePosition, baseEdgePosition, types[_typeID].SideMaterialIndex, 1 - (bottomEdgeHeight - baseLevel) * types[_typeID].SideLoopFrequency); } }
/// <summary> /// Add the geometry of this Hexagon inside the MeshData structure of the calling Chunk. /// </summary> /// <param name="meshData">MeshData structure to be filed </param> /// <param name="chunkOffSet">world offset of the parent chunk </param> /// <param name="coordinate">2d grid coordinate of this hexa relative to its chunk </param> /// <param name="neighbours"> Array containing the 6 neighbours of this hex, some can be null</param> public void AddToChunk(ref Chunk.MeshData meshData, Vector3 chunkOffSet, Vector2i coordinate, Hexagon[] neighbours, HexagonTypeData types) { //TODO: compute top vertex position of this hexagon at start to avoid multiple recomputation. // Compute hexagon position. Vector3 coordinateOffset = HexagonUtils.ConvertHexaSpaceToOrthonormal(coordinate) + chunkOffSet; GenerateTop(ref meshData, coordinateOffset, types, neighbours); for (int i = 0; i < 6; i++) { if (IsSideVisible(neighbours[i], types)) { GenerateSide(ref meshData, coordinateOffset, i, neighbours, types); } } }