Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to expand/contract path?
#4
Thanks, here's the code:

Code:
using System;
using UnityEngine;

namespace FluffyUnderware.Curvy.Generator.Modules
{
    [ModuleInfo("Modifier/Expand\u200A\u2215\u200AContract Path", ModuleName="Expand/Contract Path", Description="")]
    public class ModifierExpandContractPath : CGModule, IOnRequestProcessing, IPathProvider
    {
        [HideInInspector]
        [InputSlotInfo(typeof(CGPath), Name = "Input Path", ModifiesData = true)]
        public CGModuleInputSlot InPath = new CGModuleInputSlot();

        [HideInInspector]
        [OutputSlotInfo(typeof(CGPath), Name = "Modified Path")]
        public CGModuleOutputSlot OutPath = new CGModuleOutputSlot();

        #region ### Serialized Fields ###

        [SerializeField]
        [Tooltip("Distance of expansion/contraction. Negative values are ok.")]
        float m_Amount;

        [SerializeField]
        [Tooltip("Angle in degrees to spin the new path about the original path.")]
        float m_AngleOffset;

        #endregion

        #region ### Public Properties ###

        public float Amount
        {
            get => m_Amount;
            set
            {
                if (Math.Abs(m_Amount - value) > Mathf.Epsilon)
                {
                    m_Amount = value;
                    Dirty = true;
                }
            }
        }

        public float AngleOffset
        {
            get => m_AngleOffset;
            set
            {
                if (Math.Abs(m_AngleOffset - value) > Mathf.Epsilon)
                {
                    m_AngleOffset = value;
                    Dirty = true;
                }
            }
        }

        public bool PathIsClosed => IsConfigured && InPath.SourceSlot().PathProvider.PathIsClosed;

        #endregion

        #region ### Unity Callbacks ###

#if UNITY_EDITOR
        protected override void OnValidate()
        {
            base.OnValidate();
            Dirty = true;
        }
#endif

        public override void Reset()
        {
            base.Reset();
            Amount = 0;
            AngleOffset = 0;
        }

        #endregion


        static readonly Quaternion s_Right = Quaternion.Euler(0, -90, 0);

        #region ### IOnRequestProcessing ###

        public CGData[] OnSlotDataRequest(CGModuleInputSlot requestedBy, CGModuleOutputSlot requestedSlot, params CGDataRequestParameter[] requests)
        {
            CGData[] result;
            if (requestedSlot == OutPath)
            {
                CGPath data = InPath.GetData<CGPath>(out bool isDisposable, requests);
#if CURVY_SANITY_CHECKS
                Assert.IsTrue(data == null || isDisposable);
#endif

                if (data != null && m_Amount != 0)
                {
                    for (int i = 0; i < data.Count; i++)
                    {
                        // Direction is forward, so we need to rotate it to the right, we also spin it around by the angle offset
                        Quaternion rot = Quaternion.LookRotation(data.Directions.Array[i], data.Normals.Array[i]) *
                                        Quaternion.Euler(0, 0, 90+m_AngleOffset) * s_Right;

                        data.Positions.Array[i] += rot * (Vector3.forward * m_Amount);
                    }
                    data.Recalculate();
                }
                result = new CGData[] { data };
            }
            else
                result = null;

            return result;
        }

        #endregion
    }
}


Here is the editor script for it:
Code:
using UnityEditor;
using FluffyUnderware.Curvy.Generator.Modules;

namespace FluffyUnderware.CurvyEditor.Generator.Modules
{
    [CustomEditor(typeof(ModifierExpandContractPath))]
    public class ModifierExpandContractPathEditor : CGModuleEditor<ModifierExpandContractPath>
    {
    }
}
Reply


Messages In This Thread
How to expand/contract path? - by Ferdinand - 09-15-2022, 08:18 PM
RE: How to expand/contract path? - by Ferdinand - 09-17-2022, 02:30 PM
RE: How to expand/contract path? - by _Aka_ - 09-17-2022, 07:42 PM
RE: How to expand/contract path? - by Ferdinand - 09-17-2022, 08:39 PM
RE: How to expand/contract path? - by _Aka_ - 10-11-2022, 11:16 AM
RE: How to expand/contract path? - by _Aka_ - 10-12-2022, 02:31 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Wink Train carriage with 2 bogies following a path arcadeperfect 9 27 08-25-2023, 02:56 PM
Last Post: arcadeperfect
  Generator does not exactly follow the path F.A.L. 3 4 04-24-2023, 03:49 PM
Last Post: _Aka_
Question Spline path "Range" option: how to go from end to start CP? niuage 4 13 03-07-2022, 10:48 AM
Last Post: niuage
Question Generating a path from selected point to the beginning of spline on a closed spline? Zendariel 5 414 10-29-2021, 01:57 PM
Last Post: _Aka_

Forum Jump: