xamarin / urho

Code to integrate with the Urho3D engine
Other
462 stars 122 forks source link

Scene in UWP renders black, unless the screen is resized. #159

Open johanlindfors opened 8 years ago

johanlindfors commented 8 years ago

Trying to create minimal app with UrhoSharp to understand the setup and overall architecture, but a simple scene just gets rendered as a black screen, unless I resize the window on my computer, then occasionally the screen flickers and renders the currently spinning cube for one frame, then black again.

This is pretty much all of the code:

public class Game : Urho.Application
{
    Scene scene;
    Node cameraNode;

    public Game() : base() { }
    public Game(ApplicationOptions options) : base(options) { }

    protected override async void Start()
    {
        scene = new Scene();
        scene.CreateComponent<Octree>();
        // Box
        Node boxNode = scene.CreateChild();
        boxNode.Position = new Vector3(0, 0, 5);
        boxNode.Rotation = new Quaternion(60, 0, 30);
        boxNode.SetScale(0f);
        StaticModel modelObject = boxNode.CreateComponent<StaticModel>();
        modelObject.Model = ResourceCache.GetModel("Models/Box.mdl");
        // Light
        Node lightNode = scene.CreateChild(name: "light");
        lightNode.SetDirection(new Vector3(0.6f, -1.0f, 0.8f));
        lightNode.CreateComponent<Light>();
        // Camera
        cameraNode = scene.CreateChild(name: "camera");
        Camera camera = cameraNode.CreateComponent<Camera>();
        // Viewport
        Renderer.SetViewport(0, new Viewport(scene, camera, null));
        // Perform some actions
        await boxNode.RunActionsAsync(
            new EaseBounceOut(new ScaleTo(duration: 1f, scale: 1)));
        await boxNode.RunActionsAsync(
            new RepeatForever(new RotateBy(duration: 1,
                deltaAngleX: 90, deltaAngleY: 0, deltaAngleZ: 0)));

        SetupViewport();
    }

    void SetupViewport()
    {
        var renderer = Renderer;
        renderer.SetViewport(0, new Viewport(Context, scene, cameraNode.GetComponent<Camera>(), null));
    }
}

public sealed partial class MainPage
{
    Urho.Application game;
    public MainPage()
    {
        this.InitializeComponent();

        Loaded += delegate {
            game = urhoSurface.Run<Game>("Data.pak");
        };
    }
}

Any ideas?

/Johan

johanlindfors commented 8 years ago

After som refactoring and further testing I now have something rendering solid, but only when enabling the MonoDebugHud...

Here is the refactored code:

public class Game : Application
{
    public Game() : base() { }
    public Game(ApplicationOptions options) : base(options) { }

    protected override void Start()
    {
        var scene = new Scene();
        scene.CreateComponent<Octree>();

        // Box
        var boxNode = scene.CreateChild();
        boxNode.Position = new Vector3(0, 0, 5);
        boxNode.Rotation = new Quaternion(60, 0, 30);
        boxNode.SetScale(1f);
        var modelObject = boxNode.CreateComponent<StaticModel>();
        modelObject.Model = ResourceCache.GetModel("Models/Box.mdl");
        modelObject.SetMaterial(ResourceCache.GetMaterial("Materials/StoneSmall.xml"));

        // Light
        var lightNode = scene.CreateChild(name: "light");
        lightNode.SetDirection(new Vector3(0.6f, -1.0f, 0.8f));
        lightNode.CreateComponent<Light>();

        // Camera
        var cameraNode = scene.CreateChild(name: "camera");
        var camera = cameraNode.CreateComponent<Camera>();

        // Viewport
        Renderer.SetViewport(0, new Viewport(scene, camera, null));

        // DebugHud
        var debugHud = new MonoDebugHud(this);
        debugHud.Show(); // Stuff gets rendered ONLY when this is shown
    }

public sealed partial class MainPage { Urho.Application game; public MainPage() { this.InitializeComponent(); Loaded += delegate { game = urhoSurface.Run("Data.pak"); }; } }

The code I used originally was pretty much pulled from the getting started documentation and hence I'm curious in learning which properties I have forgot to enable to get this simple cube rendered, what does the MonoDebugHud do that I'm not already doing? Is there anything with lighting or camera?

johanlindfors commented 8 years ago

The investigation continues. This issue exists in the Feature Samples as well. If I remove the MonoDebugHud and also removes all the lines that renders 2D content (such as the Input.AddScreenJoystick) the screen renders black.

EgorBo commented 8 years ago

@johanlindfors could you please try the latest Feature samples? I've updated nuget and fixed some uwp issues, also it's not longer required to store assets in pak files.

johanlindfors commented 7 years ago

Thanks @EgorBo. I tried the latest Feature samples and the issue still remains. I fail to render the scene (unless I resize the window constantly) unless I explicitly add something to the UI.Root.AddChild(...) then everything seems to work. A pure 3d (octree) scene doesn't work...

EgorBo commented 7 years ago

@johanlindfors interesting, looks like I messed up something.

johanlindfors commented 7 years ago

Any news on this issue?

EgorBo commented 7 years ago

@johanlindfors was temporarily fixed in https://github.com/xamarin/urho/commit/7152acec5d201ac27862093084bac645e2478e52. See the comment description.