Skip to content

Commit c9cafea

Browse files
todeskurveAThousandShipsraulsntos
authored
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]>
1 parent a316e81 commit c9cafea

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

tutorials/performance/using_servers.rst

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,30 @@ This is an example of how to create a sprite from code and move it using the low
119119
var xform = Transform2D().rotated(deg_to_rad(45)).translated(Vector2(20, 30))
120120
RenderingServer.canvas_item_set_transform(ci_rid, xform)
121121

122+
.. code-tab:: csharp
123+
124+
public partial class MyNode2D : Node2D
125+
{
126+
// RenderingServer expects references to be kept around.
127+
private Texture2D _texture;
128+
129+
public override void _Ready()
130+
{
131+
// Create a canvas item, child of this node.
132+
Rid ciRid = RenderingServer.CanvasItemCreate();
133+
// Make this node the parent.
134+
RenderingServer.CanvasItemSetParent(ciRid, GetCanvasItem());
135+
// Draw a texture on it.
136+
// Remember, keep this reference.
137+
_texture = ResourceLoader.Load<Texture2D>("res://MyTexture.png");
138+
// Add it, centered.
139+
RenderingServer.CanvasItemAddTextureRect(ciRid, new Rect2(_texture.GetSize() / 2, _texture.GetSize()), _texture.GetRid());
140+
// Add the item, rotated 45 degrees and translated.
141+
Transform2D xform = Transform2D.Identity.Rotated(Mathf.DegToRad(45)).Translated(new Vector2(20, 30));
142+
RenderingServer.CanvasItemSetTransform(ciRid, xform);
143+
}
144+
}
145+
122146
The Canvas Item API in the server allows you to add draw primitives to it. Once added, they can't be modified.
123147
The Item needs to be cleared and the primitives re-added (this is not the case for setting the transform,
124148
which can be done as many times as desired).
@@ -130,6 +154,10 @@ Primitives are cleared this way:
130154

131155
RenderingServer.canvas_item_clear(ci_rid)
132156

157+
.. code-tab:: csharp
158+
159+
RenderingServer.CanvasItemClear(ciRid);
160+
133161

134162
Instantiating a Mesh into 3D space
135163
----------------------------------
@@ -161,6 +189,31 @@ The 3D APIs are different from the 2D ones, so the instantiation API must be use
161189
var xform = Transform3D(Basis(), Vector3(20, 100, 0))
162190
RenderingServer.instance_set_transform(instance, xform)
163191

192+
.. code-tab:: csharp
193+
194+
public partial class MyNode3D : Node3D
195+
{
196+
// RenderingServer expects references to be kept around.
197+
private Mesh _mesh;
198+
199+
public override void _Ready()
200+
{
201+
// Create a visual instance (for 3D).
202+
Rid instance = RenderingServer.InstanceCreate();
203+
// Set the scenario from the world, this ensures it
204+
// appears with the same objects as the scene.
205+
Rid scenario = GetWorld3D().Scenario;
206+
RenderingServer.InstanceSetScenario(instance, scenario);
207+
// Add a mesh to it.
208+
// Remember, keep the reference.
209+
_mesh = ResourceLoader.Load<Mesh>("res://MyMesh.obj");
210+
RenderingServer.InstanceSetBase(instance, _mesh.GetRid());
211+
// Move the mesh around.
212+
Transform3D xform = new Transform3D(Basis.Identity, new Vector3(20, 100, 0));
213+
RenderingServer.InstanceSetTransform(instance, xform);
214+
}
215+
}
216+
164217
Creating a 2D RigidBody and moving a sprite with it
165218
---------------------------------------------------
166219

0 commit comments

Comments
 (0)