-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Light control fix #1466
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
Merged
Merged
Light control fix #1466
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
7020a4f
Updates the light control spatialToLight method for Directional and S…
Markil3 5354454
Updates the light control lightToSpatial so it actually works with gl…
Markil3 013998f
Fixes some error messages in the tests.
Markil3 56fe6e3
Converts spatialToLight to use TempVars.
Markil3 4795c5a
Fixed some codacity issues.
Markil3 8641a8c
More formatting fixes
Markil3 9828368
Stops updating the rotation or translation when the parent light does…
Markil3 e68e2ab
Normalizes the rotation vector and adds some more readible names.
Markil3 eb3e217
Fixed some formatting/comment issues.
Markil3 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
jme3-examples/src/main/java/jme3test/light/TestLightControl2Directional.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| package jme3test.light; | ||
|
|
||
| import com.jme3.app.SimpleApplication; | ||
| import com.jme3.light.AmbientLight; | ||
| import com.jme3.light.DirectionalLight; | ||
| import com.jme3.material.Material; | ||
| import com.jme3.math.ColorRGBA; | ||
| import com.jme3.math.FastMath; | ||
| import com.jme3.math.Quaternion; | ||
| import com.jme3.math.Vector3f; | ||
| import com.jme3.scene.Geometry; | ||
| import com.jme3.scene.Node; | ||
| import com.jme3.scene.control.LightControl; | ||
| import com.jme3.scene.shape.Cylinder; | ||
| import com.jme3.scene.shape.Dome; | ||
| import com.jme3.scene.shape.Sphere; | ||
|
|
||
| /** | ||
| * Similar to {@link TestLightControlDirectional}, except that the spatial is controlled by the light this | ||
| * time. | ||
| * | ||
| * @author Markil 3 | ||
| */ | ||
| public class TestLightControl2Directional extends SimpleApplication { | ||
| private final Vector3f rotAxis = new Vector3f(Vector3f.UNIT_X); | ||
| private final float[] angles = new float[3]; | ||
|
|
||
| public static void main(String[] args) { | ||
| TestLightControl2Directional app = new TestLightControl2Directional(); | ||
| app.start(); | ||
| } | ||
|
|
||
| Node lightNode; | ||
| DirectionalLight direction; | ||
| Geometry lightMdl; | ||
|
|
||
| public void setupLighting() { | ||
| AmbientLight al = new AmbientLight(); | ||
| al.setColor(ColorRGBA.White.mult(2f)); | ||
| rootNode.addLight(al); | ||
|
|
||
| direction = new DirectionalLight(); | ||
| direction.setColor(ColorRGBA.White.mult(10)); | ||
| rootNode.addLight(direction); | ||
|
|
||
|
|
||
| // PointLight pl=new PointLight(); | ||
| // pl.setPosition(new Vector3f(77.70334f, 34.013165f, 27.1017f)); | ||
| // pl.setRadius(1000); | ||
| // pl.setColor(ColorRGBA.White.mult(2)); | ||
| // rootNode.addLight(pl); | ||
| lightMdl = new Geometry("Light", new Dome(Vector3f.ZERO, 2, 32, 5, false)); | ||
| lightMdl.setMaterial(assetManager.loadMaterial("Common/Materials/RedColor.j3m")); | ||
| lightMdl.setLocalTranslation(new Vector3f(0, 0, 0)); | ||
| lightMdl.setLocalRotation(new Quaternion().fromAngles(FastMath.PI / 2F, 0, 0)); | ||
| rootNode.attachChild(lightMdl); | ||
|
|
||
| /* | ||
| * We need this Dome doesn't have a "floor." | ||
| */ | ||
| Geometry lightFloor = new Geometry("LightFloor", new Cylinder(2, 32, 5, .1F, true)); | ||
| lightFloor.setMaterial(assetManager.loadMaterial("Common/Materials/RedColor.j3m")); | ||
| lightFloor.getMaterial().setColor("Color", ColorRGBA.White); | ||
| // lightFloor.setLocalRotation(new Quaternion().fromAngles(FastMath.PI / 2F, 0, 0)); | ||
|
|
||
| lightNode = new Node(); | ||
| lightNode.addControl(new LightControl(direction, LightControl.ControlDirection.LightToSpatial)); | ||
| // lightNode.setLocalTranslation(-5, 0, 0); | ||
| lightNode.attachChild(lightMdl); | ||
| lightNode.attachChild(lightFloor); | ||
|
|
||
| /* | ||
| * Offset the light node to check global stuff | ||
| */ | ||
| Node axis = new Node(); | ||
| axis.setLocalTranslation(5, -3, 2.5F); | ||
| axis.setLocalRotation(new Quaternion().fromAngles(FastMath.PI / -4F, FastMath.PI / 2F, 0)); | ||
| axis.attachChild(lightNode); | ||
| rootNode.attachChild(lightNode); | ||
| } | ||
|
|
||
| public void setupDome() { | ||
| Geometry dome = new Geometry("Dome", new Sphere(16, 32, 30, false, true)); | ||
| dome.setMaterial(new Material(this.assetManager, "Common/MatDefs/Light/PBRLighting.j3md")); | ||
| dome.setLocalTranslation(new Vector3f(0, 0, 0)); | ||
| rootNode.attachChild(dome); | ||
| } | ||
|
|
||
| @Override | ||
| public void simpleInitApp() { | ||
| flyCam.setMoveSpeed(30); | ||
|
|
||
| setupLighting(); | ||
| setupDome(); | ||
| } | ||
|
|
||
| @Override | ||
| public void simpleUpdate(float tpf) { | ||
| final Vector3f INIT_DIR = Vector3f.UNIT_Z.negate(); | ||
| /* | ||
| * In Radians per second | ||
| */ | ||
| final float ROT_SPEED = FastMath.PI * 2; | ||
| /* | ||
| * 360 degree rotation | ||
| */ | ||
| final float FULL_ROT = FastMath.PI * 2; | ||
| angles[0] += rotAxis.x * ROT_SPEED * tpf; | ||
| angles[1] += rotAxis.y * ROT_SPEED * tpf; | ||
| angles[2] += rotAxis.z * ROT_SPEED * tpf; | ||
| direction.setDirection(new Quaternion().fromAngles(angles).mult(INIT_DIR)); | ||
| super.simpleUpdate(tpf); | ||
| /* | ||
| * Make sure they are equal. | ||
| */ | ||
| if (FastMath.abs(direction.getDirection().normalize().subtractLocal(lightNode.getWorldRotation().mult(Vector3f.UNIT_Z).negateLocal().normalizeLocal()).lengthSquared()) > .1F) { | ||
| System.err.printf("Rotation not equal: is %s (%s), needs to be %s (%f)\n", lightNode.getWorldRotation().mult(Vector3f.UNIT_Z).normalizeLocal(), lightNode.getLocalRotation().mult(Vector3f.UNIT_Z).normalizeLocal(), direction.getDirection().normalize(), FastMath.abs(direction.getDirection().normalize().subtract(lightNode.getWorldRotation().mult(Vector3f.UNIT_Z).normalizeLocal()).lengthSquared())); | ||
| } | ||
| if (angles[0] >= FULL_ROT || angles[1] >= FULL_ROT || angles[2] >= FULL_ROT) { | ||
| direction.setDirection(INIT_DIR); | ||
| angles[0] = 0; | ||
| angles[1] = 0; | ||
| angles[2] = 0; | ||
| if (rotAxis.x > 0 && rotAxis.y == 0 && rotAxis.z == 0) { | ||
| rotAxis.set(0, 1, 0); | ||
| } else if (rotAxis.y > 0 && rotAxis.x == 0 && rotAxis.z == 0) { | ||
| rotAxis.set(0, 0, 1); | ||
| } else if (rotAxis.z > 0 && rotAxis.x == 0 && rotAxis.y == 0) { | ||
| rotAxis.set(FastMath.nextRandomFloat() % 1, FastMath.nextRandomFloat() % 1, FastMath.nextRandomFloat() % 1); | ||
| } else { | ||
| rotAxis.set(1, 0, 0); | ||
| } | ||
| } | ||
| } | ||
| } |
142 changes: 142 additions & 0 deletions
142
jme3-examples/src/main/java/jme3test/light/TestLightControl2Spot.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| package jme3test.light; | ||
|
|
||
| import com.jme3.app.SimpleApplication; | ||
| import com.jme3.light.AmbientLight; | ||
| import com.jme3.light.DirectionalLight; | ||
| import com.jme3.light.SpotLight; | ||
| import com.jme3.material.Material; | ||
| import com.jme3.math.ColorRGBA; | ||
| import com.jme3.math.FastMath; | ||
| import com.jme3.math.Quaternion; | ||
| import com.jme3.math.Vector3f; | ||
| import com.jme3.scene.Geometry; | ||
| import com.jme3.scene.Node; | ||
| import com.jme3.scene.control.LightControl; | ||
| import com.jme3.scene.shape.Cylinder; | ||
| import com.jme3.scene.shape.Dome; | ||
| import com.jme3.scene.shape.Sphere; | ||
|
|
||
| /** | ||
| * Similar to {@link TestLightControlSpot}, except that the spatial is controlled by the light this | ||
| * time. | ||
| * | ||
| * @author Markil 3 | ||
| */ | ||
| public class TestLightControl2Spot extends SimpleApplication { | ||
| private final Vector3f rotAxis = new Vector3f(Vector3f.UNIT_X); | ||
| private final float[] angles = new float[3]; | ||
|
|
||
| public static void main(String[] args) { | ||
| TestLightControl2Spot app = new TestLightControl2Spot(); | ||
| app.start(); | ||
| } | ||
|
|
||
| Node lightNode; | ||
| SpotLight spot; | ||
| Geometry lightMdl; | ||
|
|
||
| public void setupLighting() { | ||
| AmbientLight al = new AmbientLight(); | ||
| al.setColor(ColorRGBA.White.mult(2f)); | ||
| rootNode.addLight(al); | ||
|
|
||
| spot = new SpotLight(); | ||
| spot.setSpotRange(1000); | ||
| spot.setSpotInnerAngle(5 * FastMath.DEG_TO_RAD); | ||
| spot.setSpotOuterAngle(10 * FastMath.DEG_TO_RAD); | ||
| spot.setColor(ColorRGBA.White.mult(10)); | ||
| rootNode.addLight(spot); | ||
|
|
||
|
|
||
| // PointLight pl=new PointLight(); | ||
| // pl.setPosition(new Vector3f(77.70334f, 34.013165f, 27.1017f)); | ||
| // pl.setRadius(1000); | ||
| // pl.setColor(ColorRGBA.White.mult(2)); | ||
| // rootNode.addLight(pl); | ||
| lightMdl = new Geometry("Light", new Dome(Vector3f.ZERO, 2, 32, 5, false)); | ||
| lightMdl.setMaterial(assetManager.loadMaterial("Common/Materials/RedColor.j3m")); | ||
| lightMdl.setLocalTranslation(new Vector3f(0, 0, 0)); | ||
| lightMdl.setLocalRotation(new Quaternion().fromAngles(FastMath.PI / 2F, 0, 0)); | ||
| rootNode.attachChild(lightMdl); | ||
|
|
||
| /* | ||
| * We need this Dome doesn't have a "floor." | ||
| */ | ||
| Geometry lightFloor = new Geometry("LightFloor", new Cylinder(2, 32, 5, .1F, true)); | ||
| lightFloor.setMaterial(assetManager.loadMaterial("Common/Materials/RedColor.j3m")); | ||
| lightFloor.getMaterial().setColor("Color", ColorRGBA.White); | ||
| // lightFloor.setLocalRotation(new Quaternion().fromAngles(FastMath.PI / 2F, 0, 0)); | ||
|
|
||
| lightNode = new Node(); | ||
| lightNode.addControl(new LightControl(spot, LightControl.ControlDirection.LightToSpatial)); | ||
| // lightNode.setLocalTranslation(-5, 0, 0); | ||
| lightNode.attachChild(lightMdl); | ||
| lightNode.attachChild(lightFloor); | ||
|
|
||
| /* | ||
| * Offset the light node to check global stuff | ||
| */ | ||
| Node axis = new Node(); | ||
| axis.setLocalTranslation(5, -3, 2.5F); | ||
| axis.setLocalRotation(new Quaternion().fromAngles(FastMath.PI / -4F, FastMath.PI / 2F, 0)); | ||
| axis.attachChild(lightNode); | ||
| rootNode.attachChild(lightNode); | ||
| } | ||
|
|
||
| public void setupDome() { | ||
| Geometry dome = new Geometry("Dome", new Sphere(16, 32, 30, false, true)); | ||
| dome.setMaterial(new Material(this.assetManager, "Common/MatDefs/Light/PBRLighting.j3md")); | ||
| dome.setLocalTranslation(new Vector3f(0, 0, 0)); | ||
| rootNode.attachChild(dome); | ||
| } | ||
|
|
||
| @Override | ||
| public void simpleInitApp() { | ||
| flyCam.setMoveSpeed(30); | ||
|
|
||
| setupLighting(); | ||
| setupDome(); | ||
| } | ||
|
|
||
| @Override | ||
| public void simpleUpdate(float tpf) { | ||
| final Vector3f INIT_DIR = Vector3f.UNIT_Z.negate(); | ||
| /* | ||
| * In Radians per second | ||
| */ | ||
| final float ROT_SPEED = FastMath.PI * 2; | ||
| /* | ||
| * 360 degree rotation | ||
| */ | ||
| final float FULL_ROT = FastMath.PI * 2; | ||
| angles[0] += rotAxis.x * ROT_SPEED * tpf; | ||
| angles[1] += rotAxis.y * ROT_SPEED * tpf; | ||
| angles[2] += rotAxis.z * ROT_SPEED * tpf; | ||
| spot.setDirection(new Quaternion().fromAngles(angles).mult(INIT_DIR)); | ||
| super.simpleUpdate(tpf); | ||
| /* | ||
| * Make sure they are equal. | ||
| */ | ||
| if (spot.getPosition().subtract(lightNode.getWorldTranslation()).lengthSquared() > 0.1F) { | ||
| System.err.printf("Translation not equal: is %s (%s), needs to be %s\n", lightNode.getWorldTranslation(), lightNode.getLocalTranslation(), spot.getPosition()); | ||
| } | ||
| if (FastMath.abs(spot.getDirection().normalize().subtractLocal(lightNode.getWorldRotation().mult(Vector3f.UNIT_Z).negateLocal().normalizeLocal()).lengthSquared()) > .1F) { | ||
| System.err.printf("Rotation not equal: is %s (%s), needs to be %s (%f)\n", lightNode.getWorldRotation().mult(Vector3f.UNIT_Z).normalizeLocal(), lightNode.getLocalRotation().mult(Vector3f.UNIT_Z).normalizeLocal(), spot.getDirection().normalize(), FastMath.abs(spot.getDirection().normalize().subtract(lightNode.getWorldRotation().mult(Vector3f.UNIT_Z).normalizeLocal()).lengthSquared())); | ||
| } | ||
| if (angles[0] >= FULL_ROT || angles[1] >= FULL_ROT || angles[2] >= FULL_ROT) { | ||
| spot.setDirection(INIT_DIR); | ||
| angles[0] = 0; | ||
| angles[1] = 0; | ||
| angles[2] = 0; | ||
| if (rotAxis.x > 0 && rotAxis.y == 0 && rotAxis.z == 0) { | ||
| rotAxis.set(0, 1, 0); | ||
| } else if (rotAxis.y > 0 && rotAxis.x == 0 && rotAxis.z == 0) { | ||
| rotAxis.set(0, 0, 1); | ||
| } else if (rotAxis.z > 0 && rotAxis.x == 0 && rotAxis.y == 0) { | ||
| rotAxis.set(FastMath.nextRandomFloat() % 1, FastMath.nextRandomFloat() % 1, FastMath.nextRandomFloat() % 1); | ||
| } else { | ||
| rotAxis.set(1, 0, 0); | ||
| } | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.