public override void prepare(TrackSegment4 trackSegment, GameObject putMeshOnGO)
    {
        base.prepare(trackSegment, putMeshOnGO);

        liftExtruder1 = null;

        if (trackSegment.isLifthill && (trackSegment is ChangeHeight4 || trackSegment.getStartpoint().y != trackSegment.getEndpoint().y || (UnityEngine.Object)frictionWheelsGO == (UnityEngine.Object)null))
        {
            liftExtruder1 = instantiateLiftExtruder(trackSegment);
        }

        putMeshOnGO.GetComponent <Renderer>().sharedMaterial = base.material;
        centerTubeExtruder = new TubeExtruder(centerTubeRadius, centerTubeVertCount);
        centerTubeExtruder.setUV(14, 15);
        centerTubeExtruder.closeEnds = true;
        centerBoxExtruder            = new BoxExtruder(centerTubeRadius * 2f, centerTubeRadius * 2f);
        centerBoxExtruder.setUV(14, 15);
        centerBoxExtruder.closeEnds = true;
        leftTubeExtruder            = new TubeExtruder(sideTubesRadius, sideTubesVertCount);
        leftTubeExtruder.setUV(15, 14);
        rightTubeExtruder = new TubeExtruder(sideTubesRadius, sideTubesVertCount);
        rightTubeExtruder.setUV(15, 14);
        centerCrossTubeExtruder = new ResizableTubeExtruder(1f, centerTubeVertCount);
        centerCrossTubeExtruder.setUV(14, 15);
        centerCrossTubeExtruder.closeEnds = true;
        sideCrossTubeExtruder             = new ResizableTubeExtruder(1f, centerTubeVertCount);
        sideCrossTubeExtruder.setUV(15, 14);
        crossBoxExtruder = new BoxExtruder(sideTubesRadius * 2f, sideTubesRadius * 1.8f);
        crossBoxExtruder.setUV(14, 14);
        collisionMeshExtruder                  = new BoxExtruder(base.trackWidth, 0.02665f);
        base.buildVolumeMeshExtruder           = new BoxExtruder(base.trackWidth, 0.8f);
        base.buildVolumeMeshExtruder.closeEnds = true;
        base.setModelExtruders(centerTubeExtruder, leftTubeExtruder, rightTubeExtruder);
    }
    public override void afterExtrusion(TrackSegment4 trackSegment, GameObject putMeshOnGO)
    {
        base.afterExtrusion(trackSegment, putMeshOnGO);
        float tieInterval = trackSegment.getLength(0) / (float)Mathf.RoundToInt(trackSegment.getLength(0) / this.tieSpacing);
        float pos         = 0;

        // Topper Crossties
        while (pos <= trackSegment.getLength(0) + 0.1f)
        {
            float tForDistance = trackSegment.getTForDistance(pos, 0);
            pos += tieInterval;
            Vector3 normal       = trackSegment.getNormal(tForDistance);
            Vector3 tangentPoint = trackSegment.getTangentPoint(tForDistance);
            Vector3 binormal     = Vector3.Cross(normal, tangentPoint).normalized;
            Vector3 trackPivot   = base.getTrackPivot(trackSegment.getPoint(tForDistance, 0), normal);

            metalTopperCrossTie_1.extrude(trackPivot + binormal * .3f, binormal, normal);
            metalTopperCrossTie_1.extrude(trackPivot - binormal * .3f, binormal, normal);
            metalTopperCrossTie_1.end();
            metalTopperCrossTie_2.extrude(trackPivot + binormal * .3f, binormal, normal);
            metalTopperCrossTie_2.extrude(trackPivot - binormal * .3f, binormal, normal);
            metalTopperCrossTie_2.end();
            metalTopperCrossTie_3.extrude(trackPivot - normal * -0.021113f, normal, -1f * binormal);
            metalTopperCrossTie_3.extrude(trackPivot - normal * -0.150113f, normal, -1f * binormal);
            metalTopperCrossTie_3.end();
            metalTopperCrossTie_4.extrude(trackPivot - normal * -0.021113f, normal, -1f * binormal);
            metalTopperCrossTie_4.extrude(trackPivot - normal * -0.150113f, normal, -1f * binormal);
            metalTopperCrossTie_4.end();
            metalTopperCrossTie_5.extrude(trackPivot - normal * -0.021113f, normal, -1f * binormal);
            metalTopperCrossTie_5.extrude(trackPivot - normal * -0.150113f, normal, -1f * binormal);
            metalTopperCrossTie_5.end();
            metalTopperCrossTie_6.extrude(trackPivot - normal * -0.021113f, normal, -1f * binormal);
            metalTopperCrossTie_6.extrude(trackPivot - normal * -0.150113f, normal, -1f * binormal);
            metalTopperCrossTie_6.end();
            metalTopperCrossTie_7.extrude(trackPivot + tangentPoint * -0.00213f, tangentPoint * -1, normal);
            metalTopperCrossTie_7.extrude(trackPivot + tangentPoint * -0.00001f, tangentPoint * -1, normal);
            metalTopperCrossTie_7.end();
            metalTopperCrossTie_8.extrude(trackPivot + tangentPoint * -0.00213f, tangentPoint, normal);
            metalTopperCrossTie_8.extrude(trackPivot + tangentPoint * -0.00001f, tangentPoint, normal);
            metalTopperCrossTie_8.end();
        }

        //Rendering beams
        int       i       = 0;
        LandPatch terrain = GameController.Instance.park.getTerrain(trackSegment.getStartpoint());

        foreach (SupportPosition position in supportPosts[trackSegment.getStartpoint()])
        {
            if (i > 0)
            {
                if (terrain == null)
                {
                    //left post
                    woodenVerticalSupportPostExtruder.extrude(new Vector3(position.verticalSupportPostLeft.x, position.verticalSupportPostLeft.y + supportBeamExtension, position.verticalSupportPostLeft.z), new Vector3(0, -1, 0), position.verticalSupportPostTangent);
                    woodenVerticalSupportPostExtruder.extrude(position.verticalSupportPostLeft, new Vector3(0, -1, 0), position.verticalSupportPostTangent);
                    woodenVerticalSupportPostExtruder.end();

                    //right post
                    woodenVerticalSupportPostExtruder.extrude(new Vector3(position.verticalSupportPostRight.x, position.verticalSupportPostRight.y + supportBeamExtension, position.verticalSupportPostRight.z), new Vector3(0, -1, 0), position.verticalSupportPostTangent);
                    woodenVerticalSupportPostExtruder.extrude(position.verticalSupportPostRight, new Vector3(0, -1, 0), position.verticalSupportPostTangent);
                    woodenVerticalSupportPostExtruder.end();
                }
                if (!(trackSegment is Station))
                {
                    //bottom beam
                    if (position.bottomBarVisible)
                    {
                        metalCrossTieExtrude(position.bottomBarLeft, position.barsTangent, Vector3.up);
                        metalCrossTieExtrude(position.bottomBarRight, position.barsTangent, Vector3.up);
                        metalCrossTieEnd();
                    }

                    //top beam
                    if (position.topBarVisible)
                    {
                        metalCrossTieExtrude(position.topBarLeft, position.barsTangent, Vector3.up);
                        metalCrossTieExtrude(position.topBarRight, position.barsTangent, Vector3.up);
                        metalCrossTieEnd();
                    }
                }
                //i beam
                if (position.bottomBarVisible)
                {
                    metalIBeamExtrude(position.iBeamLeft, position.iBeamTangent, position.iBeamNormal);
                    metalIBeamExtrude(position.iBeamRight, position.iBeamTangent, position.iBeamNormal);
                    metalIBeamEnd();
                }
                else
                {
                    metalCrossTieExtrude(position.iBeamLeft, position.iBeamTangent, position.iBeamNormal);
                    metalCrossTieExtrude(position.iBeamRight, position.iBeamTangent, position.iBeamNormal);
                    metalCrossTieEnd();
                }
            }
            i++;
        }

        //rendering extruders
        List <ShapeExtruder> metalShapeExtruders = new List <ShapeExtruder>();

        if (useTopperTrack)
        {
            metalShapeExtruders.Add(topperLeftRailExtruder);
            metalShapeExtruders.Add(topperRightRailExtruder);

            metalShapeExtruders.Add(metalTopperCrossTie_1);
            metalShapeExtruders.Add(metalTopperCrossTie_2);
            metalShapeExtruders.Add(metalTopperCrossTie_3);
            metalShapeExtruders.Add(metalTopperCrossTie_4);
            metalShapeExtruders.Add(metalTopperCrossTie_5);
            metalShapeExtruders.Add(metalTopperCrossTie_6);
            metalShapeExtruders.Add(metalTopperCrossTie_7);
            metalShapeExtruders.Add(metalTopperCrossTie_8);
        }
        else
        {
            metalShapeExtruders.Add(iboxLeftRailExtruder);
            metalShapeExtruders.Add(iboxRightRailExtruder);
        }
        if (metalFrontCrossTieExtruder_1.vertices.Count > 0)
        {
            metalShapeExtruders.Add(metalFrontCrossTieExtruder_1);
            metalShapeExtruders.Add(metalFrontCrossTieExtruder_2);
            metalShapeExtruders.Add(metalFrontCrossTieExtruder_3);
            metalShapeExtruders.Add(metalRearCrossTieExtruder_1);
            metalShapeExtruders.Add(metalRearCrossTieExtruder_2);
            metalShapeExtruders.Add(metalRearCrossTieExtruder_3);
        }
        if (metalIBeamExtruder_1.vertices.Count > 0)
        {
            metalShapeExtruders.Add(metalIBeamExtruder_1);
            metalShapeExtruders.Add(metalIBeamExtruder_2);
            metalShapeExtruders.Add(metalIBeamExtruder_3);
        }
        foreach (ShapeExtruder extruder in metalShapeExtruders)
        {
            GameObject gameObject = new GameObject("metalObject");
            gameObject.transform.parent        = putMeshOnGO.transform;
            gameObject.transform.localPosition = Vector3.zero;
            gameObject.transform.localRotation = Quaternion.identity;
            MeshRenderer meshRenderer = gameObject.AddComponent <MeshRenderer>();
            meshRenderer.sharedMaterial = metalMaterial;
            MeshFilter meshFilter = gameObject.AddComponent <MeshFilter>();
            Mesh       mesh       = extruder.getMesh(putMeshOnGO.transform.worldToLocalMatrix);
            //trackSegment.addGeneratedMesh (mesh);
            meshFilter.mesh = mesh;
        }
    }
    public override void sampleAt(TrackSegment4 trackSegment, float t)
    {
        base.sampleAt(trackSegment, t);
        Vector3 normal       = trackSegment.getNormal(t);
        Vector3 trackPivot   = getTrackPivot(trackSegment.getPoint(t), normal);
        Vector3 tangentPoint = trackSegment.getTangentPoint(t);
        Vector3 normalized   = Vector3.Cross(normal, tangentPoint).normalized;
        Vector3 middlePoint  = trackPivot + normalized * trackWidth / 2f;
        Vector3 middlePoint2 = trackPivot - normalized * trackWidth / 2f;
        Vector3 vector       = trackPivot + normal * getCenterPointOffsetY();

        if (useTopperTrack)
        {
            topperLeftRailExtruder.extrude(middlePoint, tangentPoint, normal);
            topperLeftPlankExtruder_1.extrude(middlePoint - (normal * -0.034561f), tangentPoint, normal);
            topperLeftPlankExtruder_2.extrude(middlePoint - (normal * -0.050133f), tangentPoint, normal);
            topperLeftPlankExtruder_3.extrude(middlePoint - (normal * -0.065763f), tangentPoint, normal);
            topperLeftPlankExtruder_4.extrude(middlePoint - (normal * -0.081394f), tangentPoint, normal);
            topperLeftPlankExtruder_5.extrude(middlePoint - (normal * -0.097025f), tangentPoint, normal);
            topperLeftPlankExtruder_6.extrude(middlePoint - (normal * -0.112511f), tangentPoint, normal);
            topperRightRailExtruder.extrude(middlePoint2, tangentPoint, normal);
            topperRightPlankExtruder_1.extrude(middlePoint2 - (normal * -0.034561f), tangentPoint, normal);
            topperRightPlankExtruder_2.extrude(middlePoint2 - (normal * -0.050133f), tangentPoint, normal);
            topperRightPlankExtruder_3.extrude(middlePoint2 - (normal * -0.065763f), tangentPoint, normal);
            topperRightPlankExtruder_4.extrude(middlePoint2 - (normal * -0.081394f), tangentPoint, normal);
            topperRightPlankExtruder_5.extrude(middlePoint2 - (normal * -0.097025f), tangentPoint, normal);
            topperRightPlankExtruder_6.extrude(middlePoint2 - (normal * -0.112511f), tangentPoint, normal);
        }
        else
        {
            iboxLeftRailExtruder.extrude(middlePoint, tangentPoint, normal);
            iboxRightRailExtruder.extrude(middlePoint2, tangentPoint, normal);
        }
        collisionMeshExtruder.extrude(trackPivot, tangentPoint, normal);
        if (liftExtruder != null)
        {
            liftExtruder.extrude(vector - normal * (0.16f + chainLiftHeight / 2f), tangentPoint, normal);
        }

        //calculating beams
        if (t == 0f)
        {
            supportPosts[trackSegment.getStartpoint()] = new List <SupportPosition>();
            float supportInterval = trackSegment.getLength(0) / (float)Mathf.RoundToInt(trackSegment.getLength(0) / this.beamSpacing);
            float pos             = 0;
            while (pos <= trackSegment.getLength(0) + 0.1f)
            {
                float tForDistance = trackSegment.getTForDistance(pos, 0);
                normal       = trackSegment.getNormal(tForDistance);
                tangentPoint = trackSegment.getTangentPoint(tForDistance);
                Vector3 binormal = Vector3.Cross(normal, tangentPoint).normalized;
                trackPivot = base.getTrackPivot(trackSegment.getPoint(tForDistance, 0), normal);
                SupportPosition position = new SupportPosition();
                position.topBarVisible    = false;
                position.bottomBarVisible = false;
                float trackDirection = Mathf.Repeat(Mathf.Atan2(tangentPoint.x, tangentPoint.z) * Mathf.Rad2Deg, 360.0f);
                trackDirection += 45;
                bool trackFacingXPositive = false;
                bool trackFacingXNegative = false;
                bool trackFacingZPositive = false;
                bool trackFacingZNegative = false;
                if (trackDirection < 90)
                {
                    trackFacingZPositive = true;
                }
                else if (trackDirection < 180)
                {
                    trackFacingXPositive = true;
                }
                else if (trackDirection < 270)
                {
                    trackFacingZNegative = true;
                }
                else
                {
                    trackFacingXNegative = true;
                }

                float trackBanking = 0f;

                Vector3 bottomBeamDirection = new Vector3();

                if (trackFacingXPositive)
                {
                    trackBanking = Mathf.Repeat(Mathf.Atan2(normal.z, -normal.y), Mathf.PI * 2.0f) * Mathf.Rad2Deg;
                    if (trackBanking > 180)
                    {
                        trackBanking -= 360;
                    }
                    Vector2 tangentProjection = new Vector2(tangentPoint.x, tangentPoint.z);

                    Vector2 normalProjection = Rotate(tangentProjection, 90);
                    bottomBeamDirection.z = normalProjection.y;
                    bottomBeamDirection.x = normalProjection.x;
                    bottomBeamDirection.Normalize();
                    bottomBeamDirection *= Math.Abs(trackBanking) > 90 ? 1.0f : -1.0f;
                }
                if (trackFacingXNegative)
                {
                    trackBanking = Mathf.Repeat(Mathf.Atan2(-normal.z, -normal.y), Mathf.PI * 2.0f) * Mathf.Rad2Deg;
                    if (trackBanking > 180)
                    {
                        trackBanking -= 360;
                    }

                    bottomBeamDirection.z = tangentPoint.x;
                    bottomBeamDirection.x = tangentPoint.z;

                    Vector2 tangentProjection = new Vector2(tangentPoint.x, tangentPoint.z);

                    Vector2 normalProjection = Rotate(tangentProjection, 90);
                    bottomBeamDirection.z = normalProjection.y;
                    bottomBeamDirection.x = normalProjection.x;
                    bottomBeamDirection.Normalize();
                    bottomBeamDirection *= Math.Abs(trackBanking) > 90 ? 1.0f : -1.0f;
                }
                if (trackFacingZPositive)
                {
                    trackBanking = Mathf.Repeat(Mathf.Atan2(normal.x, -normal.y), Mathf.PI * 2.0f) * Mathf.Rad2Deg;
                    if (trackBanking > 180)
                    {
                        trackBanking -= 360;
                    }

                    bottomBeamDirection.z = tangentPoint.x;
                    bottomBeamDirection.x = tangentPoint.z;

                    Vector2 tangentProjection = new Vector2(tangentPoint.x, tangentPoint.z);

                    Vector2 normalProjection = Rotate(tangentProjection, 90);
                    bottomBeamDirection.z = normalProjection.y;
                    bottomBeamDirection.x = normalProjection.x;
                    bottomBeamDirection.Normalize();
                    bottomBeamDirection *= Math.Abs(trackBanking) <= 90 ? -1.0f : 1.0f;
                }
                if (trackFacingZNegative)
                {
                    trackBanking = Mathf.Repeat(Mathf.Atan2(-normal.x, -normal.y), Mathf.PI * 2.0f) * Mathf.Rad2Deg;
                    if (trackBanking > 180)
                    {
                        trackBanking -= 360;
                    }

                    bottomBeamDirection.z = tangentPoint.x;
                    bottomBeamDirection.x = tangentPoint.z;

                    Vector2 tangentProjection = new Vector2(tangentPoint.x, tangentPoint.z);

                    Vector2 normalProjection = Rotate(tangentProjection, 90);
                    bottomBeamDirection.z = normalProjection.y;
                    bottomBeamDirection.x = normalProjection.x;
                    bottomBeamDirection.Normalize();
                    bottomBeamDirection *= Math.Abs(trackBanking) > 90 ? 1.0f : -1.0f;
                }

                position.trackBanking = trackBanking;

                //track beam
                Vector3 startPoint = trackPivot + normal * 0.159107f + binormal * (beamWidth / 2);
                Vector3 endPoint   = trackPivot + normal * 0.159107f - binormal * (beamWidth / 2);

                bool equalHeight = Mathf.Abs(startPoint.y - endPoint.y) < 0.97f;

                //Bottom beam calculation
                Vector3 bottomBeamPivot = new Vector3(trackPivot.x, Mathf.Min(startPoint.y, endPoint.y), trackPivot.z);

                Vector3 bottomBeamStart = new Vector3();
                Vector3 bottomBeamEnd   = new Vector3();

                Vector3 bottomBeamBinormal = bottomBeamDirection.normalized;

                Vector3 planePosition      = new Vector3();
                Vector3 planeSpanVector1   = endPoint - startPoint;
                Vector3 planeSpanVector2   = tangentPoint;
                Vector3 bottomLinePosition = new Vector3();
                Vector3 topLinePosition    = new Vector3();
                Vector3 lineSpanVector     = bottomBeamDirection;
                bool    attachToStartPoint = false;
                if (((trackFacingXNegative || trackFacingXPositive) ? -1.0f : 1.0) * ((Mathf.Abs(trackBanking) <= 90) ? -1.0f : 1.0f) * trackBanking < 0)
                {
                    bottomBeamStart.x = endPoint.x;
                    bottomBeamStart.z = endPoint.z;
                    bottomBeamStart.y = endPoint.y > startPoint.y ? startPoint.y : endPoint.y;

                    bottomBeamEnd = bottomBeamStart - bottomBeamDirection.normalized * beamWidth;

                    planePosition      = endPoint;
                    bottomLinePosition = bottomBeamStart;
                    topLinePosition    = new Vector3(bottomLinePosition.x, Mathf.Max(startPoint.y, endPoint.y), bottomLinePosition.z);
                    attachToStartPoint = false;
                }
                else
                {
                    bottomBeamEnd.x = startPoint.x;
                    bottomBeamEnd.z = startPoint.z;
                    bottomBeamEnd.y = endPoint.y > startPoint.y ? startPoint.y : endPoint.y;

                    bottomBeamStart = bottomBeamEnd + bottomBeamDirection.normalized * beamWidth;

                    planePosition      = startPoint;
                    bottomLinePosition = bottomBeamEnd;
                    topLinePosition    = new Vector3(bottomLinePosition.x, Mathf.Max(startPoint.y, endPoint.y), bottomLinePosition.z);
                    attachToStartPoint = true;
                }



                if (Mathf.Abs(trackBanking) > 90)
                {
                    bottomBeamStart.y     -= ((Mathf.Abs(trackBanking) / 90) - 1) * invertHeadSpace;
                    bottomBeamEnd.y       -= ((Mathf.Abs(trackBanking) / 90) - 1) * invertHeadSpace;
                    position.topBarVisible = true;
                }
                if (Mathf.Abs(trackBanking) > iBeamBankingSwitch)
                {
                    position.bottomBarVisible = true;
                }
                position.bottomBarLeft  = bottomBeamStart;
                position.bottomBarRight = bottomBeamEnd;
                position.topBarLeft     = new Vector3(bottomBeamEnd.x, Mathf.Max(startPoint.y, endPoint.y), bottomBeamEnd.z);
                position.topBarRight    = new Vector3(bottomBeamStart.x, Mathf.Max(startPoint.y, endPoint.y), bottomBeamStart.z);
                position.barsTangent    = -1f * bottomBeamBinormal;

                if (Mathf.Abs(trackBanking) < 90)
                {
                    position.topBarLeft.y  += (1 - (Mathf.Abs(trackBanking) / 90)) * invertHeadSpace;
                    position.topBarRight.y += (1 - (Mathf.Abs(trackBanking) / 90)) * invertHeadSpace;
                }

                Vector3 projectedTangentDirection = tangentPoint;
                projectedTangentDirection.y = 0;
                projectedTangentDirection.Normalize();



                Vector3 intersectionPoint = new Vector3();

                if (Math.Abs(trackBanking) > 90 && position.topBarVisible)
                {
                    intersectionPoint = IntersectLineAndPlane(planePosition, planeSpanVector1, planeSpanVector2, topLinePosition, lineSpanVector);
                    if (!float.IsNaN(intersectionPoint.x))
                    {
                        if (attachToStartPoint)
                        {
                            endPoint = intersectionPoint;
                        }
                        else
                        {
                            startPoint = intersectionPoint;
                        }
                    }
                }
                else if (Math.Abs(trackBanking) > 0.1)
                {
                    intersectionPoint = IntersectLineAndPlane(planePosition, planeSpanVector1, planeSpanVector2, bottomLinePosition, lineSpanVector);
                }

                if (true)
                {
                    WriteToFile("IntersectionPoint:" + intersectionPoint);
                    WriteToFile("PlanePosition:" + planePosition);
                    WriteToFile("Magnitude:" + (intersectionPoint - planePosition).magnitude);
                    WriteToFile("Difference:" + (intersectionPoint - planePosition));
                    WriteToFile("TrackBanking" + trackBanking);
                    WriteToFile("planeSpanVector1" + planeSpanVector1);
                    WriteToFile("planeSpanVector2" + planeSpanVector2);
                    WriteToFile("topLinePosition" + topLinePosition);
                    WriteToFile("bottomLinePasition" + bottomLinePosition);
                    WriteToFile("lineSpanVector" + lineSpanVector);
                }
                if (!float.IsNaN(intersectionPoint.x) && (intersectionPoint - planePosition).magnitude > 0.5 && (intersectionPoint - planePosition).magnitude < 1.5)
                {
                    if (attachToStartPoint)
                    {
                        endPoint = intersectionPoint;
                    }
                    else
                    {
                        startPoint = intersectionPoint;
                    }
                }


                Vector3 leftVerticalSupportPost  = bottomBeamEnd;
                Vector3 rightVerticalSupportPost = bottomBeamStart;

                Vector3 middleOfBeam = (bottomBeamStart + bottomBeamEnd) / 2;
                if (!position.topBarVisible)
                {
                    if (trackBanking < iBeamBankingSwitch && trackBanking > -90f)
                    {
                        leftVerticalSupportPost = ((bottomBeamEnd - middleOfBeam) * 0.8f) + middleOfBeam;
                    }
                    if (trackBanking > -iBeamBankingSwitch && trackBanking < 90f)
                    {
                        rightVerticalSupportPost = ((bottomBeamStart - middleOfBeam) * 0.8f) + middleOfBeam;
                    }
                }
                if (Mathf.Abs(trackBanking) > 90)
                {
                    leftVerticalSupportPost.y  = Mathf.Max(startPoint.y, endPoint.y);
                    rightVerticalSupportPost.y = Mathf.Max(startPoint.y, endPoint.y);
                }
                else
                {
                    leftVerticalSupportPost.y  = startPoint.y;
                    rightVerticalSupportPost.y = endPoint.y;
                }



                if (pos > 0f)
                {
                    if (Mathf.Abs(trackBanking) <= iBeamBankingSwitch)
                    {
                        float distance = 1 / Mathf.Sin((90 - Mathf.Abs(trackBanking)) * Mathf.Deg2Rad);
                        if (attachToStartPoint)
                        {
                            endPoint = startPoint - ((startPoint - endPoint) * distance);
                        }
                        else
                        {
                            startPoint = endPoint - ((endPoint - startPoint) * distance);
                        }
                    }
                    position.iBeamLeft    = startPoint;
                    position.iBeamRight   = endPoint;
                    position.iBeamTangent = -1f * binormal;
                    position.iBeamNormal  = equalHeight ? Vector3.up : normal;
                }
                position.verticalSupportPostLeft    = leftVerticalSupportPost;
                position.verticalSupportPostRight   = rightVerticalSupportPost;
                position.verticalSupportPostTangent = projectedTangentDirection;
                supportPosts[trackSegment.getStartpoint()].Add(position);
                customBuildVolumeMeshExtruder.setWidth(Vector3.Distance(position.bottomBarLeft, position.bottomBarRight));
                customBuildVolumeMeshExtruder.setHeight(Vector3.Distance((position.bottomBarLeft + position.bottomBarRight) / 2, (position.topBarLeft + position.topBarRight) / 2) + (Mathf.Abs(position.trackBanking) > 90 ? supportBeamExtension : 0f));
                Vector3 middle = (position.topBarLeft + position.topBarRight + position.bottomBarLeft + position.bottomBarRight) / 4;
                if (pos > 0f)
                {
                    customBuildVolumeMeshExtruder.extrude(new Vector3(middle.x, middle.y + (Mathf.Abs(position.trackBanking) > 90 ? supportBeamExtension / 2 : 0f), middle.z), Vector3.Cross(position.barsTangent, Vector3.up) * (Mathf.Abs(position.trackBanking) > 90 ? 1f : -1f), Vector3.up);
                }
                pos += supportInterval;
            }
            customBuildVolumeMeshExtruder.end();
        }
    }
        private void recalculate(MeshGenerator meshGenerator, TrackSegment4 segment)
        {
            foreach (Transform child in segment.gameObject.transform)
            {
                if (child.name != "BetweenTracksMouseCollider" && !child.name.Contains("StationPlatformTrackTile") && child.name != "MouseSelectionCollider")
                {
                    var mesh_filter = child.gameObject.GetComponent <MeshFilter> ();
                    if (mesh_filter != null)
                    {
                        UnityEngine.Object.Destroy(mesh_filter.mesh);
                        UnityEngine.Object.Destroy(mesh_filter.sharedMesh);
                    }
                    UnityEngine.Object.Destroy(child.gameObject);
                }
            }


            if (segment.getLength() <= 0f)
            {
                Debug.LogWarning("Can't extrude this segment! Has a length of 0.");
            }
            meshGenerator.prepare(segment, segment.gameObject);
            float num  = 0f;
            float num2 = 0f;

            meshGenerator.sampleAt(segment, 0f);
            int     num3 = 0;
            int     num4 = 0;
            Vector3 b    = segment.getStartpoint();

            do
            {
                float num5 = 1f - num2;
                if (Vector3.Angle(segment.getDirection(), segment.getPoint(num2 + num5) - segment.getPoint(num2)) > 5f)
                {
                    num5 /= 2f;
                }
                int     num6  = 0;
                Vector3 point = segment.getPoint(num2 + num5);
                float   num7  = Vector3.Angle(segment.getTangentPoint(num2), segment.getTangentPoint(num2 + num5));
                num7 = Mathf.Max(num7, Vector3.Angle(segment.getNormal(num2), segment.getNormal(num2 + num5)));
                while (num5 > 0.01f && (num7 > 10f || (num7 > 2f && (point - b).magnitude > 0.225f)))
                {
                    num4++;
                    num5 /= 2f;
                    point = segment.getPoint(num2 + num5);
                    num7  = Vector3.Angle(segment.getTangentPoint(num2), segment.getTangentPoint(num2 + num5));
                    num7  = Mathf.Max(num7, Vector3.Angle(segment.getNormal(num2), segment.getNormal(num2 + num5)));
                    num6++;
                    if (num6 > 50)
                    {
                        break;
                    }
                }
                num  += (point - b).magnitude;
                num2 += num5;
                b     = point;
                if (num2 > 1f)
                {
                    break;
                }
                meshGenerator.sampleAt(segment, num2);
                num3++;
            }while (num2 < 1f && num3 < 300);
            if (!Mathf.Approximately(num2, 1f))
            {
                meshGenerator.sampleAt(segment, 1f);
            }

            meshGenerator.afterExtrusion(segment, segment.gameObject);
            MeshFilter component = segment.gameObject.GetComponent <MeshFilter>();
            Mesh       mesh      = meshGenerator.getMesh(segment.gameObject);

            UnityEngine.Object.Destroy(component.sharedMesh);
            UnityEngine.Object.Destroy(component.mesh);

            component.sharedMesh = mesh;
            meshGenerator.afterMeshGeneration(segment, segment.gameObject);

            Extruder buildVolumeMeshExtruder = meshGenerator.getBuildVolumeMeshExtruder();

            buildVolumeMeshExtruder.transform(segment.gameObject.transform.worldToLocalMatrix);
            BoundingMesh boundingMesh = segment.gameObject.GetComponent <BoundingMesh>();

            boundingMesh.layers = BoundingVolume.Layers.Buildvolume;
            boundingMesh.setMesh(buildVolumeMeshExtruder.vertices.ToArray(), buildVolumeMeshExtruder.indizes.ToArray());

            GameObject track_mouse_collider = segment.transform.Find("BetweenTracksMouseCollider").gameObject;             // new GameObject("BetweenTracksMouseCollider");

            track_mouse_collider.transform.parent        = segment.gameObject.transform;
            track_mouse_collider.transform.localPosition = Vector3.zero;
            track_mouse_collider.transform.localRotation = Quaternion.identity;
            track_mouse_collider.layer = LayerMasks.ID_MOUSECOLLIDERS;
            MeshCollider meshCollider  = track_mouse_collider.GetComponent <MeshCollider>();
            Mesh         collisionMesh = meshGenerator.getCollisionMesh(segment.gameObject);

            UnityEngine.Object.Destroy(meshCollider.sharedMesh);
            meshCollider.sharedMesh = collisionMesh;

            MouseCollider mouseCollider = segment.gameObject.GetComponent <MouseCollider>();

            mouseCollider.colliderObject = track_mouse_collider;
        }
    private void render()
    {
        Transform startPositionMarker = base.transform;
        LandPatch terrain             = GameController.Instance.park.getTerrain(startPositionMarker.position);

        if (terrain == null)
        {
            Object.Destroy(base.gameObject);
        }
        else
        {
            HybridCoasterMeshGenerator meshGenerator = (HybridCoasterMeshGenerator)track.TrackedRide.meshGenerator;
            foreach (SupportPosition position in meshGenerator.supportPosts[trackSegment.getStartpoint()])
            {
                leftVerticalSupportPost   = new Vector3(position.verticalSupportPostLeft.x, position.verticalSupportPostLeft.y + meshGenerator.supportBeamExtension, position.verticalSupportPostLeft.z);
                rightVerticalSupportPost  = new Vector3(position.verticalSupportPostRight.x, position.verticalSupportPostRight.y + meshGenerator.supportBeamExtension, position.verticalSupportPostRight.z);
                projectedTangentDirection = position.verticalSupportPostTangent;

                leftVerticalSupportPost_floor  = GameController.Instance.park.getTerrain(leftVerticalSupportPost).getPoint(leftVerticalSupportPost);
                rightVerticalSupportPost_floor = GameController.Instance.park.getTerrain(rightVerticalSupportPost).getPoint(rightVerticalSupportPost);

                leftVerticalSupportPost_footerHeight = leftVerticalSupportPost_floor.y + 0.1f;
                if (GameController.Instance.park.getTerrain(leftVerticalSupportPost).WaterHeight > leftVerticalSupportPost_footerHeight)
                {
                    leftVerticalSupportPost_footerHeight = GameController.Instance.park.getTerrain(leftVerticalSupportPost).WaterHeight + 0.025f;
                }
                rightVerticalSupportPost_footerHeight = rightVerticalSupportPost_floor.y + 0.1f;
                if (GameController.Instance.park.getTerrain(rightVerticalSupportPost).WaterHeight > rightVerticalSupportPost_footerHeight)
                {
                    rightVerticalSupportPost_footerHeight = GameController.Instance.park.getTerrain(rightVerticalSupportPost).WaterHeight + 0.025f;
                }
                if (leftVerticalSupportPost.y > leftVerticalSupportPost_floor.y)
                {
                    woodenVerticalSupportPostExtruder.extrude(leftVerticalSupportPost, new Vector3(0, -1, 0), projectedTangentDirection);
                    woodenVerticalSupportPostExtruder.extrude(leftVerticalSupportPost_floor, new Vector3(0, -1, 0), projectedTangentDirection);
                    woodenVerticalSupportPostExtruder.end();
                    concreteVerticalSupportFooterExtruder.extrude(new Vector3(leftVerticalSupportPost_floor.x, leftVerticalSupportPost_footerHeight, leftVerticalSupportPost_floor.z), new Vector3(0, -1, 0), projectedTangentDirection);
                    concreteVerticalSupportFooterExtruder.extrude(new Vector3(leftVerticalSupportPost_floor.x, leftVerticalSupportPost_floor.y - 0.05f, leftVerticalSupportPost_floor.z), new Vector3(0, -1, 0), projectedTangentDirection);
                    concreteVerticalSupportFooterExtruder.end();
                }
                if (rightVerticalSupportPost.y > rightVerticalSupportPost_floor.y)
                {
                    woodenVerticalSupportPostExtruder.extrude(rightVerticalSupportPost, new Vector3(0, -1, 0), projectedTangentDirection);
                    woodenVerticalSupportPostExtruder.extrude(rightVerticalSupportPost_floor, new Vector3(0, -1, 0), projectedTangentDirection);
                    woodenVerticalSupportPostExtruder.end();
                    concreteVerticalSupportFooterExtruder.extrude(new Vector3(rightVerticalSupportPost_floor.x, rightVerticalSupportPost_footerHeight, rightVerticalSupportPost_floor.z), new Vector3(0, -1, 0), projectedTangentDirection);
                    concreteVerticalSupportFooterExtruder.extrude(new Vector3(rightVerticalSupportPost_floor.x, rightVerticalSupportPost_floor.y - 0.05f, rightVerticalSupportPost_floor.z), new Vector3(0, -1, 0), projectedTangentDirection);
                    concreteVerticalSupportFooterExtruder.end();
                }
            }

            List <BoxExtruder> extruders = new List <BoxExtruder>();
            if (woodenVerticalSupportPostExtruder.vertices.Count > 0)
            {
                extruders.Add(woodenVerticalSupportPostExtruder);
            }
            if (woodenHorizontalSupportPostExtruder.vertices.Count > 0)
            {
                extruders.Add(woodenHorizontalSupportPostExtruder);
            }
            if (concreteVerticalSupportFooterExtruder.vertices.Count > 0)
            {
                extruders.Add(concreteVerticalSupportFooterExtruder);
            }
            foreach (BoxExtruder extruder in extruders)
            {
                GameObject gameObject = new GameObject("supportObject");
                gameObject.transform.parent        = base.instance.transform;
                gameObject.transform.localPosition = Vector3.zero;
                gameObject.transform.localRotation = Quaternion.identity;
                MeshRenderer meshRenderer = gameObject.AddComponent <MeshRenderer>();
                meshRenderer.sharedMaterial = track.TrackedRide.meshGenerator.material;
                MeshFilter meshFilter = gameObject.AddComponent <MeshFilter>();
                Mesh       mesh       = extruder.getMesh(base.instance.transform.worldToLocalMatrix);
                trackSegment.addGeneratedMesh(mesh);
                meshFilter.mesh = mesh;
            }
        }
    }
		private void recalculate(MeshGenerator meshGenerator, TrackSegment4 segment)
		{

			foreach(Transform child in segment.gameObject.transform) {
				if (child.name != "BetweenTracksMouseCollider" && !child.name.Contains("StationPlatformTrackTile") && child.name != "MouseSelectionCollider") {
					var mesh_filter = child.gameObject.GetComponent<MeshFilter> ();
					if (mesh_filter != null) {
						UnityEngine.Object.Destroy (mesh_filter.mesh);
						UnityEngine.Object.Destroy (mesh_filter.sharedMesh);
					}
					UnityEngine.Object.Destroy (child.gameObject);

				}
			}


			if (segment.getLength() <= 0f)
			{
				Debug.LogWarning("Can't extrude this segment! Has a length of 0.");
			}
			meshGenerator.prepare(segment, segment.gameObject);
			float num = 0f;
			float num2 = 0f;
			meshGenerator.sampleAt(segment, 0f);
			int num3 = 0;
			int num4 = 0;
			Vector3 b = segment.getStartpoint();
			do
			{
				float num5 = 1f - num2;
				if (Vector3.Angle(segment.getDirection(), segment.getPoint(num2 + num5) - segment.getPoint(num2)) > 5f)
				{
					num5 /= 2f;
				}
				int num6 = 0;
				Vector3 point = segment.getPoint(num2 + num5);
				float num7 = Vector3.Angle(segment.getTangentPoint(num2), segment.getTangentPoint(num2 + num5));
				num7 = Mathf.Max(num7, Vector3.Angle(segment.getNormal(num2), segment.getNormal(num2 + num5)));
				while (num5 > 0.01f && (num7 > 10f || (num7 > 2f && (point - b).magnitude > 0.225f)))
				{
					num4++;
					num5 /= 2f;
					point = segment.getPoint(num2 + num5);
					num7 = Vector3.Angle(segment.getTangentPoint(num2), segment.getTangentPoint(num2 + num5));
					num7 = Mathf.Max(num7, Vector3.Angle(segment.getNormal(num2), segment.getNormal(num2 + num5)));
					num6++;
					if (num6 > 50)
					{
						break;
					}
				}
				num += (point - b).magnitude;
				num2 += num5;
				b = point;
				if (num2 > 1f)
				{
					break;
				}
				meshGenerator.sampleAt(segment, num2);
				num3++;
			}
			while (num2 < 1f && num3 < 300);
			if (!Mathf.Approximately(num2, 1f))
			{
				meshGenerator.sampleAt(segment, 1f);
			}

			meshGenerator.afterExtrusion(segment, segment.gameObject);
			MeshFilter component = segment.gameObject.GetComponent<MeshFilter>();
			Mesh mesh = meshGenerator.getMesh(segment.gameObject);
			UnityEngine.Object.Destroy (component.sharedMesh);
			UnityEngine.Object.Destroy (component.mesh);

			component.sharedMesh = mesh;
			meshGenerator.afterMeshGeneration(segment, segment.gameObject);

			Extruder buildVolumeMeshExtruder = meshGenerator.getBuildVolumeMeshExtruder();
			buildVolumeMeshExtruder.transform(segment.gameObject.transform.worldToLocalMatrix);
			BoundingMesh boundingMesh = segment.gameObject.GetComponent<BoundingMesh>();
			boundingMesh.layers = BoundingVolume.Layers.Buildvolume;
			boundingMesh.setMesh(buildVolumeMeshExtruder.vertices.ToArray(), buildVolumeMeshExtruder.indizes.ToArray());

			GameObject track_mouse_collider = segment.transform.Find ("BetweenTracksMouseCollider").gameObject;// new GameObject("BetweenTracksMouseCollider");
			track_mouse_collider.transform.parent = segment.gameObject.transform;
			track_mouse_collider.transform.localPosition = Vector3.zero;
			track_mouse_collider.transform.localRotation = Quaternion.identity;
			track_mouse_collider.layer = LayerMasks.ID_MOUSECOLLIDERS;
			MeshCollider meshCollider = track_mouse_collider.GetComponent<MeshCollider>();
			Mesh collisionMesh = meshGenerator.getCollisionMesh(segment.gameObject);

			UnityEngine.Object.Destroy (meshCollider.sharedMesh);
			meshCollider.sharedMesh = collisionMesh;

			MouseCollider mouseCollider = segment.gameObject.GetComponent<MouseCollider>();
			mouseCollider.colliderObject = track_mouse_collider;

		}
Example #7
0
    public override void afterExtrusion(TrackSegment4 trackSegment, GameObject putMeshOnGO)
    {
        base.afterExtrusion(trackSegment, putMeshOnGO);

        WriteToFile(trackSegment.track.TrackedRide.supportConfiguration.supportSettings [0].supportGO.ToString());
        WriteToFile(trackSegment.track.TrackedRide.supportConfiguration.supportSettings [0].supportGO.GetType().ToString());
        float   supportInterval             = trackSegment.getLength(0) / ((float)Mathf.RoundToInt(trackSegment.getLength(0) / this.crossBeamSpacing) * 2);
        float   pos                         = 0;
        bool    isTopperCrosstie            = true;
        int     index                       = 0;
        Vector3 previousSupportLeft         = new Vector3();
        Vector3 previousSupportRight        = new Vector3();
        Vector3 previousSupportTangent      = new Vector3();
        bool    previousFlippedSupportPosts = false;

        while (pos <= trackSegment.getLength(0) + 0.1f)
        {
            index++;
            float tForDistance = trackSegment.getTForDistance(pos, 0);
            pos += supportInterval;

            isTopperCrosstie = !isTopperCrosstie;
            Vector3 normal       = trackSegment.getNormal(tForDistance);
            Vector3 tangentPoint = trackSegment.getTangentPoint(tForDistance);
            Vector3 binormal     = Vector3.Cross(normal, tangentPoint).normalized;
            Vector3 trackPivot   = base.getTrackPivot(trackSegment.getPoint(tForDistance, 0), normal);

            if (isTopperCrosstie)
            {
                /*
                 * metalTopperCrossTie_1.extrude(trackPivot + binormal * .3f, binormal, normal);
                 * metalTopperCrossTie_1.extrude(trackPivot - binormal * .3f, binormal, normal);
                 * metalTopperCrossTie_1.end();
                 * metalTopperCrossTie_2.extrude(trackPivot + binormal * .3f, binormal, normal);
                 * metalTopperCrossTie_2.extrude(trackPivot - binormal * .3f, binormal, normal);
                 * metalTopperCrossTie_2.end();
                 * metalTopperCrossTie_3.extrude(trackPivot - normal * -0.021113f, normal, -1f * binormal);
                 * metalTopperCrossTie_3.extrude(trackPivot - normal * -0.150113f, normal, -1f * binormal);
                 * metalTopperCrossTie_3.end();
                 * metalTopperCrossTie_4.extrude(trackPivot - normal * -0.021113f, normal, -1f * binormal);
                 * metalTopperCrossTie_4.extrude(trackPivot - normal * -0.150113f, normal, -1f * binormal);
                 * metalTopperCrossTie_4.end();
                 * metalTopperCrossTie_5.extrude(trackPivot - normal * -0.021113f, normal, -1f * binormal);
                 * metalTopperCrossTie_5.extrude(trackPivot - normal * -0.150113f, normal, -1f * binormal);
                 * metalTopperCrossTie_5.end();
                 * metalTopperCrossTie_6.extrude(trackPivot - normal * -0.021113f, normal, -1f * binormal);
                 * metalTopperCrossTie_6.extrude(trackPivot - normal * -0.150113f, normal, -1f * binormal);
                 * metalTopperCrossTie_6.end();
                 * metalTopperCrossTie_7.extrude(trackPivot + tangentPoint * -0.00213f, tangentPoint * -1, normal);
                 * metalTopperCrossTie_7.extrude(trackPivot + tangentPoint * -0.00001f, tangentPoint * -1, normal);
                 * metalTopperCrossTie_7.end();
                 * metalTopperCrossTie_8.extrude(trackPivot + tangentPoint * -0.00213f, tangentPoint, normal);
                 * metalTopperCrossTie_8.extrude(trackPivot + tangentPoint * -0.00001f, tangentPoint, normal);
                 * metalTopperCrossTie_8.end();
                 */
            }
            else
            {
                float trackDirection = Mathf.Repeat(Mathf.Atan2(tangentPoint.x, tangentPoint.z) * Mathf.Rad2Deg, 360.0f);
                trackDirection += 45;
                bool trackFacingXPositive = false;
                bool trackFacingXNegative = false;
                bool trackFacingZPositive = false;
                bool trackFacingZNegative = false;
                if (trackDirection < 90)
                {
                    trackFacingZPositive = true;
                }
                else if (trackDirection < 180)
                {
                    trackFacingXPositive = true;
                }
                else if (trackDirection < 270)
                {
                    trackFacingZNegative = true;
                }
                else
                {
                    trackFacingXNegative = true;
                }

                float trackBanking = 0f;

                Vector3 bottomBeamDirection = new Vector3();

                if (trackFacingXPositive)
                {
                    trackBanking = Mathf.Repeat(Mathf.Atan2(normal.z, -normal.y), Mathf.PI * 2.0f) * Mathf.Rad2Deg;
                    if (trackBanking > 180)
                    {
                        trackBanking -= 360;
                    }
                    Vector2 tangentProjection = new Vector2(tangentPoint.x, tangentPoint.z);

                    Vector2 normalProjection = Rotate(tangentProjection, 90);
                    bottomBeamDirection.z = normalProjection.y;
                    bottomBeamDirection.x = normalProjection.x;
                    bottomBeamDirection.Normalize();
                    bottomBeamDirection *= Math.Abs(trackBanking) > 90 ? 1.0f : -1.0f;
                }
                if (trackFacingXNegative)
                {
                    trackBanking = Mathf.Repeat(Mathf.Atan2(-normal.z, -normal.y), Mathf.PI * 2.0f) * Mathf.Rad2Deg;
                    if (trackBanking > 180)
                    {
                        trackBanking -= 360;
                    }

                    bottomBeamDirection.z = tangentPoint.x;
                    bottomBeamDirection.x = tangentPoint.z;

                    Vector2 tangentProjection = new Vector2(tangentPoint.x, tangentPoint.z);

                    Vector2 normalProjection = Rotate(tangentProjection, 90);
                    bottomBeamDirection.z = normalProjection.y;
                    bottomBeamDirection.x = normalProjection.x;
                    bottomBeamDirection.Normalize();
                    bottomBeamDirection *= Math.Abs(trackBanking) > 90 ? 1.0f : -1.0f;
                }
                if (trackFacingZPositive)
                {
                    trackBanking = Mathf.Repeat(Mathf.Atan2(normal.x, -normal.y), Mathf.PI * 2.0f) * Mathf.Rad2Deg;
                    if (trackBanking > 180)
                    {
                        trackBanking -= 360;
                    }

                    bottomBeamDirection.z = tangentPoint.x;
                    bottomBeamDirection.x = tangentPoint.z;

                    Vector2 tangentProjection = new Vector2(tangentPoint.x, tangentPoint.z);

                    Vector2 normalProjection = Rotate(tangentProjection, 90);
                    bottomBeamDirection.z = normalProjection.y;
                    bottomBeamDirection.x = normalProjection.x;
                    bottomBeamDirection.Normalize();
                    bottomBeamDirection *= Math.Abs(trackBanking) <= 90 ? -1.0f : 1.0f;
                }
                if (trackFacingZNegative)
                {
                    trackBanking = Mathf.Repeat(Mathf.Atan2(-normal.x, -normal.y), Mathf.PI * 2.0f) * Mathf.Rad2Deg;
                    if (trackBanking > 180)
                    {
                        trackBanking -= 360;
                    }

                    bottomBeamDirection.z = tangentPoint.x;
                    bottomBeamDirection.x = tangentPoint.z;

                    Vector2 tangentProjection = new Vector2(tangentPoint.x, tangentPoint.z);

                    Vector2 normalProjection = Rotate(tangentProjection, 90);
                    bottomBeamDirection.z = normalProjection.y;
                    bottomBeamDirection.x = normalProjection.x;
                    bottomBeamDirection.Normalize();
                    bottomBeamDirection *= Math.Abs(trackBanking) > 90 ? 1.0f : -1.0f;
                }

                //track beam
                Vector3 startPoint = trackPivot + normal * 0.159107f + binormal * (beamWidth / 2);
                Vector3 endPoint   = trackPivot + normal * 0.159107f - binormal * (beamWidth / 2);

                bool equalHeight = Mathf.Abs(startPoint.y - endPoint.y) < 0.97f;

                //Bottom beam calculation
                Vector3 bottomBeamPivot = new Vector3(trackPivot.x, Mathf.Min(startPoint.y, endPoint.y), trackPivot.z);

                Vector3 bottomBeamStart = new Vector3();
                Vector3 bottomBeamEnd   = new Vector3();

                Vector3 bottomBeamBinormal = bottomBeamDirection.normalized;

                Vector3 planePosition      = new Vector3();
                Vector3 planeSpanVector1   = endPoint - startPoint;
                Vector3 planeSpanVector2   = tangentPoint;
                Vector3 bottomLinePosition = new Vector3();
                Vector3 topLinePosition    = new Vector3();
                Vector3 lineSpanVector     = bottomBeamDirection;
                bool    hasTopBars         = false;
                bool    attachToStartPoint = false;
                if (((trackFacingXNegative || trackFacingXPositive) ? -1.0f : 1.0) * ((Mathf.Abs(trackBanking) <= 90) ? -1.0f : 1.0f) * trackBanking < 0)
                {
                    bottomBeamStart.x = endPoint.x;
                    bottomBeamStart.z = endPoint.z;
                    bottomBeamStart.y = endPoint.y > startPoint.y ? startPoint.y : endPoint.y;

                    bottomBeamEnd = bottomBeamStart - bottomBeamDirection.normalized * beamWidth;

                    planePosition      = endPoint;
                    bottomLinePosition = bottomBeamStart;
                    topLinePosition    = new Vector3(bottomLinePosition.x, Mathf.Max(startPoint.y, endPoint.y), bottomLinePosition.z);
                    attachToStartPoint = false;
                }
                else
                {
                    bottomBeamEnd.x = startPoint.x;
                    bottomBeamEnd.z = startPoint.z;
                    bottomBeamEnd.y = endPoint.y > startPoint.y ? startPoint.y : endPoint.y;

                    bottomBeamStart = bottomBeamEnd + bottomBeamDirection.normalized * beamWidth;

                    planePosition      = startPoint;
                    bottomLinePosition = bottomBeamEnd;
                    topLinePosition    = new Vector3(bottomLinePosition.x, Mathf.Max(startPoint.y, endPoint.y), bottomLinePosition.z);
                    attachToStartPoint = true;
                }



                if (!(trackSegment is Station))
                {
                    if (Mathf.Abs(trackBanking) > 90)
                    {
                        bottomBeamStart.y -= ((Mathf.Abs(trackBanking) / 90) - 1) * invertHeadSpace;
                        bottomBeamEnd.y   -= ((Mathf.Abs(trackBanking) / 90) - 1) * invertHeadSpace;
                        if (pos > supportInterval)
                        {
                            //Top beam extruding
                            metalCrossTieExtrude(new Vector3(bottomBeamEnd.x, Mathf.Max(startPoint.y, endPoint.y), bottomBeamEnd.z), -1f * bottomBeamBinormal, Vector3.up);
                            metalCrossTieExtrude(new Vector3(bottomBeamStart.x, Mathf.Max(startPoint.y, endPoint.y), bottomBeamStart.z), -1f * bottomBeamBinormal, Vector3.up);
                            metalCrossTieEnd();
                            hasTopBars = true;
                        }
                    }

                    //Bottom beam extruding
                    if (Mathf.Abs(trackBanking) > iBeamBankingSwitch && pos > supportInterval)
                    {
                        metalCrossTieExtrude(bottomBeamEnd, -1f * bottomBeamBinormal, Vector3.up);
                        metalCrossTieExtrude(bottomBeamStart, -1f * bottomBeamBinormal, Vector3.up);
                        metalCrossTieEnd();
                    }

                    Vector3 leftVerticalSupportPost  = bottomBeamEnd;
                    Vector3 rightVerticalSupportPost = bottomBeamStart;

                    if (!hasTopBars)
                    {
                        if (trackBanking < iBeamBankingSwitch && trackBanking > -90f)
                        {
                            leftVerticalSupportPost = ((bottomBeamEnd - trackPivot) * 0.8f) + trackPivot;
                        }
                        if (trackBanking > -iBeamBankingSwitch && trackBanking < 90f)
                        {
                            rightVerticalSupportPost = ((bottomBeamStart - trackPivot) * 0.8f) + trackPivot;
                        }
                    }

                    LandPatch terrain = GameController.Instance.park.getTerrain(trackPivot);

                    if (terrain != null)
                    {
                        groundHeight = terrain.getLowestHeight();
                        groundHeight = Mathf.Min(trackSegment.getStartpoint().y, trackSegment.getEndpoint().y);
                    }
                    else
                    {
                        groundHeight = 0;
                    }
                    Vector3 projectedTangentDirection = tangentPoint;
                    projectedTangentDirection.y = 0;
                    projectedTangentDirection.Normalize();

                    if (Mathf.Abs(trackBanking) > 90)
                    {
                        leftVerticalSupportPost.y  = Mathf.Max(startPoint.y, endPoint.y);
                        rightVerticalSupportPost.y = Mathf.Max(startPoint.y, endPoint.y);
                    }
                    else
                    {
                        leftVerticalSupportPost.y  = startPoint.y;
                        rightVerticalSupportPost.y = endPoint.y;
                    }
                    if (pos > supportInterval)
                    {
                        //left post
                        woodenVerticalSupportPostExtruder.extrude(new Vector3(leftVerticalSupportPost.x, leftVerticalSupportPost.y + supportBeamExtension, leftVerticalSupportPost.z), new Vector3(0, -1, 0), projectedTangentDirection);
                        woodenVerticalSupportPostExtruder.extrude(new Vector3(leftVerticalSupportPost.x, groundHeight, leftVerticalSupportPost.z), new Vector3(0, -1, 0), projectedTangentDirection);
                        woodenVerticalSupportPostExtruder.end();

                        //right post
                        woodenVerticalSupportPostExtruder.extrude(new Vector3(rightVerticalSupportPost.x, rightVerticalSupportPost.y + supportBeamExtension, rightVerticalSupportPost.z), new Vector3(0, -1, 0), projectedTangentDirection);
                        woodenVerticalSupportPostExtruder.extrude(new Vector3(rightVerticalSupportPost.x, groundHeight, rightVerticalSupportPost.z), new Vector3(0, -1, 0), projectedTangentDirection);
                        woodenVerticalSupportPostExtruder.end();

                        if (Mathf.Abs(trackBanking) > 90 != previousFlippedSupportPosts)
                        {
                            Vector3 temp = previousSupportLeft;
                            previousSupportLeft  = previousSupportRight;
                            previousSupportRight = temp;
                        }
                        //Horizontal beams
                        float leftY       = Mathf.Min(previousSupportLeft.y, leftVerticalSupportPost.y) - 0.06f;
                        float rightY      = Mathf.Min(previousSupportRight.y, rightVerticalSupportPost.y) - 0.06f;
                        float connectionY = Mathf.Min(leftY, rightY);
                        //left horizontal beams
                        bool first = true;
                        while (leftY > groundHeight)
                        {
                            woodenHorizontalSupportPostExtruder.extrude(new Vector3(previousSupportLeft.x, leftY, previousSupportLeft.z), previousSupportTangent, Vector3.up);
                            woodenHorizontalSupportPostExtruder.extrude(new Vector3(leftVerticalSupportPost.x, leftY, leftVerticalSupportPost.z), projectedTangentDirection, Vector3.up);
                            woodenHorizontalSupportPostExtruder.end();
                            if (first)
                            {
                                leftY -= woodenHorizontalSupportPostExtruder.height * 1.5f;
                                leftY  = Mathf.Floor(leftY / supportVerticalGrid) * supportVerticalGrid;
                                first  = false;
                            }
                            else
                            {
                                leftY -= supportVerticalGrid;
                            }
                        }
                        first = true;
                        //right horizontal beams
                        while (rightY > groundHeight)
                        {
                            woodenHorizontalSupportPostExtruder.extrude(new Vector3(previousSupportRight.x, rightY, previousSupportRight.z), previousSupportTangent, Vector3.up);
                            woodenHorizontalSupportPostExtruder.extrude(new Vector3(rightVerticalSupportPost.x, rightY, rightVerticalSupportPost.z), projectedTangentDirection, Vector3.up);
                            woodenHorizontalSupportPostExtruder.end();
                            if (first)
                            {
                                rightY -= woodenHorizontalSupportPostExtruder.height * 1.5f;
                                rightY  = Mathf.Floor(rightY / supportVerticalGrid) * supportVerticalGrid;
                                first   = false;
                            }
                            else
                            {
                                rightY -= supportVerticalGrid;
                            }
                        }
                        first = true;
                        //connector beams
                        while (connectionY > groundHeight)
                        {
                            if (connectionY < bottomBeamEnd.y)
                            {
                                woodenHorizontalSupportPostExtruder.extrude(new Vector3(rightVerticalSupportPost.x, connectionY, rightVerticalSupportPost.z), Vector3.Cross(projectedTangentDirection, Vector3.up), Vector3.up);
                                woodenHorizontalSupportPostExtruder.extrude(new Vector3(leftVerticalSupportPost.x, connectionY, leftVerticalSupportPost.z), Vector3.Cross(projectedTangentDirection, Vector3.up), Vector3.up);
                                woodenHorizontalSupportPostExtruder.end();
                                if (!first && connectionY > supportVerticalGrid)
                                {
                                    woodenHorizontalSupportPostExtruder.extrude(new Vector3(rightVerticalSupportPost.x, connectionY, rightVerticalSupportPost.z), Vector3.Cross(projectedTangentDirection, Vector3.up), Vector3.up);
                                    woodenHorizontalSupportPostExtruder.extrude(new Vector3(leftVerticalSupportPost.x, connectionY - supportVerticalGrid, leftVerticalSupportPost.z), Vector3.Cross(projectedTangentDirection, Vector3.up), Vector3.up);
                                    woodenHorizontalSupportPostExtruder.end();
                                }
                            }
                            if (first)
                            {
                                connectionY -= woodenHorizontalSupportPostExtruder.height * 1.5f;
                                connectionY  = Mathf.Floor(connectionY / supportVerticalGrid) * supportVerticalGrid;
                                first        = false;
                            }
                            else
                            {
                                connectionY -= supportVerticalGrid;
                            }
                        }
                    }
                    previousSupportLeft         = leftVerticalSupportPost;
                    previousSupportRight        = rightVerticalSupportPost;
                    previousSupportTangent      = projectedTangentDirection;
                    previousFlippedSupportPosts = Mathf.Abs(trackBanking) > 90;
                }

                Vector3 intersectionPoint = new Vector3();

                if (Math.Abs(trackBanking) > 90 && hasTopBars)
                {
                    intersectionPoint = IntersectLineAndPlane(planePosition, planeSpanVector1, planeSpanVector2, topLinePosition, lineSpanVector);
                    if (!float.IsNaN(intersectionPoint.x))
                    {
                        if (attachToStartPoint)
                        {
                            endPoint = intersectionPoint;
                        }
                        else
                        {
                            startPoint = intersectionPoint;
                        }
                    }
                }
                else if (Math.Abs(trackBanking) > 0.1)
                {
                    intersectionPoint = IntersectLineAndPlane(planePosition, planeSpanVector1, planeSpanVector2, bottomLinePosition, lineSpanVector);
                }

                if (Mathf.Abs(trackBanking) > 5 && !float.IsNaN(intersectionPoint.x) && (intersectionPoint - planePosition).magnitude > 0.5 && (intersectionPoint - planePosition).magnitude < 1.5)
                {
                    WriteToFile("IntersectionPoint:" + intersectionPoint);
                    WriteToFile("PlanePosition:" + planePosition);
                    WriteToFile("Magnitude:" + (intersectionPoint - planePosition).magnitude);
                    WriteToFile("Difference:" + (intersectionPoint - planePosition));
                    WriteToFile("TrackBanking" + trackBanking);
                    WriteToFile("planeSpanVector1" + planeSpanVector1);
                    WriteToFile("planeSpanVector2" + planeSpanVector2);
                    WriteToFile("topLinePosition" + topLinePosition);
                    WriteToFile("bottomLinePasition" + bottomLinePosition);
                    WriteToFile("lineSpanVector" + lineSpanVector);
                    if (attachToStartPoint)
                    {
                        endPoint = intersectionPoint;
                    }
                    else
                    {
                        startPoint = intersectionPoint;
                    }
                }

                if (pos > supportInterval)
                {
                    if (Mathf.Abs(trackBanking) > iBeamBankingSwitch)
                    {
                        metalIBeamExtrude(startPoint, -1f * binormal, equalHeight ? Vector3.up : normal);
                        metalIBeamExtrude(endPoint, -1f * binormal, equalHeight ? Vector3.up : normal);
                        metalIBeamEnd();
                    }
                    else
                    {
                        float distance = 1 / Mathf.Sin((90 - Mathf.Abs(trackBanking)) * Mathf.Deg2Rad);
                        if (attachToStartPoint)
                        {
                            endPoint = startPoint - ((startPoint - endPoint) * distance);
                        }
                        else
                        {
                            startPoint = endPoint - ((endPoint - startPoint) * distance);
                        }
                        metalCrossTieExtrude(startPoint, -1f * binormal, equalHeight ? Vector3.up : normal);
                        metalCrossTieExtrude(endPoint, -1f * binormal, equalHeight ? Vector3.up : normal);
                        metalCrossTieEnd();
                    }
                }
            }
        }
        List <ShapeExtruder> metalShapeExtruders = new List <ShapeExtruder>();

        if (useTopperTrack)
        {
            metalShapeExtruders.Add(topperLeftRailExtruder);
            metalShapeExtruders.Add(topperRightRailExtruder);

            /*
             * metalShapeExtruders.Add(metalTopperCrossTie_1);
             * metalShapeExtruders.Add(metalTopperCrossTie_2);
             * metalShapeExtruders.Add(metalTopperCrossTie_3);
             * metalShapeExtruders.Add(metalTopperCrossTie_4);
             * metalShapeExtruders.Add(metalTopperCrossTie_5);
             * metalShapeExtruders.Add(metalTopperCrossTie_6);
             * metalShapeExtruders.Add(metalTopperCrossTie_7);
             * metalShapeExtruders.Add(metalTopperCrossTie_8);
             */
        }
        else
        {
            metalShapeExtruders.Add(iboxLeftRailExtruder);
            metalShapeExtruders.Add(iboxRightRailExtruder);
        }
        if (metalFrontCrossTieExtruder_1.vertices.Count > 0)
        {
            metalShapeExtruders.Add(metalFrontCrossTieExtruder_1);
            metalShapeExtruders.Add(metalFrontCrossTieExtruder_2);
            metalShapeExtruders.Add(metalFrontCrossTieExtruder_3);
            metalShapeExtruders.Add(metalRearCrossTieExtruder_1);
            metalShapeExtruders.Add(metalRearCrossTieExtruder_2);
            metalShapeExtruders.Add(metalRearCrossTieExtruder_3);
        }
        if (metalIBeamExtruder_1.vertices.Count > 0)
        {
            metalShapeExtruders.Add(metalIBeamExtruder_1);
            metalShapeExtruders.Add(metalIBeamExtruder_2);
            metalShapeExtruders.Add(metalIBeamExtruder_3);
        }
        foreach (ShapeExtruder extruder in metalShapeExtruders)
        {
            GameObject gameObject = new GameObject("metalObject");
            gameObject.transform.parent        = putMeshOnGO.transform;
            gameObject.transform.localPosition = Vector3.zero;
            gameObject.transform.localRotation = Quaternion.identity;
            MeshRenderer meshRenderer = gameObject.AddComponent <MeshRenderer>();
            meshRenderer.sharedMaterial = metalMaterial;
            MeshFilter meshFilter = gameObject.AddComponent <MeshFilter>();
            Mesh       mesh       = extruder.getMesh(putMeshOnGO.transform.worldToLocalMatrix);
            trackSegment.addGeneratedMesh(mesh);
            meshFilter.mesh = mesh;
        }
    }