Search Unity

Question How to dynamically create smooth cylinders using the spline function

Discussion in 'World Building' started by MTprn82, Dec 13, 2022.

  1. MTprn82

    MTprn82

    Joined:
    Mar 17, 2022
    Posts:
    6
    Hi,

    I asked the same question in "Scripting", but I found this one and am posting it again.

    I'm trying to create a mesh by dynamically inputting coordinate values using the spline function.
    Mesh creation is successful, but does not result in a Catmull Rom curve.

    I made the following code.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using UnityEngine;
    5. using UnityEngine.Splines;
    6.  
    7. public class CreateSpline : MonoBehaviour
    8. {
    9.     private SplineContainer _splineContainer;
    10.     private SplineExtrude _splineExtrude;
    11.     private Spline _spline;
    12.  
    13.     [SerializeField] GameObject _point1;
    14.     [SerializeField] GameObject _point2;
    15.     [SerializeField] GameObject _point3;
    16.  
    17.     [SerializeField]
    18.     bool m_RebuildExtrudeOnUpdate = true;
    19.  
    20.     [SerializeField, Range(0.1f, 10f)] private float _radius = 0.1f;
    21.     public float Radius
    22.     {
    23.         set
    24.         {
    25.             _splineExtrude.radius = value;
    26.             _splineExtrude.Rebuild();
    27.         }
    28.     }
    29.  
    30.     BezierKnot[] m_Origins;
    31.  
    32.     // Start is called before the first frame update
    33.     void Start()
    34.     {
    35.      
    36.         // Create Spline using 3 Spheres positions.
    37.         _splineContainer = this.GetComponent<SplineContainer>();
    38.         _spline = _splineContainer.Spline;
    39.         _spline.Clear();
    40.         _spline.EditType = SplineType.CatmullRom;
    41.         _spline.Add(new BezierKnot(_point1.transform.position));
    42.         _spline.Add(new BezierKnot(_point2.transform.position));
    43.         _spline.Add(new BezierKnot(_point3.transform.position));
    44.         _spline.Closed = false;
    45.         _splineContainer.Spline = _spline;
    46.  
    47.         // Create Extrude Dynamic.
    48.         _splineExtrude = this.GetComponent<SplineExtrude>();
    49.         _splineExtrude.container = _splineContainer;
    50.         _splineExtrude.Rebuild();
    51.  
    52.         this.GetComponent<MeshRenderer>().enabled = true;
    53.         m_Origins = _spline.Knots.ToArray();
    54.     }
    55.  
    56.     // Update is called once per frame
    57.     void Update()
    58.     {
    59.         this.Radius = _radius;
    60.  
    61.         // Recreate Spline.
    62.         if (Input.GetKeyDown(KeyCode.Space))
    63.         {
    64.             _spline.Clear();
    65.             _spline.EditType = SplineType.CatmullRom;
    66.             _spline.Add(new BezierKnot(_point1.transform.position));
    67.             _spline.Add(new BezierKnot(_point2.transform.position));
    68.             _spline.Add(new BezierKnot(_point3.transform.position));
    69.             //_spline.Closed = true;
    70.             //_splineExtrude.Rebuild();
    71.         }
    72.  
    73.         // Down a point position.
    74.         if (Input.GetKeyDown(KeyCode.A))
    75.         {
    76.             _spline[1] += new Vector3(0f, -1f, 0f);
    77.         }
    78.     }
    79. }

    Set EditType to CatmullRom to dynamically create the mesh.
    However, the mesh is straight (now.png) .
    If I move the Knot a little in the Scene view, I get a smooth mesh (goal.png) .

    How can I create a smooth mesh using only a script?
    Thanks.
     

    Attached Files:

    • now.png
      now.png
      File size:
      79.7 KB
      Views:
      122
    • goal.png
      goal.png
      File size:
      74.9 KB
      Views:
      123
    SamoukOndra likes this.