forked from jMonkeyEngine/jmonkeyengine
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTestNormalMappingConsistency.java
More file actions
206 lines (182 loc) · 8.27 KB
/
Copy pathTestNormalMappingConsistency.java
File metadata and controls
206 lines (182 loc) · 8.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
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;
}
}