Xamarin android custom view with arcore

Viewed 381

I looked at this video to make a simple AR with Arcore for Android:

Augmented Reality with Arcore

Everything went well, but now I want to see this in xamarin forms, in a custom view. But I can not :(

what i tried last: In Forms:

   <?xml version="1.0" encoding="utf-8" ?>
   <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:controls="clr-namespace:FormsAR.Controls"
         x:Class="FormsAR.MainPage">

    <StackLayout BackgroundColor="Red"  HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
        <Label BackgroundColor="Blue" TextColor="White" Text="JUST TEST"/>
        <controls:ARView HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" BackgroundColor="Yellow"/>
    </StackLayout>

 </ContentPage>

And:

using System;
using Xamarin.Forms;

namespace FormsAR.Controls
{
    public class ARView : View
    {
    }
}

And Android:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
    android:id="@+id/fragmentAR"
    android:name="com.google.ar.sceneform.ux.ArFragment"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/>
</LinearLayout>

and finally:

[assembly: ExportRenderer(typeof(ARView), typeof(ARViewRenderer))]
namespace FormsAR.Droid.Controls
{
public class ARViewRenderer : ViewRenderer<ARView, Android.Views.View>
{      
    private ArFragment arFragment;
    private Material readyColor;
    Android.Views.View _view;

    public ARViewRenderer(Context context) : base(context){}

    protected override void OnElementChanged(ElementChangedEventArgs<ARView> e)
    {
        base.OnElementChanged(e);
        _view = LayoutInflater.From(this.Context).Inflate(Resource.Layout.ARLayout, null, false);
        AddView(_view);
        if (Control == null)
        {
            Initialize();    
        } 
    }

    private void Initialize()
    {
        var activity = this.Context as AppCompatActivity;
        arFragment = (ArFragment)activity.SupportFragmentManager.FindFragmentById(Resource.Id.fragmentAR);
        setupPlane();
        MaterialFactory.MakeOpaqueWithColor(Context, new Google.AR.Sceneform.Rendering.Color(Android.Graphics.Color.Yellow)).GetAsync().ContinueWith(materialTask =>
        {
            readyColor = (Material)materialTask.Result;
        });
    }

    private void setupPlane()
    {
        arFragment.TapArPlane += (sender, args) => this.OnTapArPlaneListener(args.HitResult, args.Plane, args.MotionEvent);
    }

    private void OnTapArPlaneListener(HitResult hitResult, Plane plane, MotionEvent motionEvent)
    {
        Anchor anchor = hitResult.CreateAnchor();
        AnchorNode anchorNode = new AnchorNode(anchor);
        anchorNode.SetParent(arFragment.ArSceneView.Scene);
        CreateModel(anchorNode);
    }

    private void CreateModel(AnchorNode anchorNode)
    {
        TransformableNode node = new TransformableNode(arFragment.TransformationSystem);
        node.SetParent(anchorNode);
        var nodeRenderable = ShapeFactory.MakeSphere(0.08f, new Google.AR.Sceneform.Math.Vector3(0.0f, 0.15f, 0.0f), readyColor);
        node.Renderable = nodeRenderable;
        node.Select();
    }
}       

}

but i can't see: image result

Link for project

Can you please help me identify where error?

0 Answers
Related