Skip to content

Commit 0c8aa04

Browse files
authored
Update LightControl: add axisRotation and axisDirection enum
1 parent 133dac7 commit 0c8aa04

1 file changed

Lines changed: 54 additions & 23 deletions

File tree

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

Lines changed: 54 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,26 @@ public enum ControlDirection {
7676
SpatialToLight
7777
}
7878

79+
/**
80+
* Represents the local axis of the spatial (X, Y, or Z) to be used
81+
* for determining the light's direction when `ControlDirection` is
82+
* `SpatialToLight`.
83+
*/
84+
public enum Axis {
85+
X, Y, Z
86+
}
87+
88+
/**
89+
* Represents the direction (positive or negative) along the chosen axis.
90+
*/
91+
public enum Direction {
92+
Positive, Negative
93+
}
94+
7995
private Light light;
8096
private ControlDirection controlDir = ControlDirection.SpatialToLight;
81-
private int axisRotation = 2; // Default to Z-axis
97+
private Axis axisRotation = Axis.Z;
98+
private Direction axisDirection = Direction.Positive;
8299

83100
/**
84101
* For serialization only. Do not use.
@@ -112,6 +129,24 @@ public LightControl(Light light, ControlDirection controlDir) {
112129
this.controlDir = controlDir;
113130
}
114131

132+
/**
133+
* Creates a new `LightControl` with a specified
134+
* axis of rotation, and axis direction.
135+
*
136+
* @param light The light to be synced.
137+
* @param axisRotation The spatial's local axis to be used as the light's forward direction
138+
* when synchronizing Spatial to Light.
139+
* @param axisDirection The direction along the chosen axis.
140+
* @throws IllegalArgumentException if the light type is not supported
141+
* (only Point, Directional, and Spot lights are supported).
142+
*/
143+
public LightControl(Light light, Axis axisRotation, Direction axisDirection) {
144+
validateSupportedLightType(light);
145+
this.light = light;
146+
this.axisRotation = axisRotation;
147+
this.axisDirection = axisDirection;
148+
}
149+
115150
public Light getLight() {
116151
return light;
117152
}
@@ -129,31 +164,22 @@ public void setControlDir(ControlDirection controlDir) {
129164
this.controlDir = controlDir;
130165
}
131166

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() {
167+
public Axis getAxisRotation() {
140168
return axisRotation;
141169
}
142170

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-
}
171+
public void setAxisRotation(Axis axisRotation) {
154172
this.axisRotation = axisRotation;
155173
}
156174

175+
public Direction getAxisDirection() {
176+
return axisDirection;
177+
}
178+
179+
public void setAxisDirection(Direction axisDirection) {
180+
this.axisDirection = axisDirection;
181+
}
182+
157183
private void validateSupportedLightType(Light light) {
158184
switch (light.getType()) {
159185
case Point:
@@ -192,7 +218,10 @@ private void spatialToLight(Light light) {
192218
worldPosition.set(spatial.getWorldTranslation());
193219

194220
final Vector3f lightDirection = vars.vect2;
195-
spatial.getWorldRotation().getRotationColumn(axisRotation, lightDirection);
221+
spatial.getWorldRotation().getRotationColumn(axisRotation.ordinal(), lightDirection);
222+
if (axisDirection == Direction.Negative) {
223+
lightDirection.negateLocal();
224+
}
196225

197226
if (light instanceof PointLight) {
198227
((PointLight) light).setPosition(worldPosition);
@@ -277,7 +306,8 @@ public void read(JmeImporter im) throws IOException {
277306
InputCapsule ic = im.getCapsule(this);
278307
controlDir = ic.readEnum("controlDir", ControlDirection.class, ControlDirection.SpatialToLight);
279308
light = (Light) ic.readSavable("light", null);
280-
axisRotation = ic.readInt("axisRotation", 2);
309+
axisRotation = ic.readEnum("axisRotation", Axis.class, Axis.Z);
310+
axisDirection = ic.readEnum("axisDirection", Direction.class, Direction.Positive);
281311
}
282312

283313
@Override
@@ -286,6 +316,7 @@ public void write(JmeExporter ex) throws IOException {
286316
OutputCapsule oc = ex.getCapsule(this);
287317
oc.write(controlDir, "controlDir", ControlDirection.SpatialToLight);
288318
oc.write(light, "light", null);
289-
oc.write(axisRotation, "axisRotation", 2);
319+
oc.write(axisRotation, "axisRotation", Axis.Z);
320+
oc.write(axisDirection, "axisDirection", Direction.Positive);
290321
}
291322
}

0 commit comments

Comments
 (0)