Skip to content

Commit a2ed571

Browse files
Lay-quqriccardobl
andauthored
Read glTF perspective camera aspectRatio (#3016)
Co-authored-by: Lay-quq <187484661+Lay-quq@users.noreply.github.com> Co-authored-by: Riccardo Balbo <riccardo0blb@gmail.com>
1 parent 05489be commit a2ed571

5 files changed

Lines changed: 52 additions & 1 deletion

File tree

‎jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/GltfLoader.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -972,7 +972,7 @@ public void readCameras() throws IOException {
972972
assertNotNull(type, "No type defined for camera");
973973
JsonObject camData = camObj.getAsJsonObject(type);
974974
if (type.equals("perspective")) {
975-
float aspectRatio = getAsFloat(camData, "aspectRation", 1f);
975+
float aspectRatio = getAsFloat(camData, "aspectRatio", 1f);
976976
Float yfov = getAsFloat(camData, "yfov");
977977
assertNotNull(yfov, "No yfov for perspective camera");
978978
Float zNear = getAsFloat(camData, "znear");

‎jme3-plugins/src/test/java/com/jme3/scene/plugins/gltf/GltfLoaderTest.java‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
import com.jme3.material.plugin.TestMaterialWrite;
4545
import com.jme3.math.ColorRGBA;
4646
import com.jme3.math.Vector3f;
47+
import com.jme3.renderer.Camera;
48+
import com.jme3.scene.CameraNode;
4749
import com.jme3.scene.Geometry;
4850
import com.jme3.scene.Mesh;
4951
import com.jme3.scene.Node;
@@ -168,6 +170,27 @@ public void testDracoExtension() {
168170
}
169171
}
170172

173+
@Test
174+
public void testPerspectiveCameraAspectRatio() {
175+
Camera camera = loadCamera("gltf/camera/perspectiveCamera.gltf");
176+
float aspectRatio = camera.getFrustumRight() / camera.getFrustumTop();
177+
Assertions.assertEquals(1.5f, aspectRatio, 1e-5f);
178+
}
179+
180+
@Test
181+
public void testPerspectiveCameraWithoutAspectRatio() {
182+
Camera camera = loadCamera("gltf/camera/perspectiveCameraNoAspectRatio.gltf");
183+
float aspectRatio = camera.getFrustumRight() / camera.getFrustumTop();
184+
Assertions.assertEquals(1f, aspectRatio, 1e-5f);
185+
}
186+
187+
private Camera loadCamera(String path) {
188+
Node scene = (Node) assetManager.loadModel(path);
189+
Spatial cameraNode = scene.getChild("cameraNode");
190+
Assertions.assertInstanceOf(CameraNode.class, cameraNode, "Camera node not found");
191+
return ((CameraNode) cameraNode).getCamera();
192+
}
193+
171194
private void dumpScene(Spatial s, int indent) {
172195
System.err.print(indentString.substring(0, indent) + s.getName() + " (" + s.getClass().getSimpleName() + ") / " +
173196
s.getLocalTransform().getTranslation().toString() + ", " +

‎jme3-plugins/src/test/resources/gltf/README.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,8 @@ Used in `com.jme3.scene.plugins.gltf.GltfLoaderTest`
1010
11x11 vertices, and texture coordinates that are stored as (normalized) unsigned
1111
short values. The asset is draco-compressed, to check the behavior of the Draco
1212
extension handler.
13+
- `camera/perspectiveCamera.gltf` and `camera/perspectiveCameraNoAspectRatio.gltf`
14+
contain a single node with a perspective camera, with and without an
15+
`aspectRatio`, to check that the camera frustum uses the aspect ratio
16+
from the file.
1317

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"asset": { "version": "2.0" },
3+
"scene": 0,
4+
"scenes": [ { "nodes": [ 0 ] } ],
5+
"nodes": [ { "name": "cameraNode", "camera": 0 } ],
6+
"cameras": [
7+
{
8+
"type": "perspective",
9+
"perspective": { "aspectRatio": 1.5, "yfov": 0.8, "znear": 0.1, "zfar": 100.0 }
10+
}
11+
]
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"asset": { "version": "2.0" },
3+
"scene": 0,
4+
"scenes": [ { "nodes": [ 0 ] } ],
5+
"nodes": [ { "name": "cameraNode", "camera": 0 } ],
6+
"cameras": [
7+
{
8+
"type": "perspective",
9+
"perspective": { "yfov": 0.8, "znear": 0.1, "zfar": 100.0 }
10+
}
11+
]
12+
}

0 commit comments

Comments
 (0)