Hey Aka, Thanks for your reply. I ended up fixing it myself - it was my bad use of the SubArray<> class where I was using spots.Spots.Array.Count instead of spots.Spots.Count - it was accessing the unused, but allocated elements of the array
For anyone who may want it in the future, here is the fixed code. I note that I had to change the original volume spots generation to use full rotation so the spots are generated at the correct angle for this filter to check their angle, then this module compares the angle to the horizon, filters spots based on that, and then sets the spots to default rotation so they always face the ground.
For anyone who may want it in the future, here is the fixed code. I note that I had to change the original volume spots generation to use full rotation so the spots are generated at the correct angle for this filter to check their angle, then this module compares the angle to the horizon, filters spots based on that, and then sets the spots to default rotation so they always face the ground.
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>();
for (int i=0; i<spots.Spots.Count; i++)
{
CGSpot spot = spots.Spots.Array[i];
Vector3 angle = spot.Rotation * Vector3.up;
float dot = Vector3.Dot(Vector3.up, angle);
if (dot > 0)
{
spot.Rotation = Quaternion.identity;
outSpots.Add(spot);
}
}
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()
//{
// return base.DeleteAllOutputManagedResources();
//}
#endregion
}
}