Skip to content

Commit 2ca7b52

Browse files
authored
Merge pull request #2437 from jMonkeyEngine/yaRnMcDonuts-TestInstanceNodeWithPbr
Add InstanceNode test using PBRLighting
2 parents b920f8c + e8e538a commit 2ca7b52

2 files changed

Lines changed: 93 additions & 1 deletion

File tree

jme3-core/src/main/resources/Common/MatDefs/Light/PBRLighting.vert

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ void main(){
7676
texCoord2 = inTexCoord2;
7777
#endif
7878

79-
wPosition = (g_WorldMatrix * vec4(inPosition, 1.0)).xyz;
79+
wPosition = TransformWorld(modelSpacePos).xyz;
8080
wNormal = TransformWorldNormal(modelSpaceNorm);
8181

8282
wTangent = vec4(TransformWorldNormal(modelSpaceTan),inTangent.w);
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package jme3test.scene.instancing;
2+
3+
import java.util.Locale;
4+
5+
import com.jme3.app.SimpleApplication;
6+
import com.jme3.font.BitmapText;
7+
import com.jme3.light.DirectionalLight;
8+
import com.jme3.material.Material;
9+
import com.jme3.math.ColorRGBA;
10+
import com.jme3.math.Vector3f;
11+
import com.jme3.scene.Geometry;
12+
import com.jme3.scene.instancing.InstancedNode;
13+
import com.jme3.scene.shape.Box;
14+
15+
/**
16+
* This test specifically validates the corrected PBR rendering when combined
17+
* with instancing, as addressed in issue #2435.
18+
*
19+
* It creates an InstancedNode
20+
* with a PBR-materialized Box to ensure the fix in PBRLighting.vert correctly
21+
* handles world position calculations for instanced geometry.
22+
*/
23+
public class TestInstanceNodeWithPbr extends SimpleApplication {
24+
25+
public static void main(String[] args) {
26+
TestInstanceNodeWithPbr app = new TestInstanceNodeWithPbr();
27+
app.start();
28+
}
29+
30+
private BitmapText bmp;
31+
private Geometry box;
32+
private float pos = -5;
33+
private float vel = 5;
34+
35+
@Override
36+
public void simpleInitApp() {
37+
configureCamera();
38+
bmp = createLabelText(10, 20, "<placeholder>");
39+
40+
InstancedNode instancedNode = new InstancedNode("InstancedNode");
41+
rootNode.attachChild(instancedNode);
42+
43+
Box mesh = new Box(0.5f, 0.5f, 0.5f);
44+
box = new Geometry("Box", mesh);
45+
Material pbrMaterial = createPbrMaterial(ColorRGBA.Red);
46+
box.setMaterial(pbrMaterial);
47+
48+
instancedNode.attachChild(box);
49+
instancedNode.instance();
50+
51+
DirectionalLight light = new DirectionalLight();
52+
light.setDirection(new Vector3f(-1, -2, -3).normalizeLocal());
53+
rootNode.addLight(light);
54+
}
55+
56+
private Material createPbrMaterial(ColorRGBA color) {
57+
Material mat = new Material(assetManager, "Common/MatDefs/Light/PBRLighting.j3md");
58+
mat.setColor("BaseColor", color);
59+
mat.setFloat("Roughness", 0.8f);
60+
mat.setFloat("Metallic", 0.1f);
61+
mat.setBoolean("UseInstancing", true);
62+
return mat;
63+
}
64+
65+
private void configureCamera() {
66+
flyCam.setMoveSpeed(15f);
67+
flyCam.setDragToRotate(true);
68+
69+
cam.setLocation(Vector3f.UNIT_XYZ.mult(12));
70+
cam.lookAt(Vector3f.ZERO, Vector3f.UNIT_Y);
71+
}
72+
73+
private BitmapText createLabelText(int x, int y, String text) {
74+
BitmapText bmp = new BitmapText(guiFont);
75+
bmp.setText(text);
76+
bmp.setLocalTranslation(x, settings.getHeight() - y, 0);
77+
bmp.setColor(ColorRGBA.Red);
78+
guiNode.attachChild(bmp);
79+
return bmp;
80+
}
81+
82+
@Override
83+
public void simpleUpdate(float tpf) {
84+
pos += tpf * vel;
85+
if (pos < -10f || pos > 10f) {
86+
vel *= -1;
87+
}
88+
box.setLocalTranslation(pos, 0f, 0f);
89+
bmp.setText(String.format(Locale.ENGLISH, "BoxPosition: (%.2f, %.1f, %.1f)", pos, 0f, 0f));
90+
}
91+
92+
}

0 commit comments

Comments
 (0)