sceneview-android
sceneview-android copied to clipboard
Is there a way to display text in the ARScene using SceneView? If so, how can I do it?
I'm having trouble figuring out how to display text in the ARScene. This is my MainActivity class class MainActivity : AppCompatActivity(R.layout.activity_main) {
lateinit var sceneView: ARSceneView
lateinit var loadingView: View
lateinit var instructionText: TextView
lateinit var viewAttachmentManager: ViewAttachmentManager
var isLoading = false
set(value) {
field = value
loadingView.isGone = !value
}
var anchorNode: AnchorNode? = null
set(value) {
if (field != value) {
field = value
updateInstructions()
}
}
var trackingFailureReason: TrackingFailureReason? = null
set(value) {
if (field != value) {
field = value
updateInstructions()
}
}
fun updateInstructions() {
instructionText.text = trackingFailureReason?.let {
it.getDescription(this)
} ?: if (anchorNode == null) {
getString(R.string.point_your_phone_down)
} else {
null
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setFullScreen(
findViewById(R.id.rootView),
fullScreen = true,
hideSystemBars = false,
fitsSystemWindows = false
)
setSupportActionBar(findViewById<Toolbar>(R.id.toolbar)?.apply {
doOnApplyWindowInsets { systemBarsInsets ->
(layoutParams as ViewGroup.MarginLayoutParams).topMargin = systemBarsInsets.top
}
title = ""
})
instructionText = findViewById(R.id.instructionText)
loadingView = findViewById(R.id.loadingView)
sceneView = findViewById<ARSceneView?>(R.id.sceneView).apply {
planeRenderer.isEnabled = true
configureSession { session, config ->
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
}
onSessionUpdated = { _, frame ->
if (anchorNode == null) {
frame.getUpdatedPlanes()
.firstOrNull { it.type == Plane.Type.VERTICAL }
?.let { plane ->
addAnchorNode(plane.createAnchor(plane.centerPose))
}
}
}
onTrackingFailureChanged = { reason ->
[email protected] = reason
}
}
viewAttachmentManager = ViewAttachmentManager(this@MainActivity, sceneView)
}
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 = -0.5f)
).apply {
isEditable = true
}
)
}
isLoading = false
}
anchorNode = this
val viewNode = ViewNode(
sceneView.engine,
sceneView.modelLoader,
viewAttachmentManager = viewAttachmentManager,
).apply {
isEditable = true
loadView(this@MainActivity, R.layout.place_view)
isVisible= true
scale = Float3(0.1f)
position.y= 0.05f
onViewLoaded = {_, _ ->
Log.d(TAG, "View Loaded!")
anchorNode?.addChildNode(this)
}
}
}
)
}
}
This is my activity_main.xml file
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/rootView" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="io.github.sceneview.sample.armodelviewer.MainActivity">
<io.github.sceneview.ar.ARSceneView
android:id="@+id/sceneView"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<FrameLayout
android:id="@+id/loadingView"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#40000000"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.progressindicator.CircularProgressIndicator
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:indeterminate="true"
app:indicatorSize="64dp"
app:trackColor="#3FFFFFFF" />
</FrameLayout>
<TextView
android:id="@+id/instructionText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:gravity="center"
android:paddingStart="32dp"
android:paddingEnd="32dp"
android:shadowColor="@android:color/black"
android:shadowRadius="8"
android:text="@string/point_your_phone_down"
android:textColor="@android:color/white"
android:textSize="28sp"
android:visibility="visible"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/toolbar" />
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/toolbar"
android:layout_width="0dp"
android:layout_height="?actionBarSize"
android:fitsSystemWindows="true"
android:minHeight="?attr/actionBarSize"
android:theme="@style/Theme.SceneViewSample.Toolbar"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
This is my place_view.xml file
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="8dp" android:background="@android:color/holo_orange_dark" >
<TextView
android:id="@+id/placeName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="34sp"
android:text="place"
android:textColor="@android:color/holo_red_light"
/>
I cannot figure it out why the text is not displaying here ?