Xamarin Forms - Urho - 在页面上创建场景

时间:2016-11-24 03:36:35

标签: c# xamarin urhosharp urho3d

这是我第一次使用urhosharp并且我遇到了一些问题。我尝试了一些示例示例,但我的应用程序崩溃了。

我安装了nuget包UrhoSharp.Forms

我只是想在中间创建一个可以360度旋转的场景。

这是我的页面:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Urho;
using Urho.Forms;

using Urho.Resources;
using Urho.Gui;

using Xamarin.Forms;

namespace testApp.Pages.Urho
{
    public partial class urhoPage : ContentPage
    {

        Scene scene;
        Camera camera;

        protected Node CameraNode { get; set; }


        public urhoPage()
        {
            InitializeComponent();

            scene = new Scene();

            scene.CreateComponent<Octree>();
            scene.CreateComponent<DebugRenderer>();

            var planeNode = scene.CreateChild("Plane");
            planeNode.Scale = new Vector3(100, 1, 100);
            var planeObject = planeNode.CreateComponent<StaticModel>();

            // Create a Zone component for ambient lighting & fog control
            var zoneNode = scene.CreateChild("Zone");
            var zone = zoneNode.CreateComponent<Zone>();

            // Set same volume as the Octree, set a close bluish fog and some ambient light
            zone.SetBoundingBox(new BoundingBox(-1000.0f, 1000.0f));
            zone.AmbientColor = new Urho.Color(0.15f, 0.15f, 0.15f);
            zone.FogColor = new Urho.Color(0.5f, 0.5f, 0.7f);
            zone.FogStart = 100;
            zone.FogEnd = 300;

            // Create the camera. Limit far clip distance to match the fog
            CameraNode = scene.CreateChild("Camera");
            camera = CameraNode.CreateComponent<Camera>();
            camera.FarClip = 300;

            // Set an initial position for the camera scene node above the plane
            CameraNode.Position = new Vector3(0.0f, 5.0f, 0.0f);

           // var renderer = Renderer;
            //renderer.SetViewport(0, new Viewport(Context, scene, camera, null));


        }
    }
}

由于我收到错误,我不得不删除这两行。渲染器和上下文未设置。我从没有使用页面的功能样本中获得了

// var renderer = Renderer; //renderer.SetViewport(0,new Viewport(Context,scene,camera,null));

1 个答案:

答案 0 :(得分:0)

  1. 浮动您的平面节点比例,FogStart,FogEnd和FarClip的Vector3()应该是浮点数。

  2. 如果我们不知道您的视口的上下文,Urho肯定会崩溃。

  3. 使用typename:Node,Component,Renderer替换'var',提高应用的稳定性。

    using Xamarin.Forms;
    namespace testApp.Pages.Urho
    {
    public partial class urhoPage : ContentPage
    {
    Scene scene;
    Camera camera;
    
    protected Node CameraNode { get; set; }
    
    
    public urhoPage()
    {
        InitializeComponent();
    
        scene = new Scene();
    
        scene.CreateComponent<Octree>();
        scene.CreateComponent<DebugRenderer>();
    
        var planeNode = scene.CreateChild("Plane");
        planeNode.Scale = new Vector3(100f, 1f, 100f);
        var planeObject = planeNode.CreateComponent<StaticModel>();
    
        // Create a Zone component for ambient lighting & fog control
        var zoneNode = scene.CreateChild("Zone");
        var zone = zoneNode.CreateComponent<Zone>();
    
        // Set same volume as the Octree, set a close bluish fog and some ambient light
        zone.SetBoundingBox(new BoundingBox(-1000.0f, 1000.0f));
        zone.AmbientColor = new Urho.Color(0.15f, 0.15f, 0.15f);
        zone.FogColor = new Urho.Color(0.5f, 0.5f, 0.7f);
        zone.FogStart = 100f;
        zone.FogEnd = 300f;
    
        // Create the camera. Limit far clip distance to match the fog
        CameraNode = new Node();
        Camera camera = CameraNode.CreateComponent<Camera>();
        camera.FarClip = 300.0f;
    
        // Set an initial position for the camera scene node above the plane
        CameraNode.Position = new Vector3(0.0f, 5.0f, 0.0f);
    
        var renderer = Application.Current.Renderer;
        renderer.SetViewport(0, new Viewport(Application.CurrentContext, scene, CameraNode.GetComponent<Camera>(), null));
    }
    
    }}