فهرست منبع

Fixed binormal calculation in Single pass lighting

Nehon 9 سال پیش
والد
کامیت
07088452ff

+ 2 - 3
jme3-core/src/main/resources/Common/MatDefs/Light/SPLighting.frag

@@ -41,8 +41,7 @@ varying vec3 SpecularSum;
   
 #ifdef NORMALMAP
   uniform sampler2D m_NormalMap;   
-  varying vec3 vTangent;
-  varying vec3 vBinormal;
+  varying vec4 vTangent;
 #endif
 varying vec3 vNormal;
 
@@ -71,7 +70,7 @@ uniform float m_Shininess;
 void main(){
     #if !defined(VERTEX_LIGHTING)
         #if defined(NORMALMAP)
-            mat3 tbnMat = mat3(normalize(vTangent.xyz) , normalize(vBinormal.xyz) , normalize(vNormal.xyz));
+             mat3 tbnMat = mat3(vTangent.xyz, vTangent.w * cross( (vNormal), (vTangent.xyz)), vNormal.xyz);
 
             if (!gl_FrontFacing)
             {

+ 2 - 4
jme3-core/src/main/resources/Common/MatDefs/Light/SPLighting.vert

@@ -39,8 +39,7 @@ attribute vec3 inNormal;
     varying vec3 vPos;
     #ifdef NORMALMAP
         attribute vec4 inTangent;
-        varying vec3 vTangent;
-        varying vec3 vBinormal;
+        varying vec4 vTangent;
     #endif
 #else
     #ifdef COLORRAMP
@@ -104,8 +103,7 @@ void main(){
   
        
     #if defined(NORMALMAP) && !defined(VERTEX_LIGHTING)
-      vTangent = TransformNormal(modelSpaceTan);
-      vBinormal = cross(wvNormal, vTangent)* inTangent.w;      
+      vTangent = vec4(TransformNormal(modelSpaceTan).xyz,inTangent.w);
       vNormal = wvNormal;         
       vPos = wvPosition;
     #elif !defined(VERTEX_LIGHTING)

+ 100 - 0
jme3-examples/src/main/java/jme3test/light/TestTangentSpace.java

@@ -0,0 +1,100 @@
+package jme3test.light;
+
+import com.jme3.app.SimpleApplication;
+import com.jme3.input.KeyInput;
+import com.jme3.input.controls.ActionListener;
+import com.jme3.input.controls.KeyTrigger;
+import com.jme3.light.DirectionalLight;
+import com.jme3.material.*;
+import com.jme3.math.ColorRGBA;
+import com.jme3.math.Quaternion;
+import com.jme3.math.Vector3f;
+import com.jme3.scene.Geometry;
+import com.jme3.scene.Node;
+import com.jme3.scene.Spatial;
+import com.jme3.util.TangentBinormalGenerator;
+import com.jme3.util.mikktspace.MikktspaceTangentGenerator;
+
+/**
+ * test
+ *
+ * @author normenhansen
+ */
+public class TestTangentSpace extends SimpleApplication {
+    
+    public static void main(String[] args) {
+        TestTangentSpace app = new TestTangentSpace();
+        app.start();
+    }
+    
+    private Node debugNode = new Node("debug");
+    
+    @Override
+    public void simpleInitApp() {
+        renderManager.setSinglePassLightBatchSize(2);
+        renderManager.setPreferredLightMode(TechniqueDef.LightMode.SinglePass);
+        initView();
+        
+        Spatial s = assetManager.loadModel("Models/Test/BasicCubeLow.obj");
+        rootNode.attachChild(s);
+
+        Material m = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
+        m.setTexture("NormalMap", assetManager.loadTexture("Models/Test/Normal_pixel.png"));
+
+        Geometry g = (Geometry)s;
+        Geometry g2 = (Geometry) g.deepClone();
+        g2.move(5, 0, 0);
+        g.getParent().attachChild(g2);
+
+        g.setMaterial(m);
+        g2.setMaterial(m);
+
+        //Regular tangent generation (left geom)
+        TangentBinormalGenerator.generate(g2.getMesh(), true);
+
+        //MikkTSPace Tangent generation (right geom)        
+
+        MikktspaceTangentGenerator.generate(g);
+        
+        createDebugTangents(g2);
+        createDebugTangents(g);
+        
+        inputManager.addListener(new ActionListener() {
+            @Override
+            public void onAction(String name, boolean isPressed, float tpf) {
+                if (name.equals("toggleDebug") && isPressed) {
+                    if (debugNode.getParent() == null) {
+                        rootNode.attachChild(debugNode);
+                    } else {
+                        debugNode.removeFromParent();
+                    }
+                }
+            }
+        }, "toggleDebug");
+
+        inputManager.addMapping("toggleDebug", new KeyTrigger(KeyInput.KEY_SPACE));
+        
+        
+        DirectionalLight dl = new DirectionalLight(new Vector3f(-1, -1, -1).normalizeLocal());
+        rootNode.addLight(dl);
+    }
+    
+    private void initView() {
+        viewPort.setBackgroundColor(ColorRGBA.DarkGray);
+        cam.setLocation(new Vector3f(8.569681f, 3.335546f, 5.4372444f));
+        cam.setRotation(new Quaternion(-0.07608022f, 0.9086564f, -0.18992864f, -0.3639813f));
+        flyCam.setMoveSpeed(10);
+    }
+    
+    private void createDebugTangents(Geometry geom) {
+        Geometry debug = new Geometry(
+                "Debug " + geom.getName(),
+                TangentBinormalGenerator.genTbnLines(geom.getMesh(), 0.8f)
+        );
+        Material debugMat = assetManager.loadMaterial("Common/Materials/VertexColor.j3m");
+        debug.setMaterial(debugMat);
+        debug.setCullHint(Spatial.CullHint.Never);
+        debug.getLocalTranslation().set(geom.getWorldTranslation());
+        debugNode.attachChild(debug);
+    }
+}

+ 46 - 0
jme3-testdata/src/main/resources/Models/Test/BasicCubeLow.obj

@@ -0,0 +1,46 @@
+# Blender v2.76 (sub 0) OBJ File: 'BasicCube.blend'
+# www.blender.org
+o Cube
+v 1.000000 -1.000000 -1.000000
+v 1.000000 -1.000000 1.000000
+v -1.000000 -1.000000 1.000000
+v -1.000000 -1.000000 -1.000000
+v 1.000000 1.000000 -0.999999
+v 0.999999 1.000000 1.000001
+v -1.000000 1.000000 1.000000
+v -1.000000 1.000000 -1.000000
+vt 0.500000 0.250043
+vt 0.749957 0.250043
+vt 0.749957 0.500000
+vt 0.000087 0.500000
+vt 0.000087 0.250043
+vt 0.250043 0.250043
+vt 0.250043 0.500000
+vt 0.250043 0.000087
+vt 0.500000 0.000087
+vt 0.999913 0.250043
+vt 0.999913 0.500000
+vt 0.500000 0.500000
+vt 0.500000 0.749957
+vt 0.250044 0.749957
+vn 0.577300 -0.577300 0.577300
+vn -0.577300 -0.577300 0.577300
+vn -0.577300 -0.577300 -0.577300
+vn -0.577300 0.577300 -0.577300
+vn -0.577300 0.577300 0.577300
+vn 0.577300 0.577300 0.577300
+vn 0.577300 0.577300 -0.577300
+vn 0.577300 -0.577300 -0.577300
+s 1
+f 2/1/1 3/2/2 4/3/3
+f 8/4/4 7/5/5 6/6/6
+f 5/7/7 6/6/6 2/1/1
+f 6/6/6 7/8/5 3/9/2
+f 3/2/2 7/10/5 8/11/4
+f 1/12/8 4/13/3 8/14/4
+f 1/12/8 2/1/1 4/3/3
+f 5/7/7 8/4/4 6/6/6
+f 1/12/8 5/7/7 2/1/1
+f 2/1/1 6/6/6 3/9/2
+f 4/3/3 3/2/2 8/11/4
+f 5/7/7 1/12/8 8/14/4

BIN
jme3-testdata/src/main/resources/Models/Test/Normal_pixel.png