Exemplo n.º 1
0
    public RailPathGenerator(TerrainGenerator terrain)
    {
        Terrain      = terrain;
        CurrentAngle = 0f;

        CurrentPoint = new RailPathPoint(new Vector3(0f, Terrain.GetElevation(new Vector2(0f, 0f)), 0f), null);
        PathPoints.Add(CurrentPoint);
        RailSettings = new RailSettings(plankWidth: 3f, plankHeight: 0.2f, trackWidth: 0.1f, trackGap: 1.5f, trackHeight: 0.2f);
    }
Exemplo n.º 2
0
    public void Init(RailPathPoint from, RailPathPoint to, RailSettings settings)
    {
        FromPoint = from;
        ToPoint   = to;
        Settings  = settings;

        SegmentVector = ToPoint.Position - FromPoint.Position;

        Angle = Vector2.SignedAngle(new Vector2(SegmentVector.x, SegmentVector.z), Vector2.up);
        Angle = (Angle > 180) ? Angle + 360 : Angle;

        FromPoint.Segments.Add(this);
        ToPoint.Segments.Add(this);
        ConnectedSegments = new List <RailSegment>();
    }
Exemplo n.º 3
0
    private Vector3 GetWheelPosition(WheelPosition position)
    {
        RailPathPosition wheelPosition = null;

        if (position == WheelPosition.FrontLeft || position == WheelPosition.FrontRight)
        {
            wheelPosition = Train.GetBackwardsPathPosition(RailPosition, WheelInset);
        }
        if (position == WheelPosition.RearLeft || position == WheelPosition.RearRight)
        {
            wheelPosition = Train.GetBackwardsPathPosition(RailPosition, Length - WheelInset);
        }

        Vector3 wheelCenterPosition = wheelPosition.Segment.GetWorldPositionAtDistance(wheelPosition.Distance);

        RailSettings railSettings = wheelPosition.Segment.Settings;
        float        height       = railSettings.TrackHeight + railSettings.PlankHeight + WheelHeight / 2;

        float wheelDistance = railSettings.TrackGap / 2;
        float wheelAngle    = 0f;

        if (position == WheelPosition.FrontLeft || position == WheelPosition.RearLeft)
        {
            wheelAngle = wheelPosition.Segment.Angle + 90;
        }
        if (position == WheelPosition.FrontRight || position == WheelPosition.RearRight)
        {
            wheelAngle = wheelPosition.Segment.Angle - 90;
        }

        float   offsetX     = Mathf.Sin(Mathf.Deg2Rad * wheelAngle) * wheelDistance;
        float   offsetZ     = Mathf.Cos(Mathf.Deg2Rad * wheelAngle) * wheelDistance;
        Vector3 wheelOffset = new Vector3(offsetX, height, offsetZ);

        return(wheelCenterPosition + wheelOffset);
    }
    private static void GeneratePlank(GameObject segmentObject, RailPathPoint p1, RailPathPoint p2, RailSettings settings, Vector3 toVector, Vector3 toVectorPpc)
    {
        GameObject plankObject = MeshGenerator.CreateEmptyObject("Plank", "Ground", segmentObject.transform);

        Vector3[] vertices  = new Vector3[0];
        int[]     triangles = new int[0];

        Vector3 frontOrigin  = p1.Position + toVector / 3f;
        Vector3 backOrigin   = p1.Position + toVector / 3f * 2f;
        Vector3 heightOffset = new Vector3(0f, settings.PlankHeight, 0f);

        Vector3 leftBackTop   = backOrigin - (toVectorPpc * settings.PlankWidth / 2f) + heightOffset;
        Vector3 rightBackTop  = backOrigin + (toVectorPpc * settings.PlankWidth / 2f) + heightOffset;
        Vector3 rightFrontTop = frontOrigin + (toVectorPpc * settings.PlankWidth / 2f) + heightOffset;
        Vector3 leftFrontTop  = frontOrigin - (toVectorPpc * settings.PlankWidth / 2f) + heightOffset;

        Vector3 leftBackBot   = backOrigin - (toVectorPpc * settings.PlankWidth / 2f);
        Vector3 rightBackBot  = backOrigin + (toVectorPpc * settings.PlankWidth / 2f);
        Vector3 rightFrontBot = frontOrigin + (toVectorPpc * settings.PlankWidth / 2f);
        Vector3 leftFrontBot  = frontOrigin - (toVectorPpc * settings.PlankWidth / 2f);

        MeshGenerator.AddCuboid(ref vertices, ref triangles, leftBackTop, rightBackTop, rightFrontTop, leftFrontTop, leftBackBot, rightBackBot, rightFrontBot, leftFrontBot);
        MeshGenerator.ApplyMesh(plankObject, vertices, triangles, MaterialHandler.Instance.RailPlankColor);
    }
    public static RailSegment GenerateRailSegment(GameObject railPathObject, RailPathPoint p1, RailPathPoint p2, RailSettings settings)
    {
        GameObject railSegmentObject = new GameObject("RailSegment");

        railSegmentObject.transform.SetParent(railPathObject.transform);
        RailSegment railSegment = railSegmentObject.AddComponent <RailSegment>();

        Vector3 toVector    = p2.Position - p1.Position;
        Vector3 toVectorPpc = (Quaternion.Euler(0, 90, 0) * new Vector3(toVector.x, 0f, toVector.z)).normalized; // PPC = perpendicular (90°)

        Vector3 fromVector    = p1.PreviousPoint != null ? p1.Position - p1.PreviousPoint.Position : toVector;
        Vector3 fromVectorPpc = (Quaternion.Euler(0, 90, 0) * new Vector3(fromVector.x, 0f, fromVector.z)).normalized;

        float offsetLeft  = -(settings.TrackGap / 2 + settings.TrackWidth);
        float offsetRight = settings.TrackGap / 2;

        GenerateTrack(railSegmentObject, p1, p2, settings, fromVectorPpc, toVectorPpc, offsetLeft);
        GenerateTrack(railSegmentObject, p1, p2, settings, fromVectorPpc, toVectorPpc, offsetRight);
        GeneratePlank(railSegmentObject, p1, p2, settings, toVector, toVectorPpc);

        return(railSegment);
    }
    private static void GenerateTrack(GameObject segmentObject, RailPathPoint p1, RailPathPoint p2, RailSettings settings, Vector3 fromVectorPpc, Vector3 toVectorPpc, float offset)
    {
        GameObject trackObject = new GameObject("Track");

        trackObject.layer = LayerMask.NameToLayer("Ground");
        trackObject.transform.SetParent(segmentObject.transform);
        MeshFilter   meshFilter   = trackObject.AddComponent <MeshFilter>();
        MeshRenderer meshRenderer = trackObject.AddComponent <MeshRenderer>();

        Vector3[] vertices  = new Vector3[0];
        int[]     triangles = new int[0];

        Vector3 heightOffsetUpper = new Vector3(0f, settings.TrackHeight + settings.PlankHeight, 0f);
        Vector3 heightOffsetLower = new Vector3(0f, settings.PlankHeight, 0f);

        // Left Side
        List <Vector3> leftSide = new List <Vector3>()
        {
            p2.Position + (toVectorPpc * offset) + heightOffsetUpper,
            p1.Position + (fromVectorPpc * offset) + heightOffsetUpper,
            p1.Position + (fromVectorPpc * offset) + heightOffsetLower,
            p2.Position + (toVectorPpc * offset) + heightOffsetLower,
        };

        MeshGenerator.AddPlane(ref vertices, ref triangles, leftSide);

        // Right Side
        List <Vector3> rightSide = new List <Vector3>()
        {
            p1.Position + (fromVectorPpc * (offset + settings.TrackWidth)) + heightOffsetUpper,
            p2.Position + (toVectorPpc * (offset + settings.TrackWidth)) + heightOffsetUpper,
            p2.Position + (toVectorPpc * (offset + settings.TrackWidth)) + heightOffsetLower,
            p1.Position + (fromVectorPpc * (offset + settings.TrackWidth)) + heightOffsetLower,
        };

        MeshGenerator.AddPlane(ref vertices, ref triangles, rightSide);

        // Top Side
        List <Vector3> topSide = new List <Vector3>()
        {
            p1.Position + (fromVectorPpc * offset) + heightOffsetUpper,
            p2.Position + (toVectorPpc * offset) + heightOffsetUpper,
            p2.Position + (toVectorPpc * (offset + settings.TrackWidth)) + heightOffsetUpper,
            p1.Position + (fromVectorPpc * (offset + settings.TrackWidth)) + heightOffsetUpper,
        };

        MeshGenerator.AddPlane(ref vertices, ref triangles, topSide);


        meshFilter.mesh.vertices  = vertices;
        meshFilter.mesh.triangles = triangles;
        meshFilter.mesh.RecalculateNormals();
        meshRenderer.material       = MaterialHandler.Instance.DefaultMaterial;
        meshRenderer.material.color = MaterialHandler.Instance.RailTrackColor;
        trackObject.AddComponent <MeshCollider>();
    }