3636using System ;
3737using System . Collections . Generic ;
3838using System . Diagnostics ;
39+ using System . IO ;
3940using System . Linq ;
4041
4142namespace Orts . Viewer3D
@@ -89,8 +90,8 @@ public LightViewer(Viewer viewer, TrainCar car, TrainCarViewer carViewer)
8990 Viewer = viewer ;
9091 Car = car ;
9192 CarViewer = carViewer ;
92-
93- LightGlowMaterial = viewer . MaterialManager . Load ( "LightGlow" ) ;
93+
94+ LightGlowMaterial = viewer . MaterialManager . Load ( "LightGlow" , DefineFullTexturePath ( Car . Lights . GeneralLightGlowGraphic ) ) ;
9495 LightConeMaterial = viewer . MaterialManager . Load ( "LightCone" ) ;
9596
9697 UpdateState ( ) ;
@@ -102,6 +103,10 @@ public LightViewer(Viewer viewer, TrainCar car, TrainCarViewer carViewer)
102103 {
103104 case LightType . Glow :
104105 LightPrimitives . Add ( new LightGlowPrimitive ( this , Viewer . RenderProcess , light ) ) ;
106+ if ( light . Graphic != null )
107+ ( LightPrimitives . Last ( ) as LightGlowPrimitive ) . SpecificGlowMaterial = viewer . MaterialManager . Load ( "LightGlow" , DefineFullTexturePath ( light . Graphic , true ) ) ;
108+ else
109+ ( LightPrimitives . Last ( ) as LightGlowPrimitive ) . SpecificGlowMaterial = LightGlowMaterial ;
105110 break ;
106111 case LightType . Cone :
107112 LightPrimitives . Add ( new LightConePrimitive ( this , Viewer . RenderProcess , light ) ) ;
@@ -149,6 +154,17 @@ public LightViewer(Viewer viewer, TrainCar car, TrainCarViewer carViewer)
149154 UpdateActiveLightCone ( ) ;
150155 }
151156
157+ string DefineFullTexturePath ( string textureName , bool searchSpecificTexture = false )
158+ {
159+ if ( File . Exists ( Path . Combine ( Path . GetDirectoryName ( Car . WagFilePath ) , textureName ) ) )
160+ return Path . Combine ( Path . GetDirectoryName ( Car . WagFilePath ) , textureName ) ;
161+ if ( searchSpecificTexture )
162+ Trace . TraceWarning ( "Could not find light graphic {0} at {1}" , textureName , Path . Combine ( Path . GetDirectoryName ( Car . WagFilePath ) , textureName ) ) ;
163+ if ( File . Exists ( Path . Combine ( Viewer . ContentPath , textureName ) ) )
164+ return Path . Combine ( Viewer . ContentPath , textureName ) ;
165+ return Path . Combine ( Viewer . ContentPath , "LightGlow.png" ) ;
166+ }
167+
152168 void UpdateActiveLightCone ( )
153169 {
154170 var newLightCone = ( LightConePrimitive ) LightPrimitives . FirstOrDefault ( lm => lm is LightConePrimitive && lm . Enabled ) ;
@@ -220,9 +236,9 @@ public void PrepareFrame(RenderFrame frame, ElapsedTime elapsedTime)
220236 if ( ( lightPrimitive . Enabled || lightPrimitive . FadeOut ) && lightPrimitive is LightGlowPrimitive )
221237 {
222238 if ( ShapeXNATranslations . TryGetValue ( lightPrimitive . Light . ShapeIndex , out Matrix lightMatrix ) )
223- frame . AddPrimitive ( LightGlowMaterial , lightPrimitive , RenderPrimitiveGroup . Lights , ref lightMatrix ) ;
239+ frame . AddPrimitive ( ( lightPrimitive as LightGlowPrimitive ) . SpecificGlowMaterial , lightPrimitive , RenderPrimitiveGroup . Lights , ref lightMatrix ) ;
224240 else
225- frame . AddPrimitive ( LightGlowMaterial , lightPrimitive , RenderPrimitiveGroup . Lights , ref xnaDTileTranslation ) ;
241+ frame . AddPrimitive ( ( lightPrimitive as LightGlowPrimitive ) . SpecificGlowMaterial , lightPrimitive , RenderPrimitiveGroup . Lights , ref xnaDTileTranslation ) ;
226242 }
227243
228244#if DEBUG_LIGHT_CONE
@@ -252,6 +268,11 @@ public void Mark()
252268 {
253269 LightGlowMaterial . Mark ( ) ;
254270 LightConeMaterial . Mark ( ) ;
271+ foreach ( var lightPrimitive in LightPrimitives )
272+ if ( lightPrimitive is LightGlowPrimitive && lightPrimitive . Light . Graphic != null )
273+ {
274+ ( lightPrimitive as LightGlowPrimitive ) . SpecificGlowMaterial . Mark ( ) ;
275+ }
255276 }
256277
257278 public static void CalculateLightCone ( LightState lightState , out Vector3 position , out Vector3 direction , out float angle , out float radius , out float distance , out Vector4 color )
@@ -680,6 +701,7 @@ public class LightGlowPrimitive : LightPrimitive
680701 static VertexDeclaration VertexDeclaration ;
681702 VertexBuffer VertexBuffer ;
682703 static IndexBuffer IndexBuffer ;
704+ public Material SpecificGlowMaterial ;
683705
684706 public LightGlowPrimitive ( LightViewer lightViewer , RenderProcess renderProcess , Light light )
685707 : base ( light )
@@ -932,11 +954,11 @@ public class LightGlowMaterial : Material
932954 {
933955 readonly Texture2D LightGlowTexture ;
934956
935- public LightGlowMaterial ( Viewer viewer )
936- : base ( viewer , null )
957+ public LightGlowMaterial ( Viewer viewer , string textureName )
958+ : base ( viewer , textureName )
937959 {
938960 // TODO: This should happen on the loader thread.
939- LightGlowTexture = SharedTextureManager . Get ( Viewer . RenderProcess . GraphicsDevice , System . IO . Path . Combine ( Viewer . ContentPath , "Lightglow.png" ) ) ;
961+ LightGlowTexture = SharedTextureManager . Get ( Viewer . RenderProcess . GraphicsDevice , textureName ) ;
940962 }
941963
942964 public override void SetState ( GraphicsDevice graphicsDevice , Material previousMaterial )
0 commit comments