11/*
2- * Copyright (c) 2009-2024 jMonkeyEngine
2+ * Copyright (c) 2009-2025 jMonkeyEngine
33 * All rights reserved.
44 *
55 * Redistribution and use in source and binary forms, with or without
7474 * abstract shadow renderer that holds commons feature to have for a shadow
7575 * renderer
7676 *
77- * @author Rémy Bouquet aka Nehon
77+ * @author Nehon
7878 */
79- public abstract class AbstractShadowRenderer implements SceneProcessor , Savable , JmeCloneable , Cloneable {
79+ public abstract class AbstractShadowRenderer implements SceneProcessor , Savable , JmeCloneable {
8080
8181 protected static final Logger logger = Logger .getLogger (AbstractShadowRenderer .class .getName ());
8282 private static final LightFilter NULL_LIGHT_FILTER = new NullLightFilter ();
@@ -100,6 +100,7 @@ public abstract class AbstractShadowRenderer implements SceneProcessor, Savable,
100100 protected RenderState forcedRenderState = new RenderState ();
101101 protected boolean renderBackFacesShadows = true ;
102102 protected AppProfiler prof ;
103+ protected boolean debugfrustums = false ;
103104
104105 /**
105106 * true if the fallback material should be used, otherwise false
@@ -130,24 +131,23 @@ public abstract class AbstractShadowRenderer implements SceneProcessor, Savable,
130131 protected boolean skipPostPass ;
131132
132133 /**
133- * used for serialization
134+ * For serialization only. Do not use.
134135 */
135136 protected AbstractShadowRenderer () {
136137 }
137138
138139 /**
139- * Create an abstract shadow renderer . Subclasses invoke this constructor.
140+ * Creates an AbstractShadowRenderer . Subclasses invoke this constructor.
140141 *
141- * @param assetManager the application asset manager
142- * @param shadowMapSize the size of the rendered shadow maps (512,1024,2048,
143- * etc...)
144- * @param nbShadowMaps the number of shadow maps rendered (the more shadow
145- * maps the more quality, the fewer fps).
142+ * @param assetManager The application's asset manager.
143+ * @param shadowMapSize The size of the rendered shadow maps (e.g., 512, 1024, 2048).
144+ * @param nbShadowMaps The number of shadow maps to render (1 to 4). More maps
145+ * improve quality but can reduce performance.
146146 */
147147 protected AbstractShadowRenderer (AssetManager assetManager , int shadowMapSize , int nbShadowMaps ) {
148148 this .assetManager = assetManager ;
149- this .nbShadowMaps = nbShadowMaps ;
150149 this .shadowMapSize = shadowMapSize ;
150+ this .nbShadowMaps = nbShadowMaps ;
151151 init (assetManager , nbShadowMaps , shadowMapSize );
152152 }
153153
@@ -301,32 +301,33 @@ public CompareMode getShadowCompareMode() {
301301 */
302302 protected Geometry createFrustum (Vector3f [] pts , int i ) {
303303 WireFrustum frustum = new WireFrustum (pts );
304- Geometry frustumMdl = new Geometry ("f" , frustum );
305- frustumMdl .setCullHint (Spatial .CullHint .Never );
306- frustumMdl .setShadowMode (ShadowMode .Off );
304+ Geometry geo = new Geometry ("WireFrustum" + i , frustum );
307305 Material mat = new Material (assetManager , "Common/MatDefs/Misc/Unshaded.j3md" );
308306 mat .getAdditionalRenderState ().setWireframe (true );
309- frustumMdl .setMaterial (mat );
307+ geo .setMaterial (mat );
308+ geo .setCullHint (Spatial .CullHint .Never );
309+ geo .setShadowMode (ShadowMode .Off );
310+
310311 switch (i ) {
311312 case 0 :
312- frustumMdl . getMaterial () .setColor ("Color" , ColorRGBA .Pink );
313+ mat .setColor ("Color" , ColorRGBA .Pink );
313314 break ;
314315 case 1 :
315- frustumMdl . getMaterial () .setColor ("Color" , ColorRGBA .Red );
316+ mat .setColor ("Color" , ColorRGBA .Red );
316317 break ;
317318 case 2 :
318- frustumMdl . getMaterial () .setColor ("Color" , ColorRGBA .Green );
319+ mat .setColor ("Color" , ColorRGBA .Green );
319320 break ;
320321 case 3 :
321- frustumMdl . getMaterial () .setColor ("Color" , ColorRGBA .Blue );
322+ mat .setColor ("Color" , ColorRGBA .Blue );
322323 break ;
323324 default :
324- frustumMdl . getMaterial () .setColor ("Color" , ColorRGBA .White );
325+ mat .setColor ("Color" , ColorRGBA .White );
325326 break ;
326327 }
327328
328- frustumMdl .updateGeometricState ();
329- return frustumMdl ;
329+ geo .updateGeometricState ();
330+ return geo ;
330331 }
331332
332333 /**
@@ -340,7 +341,7 @@ public void initialize(RenderManager rm, ViewPort vp) {
340341 renderManager = rm ;
341342 viewPort = vp ;
342343 postTechniqueName = "PostShadow" ;
343- if (zFarOverride > 0 && frustumCam == null ){
344+ if (zFarOverride > 0 && frustumCam == null ) {
344345 initFrustumCam ();
345346 }
346347 }
@@ -396,7 +397,6 @@ public boolean isInitialized() {
396397 protected void doDisplayFrustumDebug (int shadowMapIndex ) {
397398 }
398399
399- @ SuppressWarnings ("fallthrough" )
400400 @ Override
401401 public void postQueue (RenderQueue rq ) {
402402 lightReceivers .clear ();
@@ -413,13 +413,11 @@ public void postQueue(RenderQueue rq) {
413413 renderManager .setForcedTechnique ("PreShadow" );
414414
415415 for (int shadowMapIndex = 0 ; shadowMapIndex < nbShadowMaps ; shadowMapIndex ++) {
416-
417- if (debugfrustums ) {
418- doDisplayFrustumDebug (shadowMapIndex );
419- }
420- renderShadowMap (shadowMapIndex );
421-
416+ if (debugfrustums ) {
417+ doDisplayFrustumDebug (shadowMapIndex );
422418 }
419+ renderShadowMap (shadowMapIndex );
420+ }
423421
424422 debugfrustums = false ;
425423
@@ -450,8 +448,6 @@ protected void renderShadowMap(int shadowMapIndex) {
450448 renderManager .setForcedRenderState (null );
451449 }
452450
453- boolean debugfrustums = false ;
454-
455451 public void displayFrustum () {
456452 debugfrustums = true ;
457453 }
@@ -536,11 +532,11 @@ private void clearMatParams(){
536532 //renderers
537533 //Note that j start at 1 because other shadow renderers will have
538534 //at least 1 shadow map and will set it on each frame anyway.
539- for (int j = 1 ; j < nbShadowMaps ; j ++) {
540- mat .clearParam (lightViewStringCache [j ]);
535+ for (int i = 1 ; i < nbShadowMaps ; i ++) {
536+ mat .clearParam (lightViewStringCache [i ]);
541537 }
542- for (int j = 1 ; j < nbShadowMaps ; j ++) {
543- mat .clearParam (shadowMapStringCache [j ]);
538+ for (int i = 1 ; i < nbShadowMaps ; i ++) {
539+ mat .clearParam (shadowMapStringCache [i ]);
544540 }
545541 mat .clearParam ("FadeInfo" );
546542 clearMaterialParameters (mat );
@@ -556,22 +552,20 @@ private void clearMatParams(){
556552 */
557553 protected abstract void setMaterialParameters (Material material );
558554
559- private void setMatParams (GeometryList l ) {
555+ private void setMatParams (GeometryList list ) {
560556 //iterate through all the geometries of the list to gather the materials
561557
562- buildMatCache (l );
558+ buildMatCache (list );
563559
564560 //iterating through the mat cache and setting the parameters
565561 for (Material mat : matCache ) {
566-
567562 mat .setFloat ("ShadowMapSize" , shadowMapSize );
568563
569- for (int j = 0 ; j < nbShadowMaps ; j ++) {
570- mat .setMatrix4 (lightViewStringCache [j ], lightViewProjectionsMatrices [j ]);
564+ for (int i = 0 ; i < nbShadowMaps ; i ++) {
565+ mat .setMatrix4 (lightViewStringCache [i ], lightViewProjectionsMatrices [i ]);
571566 }
572-
573- for (int j = 0 ; j < nbShadowMaps ; j ++) {
574- mat .setTexture (shadowMapStringCache [j ], shadowMaps [j ]);
567+ for (int i = 0 ; i < nbShadowMaps ; i ++) {
568+ mat .setTexture (shadowMapStringCache [i ], shadowMaps [i ]);
575569 }
576570
577571 mat .setBoolean ("HardwareShadows" , shadowCompareMode == CompareMode .Hardware );
@@ -581,7 +575,7 @@ private void setMatParams(GeometryList l) {
581575 mat .setBoolean ("BackfaceShadows" , renderBackFacesShadows );
582576
583577 if (fadeInfo != null ) {
584- mat .setVector2 ("FadeInfo" , fadeInfo );
578+ mat .setVector2 ("FadeInfo" , fadeInfo );
585579 }
586580
587581 setMaterialParameters (mat );
@@ -592,13 +586,12 @@ private void setMatParams(GeometryList l) {
592586 if (needsfallBackMaterial ) {
593587 setPostShadowParams ();
594588 }
595-
596589 }
597590
598- private void buildMatCache (GeometryList l ) {
591+ private void buildMatCache (GeometryList list ) {
599592 matCache .clear ();
600- for (int i = 0 ; i < l .size (); i ++) {
601- Material mat = l .get (i ).getMaterial ();
593+ for (int i = 0 ; i < list .size (); i ++) {
594+ Material mat = list .get (i ).getMaterial ();
602595 //checking if the material has the post technique and adding it to the material cache
603596 if (mat .getMaterialDef ().getTechniqueDefs (postTechniqueName ) != null ) {
604597 if (!matCache .contains (mat )) {
0 commit comments