-
- All Implemented Interfaces:
-
android.view.View.OnTouchListener
public final class ModelViewer implements View.OnTouchListener
Helps render glTF models into a SurfaceView or TextureView with an orbit controller.
ModelViewerowns a Filament engine, renderer, swapchain, view, and scene. It allows clients to access these objects via read-only properties. The viewer can display only one glTF scene at a time, which can be scaled and translated into the viewing frustum by calling transformToUnitCube. All ECS entities can be accessed and modified via the asset property.For GLB files, clients can call loadModelGlb and pass in a Buffer with the contents of the GLB file. For glTF files, clients can call loadModelGltf and pass in a Buffer with the JSON contents, as well as a callback for loading external resources.
ModelViewerreduces much of the boilerplate required for simple Filament applications, but clients still have the responsibility of adding an IndirectLight and Skybox to the scene. Additionally, clients should:Pass the model viewer into SurfaceView.setOnTouchListener or call its onTouchEvent method from your touch handler.
Call render and Animator.applyAnimation from a
Choreographerframe callback.
NOTE: if its associated SurfaceView or TextureView has become detached from its window, the ModelViewer becomes invalid and must be recreated.
See
sample-gltf-viewerfor a usage example.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description public final classModelViewer.SurfaceCallbackpublic classModelViewer.Companion
-
Field Summary
Fields Modifier and Type Field Description private FilamentAssetassetprivate Animatoranimatorprivate final Floatprogressprivate BooleannormalizeSkinningWeightsprivate FloatcameraFocalLengthprivate FloatcameraNearprivate FloatcameraFarprivate final Scenesceneprivate final Viewviewprivate final Cameracameraprivate final Rendererrendererprivate final Integerlightprivate final Engineengine
-
Constructor Summary
Constructors Constructor Description ModelViewer(SurfaceView surfaceView, Engine engine, UiHelper uiHelper, Manipulator manipulator)ModelViewer(TextureView textureView, Engine engine, UiHelper uiHelper, Manipulator manipulator)ModelViewer(Engine engine, UiHelper uiHelper)
-
Method Summary
Modifier and Type Method Description final FilamentAssetgetAsset()final UnitsetAsset(FilamentAsset asset)final AnimatorgetAnimator()final UnitsetAnimator(Animator animator)final FloatgetProgress()final BooleangetNormalizeSkinningWeights()final UnitsetNormalizeSkinningWeights(Boolean normalizeSkinningWeights)final FloatgetCameraFocalLength()final UnitsetCameraFocalLength(Float cameraFocalLength)final FloatgetCameraNear()final UnitsetCameraNear(Float cameraNear)final FloatgetCameraFar()final UnitsetCameraFar(Float cameraFar)final ScenegetScene()final ViewgetView()final CameragetCamera()final RenderergetRenderer()final IntegergetLight()final EnginegetEngine()final UnitloadModelGlb(Buffer buffer)Loads a monolithic binary glTF and populates the Filament scene. final UnitloadModelGltf(Buffer buffer, Function1<String, Buffer> callback)Loads a JSON-style glTF file and populates the Filament scene. final UnitloadModelGltfAsync(Buffer buffer, Function1<String, Buffer> callback)Loads a JSON-style glTF file and populates the Filament scene. final UnittransformToUnitCube(Float3 centerPoint)Sets up a root transform on the current model to make it fit into a unit cube. final UnitclearRootTransform()Removes the transformation that was set up via transformToUnitCube. final UnitdestroyModel()Frees all entities associated with the most recently-loaded model. final Unitrender(Long frameTimeNanos)Renders the model and updates the Filament camera. final UnitonTouchEvent(MotionEvent event)Handles a MotionEvent to enable one-finger orbit, two-finger pan, and pinch-to-zoom. BooleanonTouch(View view, MotionEvent event)-
-
Constructor Detail
-
ModelViewer
ModelViewer(SurfaceView surfaceView, Engine engine, UiHelper uiHelper, Manipulator manipulator)
-
ModelViewer
ModelViewer(TextureView textureView, Engine engine, UiHelper uiHelper, Manipulator manipulator)
-
ModelViewer
ModelViewer(Engine engine, UiHelper uiHelper)
-
-
Method Detail
-
getAsset
final FilamentAsset getAsset()
-
getAnimator
final Animator getAnimator()
-
setAnimator
final Unit setAnimator(Animator animator)
-
getProgress
final Float getProgress()
-
getNormalizeSkinningWeights
final Boolean getNormalizeSkinningWeights()
-
setNormalizeSkinningWeights
final Unit setNormalizeSkinningWeights(Boolean normalizeSkinningWeights)
-
getCameraFocalLength
final Float getCameraFocalLength()
-
setCameraFocalLength
final Unit setCameraFocalLength(Float cameraFocalLength)
-
getCameraNear
final Float getCameraNear()
-
setCameraNear
final Unit setCameraNear(Float cameraNear)
-
getCameraFar
final Float getCameraFar()
-
setCameraFar
final Unit setCameraFar(Float cameraFar)
-
getScene
final Scene getScene()
-
getView
final View getView()
-
getCamera
final Camera getCamera()
-
getRenderer
final Renderer getRenderer()
-
getEngine
final Engine getEngine()
-
loadModelGlb
final Unit loadModelGlb(Buffer buffer)
Loads a monolithic binary glTF and populates the Filament scene.
-
loadModelGltf
final Unit loadModelGltf(Buffer buffer, Function1<String, Buffer> callback)
Loads a JSON-style glTF file and populates the Filament scene.
The given callback is triggered for each requested resource.
-
loadModelGltfAsync
final Unit loadModelGltfAsync(Buffer buffer, Function1<String, Buffer> callback)
Loads a JSON-style glTF file and populates the Filament scene.
The given callback is triggered from a worker thread for each requested resource.
-
transformToUnitCube
final Unit transformToUnitCube(Float3 centerPoint)
Sets up a root transform on the current model to make it fit into a unit cube.
- Parameters:
centerPoint- Coordinate of center point of unit cube, defaults to < 0, 0, -4 >
-
clearRootTransform
final Unit clearRootTransform()
Removes the transformation that was set up via transformToUnitCube.
-
destroyModel
final Unit destroyModel()
Frees all entities associated with the most recently-loaded model.
-
render
final Unit render(Long frameTimeNanos)
Renders the model and updates the Filament camera.
- Parameters:
frameTimeNanos- time in nanoseconds when the frame started being rendered, typically comes from {@link android.view.Choreographer.
-
onTouchEvent
final Unit onTouchEvent(MotionEvent event)
Handles a MotionEvent to enable one-finger orbit, two-finger pan, and pinch-to-zoom.
-
onTouch
Boolean onTouch(View view, MotionEvent event)
-
-
-
-