diff --git a/samples/ar-model-viewer/src/main/java/io/github/sceneview/sample/armodelviewer/MainActivity.kt b/samples/ar-model-viewer/src/main/java/io/github/sceneview/sample/armodelviewer/MainActivity.kt index 31890cb7..50f8deb0 100644 --- a/samples/ar-model-viewer/src/main/java/io/github/sceneview/sample/armodelviewer/MainActivity.kt +++ b/samples/ar-model-viewer/src/main/java/io/github/sceneview/sample/armodelviewer/MainActivity.kt @@ -6,9 +6,10 @@ import android.view.ViewGroup import android.widget.TextView import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.widget.Toolbar +import androidx.compose.ui.graphics.Color import androidx.core.view.isGone import androidx.lifecycle.lifecycleScope -import com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton +import com.google.ar.core.Anchor import com.google.ar.core.Config import com.google.ar.core.Plane import com.google.ar.core.TrackingFailureReason @@ -17,11 +18,10 @@ import io.github.sceneview.ar.arcore.getUpdatedPlanes import io.github.sceneview.ar.getDescription import io.github.sceneview.ar.node.AnchorNode import io.github.sceneview.math.Position -import io.github.sceneview.model.ModelInstance +import io.github.sceneview.node.CubeNode import io.github.sceneview.node.ModelNode import io.github.sceneview.sample.doOnApplyWindowInsets import io.github.sceneview.sample.setFullScreen -import kotlinx.coroutines.Job import kotlinx.coroutines.launch class MainActivity : AppCompatActivity(R.layout.activity_main) { @@ -29,29 +29,6 @@ class MainActivity : AppCompatActivity(R.layout.activity_main) { lateinit var sceneView: ARSceneView lateinit var loadingView: View lateinit var instructionText: TextView - lateinit var changeModelButton: ExtendedFloatingActionButton - - data class ModelAsset( - val fileLocation: String, - val scaleUnits: Float? = null - ) - - val modelAssets = listOf( - ModelAsset( - fileLocation = "https://sceneview.github.io/assets/models/DamagedHelmet.glb", - scaleUnits = 0.5f - ), - ModelAsset( - fileLocation = "https://storage.googleapis.com/ar-answers-in-search-models/static/GiantPanda/model.glb", - // Display the Tiger with a size of 1.5 m height - scaleUnits = 1.5f - ), - ModelAsset( - fileLocation = "https://storage.googleapis.com/ar-answers-in-search-models/static/Tiger/model.glb", - // Display the Tiger with a size of 3 m long - scaleUnits = 2.5f - ) - ) var isLoading = false set(value) { @@ -63,13 +40,10 @@ class MainActivity : AppCompatActivity(R.layout.activity_main) { set(value) { if (field != value) { field = value - changeModelButton.isGone = value == null updateInstructions() } } - lateinit var modelNode: ModelNode - var trackingFailureReason: TrackingFailureReason? = null set(value) { if (field != value) { @@ -78,32 +52,6 @@ class MainActivity : AppCompatActivity(R.layout.activity_main) { } } - var modelIndex = 0 - set(value) { - field = value - modelLoadingJob?.cancel() - modelLoadingJob = lifecycleScope.launch { - isLoading = true - val modelAsset = modelAssets[value] - val modelInstance = loadedModels.getOrPut(modelAsset) { - sceneView.modelLoader.loadModelInstance(modelAsset.fileLocation)!! - } - modelNode.setModelInstance( - modelInstance = modelInstance, - autoAnimate = true, - scaleToUnits = modelAsset.scaleUnits, - // Place the model origin at the bottom center - centerOrigin = Position(y = -1.0f) - ) - - isLoading = false - modelLoadingJob = null - } - } - - val loadedModels = mutableMapOf() - var modelLoadingJob: Job? = null - fun updateInstructions() { instructionText.text = trackingFailureReason?.let { it.getDescription(this) @@ -132,40 +80,22 @@ class MainActivity : AppCompatActivity(R.layout.activity_main) { }) instructionText = findViewById(R.id.instructionText) loadingView = findViewById(R.id.loadingView) - changeModelButton = - findViewById(R.id.changeModelButton).apply { - // Add system bar margins - val bottomMargin = (layoutParams as ViewGroup.MarginLayoutParams).bottomMargin - doOnApplyWindowInsets { systemBarsInsets -> - (layoutParams as ViewGroup.MarginLayoutParams).bottomMargin = - systemBarsInsets.bottom + bottomMargin - } - setOnClickListener { - modelIndex = ((modelIndex + 1) % modelAssets.size) - } - } - sceneView = findViewById(R.id.sceneView) - sceneView.apply { + sceneView = findViewById(R.id.sceneView).apply { + planeRenderer.isEnabled = true configureSession { session, config -> - config.depthMode = Config.DepthMode.AUTOMATIC + config.depthMode = when (session.isDepthModeSupported(Config.DepthMode.AUTOMATIC)) { + true -> Config.DepthMode.AUTOMATIC + else -> Config.DepthMode.DISABLED + } config.instantPlacementMode = Config.InstantPlacementMode.DISABLED config.lightEstimationMode = Config.LightEstimationMode.ENVIRONMENTAL_HDR } - modelNode = ModelNode(engine = engine).apply { - isEditable = true - } - modelIndex = 0 - onSessionUpdated = { _, frame -> + onSessionUpdate = { _, frame -> if (anchorNode == null) { frame.getUpdatedPlanes() .firstOrNull { it.type == Plane.Type.HORIZONTAL_UPWARD_FACING } ?.let { plane -> - addChildNode( - AnchorNode(engine, plane.createAnchor(plane.centerPose)) - .apply { isEditable = true } - .also { anchorNode = it } - .addChildNode(modelNode) - ) + addAnchorNode(plane.createAnchor(plane.centerPose)) } } } @@ -174,4 +104,33 @@ class MainActivity : AppCompatActivity(R.layout.activity_main) { } } } + + fun addAnchorNode(anchor: Anchor) { + sceneView.addChildNode( + AnchorNode(sceneView.engine, anchor) + .apply { + isEditable = true + lifecycleScope.launch { + isLoading = true + sceneView.modelLoader.loadModelInstance( + "https://sceneview.github.io/assets/models/DamagedHelmet.glb" + )?.let { modelInstance -> + addChildNode( + ModelNode( + modelInstance = modelInstance, + // Scale to fit in a 0.5 meters cube + scaleToUnits = 0.5f, + // Bottom origin instead of center so the model base is on floor + centerOrigin = Position(y = -1.0f) + ).apply { + isEditable = true + } + ) + } + isLoading = false + } + anchorNode = this + } + ) + } } \ No newline at end of file diff --git a/samples/ar-model-viewer/src/main/res/layout/activity_main.xml b/samples/ar-model-viewer/src/main/res/layout/activity_main.xml index 6bdf7194..6e6439d2 100644 --- a/samples/ar-model-viewer/src/main/res/layout/activity_main.xml +++ b/samples/ar-model-viewer/src/main/res/layout/activity_main.xml @@ -15,23 +15,6 @@ app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> - -