03-09-2023, 05:41 PM
Hey,
Trying to write a custom generator node that takes in a set of spots, applies a filter function, and then outputs the new modified list.
In my case, I am filtering based on the rotation to prevent poles on my rollercoaster from connecting to inverted pieces of track.
It seemed to work well when I was using a small test track, but scaling it up to larger tracks I am getting crazy results where its adding hundreds of spots that should not have even been in the original input list? Probably I'm doing something wrong in the node.
Heres my code:
Trying to write a custom generator node that takes in a set of spots, applies a filter function, and then outputs the new modified list.
In my case, I am filtering based on the rotation to prevent poles on my rollercoaster from connecting to inverted pieces of track.
It seemed to work well when I was using a small test track, but scaling it up to larger tracks I am getting crazy results where its adding hundreds of spots that should not have even been in the original input list? Probably I'm doing something wrong in the node.
Heres my code:
Code:
using UnityEngine;
using System.Collections;
using FluffyUnderware.DevTools;
using System.Collections.Generic;
namespace FluffyUnderware.Curvy.Generator.Modules
{
[ModuleInfo("Custom/Spot Angle Filter",ModuleName="Spot Angle Filter", Description="Filter spots based on their rotation")]
public class SpotAngleFilter : CGModule
{
[HideInInspector]
[InputSlotInfo(typeof(CGSpots), Name = "Spots", DisplayName = "Spots")]
public CGModuleInputSlot InData = new CGModuleInputSlot();
[HideInInspector]
[OutputSlotInfo(typeof(CGSpots), Name = "Spots", DisplayName = "Spots")]
public CGModuleOutputSlot OutData = new CGModuleOutputSlot();
#region ### Serialized Fields ###
#endregion
#region ### Public Properties ###
// Gets whether the module is properly configured i.e. has everything needed to work like intended
public override bool IsConfigured
{
get
{
return base.IsConfigured;
}
}
// Gets whether the module and all its dependencies are fully initialized
public override bool IsInitialized
{
get
{
return base.IsInitialized;
}
}
#endregion
#region ### Unity Callbacks ###
/*! \cond UNITY */
protected override void OnEnable()
{
base.OnEnable();
//Properties.MinWidth = 250;
//Properties.LabelWidth = 80;
}
#if UNITY_EDITOR
protected override void OnValidate()
{
base.OnValidate();
}
#endif
public override void Reset()
{
base.Reset();
}
/*! \endcond */
#endregion
#region ### Module Overrides ###
public override void Refresh()
{
base.Refresh();
/* Add Module processing code in here */
CGSpots spots = InData.GetData<CGSpots>();
List<CGSpot> outSpots = new List<CGSpot>();
foreach (CGSpot spot in spots.Spots.Array)
{
Vector3 angle = spot.Rotation * Vector3.forward;
float dot = Vector3.Dot(Vector3.forward, angle);
if (dot > 0)
{
outSpots.Add(spot);
//outSpots.Add(new CGSpot(outSpots.Count, spot.Position, spot.Rotation, spot.Scale)); // doesn't work..
}
}
OutData.ClearData();
OutData.SetData(new CGSpots(outSpots));
}
// Called when a module's state changes (Link added/removed, Active toggles etc..)
//public override void OnStateChange()
//{
// base.OnStateChange();
//}
// Called after a module was copied to a template
//public override void OnTemplateCreated()
//{
// base.OnTemplateCreated();
//}
//Called in multiple situations to clear all the outputs generated by this module. One example is when you click on the "Clear output" button in the Curvy Generator's toolbar
public override bool DeleteAllOutputManagedResources()
{
OutData.ClearData();
return base.DeleteAllOutputManagedResources();
}
#endregion
}
}