Exemplo n.º 1
0
    private static void CreateStraightRampPart(
        StructureGeometry structure, bool isLeftRamp,
        Vertex cornerTopLeft1, Vertex cornerTopLeft2, Vertex cornerTopRight1, Vertex cornerTopRight2,
        Vertex cornerBottomLeft1, Vertex cornerBottomLeft2, Vertex cornerBottomRight1, Vertex cornerBottomRight2,
        bool addLeftSide, bool addRightSide, int segmentWidth, int segmentLength, float curvature, RenderGeometry.FaceType rampFaceType, RenderGeometry.FaceType sideFaceType)
    {
        bool hasBase = (cornerTopLeft1 != cornerBottomLeft1) && (cornerTopRight1 != cornerBottomRight1);

        SurfaceComponentGeometry rampFace = SurfaceComponentGeometries.CreatePlaneGeometry(1, 1, segmentWidth, segmentLength, 0, rampFaceType);
        SurfaceComponentGeometry rampBottom = SurfaceComponentGeometries.CreatePlaneGeometry(1, 1, 1, segmentLength, 1, sideFaceType);
        SurfaceComponentGeometry rampLeftSide = null, rampRightSide = null;

        if (addLeftSide && (cornerTopLeft1 != cornerBottomLeft1))
        {
            rampLeftSide = SurfaceComponentGeometries.CreatePlaneGeometry(1, 1, 1, segmentLength, 2, sideFaceType);
        }
        if (addRightSide && (cornerTopRight1 != cornerBottomRight1))
        {
            rampRightSide = SurfaceComponentGeometries.CreatePlaneGeometry(1, 1, 1, segmentLength, 3, sideFaceType);
        }

        if (curvature != 0)
        {
            float angle = -curvature * Mathf.PI / 2;
            rampFace.ApplySpaceWarp(new SpaceWarp($"sin(x*{angle})/{angle}", $"(1-cos(x*{angle}))/{angle}", "z"));
        }
        // Fit to a 45 degree ramp, then scale.
        rampFace.ApplyLinearTransform(
            Matrix4x4.Translate(cornerTopLeft1.p) *
            Matrix4x4.Scale(VectorUtil.Abs(cornerTopRight2.p - cornerTopLeft1.p)) *
            MatrixUtil.PointToPointTransform(
                rampFace.corners[0].p, rampFace.corners[3].p, rampFace.corners[1].p,
                Vector3.zero, new Vector3(1, isLeftRamp ? 1 : -1, 0), Vector3.forward));

        structure.CreateFace(rampFace, false, cornerTopLeft1, cornerTopLeft2, cornerTopRight2, cornerTopRight1);
        structure.CreateFace(rampBottom, true, cornerBottomRight1, cornerBottomRight2, cornerBottomLeft2, cornerBottomLeft1);
        if (rampLeftSide != null)
        {
            structure.CreateFace(rampLeftSide, true, cornerBottomLeft1, cornerBottomLeft2, cornerTopLeft2, cornerTopLeft1);
        }
        if (rampRightSide != null)
        {
            structure.CreateFace(rampRightSide, true, cornerTopRight1, cornerTopRight2, cornerBottomRight2, cornerBottomRight1);
        }

        var backVertices = new List <Vector3>();

        backVertices.Add(isLeftRamp ? cornerBottomRight1.p : cornerBottomLeft1.p);
        if (isLeftRamp && hasBase)
        {
            backVertices.Add(cornerBottomLeft1.p);
        }
        backVertices.Add(rampFace.boundaries[3].Last().prev.vertex.p);
        backVertices.AddRange(rampFace.boundaries[3].Select(e => e.vertex.p).Reverse());
        if (!isLeftRamp && hasBase)
        {
            backVertices.Add(cornerBottomRight1.p);
        }
        var movedBackVertices = backVertices.Select(p => p + (cornerTopLeft2.p - cornerTopLeft1.p));
        var frontVertices = movedBackVertices.Take(1).Concat(movedBackVertices.Reverse().Take(backVertices.Count - 1)).ToList();

        SurfaceComponentGeometry backFace  = SurfaceComponentGeometries.CreateStarConvexPolygonGeometry(backVertices, 4, RenderGeometry.FaceType.Smooth);
        SurfaceComponentGeometry frontFace = SurfaceComponentGeometries.CreateStarConvexPolygonGeometry(frontVertices, 5, RenderGeometry.FaceType.Smooth);

        if (hasBase)
        {
            if (isLeftRamp)
            {
                backFace.CombineBoundaries(1, 1, segmentWidth, 1);
                frontFace.CombineBoundaries(1, segmentWidth, 1, 1);
                structure.CreateFace(backFace, false, cornerBottomRight1, cornerBottomLeft1, cornerTopLeft1, cornerTopRight1);
                structure.CreateFace(frontFace, false, cornerBottomRight2, cornerTopRight2, cornerTopLeft2, cornerBottomLeft2);
            }
            else
            {
                backFace.CombineBoundaries(1, segmentWidth, 1, 1);
                frontFace.CombineBoundaries(1, 1, segmentWidth, 1);
                structure.CreateFace(backFace, false, cornerBottomLeft1, cornerTopLeft1, cornerTopRight1, cornerBottomRight1);
                structure.CreateFace(frontFace, false, cornerBottomLeft2, cornerBottomRight2, cornerTopRight2, cornerTopLeft2);
            }
        }
        else
        {
            backFace.CombineBoundaries(1, segmentWidth, 1);
            frontFace.CombineBoundaries(1, segmentWidth, 1);
            structure.CreateFace(backFace, false, isLeftRamp ? cornerBottomRight1 : cornerBottomLeft1, cornerTopLeft1, cornerTopRight1);
            structure.CreateFace(frontFace, false, isLeftRamp ? cornerBottomRight2 : cornerBottomLeft2, cornerTopRight2, cornerTopLeft2);
        }
    }