@@ -106,8 +106,8 @@ class ModelViewer(
106106 var skyboxCubemap: Texture ? = null
107107
108108 private lateinit var displayHelper: DisplayHelper
109- private lateinit var cameraManipulator: Manipulator
110- private lateinit var gestureDetector: GestureDetector
109+ private var cameraManipulator: Manipulator ? = null
110+ private var gestureDetector: GestureDetector ? = null
111111 private var surfaceView: SurfaceView ? = null
112112 private var textureView: TextureView ? = null
113113
@@ -157,15 +157,13 @@ class ModelViewer(
157157 surfaceView: SurfaceView ,
158158 engine: Engine = Engine .create(),
159159 uiHelper: UiHelper = UiHelper (UiHelper .ContextErrorPolicy .DONT_CHECK ),
160- manipulator: Manipulator ? = null
160+ manipulator: Manipulator ? = defaultCameraManipulator(surfaceView.width, surfaceView.height)
161161 ) : this (engine, uiHelper) {
162- cameraManipulator = manipulator ? : Manipulator .Builder ()
163- .targetPosition(kDefaultObjectPosition.x, kDefaultObjectPosition.y, kDefaultObjectPosition.z)
164- .viewport(surfaceView.width, surfaceView.height)
165- .build(Manipulator .Mode .ORBIT )
166-
167162 this .surfaceView = surfaceView
168- gestureDetector = GestureDetector (surfaceView, cameraManipulator)
163+ cameraManipulator = manipulator
164+ cameraManipulator?.let { c ->
165+ gestureDetector = GestureDetector (surfaceView, c)
166+ }
169167 displayHelper = DisplayHelper (surfaceView.context)
170168 uiHelper.renderCallback = SurfaceCallback ()
171169 uiHelper.attachTo(surfaceView)
@@ -177,15 +175,14 @@ class ModelViewer(
177175 textureView: TextureView ,
178176 engine: Engine = Engine .create(),
179177 uiHelper: UiHelper = UiHelper (UiHelper .ContextErrorPolicy .DONT_CHECK ),
180- manipulator: Manipulator ? = null
178+ manipulator: Manipulator ? = defaultCameraManipulator(textureView.width, textureView.height)
181179 ) : this (engine, uiHelper) {
182- cameraManipulator = manipulator ? : Manipulator .Builder ()
183- .targetPosition(kDefaultObjectPosition.x, kDefaultObjectPosition.y, kDefaultObjectPosition.z)
184- .viewport(textureView.width, textureView.height)
185- .build(Manipulator .Mode .ORBIT )
186-
180+ cameraManipulator = manipulator
187181 this .textureView = textureView
188- gestureDetector = GestureDetector (textureView, cameraManipulator)
182+ cameraManipulator = manipulator
183+ cameraManipulator?.let { c ->
184+ gestureDetector = GestureDetector (textureView, c)
185+ }
189186 displayHelper = DisplayHelper (textureView.context)
190187 uiHelper.renderCallback = SurfaceCallback ()
191188 uiHelper.attachTo(textureView)
@@ -302,11 +299,13 @@ class ModelViewer(
302299 asset?.let { populateScene(it) }
303300
304301 // Extract the camera basis from the helper and push it to the Filament camera.
305- cameraManipulator.getLookAt(eyePos, target, upward)
306- camera.lookAt(
302+ cameraManipulator?.let { cm ->
303+ cm.getLookAt(eyePos, target, upward)
304+ camera.lookAt(
307305 eyePos[0 ], eyePos[1 ], eyePos[2 ],
308306 target[0 ], target[1 ], target[2 ],
309307 upward[0 ], upward[1 ], upward[2 ])
308+ }
310309
311310 // Render the scene, unless the renderer wants to skip the frame.
312311 if (renderer.beginFrame(swapChain!! , frameTimeNanos)) {
@@ -398,7 +397,7 @@ class ModelViewer(
398397 * Handles a [MotionEvent] to enable one-finger orbit, two-finger pan, and pinch-to-zoom.
399398 */
400399 fun onTouchEvent (event : MotionEvent ) {
401- gestureDetector.onTouchEvent(event)
400+ gestureDetector? .onTouchEvent(event)
402401 }
403402
404403 @SuppressWarnings(" ClickableViewAccessibility" )
@@ -451,7 +450,7 @@ class ModelViewer(
451450
452451 override fun onResized (width : Int , height : Int ) {
453452 view.viewport = Viewport (0 , 0 , width, height)
454- cameraManipulator.setViewport(width, height)
453+ cameraManipulator? .setViewport(width, height)
455454 updateCameraProjection()
456455 synchronizePendingFrames(engine)
457456 }
@@ -468,5 +467,11 @@ class ModelViewer(
468467
469468 companion object {
470469 private val kDefaultObjectPosition = Float3 (0.0f , 0.0f , - 4.0f )
470+ private fun defaultCameraManipulator (width : Int , height : Int ) : Manipulator {
471+ return Manipulator .Builder ()
472+ .targetPosition(kDefaultObjectPosition.x, kDefaultObjectPosition.y, kDefaultObjectPosition.z)
473+ .viewport(width, height)
474+ .build(Manipulator .Mode .ORBIT )
475+ }
471476 }
472477}
0 commit comments