Hi. New to Godot and trying to re-visit something I started years ago. Getting stuck on an error that I can find no explanation for. This code should display an nth frequency truncated icosahedron but keeps throwing errors. No idea how to fix this as no idea what the error message means.
Any help appreciated...
extends MeshInstance3D
func _ready():
var n = 3
var phi = (1.0 + sqrt(5.0)) / 2.0
# Define the 12 vertices of a regular icosahedron
var icosahedron_vertices = [
Vector3(1, phi, 0),
Vector3(-1, phi, 0),
Vector3(1, -phi, 0),
Vector3(-1, -phi, 0),
Vector3(0, 1, phi),
Vector3(0, -1, phi),
Vector3(0, 1, -phi),
Vector3(0, -1, -phi),
Vector3(phi, 0, 1),
Vector3(-phi, 0, 1),
Vector3(phi, 0, -1),
Vector3(-phi, 0, -1)
]
# Define the indices to connect vertices and form faces
var face_indices = [
0, 1, 4, 0, 4, 8, 0, 8, 5, 0, 5, 2, 0, 2, 1,
3, 1, 2, 3, 2, 7, 3, 7, 11, 3, 11, 9, 3, 9, 1,
6, 10, 11, 6, 11, 7, 6, 7, 2, 6, 2, 5, 6, 5, 10,
4, 10, 5, 4, 8, 10, 4, 9, 11, 4, 11, 8, 4, 1, 9,
10, 8, 11, 9, 1, 10, 5, 8, 11, 2, 7, 9, 7, 2, 6
]
# Calculate the vertices of the nth-frequency truncated icosahedron
var truncated_icosahedron_vertices = []
for vertex in icosahedron_vertices:
var length = vertex.length()
var new_vertex = vertex.normalized() * (length + n)
truncated_icosahedron_vertices.append(new_vertex)
# Create an ArrayMesh to store the faces
var array_mesh = ArrayMesh.new()
# Convert vertices to an array
var vertices_array = []
for vertex in truncated_icosahedron_vertices:
vertices_array += [vertex.x, vertex.y, vertex.z]
# Convert indices to an array
var indices_array = []
for index in face_indices:
indices_array.append(index)
# Add arrays to the ArrayMesh
array_mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, [vertices_array, indices_array])
# Load the "colr" material resource
var colr_material = load("res://colr.tres")
# Create and display the faces of the truncated icosahedron
for i in range(0, len(face_indices), 3):
var triangle_instance = MeshInstance3D.new()
triangle_instance.mesh = array_mesh
triangle_instance.surface_set_material(0, colr_material)
triangle_instance.surface_set_primitive_type(0, Mesh.PRIMITIVE_TRIANGLES)
triangle_instance.surface_set_array_index(0, i)
add_child(triangle_instance)