• Examples
  • [Runtime] Create Architect Curves with deformations

using System.Collections.Generic;

using UnityEngine;

using CurveArchitect;
using CurveArchitect.Objects;
using CurveArchitect.Utilities;

public class CreateArchitectCurves : MonoBehaviour
{
    public List<GameObject> prefabs;

    void Start()
    {
        //Anchor, Tangent A, Tangent B
        Vector3[] controlPoints = { new Vector3(0, 0, 0), new Vector3(0, 0, 3), new Vector3(0, 0, -3),
                                    new Vector3(0, 0, 10), new Vector3(0, 0, 13), new Vector3(0, 0, 7)};

        CreateArchitectCurve("Runtime ArchitectCurve", controlPoints);
    }

    ArchitectCurve CreateArchitectCurve(string name, Vector3[] controlPoints)
    {
        //Create GameObject that the ArchitectCurve component should be attached to.
        GameObject acGo = new GameObject();

        //Create Architect Curve component
        ArchitectCurve ac = HelperArchitectCurve.Create(acGo);
        ac.name = name;

        //Create segments for the Architect Curve
        for (int i = 0; i < controlPoints.Length; i += 3)
        {
            //Anchor
            Vector3 point1 = controlPoints[i];
            //Tangent A
            Vector3 point2 = controlPoints[i + 1];
            //Tangent B
            Vector3 point3 = controlPoints[i + 2];

            ac.CreateSegment(point1, point2, point3);
        }

        //Add the Architect Curve and all its Segments to the registry. Needed for link crossings to work.
        HandleRegistry.AddArchitectCurve(ac);
        HandleRegistry.AddSegments(ac.segements);

        //Set resolution values for the new Architect Curve. Higher numbers = uses more performence when moving the curve.
        //Default 750
        ac.SetResolutionLength(150);
        //Default 1250
        ac.SetResolutionLengthMap(300);

        //This calculations is only needed if you want to add deformations or followers during the same frame to the Architect Curve. Else you can skip this.
        HelperArchitectCurve.CalculateLengthMapAndLength(ac);
        HelperArchitectCurve.CalculateSegmentData(ac);
        HelperArchitectCurve.CaluclateControlpointBounds(ac);
        HelperArchitectCurve.CalculatePositionMap(ac, ac.positionMapLocal, Space.Self);
        //Only calculate normals for dynamic Architect Curves.
        if(ac.normalType == ArchitectCurve.NormalType.DYNAMIC)
        {
            HelperArchitectCurve.CalculateCachedNormals(ac);
        }

        //Create deformations or followers from a prefab.
        Vector3 position = new Vector3(0, 0, 0);
        foreach (GameObject p in prefabs)
        {
            //Create prefab for Deformation or Follower
            GameObject prefab = Instantiate(p);
            //Create deformation on prefab
            ArchitectCurveObject aco = HelperArchitectCurveObject.CreateDeformation(ac, prefab, position);
            //If you use primitive colliders do this. MeshColliders will be created during the CreateDeformation process.
            HelperArchitectCurveObject.TryAttachPrimitiveColliders(aco);
            position.z += 1;
        }

        return ac;
    }
}
MikeDanielsson changed the title to [Runtime] Create Architect Curves with deformations .