Skip to content

Commit

Permalink
Fix sign of tbn generator that caused normal map flipping in some sit…
Browse files Browse the repository at this point in the history
…uations (jMonkeyEngine#2140)
  • Loading branch information
riccardobl authored Nov 10, 2023
1 parent cf74247 commit b6735af
Show file tree
Hide file tree
Showing 15 changed files with 22,751 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ public static boolean genTangSpace(MikkTSpaceContext mikkTSpace, final float ang
float tang[] = {pTSpace.os.x, pTSpace.os.y, pTSpace.os.z};
float bitang[] = {pTSpace.ot.x, pTSpace.ot.y, pTSpace.ot.z};
mikkTSpace.setTSpace(tang, bitang, pTSpace.magS, pTSpace.magT, pTSpace.orient, f, i);
mikkTSpace.setTSpaceBasic(tang, pTSpace.orient == true ? 1.0f : (-1.0f), f, i);
mikkTSpace.setTSpaceBasic(tang, pTSpace.orient == true ? -1.0f : 1.0f, f, i);
++index;
}
}
Expand Down
12 changes: 7 additions & 5 deletions jme3-core/src/main/resources/Common/MatDefs/Light/Lighting.frag
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ uniform float m_AlphaDiscardThreshold;
#endif
#endif


#ifndef NORMAL_TYPE
#define NORMAL_TYPE -1.0
#endif

void main(){
vec2 newTexCoord;

Expand Down Expand Up @@ -141,11 +146,8 @@ void main(){
// ***********************
#if defined(NORMALMAP) && !defined(VERTEX_LIGHTING)
vec4 normalHeight = texture2D(m_NormalMap, newTexCoord);
//Note the -2.0 and -1.0. We invert the green channel of the normal map,
//as it's compliant with normal maps generated with blender.
//see http://hub.jmonkeyengine.org/forum/topic/parallax-mapping-fundamental-bug/#post-256898
//for more explanation.
vec3 normal = normalize((normalHeight.xyz * vec3(2.0,-2.0,2.0) - vec3(1.0,-1.0,1.0)));
// Note we invert directx style normal maps to opengl style
vec3 normal = normalize((normalHeight.xyz * vec3(2.0,NORMAL_TYPE * 2.0,2.0) - vec3(1.0,NORMAL_TYPE * 1.0,1.0)));
#ifdef LATC
normal.z = sqrt(1.0 - (normal.x * normal.x) - (normal.y * normal.y));
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ MaterialDef Phong Lighting {
// The glow color of the object
Color GlowColor

//The type of normal map: -1.0 (DirectX = default), 1.0 (OpenGl)
Float NormalType

// Parameters for fresnel
// X = bias
// Y = scale
Expand Down Expand Up @@ -169,6 +172,7 @@ MaterialDef Phong Lighting {
INSTANCING : UseInstancing
NUM_MORPH_TARGETS: NumberOfMorphTargets
NUM_TARGETS_BUFFERS: NumberOfTargetsBuffers
NORMAL_TYPE: NormalType

// fog - jayfella
USE_FOG : UseFog
Expand Down Expand Up @@ -217,6 +221,7 @@ MaterialDef Phong Lighting {
INSTANCING : UseInstancing
NUM_MORPH_TARGETS: NumberOfMorphTargets
NUM_TARGETS_BUFFERS: NumberOfTargetsBuffers
NORMAL_TYPE: NormalType

// fog - jayfella
USE_FOG : UseFog
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,8 @@ void main(){
// ***********************
#if defined(NORMALMAP)
vec4 normalHeight = texture2D(m_NormalMap, newTexCoord);
//Note the -2.0 and -1.0. We invert the green channel of the normal map,
//as it's compliant with normal maps generated with blender.
//see http://hub.jmonkeyengine.org/forum/topic/parallax-mapping-fundamental-bug/#post-256898
//for more explanation.
// Note we invert directx style normal maps to opengl style

#ifdef NORMALSCALE
vec3 normal = normalize((normalHeight.xyz * vec3(2.0, NORMAL_TYPE * 2.0, 2.0) - vec3(1.0, NORMAL_TYPE * 1.0, 1.0)) * vec3(m_NormalScale, m_NormalScale, 1.0));
#else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ uniform float m_Shininess;
#endif
#endif

#ifndef NORMAL_TYPE
#define NORMAL_TYPE -1.0
#endif

void main(){
#if !defined(VERTEX_LIGHTING)
#if defined(NORMALMAP)
Expand Down Expand Up @@ -152,11 +156,7 @@ void main(){
// ***********************
#if defined(NORMALMAP) && !defined(VERTEX_LIGHTING)
vec4 normalHeight = texture2D(m_NormalMap, newTexCoord);
//Note the -2.0 and -1.0. We invert the green channel of the normal map,
//as it's compliant with normal maps generated with blender.
//see http://hub.jmonkeyengine.org/forum/topic/parallax-mapping-fundamental-bug/#post-256898
//for more explanation.
vec3 normal = normalize((normalHeight.xyz * vec3(2.0,-2.0,2.0) - vec3(1.0,-1.0,1.0)));
vec3 normal = normalize((normalHeight.xyz * vec3(2.0, NORMAL_TYPE * 2.0 ,2.0) - vec3(1.0, NORMAL_TYPE * 1.0,1.0)));
#elif !defined(VERTEX_LIGHTING)
vec3 normal = normalize(vNormal);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
/*
* Copyright (c) 2009-2021 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 jme3test.material;

import com.jme3.app.SimpleApplication;
import com.jme3.asset.TextureKey;
import com.jme3.font.BitmapText;
import com.jme3.light.AmbientLight;
import com.jme3.light.DirectionalLight;
import com.jme3.material.Material;
import com.jme3.material.TechniqueDef.LightMode;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.plugins.gltf.GltfModelKey;
import com.jme3.system.AppSettings;
import com.jme3.util.mikktspace.MikktspaceTangentGenerator;

/**
* This test cycles through a model exported in different formats and with different materials with tangents
* generated in different ways. The normal map should look correct in all cases. Refer to
* https://github.com/KhronosGroup/glTF-Sample-Models/tree/master/2.0/NormalTangentMirrorTest for details on
* the correct result and debugging.
*/
public class TestNormalMappingConsistency extends SimpleApplication {
Node probeNode;
DirectionalLight light;
BitmapText materialTxt;
BitmapText modelTxt;
boolean flipTextures = false;
float t = -1;
int modelType = 0;
int materialType = 0;
Spatial loadedSpatial;

final int maxModels = 4;
final int maxMaterials = 3;

public static void main(String[] args) {
AppSettings sett = new AppSettings(true);
sett.setWidth(1024);
sett.setHeight(768);
TestNormalMappingConsistency app = new TestNormalMappingConsistency();
app.setSettings(sett);
app.start();
}

@Override
public void simpleInitApp() {
setPauseOnLostFocus(false);
flyCam.setMoveSpeed(20);
viewPort.setBackgroundColor(new ColorRGBA().setAsSrgb(0.2f, 0.2f, 0.2f, 1.0f));
probeNode = (Node) assetManager.loadModel("Scenes/defaultProbe.j3o");
rootNode.attachChild(probeNode);

probeNode.addLight(new AmbientLight(ColorRGBA.Gray));
light = new DirectionalLight(new Vector3f(-1, -1, -1), ColorRGBA.White);
rootNode.addLight(light);

modelTxt = new BitmapText(guiFont);
modelTxt.setSize(guiFont.getCharSet().getRenderedSize());
modelTxt.setLocalTranslation(0, 700, 0);
guiNode.attachChild(modelTxt);

materialTxt = new BitmapText(guiFont);
materialTxt.setSize(guiFont.getCharSet().getRenderedSize());
materialTxt.setLocalTranslation(300, 700, 0);
guiNode.attachChild(materialTxt);
}

@Override
public void simpleUpdate(float tpf) {
if (t == -1 || t > 5) {
t = 0;
loadModel(new Vector3f(0, 0, 0), 3, modelType, materialType);
materialType++;
if (materialType >= maxMaterials) {
materialType = 0;
modelType++;
if (modelType >= maxModels) {
modelType = 0;
}
}
}
t += tpf;

}

private void loadModel(Vector3f offset, float scale, int modelType, int materialType) {
if (loadedSpatial != null) {
loadedSpatial.removeFromParent();
}


if (modelType == 0) loadedSpatial = loadGltf();
else if (modelType == 1) loadedSpatial = loadGltfGen();
else if (modelType == 2) loadedSpatial = loadOgre();
else if (modelType == 3) loadedSpatial = loadOgreGen();

loadedSpatial.scale(scale);
loadedSpatial.move(offset);
if (materialType == 0) loadedSpatial.setMaterial(createPBRLightingMat());
else if (materialType == 1) loadedSpatial.setMaterial(createSPLightingMat());
else if (materialType == 2) loadedSpatial.setMaterial(createLightingMat());
probeNode.attachChild(loadedSpatial);
}

private Spatial loadGltf() {
GltfModelKey k = new GltfModelKey("jme3test/normalmapCompare/NormalTangentMirrorTest.gltf");
Spatial sp = assetManager.loadModel(k);
modelTxt.setText("GLTF");
return sp;
}

private Spatial loadGltfGen() {
GltfModelKey k = new GltfModelKey("jme3test/normalmapCompare/NormalTangentMirrorTest.gltf");
Spatial sp = assetManager.loadModel(k);
MikktspaceTangentGenerator.generate(loadedSpatial);
modelTxt.setText("GLTF - regen tg");
return sp;
}

private Spatial loadOgre() {
GltfModelKey k = new GltfModelKey("jme3test/normalmapCompare/ogre/NormalTangentMirrorTest.scene");
Spatial sp = assetManager.loadModel(k);
modelTxt.setText("OGRE");
return sp;
}

private Spatial loadOgreGen() {
GltfModelKey k = new GltfModelKey("jme3test/normalmapCompare/ogre/NormalTangentMirrorTest.scene");
Spatial sp = assetManager.loadModel(k);
MikktspaceTangentGenerator.generate(loadedSpatial);
modelTxt.setText("OGRE - regen tg");
return sp;
}

private Material createPBRLightingMat() {
renderManager.setPreferredLightMode(LightMode.SinglePassAndImageBased);
Material mat = new Material(assetManager, "Common/MatDefs/Light/PBRLighting.j3md");
mat.setTexture("BaseColorMap", assetManager.loadTexture(new TextureKey(
"jme3test/normalmapCompare/NormalTangentMirrorTest_BaseColor.png", flipTextures)));
mat.setTexture("NormalMap", assetManager.loadTexture(
new TextureKey("jme3test/normalmapCompare/NormalTangentTest_Normal.png", flipTextures)));
mat.setFloat("NormalType", 1);
materialTxt.setText("PBR Lighting");
return mat;
}

private Material createSPLightingMat() {
renderManager.setPreferredLightMode(LightMode.SinglePass);
Material mat = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
mat.setTexture("DiffuseMap", assetManager.loadTexture(new TextureKey(
"jme3test/normalmapCompare/NormalTangentMirrorTest_BaseColor.png", flipTextures)));
mat.setTexture("NormalMap", assetManager.loadTexture(
new TextureKey("jme3test/normalmapCompare/NormalTangentTest_Normal.png", flipTextures)));
mat.setFloat("NormalType", 1);
materialTxt.setText("SP Lighting");
return mat;
}

private Material createLightingMat() {
renderManager.setPreferredLightMode(LightMode.MultiPass);
Material mat = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
mat.setTexture("DiffuseMap", assetManager.loadTexture(new TextureKey(
"jme3test/normalmapCompare/NormalTangentMirrorTest_BaseColor.png", flipTextures)));
mat.setTexture("NormalMap", assetManager.loadTexture(
new TextureKey("jme3test/normalmapCompare/NormalTangentTest_Normal.png", flipTextures)));
materialTxt.setText("Lighting");
mat.setFloat("NormalType", 1);

return mat;
}

}
Loading

0 comments on commit b6735af

Please sign in to comment.