Skip to content

Commit 9612b18

Browse files
authored
Update LightControl: add axisRotation control
1 parent 826c860 commit 9612b18

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

jme3-core/src/main/java/com/jme3/scene/control/LightControl.java

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ public enum ControlDirection {
7878

7979
private Light light;
8080
private ControlDirection controlDir = ControlDirection.SpatialToLight;
81+
private int axisRotation = 2; // Default to Z-axis
8182

8283
/**
8384
* For serialization only. Do not use.
@@ -128,6 +129,31 @@ public void setControlDir(ControlDirection controlDir) {
128129
this.controlDir = controlDir;
129130
}
130131

132+
/**
133+
* Returns the index of the spatial's local axis (0=X, 1=Y, 2=Z) that determines
134+
* the light's direction when synchronizing Spatial to Light.
135+
* Defaults to 2 (Z-axis).
136+
*
137+
* @return The axis index (0 for X, 1 for Y, 2 for Z).
138+
*/
139+
public int getAxisRotation() {
140+
return axisRotation;
141+
}
142+
143+
/**
144+
* Sets the spatial's local axis to be used as the light's forward direction
145+
* when synchronizing Spatial to Light.
146+
*
147+
* @param axisRotation The index of the axis (0, 1, or 2).
148+
* @throws IllegalArgumentException if {@code axisRotation} is not 0, 1, or 2.
149+
*/
150+
public void setAxisRotation(int axisRotation) {
151+
if (axisRotation < 0 || axisRotation > 2) {
152+
throw new IllegalArgumentException("Axis rotation must be 0 (X), 1 (Y), or 2 (Z).");
153+
}
154+
this.axisRotation = axisRotation;
155+
}
156+
131157
private void validateSupportedLightType(Light light) {
132158
switch (light.getType()) {
133159
case Point:
@@ -165,19 +191,19 @@ private void spatialToLight(Light light) {
165191
final Vector3f worldPosition = vars.vect1;
166192
worldPosition.set(spatial.getWorldTranslation());
167193

168-
final Vector3f zDirection = vars.vect2;
169-
spatial.getWorldRotation().mult(Vector3f.UNIT_Z, zDirection).negateLocal();
194+
final Vector3f lightDirection = vars.vect2;
195+
spatial.getWorldRotation().getRotationColumn(axisRotation, lightDirection).negateLocal();
170196

171197
if (light instanceof PointLight) {
172198
((PointLight) light).setPosition(worldPosition);
173199

174200
} else if (light instanceof DirectionalLight) {
175-
((DirectionalLight) light).setDirection(zDirection);
201+
((DirectionalLight) light).setDirection(lightDirection);
176202

177203
} else if (light instanceof SpotLight) {
178204
SpotLight sl = (SpotLight) light;
179205
sl.setPosition(worldPosition);
180-
sl.setDirection(zDirection);
206+
sl.setDirection(lightDirection);
181207
}
182208
vars.release();
183209
}
@@ -251,6 +277,7 @@ public void read(JmeImporter im) throws IOException {
251277
InputCapsule ic = im.getCapsule(this);
252278
controlDir = ic.readEnum("controlDir", ControlDirection.class, ControlDirection.SpatialToLight);
253279
light = (Light) ic.readSavable("light", null);
280+
axisRotation = ic.readInt("axisRotation", 2);
254281
}
255282

256283
@Override
@@ -259,5 +286,6 @@ public void write(JmeExporter ex) throws IOException {
259286
OutputCapsule oc = ex.getCapsule(this);
260287
oc.write(controlDir, "controlDir", ControlDirection.SpatialToLight);
261288
oc.write(light, "light", null);
289+
oc.write(axisRotation, "axisRotation", 2);
262290
}
263291
}

0 commit comments

Comments
 (0)