Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed the problem with LightControl. #771

Merged
merged 1 commit into from Nov 26, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
53 changes: 29 additions & 24 deletions jme3-core/src/main/java/com/jme3/scene/control/LightControl.java
Expand Up @@ -42,8 +42,9 @@
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Spatial;
import com.jme3.util.TempVars;
import com.jme3.util.clone.Cloner;

import java.io.IOException;

/**
Expand All @@ -54,7 +55,10 @@
*/
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"
Expand All @@ -67,6 +71,7 @@ public static enum ControlDirection {
*/
SpatialToLight;
}

private Light light;
private ControlDirection controlDir = ControlDirection.SpatialToLight;

Expand Down Expand Up @@ -113,7 +118,7 @@ protected void controlUpdate(float tpf) {
if (spatial != null && light != null) {
switch (controlDir) {
case SpatialToLight:
spatialTolight(light);
spatialToLight(light);
break;
case LightToSpatial:
lightToSpatial(light);
Expand All @@ -122,22 +127,29 @@ protected void controlUpdate(float tpf) {
}
}

private void spatialTolight(Light light) {
private void spatialToLight(Light light) {

final Vector3f worldTranslation = spatial.getWorldTranslation();

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) {
((DirectionalLight) light).setDirection(vars.vect1.set(spatial.getWorldTranslation()).multLocal(-1.0f));
((DirectionalLight) light).setDirection(vec.set(worldTranslation).multLocal(-1.0f));
}

if (light instanceof SpotLight) {
((SpotLight) light).setPosition(spatial.getWorldTranslation());
((SpotLight) light).setDirection(spatial.getWorldRotation().multLocal(vars.vect1.set(Vector3f.UNIT_Y).multLocal(-1)));
final SpotLight spotLight = (SpotLight) light;
spotLight.setPosition(worldTranslation);
spotLight.setDirection(spatial.getWorldRotation().multLocal(vec.set(Vector3f.UNIT_Y).multLocal(-1)));
}
vars.release();

vars.release();
}

private void lightToSpatial(Light light) {
Expand All @@ -158,32 +170,25 @@ private void lightToSpatial(Light light) {
}
vars.release();
//TODO add code for Spot light here when it's done


}

@Override
protected void controlRender(RenderManager rm, ViewPort vp) {
// nothing to do
}

// default implementation from AbstractControl is equivalent
//@Override
//public Control cloneForSpatial(Spatial newSpatial) {
// LightControl control = new LightControl(light, controlDir);
// control.setSpatial(newSpatial);
// control.setEnabled(isEnabled());
// return control;
//}
private static final String CONTROL_DIR_NAME = "controlDir";
private static final String LIGHT_NAME = "light";

@Override
public void cloneFields(final Cloner cloner, final Object original) {
super.cloneFields(cloner, original);
light = cloner.clone(light);
}

@Override
public void read(JmeImporter im) throws IOException {
super.read(im);
InputCapsule ic = im.getCapsule(this);
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
Expand Down