void TriangulateWater() { foreach (Direction dir in Direction.directions) { Edge edge = GetEdge(dir); Edge rightEdge = GetEdge(dir.Clockwise); Edge leftEdge = GetEdge(dir.CounterClockwise); WaterVertex left = new WaterVertex(ground[dir.CounterClockwise][3]); WaterVertex right = new WaterVertex(ground[dir][3]); if (rightEdge != null && rightEdge.water != null) { right = rightEdge.water[dir.Clockwise][0]; } if (leftEdge != null && leftEdge.water != null) { left = leftEdge.water[dir.CounterClockwise][1]; } if (edge != null && edge.water != null) { left = edge.water[dir][0]; right = edge.water[dir][1]; } chunk.water.AddTriangle(waterCenter, left, right); } }
/// <summary> /// Initialize vertex positions. /// </summary> void InitVertices(HexWorld world) { center = new GroundVertex(this, Random.insideUnitCircle / 5f); ground = new Vertices <GroundVertex>(); river = new Vertices <RiverVertex>(); riverCenter = new RiverVertex(center); waterCenter = new WaterVertex(center); foreach (Direction dir in Direction.directions) { float centerHexOuterRadius = world.RiverWidth / HexWorld.COS_30 * 0.5f; Vector2 edgeLeft = world.LeftVertex(dir); Vector2 edgeMid = world.MiddleVertex(dir); Vector2 edgeRight = world.RightVertex(dir); Vector2 riverEdgeLeft = edgeMid + (edgeLeft - edgeRight).normalized * world.RiverWidth * 0.5f; Vector2 riverEdgeRight = edgeMid + (edgeRight - edgeLeft).normalized * world.RiverWidth * 0.5f; Vector2 a = edgeMid.normalized * centerHexOuterRadius + center.localPos; Vector2 b = edgeLeft.normalized * world.RiverWidth + center.localPos; Vector2 c = b + (edgeRight.normalized * world.RiverWidth + center.localPos - b) * 0.5f; ground[dir] = new GroundVertex[] { new GroundVertex(this, riverEdgeLeft), new GroundVertex(this, edgeMid), new GroundVertex(this, riverEdgeRight), new GroundVertex(this, edgeRight), new GroundVertex(this, b), new GroundVertex(this, c), new GroundVertex(this, a) }; } foreach (Direction dir in Direction.directions) { river[dir] = new RiverVertex[] { new RiverVertex(ground[dir][0]), new RiverVertex(ground[dir][2]), new RiverVertex(ground[dir][4]), new RiverVertex(ground[dir.Clockwise][4]), new RiverVertex(ground[dir][6]) }; } }
public void RefreshWater(bool refreshNeighbors) { if (water == null) { return; } Edge bottomLeft = Lower.GetEdge(Upstream.CounterClockwise); Edge topLeft = Upper.GetEdge(Upstream.Opposite.Clockwise); Edge bottomRight = Lower.GetEdge(Upstream.Clockwise); Edge topRight = Upper.GetEdge(Upstream.Opposite.CounterClockwise); switch (Type) { case EdgeType.LAND: case EdgeType.WATER: water[Upstream] = new WaterVertex[] { new WaterVertex(Lower, (bottomLeft == null || bottomLeft.Type != EdgeType.SHORE? ground[Upstream][0].GlobalPos : bottomLeft.water[Upstream.CounterClockwise][1].GlobalPos) - Lower.position), new WaterVertex(Lower, (bottomRight == null || bottomRight.Type != EdgeType.SHORE? ground[Upstream][4].GlobalPos : bottomRight.water[Upstream.Clockwise][0].GlobalPos) - Lower.position) }; water[Upstream.Opposite] = new WaterVertex[] { new WaterVertex(Upper, (topRight == null || topRight.Type != EdgeType.SHORE? ground[Upstream.Opposite][0].GlobalPos : topRight.water[Upstream.Opposite.CounterClockwise][1].GlobalPos) - Upper.position), new WaterVertex(Upper, (topLeft == null || topLeft.Type != EdgeType.SHORE? ground[Upstream.Opposite][4].GlobalPos : topLeft.water[Upstream.Opposite.Clockwise][0].GlobalPos) - Upper.position) }; break; case EdgeType.SHORE: water[Upstream.Opposite] = new WaterVertex[] { new WaterVertex(Lower, ground[Upstream][4].GlobalPos + Lerp(Lower.WaterLevel) - Lower.position, new Vector2(1f, 1f)), new WaterVertex(Lower, ground[Upstream][0].GlobalPos + Lerp(Lower.WaterLevel) - Lower.position, new Vector2(0f, 1f)) }; water[Upstream] = new WaterVertex[] { new WaterVertex(Lower, (bottomLeft == null || bottomLeft.Type != EdgeType.SHORE? chunk.world.LeftShoreVertex(this) : chunk.world.ShoreVertex(this, bottomLeft)) - Lower.position, new Vector2(0f, 0f)), new WaterVertex(Lower, (bottomRight == null || bottomRight.Type != EdgeType.SHORE? chunk.world.RightShoreVertex(this) : chunk.world.ShoreVertex(bottomRight, this)) - Lower.position, new Vector2(1f, 0f)) }; shore = new WaterVertex[] { new WaterVertex(Lower, ground[Upstream][1].GlobalPos + Lerp(Lower.WaterLevel) - Lower.position), new WaterVertex(Lower, ground[Upstream][3].GlobalPos + Lerp(Lower.WaterLevel) - Lower.position) }; shore[0].uv = new Vector2(1f, 1f); shore[1].uv = new Vector2(0f, 1f); if (refreshNeighbors) { if (bottomLeft != null) { bottomLeft.RefreshWater(false); } if (bottomRight != null) { bottomRight.RefreshWater(false); } } break; } }