Skip to content

Commit 246a00f

Browse files
committed
Start moving CameraNode and ArCameraNode to Kotlin-math
1 parent ff499a5 commit 246a00f

File tree

2 files changed

+456
-0
lines changed

2 files changed

+456
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package io.github.sceneview.ar.node
2+
3+
import com.google.android.filament.Engine
4+
import com.google.ar.core.Camera
5+
import dev.romainguy.kotlin.math.degrees
6+
import io.github.sceneview.ar.arcore.transform
7+
import io.github.sceneview.math.toTransform
8+
import io.github.sceneview.node.CameraNode2
9+
import kotlin.math.atan
10+
11+
/**
12+
* Represents a virtual camera, which determines the perspective through which the scene is viewed.
13+
*
14+
*
15+
* If the camera is part of an [ArSceneView], then the camera automatically tracks the
16+
* camera pose from ARCore. Additionally, the following methods will throw [ ] when called:
17+
*
18+
*
19+
* * [.setParent] - CameraNode's parent cannot be changed, it is always the scene.
20+
* * [.setPosition] - CameraNode's position cannot be changed, it is controlled
21+
* by the ARCore camera pose.
22+
* * [.setRotation] - CameraNode's rotation cannot be changed, it is
23+
* controlled by the ARCore camera pose.
24+
*
25+
*
26+
*
27+
* All other functionality in Node is supported. You can access the position and rotation of the
28+
* camera, assign a collision shape to the camera, or add children to the camera. Disabling the
29+
* camera turns off rendering.
30+
*/
31+
class ArCameraNode2(engine: Engine) : CameraNode2(engine, false) {
32+
33+
override var verticalFovDegrees: Float
34+
get() {
35+
val fovRadians = 2.0f * atan(1.0f / projectionTransform.y.x)
36+
return degrees(fovRadians)
37+
}
38+
set(_) {}
39+
40+
/**
41+
* Updates the pose and projection of the camera to match the tracked pose from ARCore.
42+
*
43+
* Called internally as part of the integration with ARCore, should not be called directly.
44+
*/
45+
fun updateTrackedPose(camera: Camera) {
46+
// Update the projection matrix.
47+
projectionTransform = FloatArray(16).apply {
48+
camera.getProjectionMatrix(
49+
this, 0,
50+
nearClipPlane, farClipPlane
51+
)
52+
}.toTransform()
53+
54+
// Update the view matrix.
55+
camera.getViewMatrix(viewMatrix.data, 0)
56+
57+
// Update the node's transformation properties to match the tracked pose.
58+
// if (camera.trackingState == TrackingState.TRACKING) {
59+
val cameraTransform = camera.displayOrientedPose.transform
60+
if (transform != cameraTransform) {
61+
transform = cameraTransform
62+
}
63+
// }
64+
}
65+
}

0 commit comments

Comments
 (0)