@@ -13,18 +13,16 @@ namespace PAPIPlugin.Arrays
1313{
1414 public class PAPIArray : AbstractLightArray , IConfigNode
1515 {
16- private const int PAPIPartCount = 4 ;
16+ public const int DefaultPartCount = 4 ;
1717
18- private const float PAPILightRadius = 10.0f ;
18+ public const float DefaultLightRadius = 10.0f ;
1919
20- private const double DefaultTargetGlidePath = 6 ;
20+ public const double DefaultTargetGlidePath = 6 ;
2121
2222 /// <summary>
2323 /// If the difference of the gliepath from the target is more than this the whole array will show either red or white.
2424 /// </summary>
25- private const double DefaultBadGlidepathVariance = 1.5 ;
26-
27- private static readonly Vector3 PAPILightDifference = Vector3 . right * PAPILightRadius * 1.5f ;
25+ public const double DefaultGlideslopeTolerance = 1.5 ;
2826
2927 private GameObject _papiGameObject ;
3028
@@ -34,8 +32,8 @@ public class PAPIArray : AbstractLightArray, IConfigNode
3432
3533 public PAPIArray ( )
3634 {
37- TargetGlidePath = DefaultTargetGlidePath ;
38- BadGlidepathVariance = DefaultBadGlidepathVariance ;
35+ TargetGlideslope = DefaultTargetGlidePath ;
36+ GlideslopeTolerance = DefaultGlideslopeTolerance ;
3937
4038 EnabledChanged += ( sender , args ) =>
4139 {
@@ -51,22 +49,34 @@ public PAPIArray()
5149 } ;
5250 }
5351
54- public double BadGlidepathVariance { get ; set ; }
52+ public double GlideslopeTolerance { get ; set ; }
5553
56- public double TargetGlidePath { get ; set ; }
54+ public double TargetGlideslope { get ; set ; }
5755
5856 public double Longitude { get ; set ; }
5957
6058 public double Latitude { get ; set ; }
6159
6260 public double Heading { get ; set ; }
6361
62+ public double HeightAboveTerrain { get ; set ; }
63+
64+ public int PartCount { get ; set ; }
65+
66+ public float LightRadius { get ; set ; }
67+
68+ public float LightDistance { get ; set ; }
69+
6470 #region IConfigNode Members
6571
6672 public void Load ( ConfigNode node )
6773 {
68- BadGlidepathVariance = node . ConvertValue ( "BadGlidepath" , DefaultBadGlidepathVariance ) ;
69- TargetGlidePath = node . ConvertValue ( "TargetGlidepath" , DefaultTargetGlidePath ) ;
74+ GlideslopeTolerance = node . ConvertValue ( "GlideslopeTolerance" , DefaultGlideslopeTolerance ) ;
75+ TargetGlideslope = node . ConvertValue ( "TargetGlideslope" , DefaultTargetGlidePath ) ;
76+ HeightAboveTerrain = node . ConvertValue ( "Height" , 0 ) ;
77+ PartCount = node . ConvertValue ( "PartCount" , DefaultPartCount ) ;
78+ LightRadius = node . ConvertValue ( "LightRadius" , DefaultLightRadius ) ;
79+ LightDistance = node . ConvertValue ( "LightDistance" , LightRadius * 0.5f ) ;
7080
7181 try
7282 {
@@ -110,6 +120,11 @@ public override void Update()
110120
111121 var currentCamera = Camera . main ;
112122
123+ if ( currentCamera == null )
124+ {
125+ return ;
126+ }
127+
113128 var relativePosition = _papiGameObject . transform . InverseTransformPoint ( currentCamera . transform . position ) ;
114129
115130 var normalizedPosition = relativePosition . normalized ;
@@ -120,9 +135,9 @@ public override void Update()
120135
121136 var angle = 90 - Math . Acos ( normalDot ) * ( 180 / Math . PI ) ;
122137
123- var difference = angle - TargetGlidePath ;
138+ var difference = angle - TargetGlideslope ;
124139
125- for ( var i = 0 ; i < PAPIPartCount ; i ++ )
140+ for ( var i = 0 ; i < PartCount ; i ++ )
126141 {
127142 if ( directionDot <= 0 )
128143 {
@@ -184,15 +199,13 @@ private void InitializePAPIParts(double lat, double lon, double heading)
184199 _papiGameObject . transform . localRotation = Quaternion . LookRotation ( headingVector , surfaceNormal ) ;
185200
186201 var maxHeight = double . MinValue ;
187- _partObjects = new GameObject [ PAPIPartCount ] ;
188- for ( var i = 0 ; i < PAPIPartCount ; i ++ )
202+ _partObjects = new GameObject [ PartCount ] ;
203+ for ( var i = 0 ; i < PartCount ; i ++ )
189204 {
190- var obj = new GameObject ( ) ;
191-
192- AddPAPIPart ( obj ) ;
205+ var obj = AddPAPIPart ( ) ;
193206
194207 obj . transform . parent = _papiGameObject . transform ;
195- obj . transform . localPosition = ( i - ( PAPIPartCount / 2 ) ) * PAPILightDifference ;
208+ obj . transform . localPosition = GetLocalLighPosition ( i ) ;
196209
197210 maxHeight = Math . Max ( maxHeight , parentBody . GetSurfaceHeight ( Latitude , Longitude ) ) ;
198211
@@ -201,10 +214,26 @@ private void InitializePAPIParts(double lat, double lon, double heading)
201214
202215 maxHeight = Math . Max ( 0 , maxHeight ) ;
203216 _relativeSurfacePosition =
204- parentBody . transform . InverseTransformPoint ( parentBody . GetWorldSurfacePosition ( lat , lon , maxHeight + PAPILightRadius * 0.5 ) ) ;
217+ parentBody . transform . InverseTransformPoint ( parentBody . GetWorldSurfacePosition ( lat , lon , maxHeight + HeightAboveTerrain + LightRadius ) ) ;
205218 _papiGameObject . transform . localPosition = _relativeSurfacePosition ;
206219 }
207220
221+ /// <summary>
222+ /// Gets the local position given a zero-based index.
223+ /// </summary>
224+ /// <param name="i">The index of the light, zero-based</param>
225+ /// <returns>A local position specifying the light position</returns>
226+ private Vector3 GetLocalLighPosition ( int i )
227+ {
228+ var countHalf = PartCount / 2.0 ;
229+
230+ var offsetMult = ( float ) ( i - countHalf - 0.5 ) ;
231+
232+ var distance = LightRadius + LightDistance ;
233+
234+ return Vector3 . right * offsetMult * distance ;
235+ }
236+
208237 private static Vector3d Orthonormalise ( Vector3d direction , Vector3d firstVector )
209238 {
210239 // This is basically the first step of a Gram–Schmidt process
@@ -213,48 +242,46 @@ private static Vector3d Orthonormalise(Vector3d direction, Vector3d firstVector)
213242 return direction - Vector3d . Dot ( firstVector , direction ) * firstVector ;
214243 }
215244
216- private static void AddPAPIPart ( GameObject obj )
245+ private GameObject AddPAPIPart ( )
217246 {
218- var lineRenderer = obj . AddComponent < LineRenderer > ( ) ;
219-
220- lineRenderer . useWorldSpace = false ;
221- lineRenderer . transform . parent = obj . transform ;
222- lineRenderer . transform . localPosition = Vector3 . zero ;
223- lineRenderer . transform . eulerAngles = Vector3 . zero ;
224-
225- lineRenderer . material = new Material ( Shader . Find ( "Particles/Additive" ) ) ;
226- lineRenderer . SetColors ( Color . red , Color . red ) ;
227- lineRenderer . SetWidth ( PAPILightRadius , PAPILightRadius ) ;
228- lineRenderer . SetVertexCount ( 2 ) ;
229- lineRenderer . SetPosition ( 0 , Vector3 . zero ) ;
230- lineRenderer . SetPosition ( 1 , Vector3 . up * PAPILightRadius ) ;
247+ var obj = GameObject . CreatePrimitive ( PrimitiveType . Sphere ) ;
248+
249+ var material = new Material ( Shader . Find ( "Particles/Additive" ) ) ;
250+ obj . renderer . sharedMaterial = material ;
251+
252+ obj . transform . localScale = new Vector3 ( LightRadius , LightRadius , LightRadius ) ;
253+
254+ var sphereCollider = obj . GetComponent < SphereCollider > ( ) ;
255+ sphereCollider . enabled = false ;
256+
257+ return obj ;
231258 }
232259
233260 private void UpdatePAPIPart ( int index , double difference , float alpha )
234261 {
235262 var gameObj = _partObjects [ index ] ;
236263
237- var lineRenderer = gameObj . GetComponent < LineRenderer > ( ) ;
238-
239264 var color = GetArrayPartColor ( index , difference ) ;
240265 color . a = alpha ;
241- lineRenderer . SetColors ( color , color ) ;
266+
267+ gameObj . renderer . material . SetColor ( "_TintColor" , color ) ;
242268 }
243269
244270 private Color GetArrayPartColor ( int index , double difference )
245271 {
246- if ( difference < - BadGlidepathVariance )
272+ if ( difference < - GlideslopeTolerance )
247273 {
248274 return Color . red ;
249275 }
250- if ( difference > BadGlidepathVariance )
276+ if ( difference > GlideslopeTolerance )
251277 {
252278 return Color . white ;
253279 }
254280
255281 // This should map temp into [-1, 1]
256- double temp = index - ( PAPIPartCount / 2 ) ;
257- temp = temp / ( PAPIPartCount / 2 ) ;
282+ double temp = index - ( PartCount / 2 ) ;
283+ // ReSharper disable once PossibleLossOfFraction
284+ temp = temp / ( PartCount / 2 ) ;
258285
259286 return temp > difference ? Color . red : Color . white ;
260287 }
0 commit comments