Exemplo n.º 1
0
        public void TrianglateOasis(IHexCell cell, IHexMesh waterMesh, IHexMesh landMesh)
        {
            if (cell.Feature != CellFeature.Oasis)
            {
                return;
            }

            float waterDistance = RenderConfig.OasisWaterRadius;
            float landDistance  = RenderConfig.OasisWaterRadius + RenderConfig.OasisLandWidth;

            float tDelta = 2 * Mathf.PI / RenderConfig.OasisBoundarySegments;

            Vector3 perturbedCenter = NoiseGenerator.Perturb(cell.AbsolutePosition);

            Vector3 waterTwo = NoiseGenerator.Perturb(
                cell.AbsolutePosition + new Vector3(waterDistance, 0f, 0f)
                );

            Vector3 landTwo = NoiseGenerator.Perturb(
                cell.AbsolutePosition + new Vector3(landDistance, 0f, 0f)
                );

            for (float t = 0f; t < 2 * Mathf.PI; t += tDelta)
            {
                Vector3 waterOne = waterTwo;
                Vector3 landOne  = landTwo;

                waterTwo = NoiseGenerator.Perturb(
                    cell.AbsolutePosition + new Vector3(waterDistance * Mathf.Cos(t), 0f, waterDistance * Mathf.Sin(t))
                    );

                landTwo = NoiseGenerator.Perturb(
                    cell.AbsolutePosition + new Vector3(landDistance * Mathf.Cos(t), 0f, landDistance * Mathf.Sin(t))
                    );

                waterMesh.AddTriangle(perturbedCenter, waterTwo, waterOne);

                waterMesh.AddTriangleUV(Vector2.one, Vector2.one, Vector2.one);

                landMesh.AddQuad(waterTwo, waterOne, landTwo, landOne);
            }

            waterMesh.AddTriangle(
                perturbedCenter, NoiseGenerator.Perturb(cell.AbsolutePosition + new Vector3(waterDistance, 0f, 0f)), waterTwo
                );

            landMesh.AddQuad(
                NoiseGenerator.Perturb(cell.AbsolutePosition + new Vector3(waterDistance, 0f, 0f)), waterTwo,
                NoiseGenerator.Perturb(cell.AbsolutePosition + new Vector3(landDistance, 0f, 0f)), landTwo
                );

            waterMesh.AddTriangleUV(Vector2.one, Vector2.one, Vector2.one);
        }
Exemplo n.º 2
0
        private void TriangulateCultureAlongContour(
            IHexCell center, HexDirection direction, Color color, IHexMesh cultureMesh
            )
        {
            var contour = CellEdgeContourCanon.GetContourForCellEdge(center, direction);

            Vector2 innerCCW, innerCW, outerCCW, outerCW;

            for (int i = 0; i < contour.Count - 1; i++)
            {
                outerCCW = contour[i];
                outerCW  = contour[i + 1];

                innerCCW = Vector2.Lerp(outerCCW, center.AbsolutePositionXZ, RenderConfig.CultureWidthPercent);
                innerCW  = Vector2.Lerp(outerCW, center.AbsolutePositionXZ, RenderConfig.CultureWidthPercent);

                cultureMesh.AddQuad(
                    new Vector3(innerCCW.x, 0f, innerCCW.y), new Vector3(innerCW.x, 0f, innerCW.y),
                    new Vector3(outerCCW.x, 0f, outerCCW.y), new Vector3(outerCW.x, 0f, outerCW.y)
                    );

                cultureMesh.AddQuadUV(new Vector2(0f, 0f), new Vector2(0f, 0f), new Vector2(0f, 1f), new Vector2(0f, 1f));

                cultureMesh.AddQuadColor(color);
            }
        }
        private void RenderSpline(BezierSpline spline, IHexMesh mesh)
        {
            if (spline == null)
            {
                return;
            }

            float delta = 1f / RenderConfig.RoadQuadsPerCurve;

            float v1, v2 = 0;

            Vector3 lowerLeft, lowerRight, upperLeft, upperRight;

            for (float t = 0f; t < 1f; t += delta)
            {
                lowerLeft  = spline.GetPoint(t) + spline.GetNormalXZ(t).normalized *RenderConfig.RoadWidth / 2f;
                lowerRight = spline.GetPoint(t) - spline.GetNormalXZ(t).normalized *RenderConfig.RoadWidth / 2f;

                upperLeft  = spline.GetPoint(t + delta) + spline.GetNormalXZ(t + delta).normalized *RenderConfig.RoadWidth / 2f;
                upperRight = spline.GetPoint(t + delta) - spline.GetNormalXZ(t + delta).normalized *RenderConfig.RoadWidth / 2f;

                v1 = v2;

                v2 += (lowerLeft - lowerRight).magnitude / RenderConfig.RoadVRepeatLength;

                mesh.AddQuad(lowerLeft, lowerRight, upperLeft, upperRight);
                mesh.AddQuadUV(0f, 1f, v1, v2);
            }
        }
Exemplo n.º 4
0
        public void TriangulateContoursBetween(
            IHexCell center, IHexCell right, HexDirection direction, Color centerWeights, Color rightWeights, IHexMesh mesh
            )
        {
            var centerRightContour = CellContourCanon.GetContourForCellEdge(center, direction);
            var rightCenterContour = CellContourCanon.GetContourForCellEdge(right, direction.Opposite());

            int centerRightIndex = 1, rightCenterIndex = rightCenterContour.Count - 1;

            while (centerRightIndex < centerRightContour.Count && rightCenterIndex > 0)
            {
                mesh.AddQuad(
                    centerRightContour[centerRightIndex - 1].ToXYZ(), centerRightContour[centerRightIndex].ToXYZ(),
                    rightCenterContour[rightCenterIndex].ToXYZ(), rightCenterContour[rightCenterIndex - 1].ToXYZ()
                    );

                mesh.AddQuadColor(centerWeights, rightWeights);

                centerRightIndex++;
                rightCenterIndex--;
            }

            for (; centerRightIndex < centerRightContour.Count; centerRightIndex++)
            {
                mesh.AddTriangle(
                    centerRightContour[centerRightIndex - 1].ToXYZ(), rightCenterContour[0].ToXYZ(), centerRightContour[centerRightIndex].ToXYZ()
                    );

                mesh.AddTriangleColor(centerWeights, rightWeights, centerWeights);
            }

            for (; rightCenterIndex > 0; rightCenterIndex--)
            {
                mesh.AddTriangle(
                    centerRightContour.Last().ToXYZ(), rightCenterContour[rightCenterIndex].ToXYZ(), rightCenterContour[rightCenterIndex - 1].ToXYZ()
                    );

                mesh.AddTriangleColor(centerWeights, rightWeights, rightWeights);
            }

            if (RiverCanon.HasRiverAlongEdge(right, direction.Next2()))
            {
                var nextRight = Grid.GetNeighbor(center, direction.Next());

                var rightNextRightContour = CellContourCanon.GetContourForCellEdge(right, direction.Next2());
                var nextRightRightContour = CellContourCanon.GetContourForCellEdge(nextRight, direction.Previous());

                mesh.AddTriangle(
                    centerRightContour.Last().ToXYZ(),
                    rightNextRightContour.Last().ToXYZ(),
                    nextRightRightContour.First().ToXYZ()
                    );

                mesh.AddTriangleColor(centerWeights, rightWeights, rightWeights);
            }
        }
        private void TriangulateMarshEdge(
            IHexCell center, IHexCell right, float centerV, float rightV, HexDirection direction, IHexMesh mesh
            )
        {
            mesh.AddQuad(
                center.AbsolutePosition + RenderConfig.GetFirstSolidCorner(direction),
                center.AbsolutePosition + RenderConfig.GetSecondSolidCorner(direction),
                right.AbsolutePosition + RenderConfig.GetSecondSolidCorner(direction.Opposite()),
                right.AbsolutePosition + RenderConfig.GetFirstSolidCorner(direction.Opposite())
                );

            mesh.AddQuadUV(0f, 0f, centerV, rightV);
        }
Exemplo n.º 6
0
        private void TriangulateWaterEdge(
            IHexCell center, Vector3 localCenterPos, Color centerColor,
            IHexCell right, Vector3 localRightPos, Color rightColor,
            HexDirection direction, IHexMesh mesh
            )
        {
            mesh.AddQuad(
                localCenterPos + RenderConfig.GetFirstSolidCorner(direction),
                localCenterPos + RenderConfig.GetSecondSolidCorner(direction),
                localRightPos + RenderConfig.GetSecondSolidCorner(direction.Opposite()),
                localRightPos + RenderConfig.GetFirstSolidCorner(direction.Opposite())
                );

            mesh.AddQuadColor(centerColor, rightColor);
        }
Exemplo n.º 7
0
        private void TriangulateCellWeights_River(
            IHexCell center, IHexCell right, HexDirection direction, bool hasCenterRightRiver, IHexMesh weightsMesh
            )
        {
            var centerRightContour = CellContourCanon.GetContourForCellEdge(center, direction);

            Vector3 innerOne, innerTwo, contourOneXYZ, contourTwoXYZ;

            for (int i = 1; i < centerRightContour.Count; i++)
            {
                contourOneXYZ = centerRightContour[i - 1].ToXYZ();
                contourTwoXYZ = centerRightContour[i].ToXYZ();

                innerOne = Vector3.Lerp(center.AbsolutePosition, contourOneXYZ, RenderConfig.SolidFactor);
                innerTwo = Vector3.Lerp(center.AbsolutePosition, contourTwoXYZ, RenderConfig.SolidFactor);

                weightsMesh.AddTriangle(center.AbsolutePosition, innerOne, innerTwo);
                weightsMesh.AddTriangleColor(CenterWeights);

                weightsMesh.AddQuad(innerOne, innerTwo, contourOneXYZ, contourTwoXYZ);

                if (hasCenterRightRiver)
                {
                    weightsMesh.AddQuadColor(CenterWeights);
                }
                else
                {
                    weightsMesh.AddQuadColor(CenterWeights, CenterRightWeights);
                }
            }

            if (hasCenterRightRiver && direction <= HexDirection.SE)
            {
                ContourTriangulator.TriangulateContoursBetween(
                    center, right, direction, CenterWeights, RightWeights, weightsMesh
                    );
            }
        }
Exemplo n.º 8
0
        private void TriangulateCellWeights_NoRiver(
            IHexCell center, IHexCell right, HexDirection direction, IHexMesh weightsMesh
            )
        {
            if (right == null)
            {
                weightsMesh.AddTriangle(
                    center.AbsolutePosition,
                    center.AbsolutePosition + RenderConfig.GetFirstCorner(direction),
                    center.AbsolutePosition + RenderConfig.GetSecondCorner(direction)
                    );
                weightsMesh.AddTriangleColor(CenterWeights);

                return;
            }

            Vector3 firstEdgeInner  = center.AbsolutePosition + RenderConfig.GetFirstSolidCorner(direction);
            Vector3 secondEdgeInner = center.AbsolutePosition + RenderConfig.GetSecondSolidCorner(direction);

            //Solid center region
            weightsMesh.AddTriangle(center.AbsolutePosition, firstEdgeInner, secondEdgeInner);
            weightsMesh.AddTriangleColor(CenterWeights);

            Vector3 firstEdgeOuter  = (firstEdgeInner + right.AbsolutePosition + RenderConfig.GetSecondSolidCorner(direction.Opposite())) / 2f;
            Vector3 secondEdgeOuter = (secondEdgeInner + right.AbsolutePosition + RenderConfig.GetFirstSolidCorner(direction.Opposite())) / 2f;

            //The edge between Center and Right, going up to the dividing line
            weightsMesh.AddQuad(firstEdgeInner, secondEdgeInner, firstEdgeOuter, secondEdgeOuter);
            weightsMesh.AddQuadColor(CenterWeights, CenterRightWeights);

            //Previous corner out to the edge of the cell
            weightsMesh.AddTriangle(firstEdgeInner, center.AbsolutePosition + RenderConfig.GetFirstCorner(direction), firstEdgeOuter);
            weightsMesh.AddTriangleColor(CenterWeights, CenterLeftRightWeights, CenterRightWeights);

            //Next corner out to the edge of the cell
            weightsMesh.AddTriangle(secondEdgeInner, secondEdgeOuter, center.AbsolutePosition + RenderConfig.GetSecondCorner(direction));
            weightsMesh.AddTriangleColor(CenterWeights, CenterRightWeights, CenterRightNextRightWeights);
        }
Exemplo n.º 9
0
        private void TriangulateCultureCorners_River(
            IHexCell center, IHexCell left, IHexCell right, IHexCell nextRight, HexDirection direction,
            ICivilization centerOwner, ReadOnlyCollection <Vector2> centerRightContour, IHexMesh cultureMesh
            )
        {
            Color cultureColor = centerOwner.Template.Color;

            if (left != null && CivTerritoryLogic.GetCivClaimingCell(left) != centerOwner)
            {
                float ccwTransparency = 1f, cwTransparency;

                Vector2 innerCCW, innerCW, outerCCW, outerCW;

                float cultureWidth = (centerRightContour.First() - center.AbsolutePositionXZ).magnitude * RenderConfig.CultureWidthPercent;

                int i = 0;
                do
                {
                    outerCCW = centerRightContour[i];
                    outerCW  = centerRightContour[i + 1];

                    float distanceFromStart = (centerRightContour.First() - outerCW).magnitude;

                    cwTransparency = Mathf.Clamp01(1f - distanceFromStart / cultureWidth);

                    innerCCW = Vector2.Lerp(outerCCW, center.AbsolutePositionXZ, RenderConfig.CultureWidthPercent);
                    innerCW  = Vector2.Lerp(outerCW, center.AbsolutePositionXZ, RenderConfig.CultureWidthPercent);

                    cultureMesh.AddQuad(
                        new Vector3(innerCCW.x, 0f, innerCCW.y), new Vector3(innerCW.x, 0f, innerCW.y),
                        new Vector3(outerCCW.x, 0f, outerCCW.y), new Vector3(outerCW.x, 0f, outerCW.y)
                        );

                    cultureMesh.AddQuadUV(
                        new Vector2(0f, 0f), new Vector2(0f, 0f), new Vector2(0f, ccwTransparency), new Vector2(0f, cwTransparency)
                        );

                    cultureMesh.AddQuadColor(cultureColor);

                    ccwTransparency = cwTransparency;
                    i++;
                } while(cwTransparency > 0f && i < centerRightContour.Count - 1);
            }

            if (nextRight != null && CivTerritoryLogic.GetCivClaimingCell(nextRight) != centerOwner)
            {
                float cwTransparency = 1f, ccwTransparency;

                Vector2 innerCCW, innerCW, outerCCW, outerCW;

                float cultureWidth = (centerRightContour.Last() - center.AbsolutePositionXZ).magnitude * RenderConfig.CultureWidthPercent;

                int i = centerRightContour.Count - 1;
                do
                {
                    outerCCW = centerRightContour[i - 1];
                    outerCW  = centerRightContour[i];

                    float distanceFromStart = (centerRightContour.Last() - outerCCW).magnitude;

                    ccwTransparency = Mathf.Clamp01(1f - distanceFromStart / cultureWidth);

                    innerCCW = Vector2.Lerp(outerCCW, center.AbsolutePositionXZ, RenderConfig.CultureWidthPercent);
                    innerCW  = Vector2.Lerp(outerCW, center.AbsolutePositionXZ, RenderConfig.CultureWidthPercent);

                    cultureMesh.AddQuad(
                        new Vector3(innerCCW.x, 0f, innerCCW.y), new Vector3(innerCW.x, 0f, innerCW.y),
                        new Vector3(outerCCW.x, 0f, outerCCW.y), new Vector3(outerCW.x, 0f, outerCW.y)
                        );

                    cultureMesh.AddQuadUV(
                        new Vector2(0f, 0f), new Vector2(0f, 0f), new Vector2(0f, ccwTransparency), new Vector2(0f, cwTransparency)
                        );

                    cultureMesh.AddQuadColor(cultureColor);

                    cwTransparency = ccwTransparency;
                    i--;
                }while(ccwTransparency > 0f && i > 0);
            }
        }