0 votes

I've been working in the Mono version of 3.2.1 (and 3.2.2) to develop a top-down ARPG, but I may have run in to a major roadblock. After searching around for the best way to build out my art pipeline, I thought I had found the exact technique I needed. Using viewports to render a 3d object (main character) in a 2d project seems to do the job I had in mind to a tee. I found these resources as examples:

Example project on the Godot Github:

Video explaining the process:

Among others. The documentation also talks about this process. However, every example I've found only uses GDScript. I went ahead and tried implementing this in a test project using C# instead. This is one version of the script I wrote:

public class test : Node2D
    Texture texture;
    Sprite screen;

    public override void _Ready()
        texture = (Texture)GetViewport().GetTexture();
        screen = (Sprite)GetNode("Screen");

    public override void _Process(float delta)
        screen.Texture = texture;

I believe that the above should do effectively the same thing as the GDScript examples, however the results are pretty bizarre. It's tough to describe what it looks like on running it, but with the project otherwise built exactly the same way as the YouTube example, the place-holder images glitch and slide across the screen in pieces, with no sign of the 3d object at all. I've tried building the code and the project a few different ways and playing with a lot of options in the editor, but always with the same result. It's as if the texture from the viewport doesn't read properly in or out of video memory? Or maybe something in the bindings to cast a ViewportTexture object to Sprite.Texture doesn't work as I expect?

Just to confirm; I rebuilt the project in GDScript as well and it worked perfectly.

I'm pretty dead-set on continuing development in C#, but not sure I can without access to this technique or a similar one (I've built many of the other game mechanics in C# already). Does anyone know if this is a bug or am I scripting this incorrectly? I hesitate to open an issue on Github until I know that the error isn't mine (and I really hope it is so I can move forward!)

asked May 19 in Engine by jayCgee (15 points)

Won't GetViewport() be returning the main viewport your node is in?

Having a quick skim through the example code, it's using a separate viewport (defined on line 10 of https://github.com/godotengine/godot-demo-projects/blob/master/viewport/3d_in_2d/3d_in_2d.gd)

Do you have your broken example uploaded anywhere?

1 Answer

+1 vote
Best answer

Yeah, definitely a copy/paste error :)

Update the first line of your _Ready() method to use the Viewport that contains the 3d model instead of the parent viewport:
texture = (Texture)((Viewport)GetNode("Viewport")).GetTexture();

answered May 20 by MitchReidNZ (182 points)
selected May 20 by jayCgee

Thanks so much for the help!

Makes a lot of sense of what I was seeing when I ran it; it was trying to slap a texture of it's self over it's self every frame.

Not the first time I get brain freeze when looking at GDScript methods, then trying to spit out C#... I should have taken a step back and looked more carefully at what the methods I was calling actually did (especially before panicking lol!) :)

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.