Skip to content

Commit 48aaefb

Browse files
RubonnekCalinou
andauthored
Simplify mesh custom drawing demo (#1231)
Co-authored-by: Hugo Locurcio <[email protected]>
1 parent ba8718e commit 48aaefb

File tree

1 file changed

+27
-41
lines changed

1 file changed

+27
-41
lines changed

2d/custom_drawing/meshes.gd

Lines changed: 27 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,55 +2,30 @@
22
@tool
33
extends Panel
44

5-
var use_antialiasing := false
6-
7-
8-
func _draw() -> void:
9-
var margin := Vector2(300, 70)
10-
11-
var offset := Vector2()
12-
var text_mesh := TextMesh.new()
5+
# You must hold a reference to the Resources either as member variables or within an Array or Dictionary.
6+
# Otherwise, they get freed automatically and the renderer won't be able to draw them.
7+
var text_mesh := TextMesh.new()
8+
var noise_texture := NoiseTexture2D.new()
9+
var gradient_texture := GradientTexture2D.new()
10+
var sphere_mesh := SphereMesh.new()
11+
var multi_mesh := MultiMesh.new()
12+
13+
func _ready() -> void:
1314
text_mesh.text = "TextMesh"
1415
# In 2D, 1 unit equals 1 pixel, so the default size at which PrimitiveMeshes are displayed is tiny.
1516
# Use much larger mesh size to compensate, or use `draw_set_transform()` before using `draw_mesh()`
1617
# to scale the draw command.
1718
text_mesh.pixel_size = 2.5
1819

19-
var noise_texture := NoiseTexture2D.new()
2020
noise_texture.seamless = true
2121
noise_texture.as_normal_map = true
2222
noise_texture.noise = FastNoiseLite.new()
2323

24-
# FIXME: The mesh needs to be added to a MeshInstance2D (even out-of-tree)
25-
# for it to be usable in `draw_mesh()` (otherwise, nothing appears and an error is printed).
26-
# Same goes for the texture. I don't know why.
27-
var mi2d := MeshInstance2D.new()
28-
mi2d.mesh = text_mesh
29-
mi2d.texture = noise_texture
24+
gradient_texture.gradient = Gradient.new()
3025

31-
var mi2d2 := MeshInstance2D.new()
32-
var sphere_mesh := SphereMesh.new()
3326
sphere_mesh.height = 80.0
3427
sphere_mesh.radius = 40.0
35-
mi2d2.mesh = sphere_mesh
36-
mi2d2.texture = noise_texture
37-
38-
# `draw_set_transform()` is a stateful command: it affects *all* `draw_` methods within this
39-
# `_draw()` function after it. This can be used to translate, rotate or scale `draw_` methods
40-
# that don't offer dedicated parameters for this (such as `draw_primitive()` not having a position parameter).
41-
# To reset back to the initial transform, call `draw_set_transform(Vector2())`.
42-
#
43-
# Flip drawing on the Y axis so the text appears upright.
44-
draw_set_transform(margin + offset, 0.0, Vector2(1, -1))
45-
draw_mesh(text_mesh, noise_texture)
46-
47-
offset += Vector2(150, 0)
48-
draw_set_transform(margin + offset)
49-
draw_mesh(sphere_mesh, noise_texture)
5028

51-
var gradient_texture := GradientTexture2D.new()
52-
gradient_texture.gradient = Gradient.new()
53-
var multi_mesh := MultiMesh.new()
5429
multi_mesh.use_colors = true
5530
multi_mesh.instance_count = 5
5631
multi_mesh.set_instance_transform_2d(0, Transform2D(0.0, Vector2(0, 0)))
@@ -64,13 +39,24 @@ func _draw() -> void:
6439
multi_mesh.set_instance_transform_2d(4, Transform2D(0.0, Vector2(50, 50)))
6540
multi_mesh.set_instance_color(4, Color(0.7, 1, 1))
6641
multi_mesh.mesh = sphere_mesh
67-
var mmi2d := MultiMeshInstance2D.new()
68-
mmi2d.multimesh = multi_mesh
69-
mmi2d.texture = gradient_texture
7042

71-
# FIXME: The multimesh needs to be added to a MultiMeshInstance2D (even out-of-tree)
72-
# for it to be usable in `draw_multimesh()` (otherwise, it crashes).
73-
# Same goes for the texture. I don't know why.
43+
func _draw() -> void:
44+
const margin := Vector2(300, 70)
45+
var offset := Vector2()
46+
47+
# `draw_set_transform()` is a stateful command: it affects *all* `draw_` methods within this
48+
# `_draw()` function after it. This can be used to translate, rotate or scale `draw_` methods
49+
# that don't offer dedicated parameters for this (such as `draw_primitive()` not having a position parameter).
50+
# To reset back to the initial transform, call `draw_set_transform(Vector2())`.
51+
#
52+
# Flip drawing on the Y axis so the text appears upright.
53+
draw_set_transform(margin + offset, 0.0, Vector2(1, -1))
54+
draw_mesh(text_mesh, noise_texture)
55+
56+
offset += Vector2(150, 0)
57+
draw_set_transform(margin + offset)
58+
draw_mesh(sphere_mesh, noise_texture)
59+
7460
offset = Vector2(0, 120)
7561
draw_set_transform(margin + offset)
7662
draw_multimesh(multi_mesh, gradient_texture)

0 commit comments

Comments
 (0)