Skip to content

Instantly share code, notes, and snippets.

@khan-mujeeb
Created August 19, 2023 19:37
Show Gist options
  • Save khan-mujeeb/d747ec7a95bfe37e2c0efa867294b87a to your computer and use it in GitHub Desktop.
Save khan-mujeeb/d747ec7a95bfe37e2c0efa867294b87a to your computer and use it in GitHub Desktop.
ar code
import android.content.Context
import android.graphics.BitmapFactory
import android.media.MediaPlayer
import android.net.Uri
import android.opengl.Visibility
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.core.content.ContentProviderCompat.requireContext
import androidx.core.view.isGone
import com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
import com.google.ar.core.Config
import io.github.sceneview.ar.ArSceneView
import io.github.sceneview.ar.node.ArModelNode
import io.github.sceneview.ar.node.AugmentedImageNode
import io.github.sceneview.ar.node.PlacementMode
import io.github.sceneview.material.setExternalTexture
import io.github.sceneview.math.Position
import io.github.sceneview.math.Rotation
import io.github.sceneview.node.VideoNode
class MainActivity : AppCompatActivity() {
// AR SceneView instance for rendering AR content
private lateinit var sceneView: ArSceneView
// Button for placing the 3D model
lateinit var placeButton: ExtendedFloatingActionButton
// Node for the 3D model
private lateinit var modelNode: ArModelNode
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Initialize UI elements and setup the AR scene
variableInit()
// Subscribe to button click events
subscribeClick()
// Configure and display the UI elements
subscribeUI()
}
// Subscribes the UI elements to the AR scene
private fun subscribeUI() {
// Add the model node to the AR scene
sceneView.addChild(modelNode)
}
// Subscribes the button to click events
private fun subscribeClick() {
placeButton.setOnClickListener {
placeModel()
}
}
// Initialize variables and UI elements
private fun variableInit() {
placeButton = findViewById(R.id.place)
// Find the AR SceneView in the layout and configure it
sceneView = findViewById<ArSceneView?>(R.id.sceneView).apply {
// Disable automatic light estimation
this.lightEstimationMode = Config.LightEstimationMode.DISABLED
}
// Create an AR model node and load the 3D model
modelNode = ArModelNode(
sceneView.engine,
PlacementMode.BEST_AVAILABLE).apply {
// Load the 3D model asynchronously
loadModelGlbAsync(
glbFileLocation = "models/dragon_animation_flying.glb",
scaleToUnits = 2.0f
) {
// Make the plane renderer visible
sceneView.planeRenderer.isVisible = true
// Get the material instance of the model
val materialInstance = it.materialInstances[0]
}
// Set the onAnchorChanged callback
onAnchorChanged = {
// Hide the "Place Model" button if the model is anchored
placeButton.isGone = it != null
}
}
}
// Function to place the model in the AR scene
private fun placeModel() {
// Anchor the model in the scene
modelNode.anchor()
// Hide the plane renderer
sceneView.planeRenderer.isVisible = false
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment