У меня проблема с тем, что объект 3d не отображается И в моем журнале не регистрируется никогда ошибка
Я пытался написать код несколькими способами, но он не работает. Я не знаю, если что-то отсутствует в моем коде. Я уже давно устал, когда пытался решить эту проблему
lateinit var aRFragment: customARfragment
private var shouldAddPlane = true
private var shouldAddXwing = true
private lateinit var selectObjectUri: Uri
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
aRFragment =
supportFragmentManager.findFragmentById(customFragmentView.id) as customARfragment
aRFragment.let { customARfragment ->
customARfragment.planeDiscoveryController.hide()
customARfragment.arSceneView.scene.addOnUpdateListener(this::onUpdateFrame)
}
}
fun setAugmentdeImageDb(config: Config, session: Session): Boolean {
val augmentedImageDatabase = AugmentedImageDatabase(session)
augmentedImageDatabase.addImage("sfondo", loadAugmentedImages("sfondo.jpg"))
augmentedImageDatabase.addImage("sfondoo", loadAugmentedImages("sfondoo.jpg"))
config.augmentedImageDatabase = augmentedImageDatabase
return true
}
private fun loadAugmentedImages(imagePath: String): Bitmap? {
val assetManger = assets
val inputStream: InputStream = assetManger.open(imagePath)
return BitmapFactory.decodeStream(inputStream)
}
fun onUpdateFrame(frameTime: FrameTime) {
// get the frame from fragment
val aRframe: Frame? = aRFragment.arSceneView.arFrame
aRframe?.let { frame ->
var augmentdeImages: Collection<AugmentedImage> =
frame.getUpdatedTrackables(AugmentedImage::class.java)
for (ugmentdeImage in augmentdeImages) {
//Check the augmentedImages state
if (ugmentdeImage.trackingState == TrackingState.TRACKING && (shouldAddPlane || shouldAddXwing)) {
//when the Augmanted Images is tracking
Log.d("LOL", "Tracking")
when (ugmentdeImage.name) {
"sfondo" -> {
setModePath("1462_TheArmoredTank_LODa.sfb")
placeObject(
aRFragment,
ugmentdeImage.createAnchor(ugmentdeImage.centerPose),
selectObjectUri
)
shouldAddPlane = false
}
"sfondoo" -> {
setModePath("Andy.sfb")
placeObject(
aRFragment,
ugmentdeImage.createAnchor(ugmentdeImage.centerPose),
selectObjectUri
)
shouldAddPlane = false
}
}
}
}
}
}
private fun placeObject(fragment: ArFragment, anchor: Anchor, modelUri: Uri) {
// val modelRenderable =
ModelRenderable.builder()
.setSource((fragment.requireContext()), modelUri)
.build()
.thenAccept { renderableObject -> addNodeToScene(fragment, anchor, renderableObject) }
.exceptionally {
Toast.makeText(applicationContext, "Error", Toast.LENGTH_SHORT).show()
null
}
}
private fun addNodeToScene(fragment: ArFragment, anchor: Anchor, renderableObject: Renderable) {
var anchorMode = AnchorNode(anchor)
val transformableNode = TransformableNode(fragment.transformationSystem)
transformableNode.renderable = renderableObject
transformableNode.setParent(anchorMode)
fragment.arSceneView.scene.addChild(anchorMode)
transformableNode.select()
}
private fun setModePath(renderableObject: String) {
selectObjectUri = Uri.parse(renderableObject)
}
override fun onResume() {
super.onResume()
var arSession = aRFragment.arSceneView.session
if (arSession == null) {
arSession = Session(this@MainActivity)
}
val config = arSession.config
if (config?.focusMode == Config.FocusMode.FIXED) {
config.focusMode = Config.FocusMode.AUTO
}
config?.updateMode = UpdateMode.LATEST_CAMERA_IMAGE
arSession.configure(config)
aRFragment.arSceneView?.setupSession(arSession)
arSession.resume()
// это другой класс
class customARfragment:ArFragment(){
override fun getSessionConfiguration(session: Session?): Config {
// return super.getSessionConfiguration(session)
planeDiscoveryController.setInstructionView(null)
val config = Config(session)
session?.configure(config)
arSceneView.setupSession(session)
session?.let {safesession ->
if((requireActivity()as MainActivity).setAugmentdeImageDb(config, safesession))
{
Log.d("SetUpAugImageDb", "Success")
}else{
Log.d("SetUpAugImageDb", "faillure")
}
}
return config
}