Skip to content

Commit

Permalink
Model viewer sample cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasGorisse committed Oct 25, 2023
1 parent 6ce1d93 commit 7272548
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -17,41 +18,17 @@ 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) {

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) {
Expand All @@ -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) {
Expand All @@ -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<ModelAsset, ModelInstance>()
var modelLoadingJob: Job? = null

fun updateInstructions() {
instructionText.text = trackingFailureReason?.let {
it.getDescription(this)
Expand Down Expand Up @@ -132,40 +80,22 @@ class MainActivity : AppCompatActivity(R.layout.activity_main) {
})
instructionText = findViewById(R.id.instructionText)
loadingView = findViewById(R.id.loadingView)
changeModelButton =
findViewById<ExtendedFloatingActionButton>(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<ARSceneView?>(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))
}
}
}
Expand All @@ -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
}
)
}
}
17 changes: 0 additions & 17 deletions samples/ar-model-viewer/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,6 @@
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
android:id="@+id/changeModelButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:backgroundTint="@color/translucent"
android:gravity="center_vertical|start"
android:text="@string/change_model"
android:textColor="@android:color/white"
android:visibility="gone"
app:icon="@drawable/ic_autorenew"
app:iconGravity="textStart"
app:iconTint="@android:color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />

<FrameLayout
android:id="@+id/loadingView"
android:layout_width="0dp"
Expand Down

0 comments on commit 7272548

Please sign in to comment.