Can't see mesh

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By lucrious42

I have a game where I want to grow some grass behind the player, and the way I plan to do this is by first creating a mesh, like a little plane, which sits at the origin point. Essentially, I want to have a raycast follow the player, and wherever the raycast hits, new vertices are added to the mesh in that location. The mesh has a grass texture on it as well. This way, I could avoid spawning millions of tiny little meshes. The problem is that when I run the script, I can’t see the mesh.

var mesh = ArrayMesh.new()

var vertices = PackedVector3Array()
var normals = PackedVector3Array()
var uvs = PackedVector2Array()
var indices = PackedInt64Array()
var texture_size = Vector2(4, 4)
var moss_mesh_instance = MeshInstance3D
var green_material = load(“res://icon.svg”)

@onready var raycast_list = [$MossRaycasts/RayCast3D, $MossRaycasts/RayCast3D2, $MossRaycasts/RayCast3D3]

func _ready():
# Create the base mesh for the moss
var base_vertices = PackedVector3Array([
Vector3(-1, 0, -1),
Vector3(-1, 0, 1),
Vector3(1, 0, 1),
Vector3(1, 0, -1)
])
var base_normals = PackedVector3Array([Vector3(0, 1, 0), Vector3(0, 1, 0), Vector3(0, 1, 0), Vector3(0, 1, 0)])

var base_uvs = PackedVector2Array([
	Vector2(0, 0),
	Vector2(0, texture_size.y),
	Vector2(texture_size.x, texture_size.y),
	Vector2(texture_size.x, 0)
])
var base_indices = PackedInt64Array([0, 1, 2, 0, 2, 3])
mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, [base_vertices, base_normals, base_uvs, base_indices])
moss_mesh_instance = MeshInstance3D.new()
moss_mesh_instance.mesh = mesh
add_child(moss_mesh_instance)
var material = StandardMaterial3D.new()
material.albedo_color = Color(0, 1, 0)
moss_mesh_instance.set_surface_override_material(1, material)

func _input(event):
if event.is_action_pressed(“mouse_left”):
# Add a new vertex to the mesh at the location of the click
for ray in raycast_list:
var collider = ray.get_collider()
if collider != null:
var vertex = ray.get_collision_point()
vertices.append(vertex)
normals.append(Vector3.UP)
var uv = Vector2(vertex.x, vertex.z) / texture_size
uvs.append(uv)

	# Triangulate the new vertices to create new faces on the mesh
	var vertex_index = vertices.size() - 1
	if vertex_index > 2:
		var index_offset = indices.size()
		for i in range(1, vertex_index - 1):
			indices.append(index_offset)
			indices.append(index_offset + i)
			indices.append(index_offset + i + 1)
	
	# Update the mesh with the new vertices and faces
	
	mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, [vertices, normals, uvs, indices])

This is my script. I have checked in the Remote feedback, and the mesh is being created. When I contact it, the mesh says it is at the coordinates of (0, 0, 0). I even give the mesh a material. But I still can’t see it. Maybe I am doing this all wrong, but some feedback would be great.
(Also, I’m sorry about the weird code blocks.)

moss_mesh_instance.set_surface_override_material(1, material)

Is 1 the correct surface index? They typically start from 0.

spaceyjase | 2023-04-11 14:49

I tried that, and it doesn’t work. However, I am getting two different error messages:

  1. E 0:00:01:0793 main.gd:39 @ _ready(): Index p_surface = 0 is out of bounds (surface_override_materials.size() = 0). <C++ Source> scene/3d/mesh_instance_3d.cpp:341 @ set_surface_override_material() <Stack Trace> main.gd:39 @ _ready() This one applies directly to the line that you referenced.
  2. E 0:00:01:0793 main.gd:33 @ _ready(): Condition "p_arrays.size() != ARRAY_MAX" is true. <C++ Source> scene/resources/mesh.cpp:1597 @ add_surface_from_arrays() <Stack Trace> main.gd:33 @ _ready() Do you think these are the problem?

lucrious42 | 2023-04-13 22:25

Yeah, for sure; looks like surface 0 isn’t there which is strange. Can’t really tell from the code as the post is a bit mangled so it may be easier to provide a small project. It was mentioned that it appeared to work so might be worth checking the normals and mesh dimensions to see what’s visible.

Perhaps this may also help? I have a little mesh toy project that does the same, albeit it written in C#. Maybe compare what you’re doing to the example:

GitHub - spaceyjase/mesh-toy: runtime meshes in godot 3,5,2

spaceyjase | 2023-04-14 08:43