sceneview-android
sceneview-android copied to clipboard
Restrict camera angles in 3D viewer
For some 3D viewer use cases the camera angle should be restricted to a certain angle (like prevent the user to view the model from the bottom). Frameworks for iOS like SceneKit and RealityKit allow this and I think SceneView should also allow this easily.
As it's probably something that should be added to Manipulator I created a feature request in https://github.com/google/filament/issues/5451 .
I have wanted the same, and I have a working solution that gets rid of the default cameraManipulator, and allows for single finger camera movement though you may lose any scene interaction as I haven't tested that.
... sceneView.apply {
// outside of detector so I can use them in scale gesture too.
var cameraHorizontal=0.0f
var cameraVertical=0.0f
var camDistance=1.5f
gestureDetector.cameraManipulator=null
gestureDetector.moveGestureDetector = MoveGestureDetector(this@NonARActivity, object : MoveGestureDetector.OnMoveGestureListener {
var currentX=0.0f
var currentY=0.0f
override fun onMove(detector: MoveGestureDetector): Boolean {
val distX=detector.lastDistanceX
val distY=detector.lastDistanceY
val diffX=(distX ?: 0f) - currentX
val diffY=(distY ?: 0f) - currentY
cameraHorizontal-=diffX/200f
//change the AtMost and AtLeast values that make sense to your application
cameraVertical=(cameraVertical+diffY/200f).coerceAtMost(HALF_PI-0.01f).coerceAtLeast(-HALF_PI+0.01f)
camera.transform=genCameraView(cameraHorizontal,cameraVertical,camDistance)
currentX=distX ?: 0f
currentY=distY ?: 0f
return true
}
override fun onMoveBegin(detector: MoveGestureDetector):Boolean{
currentX=0f
currentY=0f
return true
}
override fun onMoveEnd(detector: MoveGestureDetector) {
currentX=0f
currentY=0f
}
})
...
fun genCameraView(horizontalAngle:Float, verticalAngle:Float, distanceToTarget:Float): Mat4 {
val camPos=Float3(
x = distanceToTarget * sin(horizontalAngle) * cos(verticalAngle),
y = distanceToTarget * sin(verticalAngle),
z = distanceToTarget * cos(horizontalAngle) * cos(verticalAngle))
val targetPos=Float3(0f,0f,0f)
val up=Float3(0f,1f,0f)
return lookAt(camPos,targetPos,up)
}
@JohnGilbertson Thanks for your share. @StevenMohr @grassydragon Does that could help you fix the camera manipulator?
Does that could help you fix the camera manipulator?
It actually replaces the Filament camera manipulator. If Filament doesn't accept pull request to it, maybe the only solution is to copy the classes from Filament and rewrite the camera manipulator completely in Kotlin while supporting restricting the camera angle. It is a little bit sad that in the beginning we aimed to reuse the existing APIs from Filament, went through the difficulties of implementing everything this way and now we have to copy everything from Filament to be able to fix issues and add missing features.
I've got a solution for doing the restriction using the CameraManipulator. I can share it when I'm back from vacation. It's basically saving the camera position and creating a new CameraManipulator whenever the camera is moved to a forbidden position.
Nikita Zaytsev @.***> schrieb am Fr., 27. Mai 2022, 09:13:
Does that could help you fix the camera manipulator?
It actually replaces the Filament camera manipulator. If Filament doesn't accept pull request to it, maybe the only solution is to copy the classes from Filament and rewrite the camera manipulator completely in Kotlin while supporting restricting the camera angle. It is a little be sad that in the beginning we aimed to reuse the existing APIs from Filament, went through the difficulties of implementing everything this way and now we have to copy everything from Filament to be able to fix issues and add missing features.
— Reply to this email directly, view it on GitHub https://github.com/SceneView/sceneview-android/issues/62#issuecomment-1139349753, or unsubscribe https://github.com/notifications/unsubscribe-auth/AXICR5G53DRTVX6V75K4Z5LVMBYZVANCNFSM5TAMVKGQ . You are receiving this because you authored the thread.Message ID: @.***>
Hello Steven, can you please share your solution
Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. Thank you for your contributions.
Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please feel free to create a new issue with up-to-date information.
Hey folks, @StevenMOG @gabrielazwaat Was anyone able to solve this? I need a solution to restrict camera movement on the x and z axis. Would appreciate any help.