Browse Source

DirectionalLight: Rotation should now work the best, fixed the actual bug.

MeFisto94 7 years ago
parent
commit
b940a33d77

+ 4 - 3
jme3-core/src/com/jme3/gde/core/sceneexplorer/nodes/JmeDirectionalLight.java

@@ -93,11 +93,12 @@ public class JmeDirectionalLight extends JmeLight {
 
     public void setDirection(Vector3f direction) {
         // Don't directly pass it on to the Light
-        if (gizmo == null) {
+         /* See Implementation in DirectionalLightGizmo, why it's commented */
+        //if (gizmo == null) {
             directionalLight.setDirection(direction.normalize());
-        } else {
+        /*} else {
             gizmo.onSetDirection(direction.normalize());
-        }
+        }*/
     }
     
     public Vector3f getDirection() {

+ 39 - 5
jme3-scenecomposer/src/com/jme3/gde/scenecomposer/gizmo/light/DirectionalLightGizmo.java

@@ -1,7 +1,33 @@
 /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
+ *  Copyright (c) 2009-2018 jMonkeyEngine
+ *  All rights reserved.
+ * 
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions are
+ *  met:
+ * 
+ *  * Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 
+ *  * Neither the name of 'jMonkeyEngine' nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ * 
+ *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ *  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ *  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ *  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ *  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 package com.jme3.gde.scenecomposer.gizmo.light;
 
@@ -11,7 +37,6 @@ import com.jme3.gde.core.sceneexplorer.nodes.JmeDirectionalLight;
 import com.jme3.gde.core.sceneexplorer.nodes.gizmo.DirectionalLightGizmoInterface;
 import com.jme3.gde.scenecomposer.SceneComposerToolController;
 import com.jme3.gde.scenecomposer.SceneComposerTopComponent;
-import com.jme3.gde.scenecomposer.SceneEditTool;
 import com.jme3.gde.scenecomposer.gizmo.NodeCallback;
 import com.jme3.light.DirectionalLight;
 import com.jme3.math.FastMath;
@@ -60,6 +85,15 @@ public class DirectionalLightGizmo extends NodeCallback implements DirectionalLi
     
     @Override
     public void onSetDirection(Vector3f direction) {
+        /*
+          Commented out on 17/01/2018 by MeFisto94
+          Reason: This method and interface have been introduced by me in order
+          to fix a bug. Unfortunately I did not see the real root cause back then.
+          I've now fixed the actual cause, but this block of code will stay, because
+          it's what we'll ultimatively refactor to: Event-Driven Property -> Gizmos.
+          Currently we have stupid Controls checking the Direction every frame and
+          adjusting the gizmos if they changed...
+        
         // We cannot set the light direction directly, we have to adjust the
         // rotation as well (which in turn calls onRotation to set the direction)
          
@@ -81,7 +115,7 @@ public class DirectionalLightGizmo extends NodeCallback implements DirectionalLi
         SceneComposerToolController sctc = SceneComposerTopComponent.findInstance().getToolController();
         if (sctc.getSelectedSpatial() != null) {
            sctc.selectedSpatialTransformed();
-        }
+        }*/
     }
 
     private final BoundingSphere bv = new BoundingSphere(10f, getWorldTranslation());

+ 11 - 3
jme3-scenecomposer/src/com/jme3/gde/scenecomposer/gizmo/light/LightDirectionUpdate.java

@@ -34,6 +34,7 @@ package com.jme3.gde.scenecomposer.gizmo.light;
 import com.jme3.gde.core.errorreport.ExceptionUtils;
 import com.jme3.gde.scenecomposer.gizmo.NodeCallback;
 import com.jme3.light.Light;
+import com.jme3.math.FastMath;
 import com.jme3.math.Quaternion;
 import com.jme3.math.Vector3f;
 import com.jme3.renderer.RenderManager;
@@ -55,6 +56,7 @@ public class LightDirectionUpdate extends AbstractControl {
     private Method getDirection = null;
 
     private final Vector3f lastDir = new Vector3f();
+    private final Vector3f initialDir = new Vector3f();
     private Vector3f lightDir;
 
     public LightDirectionUpdate(Light light, NodeCallback gizmo) {
@@ -69,6 +71,9 @@ public class LightDirectionUpdate extends AbstractControl {
                 + "have a direction. This means someone has seriously "
                 + "fucked up. I just hope it's not me ;)");
         }
+        
+        refreshLightDirection();
+        initialDir.set(lightDir);
     }
     
     protected void refreshLightDirection() {
@@ -90,9 +95,12 @@ public class LightDirectionUpdate extends AbstractControl {
             Vector3f axis = Vector3f.UNIT_Y.cross(lastDir);
             float angle = Vector3f.UNIT_Y.angleBetween(lastDir);
             //Quaternion rotation = gizmo.getWorldRotation().inverse().mult(new Quaternion().fromAngleAxis(angle, axis));
-            Quaternion rotation = new Quaternion().fromAngleAxis(angle, axis);
-            gizmo.silentLocalRotation(rotation); /* silent, because otherwise
-            the gizmo would call light.setDirection() and update the property */
+            
+            if (angle > 1f * FastMath.DEG_TO_RAD) { // Anti Flickering
+                Quaternion rotation = new Quaternion().fromAngleAxis(angle, axis);
+                gizmo.silentLocalRotation(rotation); /* silent, because otherwise
+                the gizmo would call light.setDirection() and update the property */
+            }
         }
     }
 

+ 3 - 1
jme3-scenecomposer/src/com/jme3/gde/scenecomposer/gizmo/light/LightGizmoFactory.java

@@ -85,10 +85,11 @@ public class LightGizmoFactory {
     private static Node createDirectionalGizmo(AssetManager assetManager, JmeDirectionalLight jmeLight, Light light) {
         DirectionalLightGizmo gizmo = new DirectionalLightGizmo(jmeLight);
         gizmo.move(0, 5, 0);
+        gizmo.addControl(new LightDirectionUpdate(light, gizmo));
         
         Node billboardNode = new Node("billboard lightGizmo");
         billboardNode.addControl(new BillboardControl());
-        gizmo.attachChild(billboardNode);
+        
         billboardNode.attachChild(createLightBulb(assetManager));
         
         Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
@@ -100,6 +101,7 @@ public class LightGizmoFactory {
         arrow.addControl(new LightColorUpdate(light, arrow.getMaterial(), "Color"));
         
         gizmo.attachChild(arrow);
+        gizmo.attachChild(billboardNode);
         
         jmeLight.setGizmo(gizmo);
         return gizmo;