Skip to content

Commit e2bdead

Browse files
authored
Update LightsDebugState.java
1 parent c110698 commit e2bdead

File tree

1 file changed

+76
-43
lines changed

1 file changed

+76
-43
lines changed

jme3-core/src/main/java/com/jme3/environment/util/LightsDebugState.java

Lines changed: 76 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2009-2021 jMonkeyEngine
2+
* Copyright (c) 2009-2025 jMonkeyEngine
33
* All rights reserved.
44
*
55
* Redistribution and use in source and binary forms, with or without
@@ -42,6 +42,7 @@
4242
import com.jme3.scene.shape.Sphere;
4343
import java.util.ArrayList;
4444
import java.util.HashMap;
45+
import java.util.Iterator;
4546
import java.util.List;
4647
import java.util.Map;
4748

@@ -53,24 +54,31 @@
5354
*/
5455
public class LightsDebugState extends BaseAppState {
5556

57+
private static final String PROBE_GEOMETRY_NAME = "DebugProbeGeometry";
58+
private static final String PROBE_BOUNDS_NAME = "DebugProbeBounds";
59+
5660
private Node debugNode;
57-
private final Map<LightProbe, Node> probeMapping = new HashMap<>();
58-
private final List<LightProbe> garbage = new ArrayList<>();
59-
private Geometry debugGeom;
60-
private Geometry debugBounds;
61-
private Material debugMaterial;
61+
private final Map<Light, Spatial> lightGizmoMap = new HashMap<>();
62+
private final List<Light> lightList = new ArrayList<>();
63+
private Spatial scene;
64+
65+
private Geometry baseProbeGeom;
66+
private Geometry baseProbeBounds;
67+
private Material lightProbeMaterial;
6268
private float probeScale = 1.0f;
63-
private Spatial scene = null;
64-
private final List<LightProbe> probes = new ArrayList<>();
6569

6670
@Override
6771
protected void initialize(Application app) {
68-
debugNode = new Node("Environment debug Node");
72+
debugNode = new Node("LightsDebugNode");
73+
6974
Sphere s = new Sphere(16, 16, 0.15f);
70-
debugGeom = new Geometry("debugEnvProbe", s);
71-
debugMaterial = new Material(app.getAssetManager(), "Common/MatDefs/Misc/reflect.j3md");
72-
debugGeom.setMaterial(debugMaterial);
73-
debugBounds = BoundingSphereDebug.createDebugSphere(app.getAssetManager());
75+
baseProbeGeom = new Geometry(PROBE_GEOMETRY_NAME, s);
76+
lightProbeMaterial = new Material(app.getAssetManager(), "Common/MatDefs/Misc/reflect.j3md");
77+
baseProbeGeom.setMaterial(lightProbeMaterial);
78+
79+
baseProbeBounds = BoundingSphereDebug.createDebugSphere(app.getAssetManager());
80+
baseProbeBounds.setName(PROBE_BOUNDS_NAME);
81+
7482
if (scene == null) {
7583
scene = app.getViewPort().getScenes().get(0);
7684
}
@@ -93,24 +101,32 @@ public void updateLights(Spatial scene) {
93101

94102
case Probe:
95103
LightProbe probe = (LightProbe) light;
96-
probes.add(probe);
97-
Node n = probeMapping.get(probe);
98-
if (n == null) {
99-
n = new Node("DebugProbe");
100-
n.attachChild(debugGeom.clone(true));
101-
n.attachChild(debugBounds.clone(false));
102-
debugNode.attachChild(n);
103-
probeMapping.put(probe, n);
104+
lightList.add(probe);
105+
Node gizmo = (Node) lightGizmoMap.get(probe);
106+
if (gizmo == null) {
107+
gizmo = new Node("DebugProbe");
108+
gizmo.attachChild(baseProbeGeom.clone(true));
109+
gizmo.attachChild(baseProbeBounds.clone(false));
110+
debugNode.attachChild(gizmo);
111+
lightGizmoMap.put(probe, gizmo);
104112
}
105-
Geometry probeGeom = ((Geometry) n.getChild(0));
106-
Material m = probeGeom.getMaterial();
107-
probeGeom.setLocalScale(probeScale);
113+
Geometry probeGeom = (Geometry) gizmo.getChild(PROBE_GEOMETRY_NAME);
114+
Geometry probeBounds = (Geometry) gizmo.getChild(PROBE_BOUNDS_NAME);
115+
116+
Material mat = probeGeom.getMaterial();
108117
if (probe.isReady()) {
109-
m.setTexture("CubeMap", probe.getPrefilteredEnvMap());
118+
mat.setTexture("CubeMap", probe.getPrefilteredEnvMap());
110119
}
111-
n.setLocalTranslation(probe.getPosition());
112-
n.getChild(1).setLocalScale(probe.getArea().getRadius());
120+
121+
probeGeom.setLocalScale(probeScale);
122+
probeBounds.setLocalScale(probe.getArea().getRadius());
123+
gizmo.setLocalTranslation(probe.getPosition());
113124
break;
125+
126+
case Point:
127+
case Spot:
128+
case Directional:
129+
// work in progress...
114130
default:
115131
break;
116132
}
@@ -124,27 +140,35 @@ public void updateLights(Spatial scene) {
124140
}
125141

126142
/**
127-
* Set the scenes for which to render light gizmos.
143+
* Sets the scene for which to render light gizmos.
144+
* If no scene is set, it defaults to the first scene in the viewport.
128145
*
129-
* @param scene the root of the desired scene (alias created)
146+
* @param scene The root of the desired scene.
130147
*/
131148
public void setScene(Spatial scene) {
132149
this.scene = scene;
150+
151+
// Clear existing gizmos when the scene changes to avoid displaying gizmos from the old scene
152+
debugNode.detachAllChildren();
153+
lightGizmoMap.clear();
154+
lightList.clear();
133155
}
134156

135157
private void cleanProbes() {
136-
if (probes.size() != probeMapping.size()) {
137-
for (LightProbe probe : probeMapping.keySet()) {
138-
if (!probes.contains(probe)) {
139-
garbage.add(probe);
140-
}
141-
}
142-
for (LightProbe probe : garbage) {
143-
probeMapping.remove(probe);
158+
Iterator<Map.Entry<Light, Spatial>> iterator = lightGizmoMap.entrySet().iterator();
159+
160+
while (iterator.hasNext()) {
161+
Map.Entry<Light, Spatial> entry = iterator.next();
162+
Light light = entry.getKey();
163+
164+
if (!lightList.contains(light)) {
165+
Spatial gizmoToRemove = entry.getValue();
166+
gizmoToRemove.removeFromParent();
167+
iterator.remove();
144168
}
145-
garbage.clear();
146-
probes.clear();
147169
}
170+
171+
lightList.clear();
148172
}
149173

150174
@Override
@@ -156,24 +180,32 @@ public void render(RenderManager rm) {
156180
}
157181

158182
/**
159-
* returns the scale of the probe's debug sphere
160-
* @return the scale factor
183+
* Returns the current scale of the light probe's debug sphere.
184+
*
185+
* @return The scale factor.
161186
*/
162187
public float getProbeScale() {
163188
return probeScale;
164189
}
165190

166191
/**
167-
* sets the scale of the probe's debug sphere
192+
* Sets the scale of the light probe's debug sphere.
168193
*
169-
* @param probeScale the scale factor (default=1)
194+
* @param probeScale The scale factor (default is 1.0).
170195
*/
171196
public void setProbeScale(float probeScale) {
172197
this.probeScale = probeScale;
173198
}
174199

175200
@Override
176201
protected void cleanup(Application app) {
202+
debugNode.removeFromParent();
203+
lightGizmoMap.clear();
204+
lightList.clear();
205+
206+
lightProbeMaterial = null;
207+
baseProbeGeom = null;
208+
baseProbeBounds = null;
177209
}
178210

179211
@Override
@@ -183,4 +215,5 @@ protected void onEnable() {
183215
@Override
184216
protected void onDisable() {
185217
}
218+
186219
}

0 commit comments

Comments
 (0)