|
@@ -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
|
|
|
---------------------------------------------------
|
|
|
|