Browse Source

Added some C#-snippets to tutorials/using_servers.rst (#8241)

* Update using_servers.rst Added C# for 3D-Mesh

with limited C# knowledge there is a problem when not using "Basis.Identity" but trying to use "new Basis()" instead of GDScript's "Basis()"

---------

Co-authored-by: A Thousand Ships <[email protected]>
Co-authored-by: Raul Santos <[email protected]>
todeskurve 1 year ago
parent
commit
c9cafeab59
1 changed files with 53 additions and 0 deletions
  1. 53 0
      tutorials/performance/using_servers.rst

+ 53 - 0
tutorials/performance/using_servers.rst

@@ -119,6 +119,30 @@ This is an example of how to create a sprite from code and move it using the low
         var xform = Transform2D().rotated(deg_to_rad(45)).translated(Vector2(20, 30))
         RenderingServer.canvas_item_set_transform(ci_rid, xform)
 
+ .. code-tab:: csharp
+
+    public partial class MyNode2D : Node2D
+    {
+        // RenderingServer expects references to be kept around.
+        private Texture2D _texture;
+
+        public override void _Ready()
+        {
+            // Create a canvas item, child of this node.
+            Rid ciRid = RenderingServer.CanvasItemCreate();
+            // Make this node the parent.
+            RenderingServer.CanvasItemSetParent(ciRid, GetCanvasItem());
+            // Draw a texture on it.
+            // Remember, keep this reference.
+            _texture = ResourceLoader.Load<Texture2D>("res://MyTexture.png");
+            // Add it, centered.
+            RenderingServer.CanvasItemAddTextureRect(ciRid, new Rect2(_texture.GetSize() / 2, _texture.GetSize()), _texture.GetRid());
+            // Add the item, rotated 45 degrees and translated.
+            Transform2D xform = Transform2D.Identity.Rotated(Mathf.DegToRad(45)).Translated(new Vector2(20, 30));
+            RenderingServer.CanvasItemSetTransform(ciRid, xform);
+        }
+    }
+
 The Canvas Item API in the server allows you to add draw primitives to it. Once added, they can't be modified.
 The Item needs to be cleared and the primitives re-added (this is not the case for setting the transform,
 which can be done as many times as desired).
@@ -130,6 +154,10 @@ Primitives are cleared this way:
 
     RenderingServer.canvas_item_clear(ci_rid)
 
+ .. code-tab:: csharp
+
+    RenderingServer.CanvasItemClear(ciRid);
+
 
 Instantiating a Mesh into 3D space
 ----------------------------------
@@ -161,6 +189,31 @@ The 3D APIs are different from the 2D ones, so the instantiation API must be use
         var xform = Transform3D(Basis(), Vector3(20, 100, 0))
         RenderingServer.instance_set_transform(instance, xform)
 
+ .. code-tab:: csharp
+
+    public partial class MyNode3D : Node3D
+    {
+        // RenderingServer expects references to be kept around.
+        private Mesh _mesh;
+
+        public override void _Ready()
+        {
+            // Create a visual instance (for 3D).
+            Rid instance = RenderingServer.InstanceCreate();
+            // Set the scenario from the world, this ensures it
+            // appears with the same objects as the scene.
+            Rid scenario = GetWorld3D().Scenario;
+            RenderingServer.InstanceSetScenario(instance, scenario);
+            // Add a mesh to it.
+            // Remember, keep the reference.
+            _mesh = ResourceLoader.Load<Mesh>("res://MyMesh.obj");
+            RenderingServer.InstanceSetBase(instance, _mesh.GetRid());
+            // Move the mesh around.
+            Transform3D xform = new Transform3D(Basis.Identity, new Vector3(20, 100, 0));
+            RenderingServer.InstanceSetTransform(instance, xform);
+        }
+    }
+
 Creating a 2D RigidBody and moving a sprite with it
 ---------------------------------------------------