google-ar / sceneform-android-sdk

Sceneform SDK for Android
https://developers.google.com/sceneform/develop/
Apache License 2.0
1.23k stars 603 forks source link

Adding an image on vertical plane always rotated. #493

Open ShagunParikh opened 5 years ago

ShagunParikh commented 5 years ago

I am adding an image on a vertical plane. Image is always rotated in the y-axis. It should be stick to vertical planes like walls , door etc. It should look like a photo frame on the wall.

Here is my code: XML code for putting an image as an AR object

<?xml version="1.0" encoding="utf-8"?>
<ImageView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/imageCard"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="2dp">
</ImageView>

Create and initialise view to set image in ViewRenderable

val view = inflater.inflate(R.layout.layout_ar_object_image, null)
Glide.with(this)
                .asDrawable()
                .load(getImageURL)
                .listener(object : RequestListener<Drawable> {
                    override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<Drawable>?, isFirstResource: Boolean): Boolean {
                        FunctionHelper.showToast(this@StartARCameraActivity, getString(R.string.unable_to_read_images))
                        return false
                    }
                    override fun onResourceReady(resource: Drawable?, model: Any?, target: Target<Drawable>?, dataSource: DataSource?, isFirstResource: Boolean): Boolean {
                        val imageBorderDrawable = getDrawable(R.drawable.layout_ar_image_background)
                        imageBorderDrawable.colorFilter = PorterDuffColorFilter(Color.parseColor(colorArrayList[0]), PorterDuff.Mode.SRC_IN)
                        view.arImageLayout.background = imageBorderDrawable
                        view.imageCard.background = resource
                        return true
                    }
                })
                .submit(100, 100)
        ViewRenderable.builder()
                .setView(this, view)
                .build()
                .thenAccept { renderable ->
                    andyRenderable = renderable
                }

// on Tap Listener of AR surface
arFragment.setOnTapArPlaneListener { hitResult: HitResult, plane: Plane, motionEvent: MotionEvent ->
                val anchor = hitResult.createAnchor()
                val anchorNode = AnchorNode(anchor)
                anchorNode.setParent(arFragment.arSceneView.scene)
                andy = TransformableNode(arFragment.transformationSystem)
                if(plane.type == Plane.Type.VERTICAL){
                    val anchorUp = anchorNode.up
                    andy.setLookDirection(Vector3.up() , anchorUp)
                }
                andy.setParent(anchorNode)
                andy.renderable = andyRenderable
                andy.select()
            }
        }

And here is the output:

screenshot_2019-01-10-13-18-09

khurramengr commented 5 years ago

@ShagunParikh did you manage to find any solution to this problem, i am having the same issue.