Geometry using ArrayMesh

Godot Version

4.6

Question

Just looking in documentation examples and found how normals , uvs , vertices , indices are assigned into packed scenes …

It all be placed in ready()

verts = PackedVector3Array([
        Vector3(0, 0, 0),
        Vector3(0, 0, 1),
        Vector3(1, 0, 0),
        Vector3(1, 0, 1),
    ])

But what case when I want to do solving geometry examples and assign values from formula and ArrayMesh be serving as visualisation .

Example :
A = pi * (r * r)
r = 2

Simple circle :hollow_red_circle:. How could I go about it in code ?

push_back() vectors into arrays from a loop that calculates the values.

1 Like

Hmm , trying to figure out how this would get converted to Vector3 from float value .

If you push to Vector3 array you need to push a Vector3, not a float.

So to get from this simple Circle formula-> Vector3 coordinates for vertex , is amount or subdivision need to be set first to return let say 64 positions ?

Sorry just can’t imagine how to do it in code from math simple input to vertex position.

it will be 4 of them and then between those 4 curve where live 15 in each quadrant .

So what type of curve could I use to calculate 15 between or is it different method should be in place ?

Start with Vector3(1.0, 0.0, 0.0) and rotate it by 360 / number_of_steps degrees each step of the loop.

1 Like
extends MeshInstance3D

func _ready() -> void:
	var surface_array = []
	surface_array.resize(Mesh.ARRAY_MAX)
	
	var uvs = PackedVector2Array()
	var indices = PackedInt32Array()
	
	# circle vars
	var start_vector = Vector3(1.0, 0 ,0)
	var rotation_degree_value = 360
	var current_step_iteration = 0
	var numbers_of_steps = 4
	var my_vertices = PackedVector3Array()
	var my_uvs = PackedVector2Array()
	var my_normals = PackedVector3Array()
	var my_indices = PackedInt32Array()
	
	# loop 
	for i in numbers_of_steps:
		current_step_iteration += 1
		my_vertices.push_back(Vector3(start_vector.rotated(Vector3.UP, rotation_degree_value/float(current_step_iteration))))
		#TODO - my_uvs
		my_normals.push_back(Vector3.UP)
		#TODO my_indices
		
	
	# space for generating mesh
	
	uvs = PackedVector2Array([
		Vector2(0, 0),
		Vector2(1, 0),
		Vector2(0, 1),
		Vector2(1, 1),
	])
	
	
	indices = PackedInt32Array([
		  0, 2, 1,
		  2, 3, 1,
	  ])
	surface_array[Mesh.ARRAY_VERTEX] = my_vertices
	surface_array[Mesh.ARRAY_TEX_UV] = uvs
	surface_array[Mesh.ARRAY_NORMAL] = my_normals
	surface_array[Mesh.ARRAY_INDEX] = indices
	  
	mesh.add_surface_from_arrays(Mesh.PRIMITIVE_LINES, surface_array)

current code - not sure about uvs and indices .

current results , sort of there :

extends MeshInstance3D

func _ready() -> void:
	var surface_array = []
	surface_array.resize(Mesh.ARRAY_MAX)
	
	var uvs = PackedVector2Array()
	var indices = PackedInt32Array()
	
	# circle vars
	var start_vector = Vector3(1.0, 0 ,0)
	var rotation_degree_value = 360
	var current_step_iteration = 0
	var current_indices_iteration = 0
	var numbers_of_steps = 64
	var my_vertices = PackedVector3Array()
	var my_uvs = PackedVector2Array()
	var my_normals = PackedVector3Array()
	var my_indices = PackedInt32Array()
	
	# loop 
	for i in numbers_of_steps:
		current_step_iteration += 1
		my_vertices.push_back(Vector3(start_vector.rotated(Vector3.UP, rotation_degree_value/float(current_step_iteration))))
		my_uvs.push_back(Vector2(0,0))
		my_normals.push_back(Vector3.UP)
		var a = 0
		var b = 1
		var c = 2
		var d = 3
		print(str(a + current_indices_iteration) +",", str(c + current_indices_iteration) + ",",str(b + current_indices_iteration )+ ",")
		print(str(a + current_indices_iteration) +",", str( d + current_indices_iteration) + ",",str( b + current_indices_iteration) + ",")
		current_indices_iteration += 1
		
	
	# space for generating mesh
	
	uvs = PackedVector2Array([
		Vector2(0, 0),
		Vector2(1, 0),
		Vector2(0, 1),
		Vector2(1, 1),
	])
	
	
	indices = PackedInt32Array([
0,2,1,
0,3,1,
1,3,2,
1,4,2,
2,4,3,
2,5,3,
3,5,4,
3,6,4,
4,6,5,
4,7,5,
5,7,6,
5,8,6,
6,8,7,
6,9,7,
7,9,8,
7,10,8,
8,10,9,
8,11,9,
9,11,10,
9,12,10,
10,12,11,
10,13,11,
11,13,12,
11,14,12,
12,14,13,
12,15,13,
13,15,14,
13,16,14,
14,16,15,
14,17,15,
15,17,16,
15,18,16,
16,18,17,
16,19,17,
17,19,18,
17,20,18,
18,20,19,
18,21,19,
19,21,20,
19,22,20,
20,22,21,
20,23,21,
21,23,22,
21,24,22,
22,24,23,
22,25,23,
23,25,24,
23,26,24,
24,26,25,
24,27,25,
25,27,26,
25,28,26,
26,28,27,
26,29,27,
27,29,28,
27,30,28,
28,30,29,
28,31,29,
29,31,30,
29,32,30,
30,32,31,
30,33,31,
31,33,32,
31,34,32,
32,34,33,
32,35,33,
33,35,34,
33,36,34,
34,36,35,
34,37,35,
35,37,36,
35,38,36,
36,38,37,
36,39,37,
37,39,38,
37,40,38,
38,40,39,
38,41,39,
39,41,40,
39,42,40,
40,42,41,
40,43,41,
41,43,42,
41,44,42,
42,44,43,
42,45,43,
43,45,44,
43,46,44,
44,46,45,
44,47,45,
45,47,46,
45,48,46,
46,48,47,
46,49,47,
47,49,48,
47,50,48,
48,50,49,
48,51,49,
49,51,50,
49,52,50,
50,52,51,
50,53,51,
51,53,52,
51,54,52,
52,54,53,
52,55,53,
53,55,54,
53,56,54,
54,56,55,
54,57,55,
55,57,56,
55,58,56,
56,58,57,
56,59,57,
57,59,58,
57,60,58,
58,60,59,
58,61,59,
59,61,60,
59,62,60,
60,62,61,
60,63,61,
61,63,62,
61,64,62,
62,64,63,
62,65,63,
63,65,64,
63,66,64,


		
		  

	  ])
	surface_array[Mesh.ARRAY_VERTEX] = my_vertices
	surface_array[Mesh.ARRAY_TEX_UV] = my_uvs
	surface_array[Mesh.ARRAY_NORMAL] = my_normals
	surface_array[Mesh.ARRAY_INDEX] = indices
	  
	mesh.add_surface_from_arrays(Mesh.PRIMITIVE_LINES, surface_array)