Generating Single 3D triangle

Godot Version

4

Question

`I have attached the following script to the only node in my scene tree (MeshInstance3d):

extends MeshInstance3D



func _ready():
	
	var vertices = PackedVector3Array()
	vertices.push_back(Vector3(0, 1, 0))
	vertices.push_back(Vector3(1, 0, 0))
	vertices.push_back(Vector3(0, 0, 1))

# Initialize the ArrayMesh.
	var arr_mesh = ArrayMesh.new()
	var arrays = []
	arrays.resize(Mesh.ARRAY_MAX)
	arrays[Mesh.ARRAY_VERTEX] = vertices

# Create the Mesh.
	arr_mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, arrays)
	var m = MeshInstance3D.new()
	m.mesh = arr_mesh

Can anyone tell me why I only see a grey screen? It does not matter what angle I move the camera to. Question #2: does Godot remember camera positioning from the scene window during runtime?
`

Do you have a Camera3D, world environment and light source? You won’t see anything in 3D without a Camera, and you won’t see much without an environment and light source.

Maybe an ImmediateMesh would be easier than ArrayMesh too.

var mesh := ImmediateMesh.new()
mesh.surface_begin(Mesh.PRIMITIVE_TRIANGLES)
mesh.surface_add_vertex(Vector3.LEFT)
mesh.surface_add_vertex(Vector3.FORWARD)
mesh.surface_add_vertex(Vector3.ZERO)
mesh.surface_end()

Thanks for camera/environment clarification for runtime. I think my issue was

  1. I needed to designate the script as @tool to see the mesh in the inspector
  2. The scene needs to be closed/reopened to “reload” and see the changes to the mesh.

Any clarification as to why I need to designate the script as a “tool” to view the mesh in the inspect + and shortcuts to reloading the scene would be appreciated

@tool scripts run in the editor, normally scripts do not run in the editor. Your mesh code was a regular script, so it doesn’t run in the editor.

If you want the script to update the triangle more often then you can call the updating function as you see fit, 4.4 just added @export_tool_button for such situations, or you could put your code in _process.

1 Like