Skip to content

Commit

Permalink
Merge pull request #9804 from ShawnHardern/update-viewports-examples-…
Browse files Browse the repository at this point in the history
…csharp

Add C# examples to Using Viewports documentation
  • Loading branch information
skyace65 authored Aug 23, 2024
2 parents c0d608c + 83f6133 commit 88d2383
Showing 1 changed file with 41 additions and 6 deletions.
47 changes: 41 additions & 6 deletions tutorials/rendering/viewports.rst
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,15 @@ There can only be one active camera per :ref:`Viewport <class_Viewport>`, so if
than one, make sure that the desired one has the :ref:`current <class_Camera3D_property_current>` property set,
or make it the current camera by calling:

::
.. tabs::
.. code-tab:: gdscript GDScript

camera.make_current()

.. code-tab:: csharp

camera.MakeCurrent();

By default, cameras will render all objects in their world. In 3D, cameras can use their
:ref:`cull_mask <class_Camera3D_property_cull_mask>` property combined with the
:ref:`VisualInstance3D's <class_VisualInstance3D>` :ref:`layer <class_VisualInstance3D_property_layers>`
Expand All @@ -95,11 +100,17 @@ these values are overridden, but for all others, this sets their resolution.
It is also possible to scale the 2D content and make the :ref:`SubViewport <class_SubViewport>` resolution
different from the one specified in size, by calling:

::
.. tabs::
.. code-tab:: gdscript GDScript

sub_viewport.set_size_2d_override(Vector2i(width, height)) # Custom size for 2D.
sub_viewport.set_size_2d_override_stretch(true) # Enable stretch for custom size.

.. code-tab:: csharp

subViewport.Size2DOverride = new Vector2I(width, height); // Custom size for 2D.
subViewport.Size2DOverrideStretch = true; // Enable stretch for custom size.

For information on scaling and stretching with the Root Viewport visit the :ref:`Multiple Resolutions Tutorial <doc_multiple_resolutions>`

Worlds
Expand Down Expand Up @@ -137,7 +148,8 @@ It is possible to query a capture of the :ref:`Viewport <class_Viewport>` conten
Viewport, this is effectively a screen capture. This is done with the
following code:

::
.. tabs::
.. code-tab:: gdscript GDScript

# Retrieve the captured Image using get_image().
var img = get_viewport().get_texture().get_image()
Expand All @@ -146,16 +158,32 @@ following code:
# Set sprite texture.
sprite.texture = tex

.. code-tab:: csharp

// Retrieve the captured Image using get_image().
var img = GetViewport().GetTexture().GetImage();
// Convert Image to ImageTexture.
var tex = ImageTexture.CreateFromImage(img);
// Set sprite texture.
sprite.Texture = tex;

But if you use this in ``_ready()`` or from the first frame of the :ref:`Viewport's <class_Viewport>` initialization,
you will get an empty texture because there is nothing to get as texture. You can deal with
it using (for example):

::
.. tabs::
.. code-tab:: gdscript GDScript

# Wait until the frame has finished before getting the texture.
await RenderingServer.frame_post_draw
# You can get the image after this.

.. code-tab:: csharp

// Wait until the frame has finished before getting the texture.
await RenderingServer.Singleton.ToSignal(RenderingServer.SignalName.FramePostDraw);
// You can get the image after this.

Viewport Container
------------------

Expand Down Expand Up @@ -218,12 +246,19 @@ When rendering to a :ref:`SubViewport <class_SubViewport>`, whatever is inside w
visible in the scene editor. To display the contents, you have to draw the SubViewport's :ref:`ViewportTexture <class_ViewportTexture>` somewhere.
This can be requested via code using (for example):

::
.. tabs::
.. code-tab:: gdscript GDScript

# This gives us the ViewportTexture.
var tex = viewport.get_texture()
sprite.texture = tex

.. code-tab:: csharp

// This gives us the ViewportTexture.
var tex = viewport.GetTexture();
sprite.Texture = tex;

Or it can be assigned in the editor by selecting "New ViewportTexture"

.. image:: img/texturemenu.webp
Expand All @@ -245,6 +280,6 @@ This flexibility allows users to render an image once and then use the texture w

.. note::

Make sure to check the Viewport demos. They are available in the
Make sure to check the Viewport demos. They are available in the
viewport folder of the demos archive, or at
https://github.com/godotengine/godot-demo-projects/tree/master/viewport.

0 comments on commit 88d2383

Please sign in to comment.