protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            //check to see if the device supports SceneForm
            if (!CheckIsSupportedDeviceOrFinish(this))
            {
                return;
            }

            //set the content
            SetContentView(Resource.Layout.activity_ux);

            //set the fragment
            arFragment = (ArFragment)SupportFragmentManager.FindFragmentById(Resource.Id.ux_fragment);

            //load and build the model
            ((ModelRenderable.Builder)ModelRenderable.InvokeBuilder().SetSource(this, Resource.Raw.andy)).Build(((renderable) =>
            {
                andyRenderable = renderable;
            }));

            //add the event handler
            arFragment.TapArPlane += OnTapArPlane;
        }
 /// <summary>
 /// Loads the 3D models to use
 /// </summary>
 public void LoadModels()
 {
     ModelRenderable.InvokeBuilder().SetSource(this.context, Resource.Raw.andy).Build(renderable =>
     {
         modelRenderable = renderable;
     });
 }
        // CompletableFuture requires api level 24
        // FutureReturnValueIgnored is not valid
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            if (!CheckIsSupportedDeviceOrFinish(this))
            {
                return;
            }

            SetContentView(Resource.Layout.activity_main);
            arFragment = (ArFragment)SupportFragmentManager.FindFragmentById(Resource.Id.ux_fragment);

            // When you build a Renderable, Sceneform loads its resources in the background while returning
            // a CompletableFuture. Call thenAccept(), handle(), or check isDone() before calling get().
            ModelRenderable.InvokeBuilder()
            .SetSource(this, Resource.Raw.andy)
            .Build()
            .ThenAccept(new Consumer(t => andyRenderable = (ModelRenderable)t))
            .Exceptionally(new Function(_ =>
            {
                Toast toast = Toast.MakeText(this, "Unable to load andy renderable", ToastLength.Long);
                toast.SetGravity(GravityFlags.Center, 0, 0);
                toast.Show();
                return(null);
            }
                                        ));

            arFragment.SetOnTapArPlaneListener(new OnTapArPlaneListener((hitResult, plane, motionEvent) =>
            {
                if (andyRenderable == null)
                {
                    return;
                }

                // Create the Anchor.
                Anchor anchor         = hitResult.CreateAnchor();
                AnchorNode anchorNode = new AnchorNode(anchor);
                anchorNode.SetParent(arFragment.ArSceneView.Scene);

                // Create the transformable andy and add it to the anchor.
                TransformableNode andy = new TransformableNode(arFragment.TransformationSystem);
                andy.SetParent(anchorNode);
                andy.Renderable = andyRenderable;
                andy.Select();
            }
                                                                        ));
        }
Esempio n. 4
0
        /// <summary>
        /// Gets a reference to the ViewModel and the ArFragment
        /// </summary>
        /// <param name="e"></param>
        protected override void OnElementChanged(ElementChangedEventArgs <Xamarin.Forms.Page> e)
        {
            base.OnElementChanged(e);
            var activity = this.Context as Activity;

            this.viewModel = this.Element.BindingContext as ARViewModel;

            this.view = activity.LayoutInflater.Inflate(Resource.Layout.ARLayout, this, false);
            AddView(this.view);

            this.arFragment = activity.GetFragmentManager().FindFragmentById(Resource.Id.ar_fragment) as ArFragment;
            if (this.arFragment != null)
            {
                ModelRenderable.InvokeBuilder().SetSource(this.context, Resource.Raw.andy).Build(renderable =>
                {
                    andyRenderable = renderable;
                });
                arFragment.TapArPlane += OnTapArPlane;
            }
        }
Esempio n. 5
0
        /**
         * Starts loading the model specified. The result of the loading is returned asynchrounously via
         * {@link MainActivity#setRenderable(int, ModelRenderable)} or {@link
         * MainActivity#onException(int, Throwable)}.
         *
         * <p>Multiple models can be loaded at a time by specifying separate ids to differentiate the
         * result on callback.
         *
         * @param id the id for this call to loadModel.
         * @param resourceId the resource id of the .sfb to load.
         * @return true if loading was initiated.
         */
        internal bool LoadModel(int id, int resourceId)
        {
            MainActivity activity = (MainActivity)owner.Get();

            if (activity == null)
            {
                Log.Debug(TAG, "Activity is null.  Cannot load model.");
                return(false);
            }
            CompletableFuture future =
                (CompletableFuture)ModelRenderable.InvokeBuilder()
                .SetSource((MainActivity)owner.Get(), resourceId)
                .Build()
                .ThenApply(new Function(renderable => this.SetRenderable(id, (ModelRenderable)renderable)))
                .Exceptionally(new Function(throwable => this.OnException(id, (Throwable)(IJavaObject)throwable)));

            if (future != null)
            {
                futureSet.Put(id, future);
            }
            return(future != null);
        }