fixed the problem with the reference to light, when we had incorrect reference to the light after loading/cloning spatial of this control.

empirephoenix-patch-1
javasabr 7 years ago committed by Rémy Bouquet
parent 8674d8af4b
commit 24e467a871
  1. 53
      jme3-core/src/main/java/com/jme3/scene/control/LightControl.java

@ -42,8 +42,9 @@ import com.jme3.light.SpotLight;
import com.jme3.math.Vector3f; import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager; import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort; import com.jme3.renderer.ViewPort;
import com.jme3.scene.Spatial;
import com.jme3.util.TempVars; import com.jme3.util.TempVars;
import com.jme3.util.clone.Cloner;
import java.io.IOException; import java.io.IOException;
/** /**
@ -54,7 +55,10 @@ import java.io.IOException;
*/ */
public class LightControl extends AbstractControl { public class LightControl extends AbstractControl {
public static enum ControlDirection { private static final String CONTROL_DIR_NAME = "controlDir";
private static final String LIGHT_NAME = "light";
public enum ControlDirection {
/** /**
* Means, that the Light's transform is "copied" * Means, that the Light's transform is "copied"
@ -67,6 +71,7 @@ public class LightControl extends AbstractControl {
*/ */
SpatialToLight; SpatialToLight;
} }
private Light light; private Light light;
private ControlDirection controlDir = ControlDirection.SpatialToLight; private ControlDirection controlDir = ControlDirection.SpatialToLight;
@ -113,7 +118,7 @@ public class LightControl extends AbstractControl {
if (spatial != null && light != null) { if (spatial != null && light != null) {
switch (controlDir) { switch (controlDir) {
case SpatialToLight: case SpatialToLight:
spatialTolight(light); spatialToLight(light);
break; break;
case LightToSpatial: case LightToSpatial:
lightToSpatial(light); lightToSpatial(light);
@ -122,22 +127,29 @@ public class LightControl extends AbstractControl {
} }
} }
private void spatialTolight(Light light) { private void spatialToLight(Light light) {
final Vector3f worldTranslation = spatial.getWorldTranslation();
if (light instanceof PointLight) { if (light instanceof PointLight) {
((PointLight) light).setPosition(spatial.getWorldTranslation()); ((PointLight) light).setPosition(worldTranslation);
return;
} }
TempVars vars = TempVars.get();
final TempVars vars = TempVars.get();
final Vector3f vec = vars.vect1;
if (light instanceof DirectionalLight) { if (light instanceof DirectionalLight) {
((DirectionalLight) light).setDirection(vars.vect1.set(spatial.getWorldTranslation()).multLocal(-1.0f)); ((DirectionalLight) light).setDirection(vec.set(worldTranslation).multLocal(-1.0f));
} }
if (light instanceof SpotLight) { if (light instanceof SpotLight) {
((SpotLight) light).setPosition(spatial.getWorldTranslation()); final SpotLight spotLight = (SpotLight) light;
((SpotLight) light).setDirection(spatial.getWorldRotation().multLocal(vars.vect1.set(Vector3f.UNIT_Y).multLocal(-1))); spotLight.setPosition(worldTranslation);
spotLight.setDirection(spatial.getWorldRotation().multLocal(vec.set(Vector3f.UNIT_Y).multLocal(-1)));
} }
vars.release();
vars.release();
} }
private void lightToSpatial(Light light) { private void lightToSpatial(Light light) {
@ -158,8 +170,6 @@ public class LightControl extends AbstractControl {
} }
vars.release(); vars.release();
//TODO add code for Spot light here when it's done //TODO add code for Spot light here when it's done
} }
@Override @Override
@ -167,23 +177,18 @@ public class LightControl extends AbstractControl {
// nothing to do // nothing to do
} }
// default implementation from AbstractControl is equivalent @Override
//@Override public void cloneFields(final Cloner cloner, final Object original) {
//public Control cloneForSpatial(Spatial newSpatial) { super.cloneFields(cloner, original);
// LightControl control = new LightControl(light, controlDir); light = cloner.clone(light);
// control.setSpatial(newSpatial); }
// control.setEnabled(isEnabled());
// return control;
//}
private static final String CONTROL_DIR_NAME = "controlDir";
private static final String LIGHT_NAME = "light";
@Override @Override
public void read(JmeImporter im) throws IOException { public void read(JmeImporter im) throws IOException {
super.read(im); super.read(im);
InputCapsule ic = im.getCapsule(this); InputCapsule ic = im.getCapsule(this);
controlDir = ic.readEnum(CONTROL_DIR_NAME, ControlDirection.class, ControlDirection.SpatialToLight); controlDir = ic.readEnum(CONTROL_DIR_NAME, ControlDirection.class, ControlDirection.SpatialToLight);
light = (Light)ic.readSavable(LIGHT_NAME, null); light = (Light) ic.readSavable(LIGHT_NAME, null);
} }
@Override @Override

Loading…
Cancel
Save