1
\$\begingroup\$

I'm trying to create a dynamic mesh using ArrayMesh but my mesh isn't visible unless I turn on wireframe debug mode. Here is a minimal example of a plane mesh consisting of 2 triangles (this script is currently placed on a Spatial node):

func _ready():
    # With these two lines added I can see the wireframe of the mesh (picture below) otherwise I see nothing.
    VisualServer.set_debug_generate_wireframes(true)
    get_viewport().set_debug_draw(Viewport.DEBUG_DRAW_WIREFRAME)

    var ve : PoolVector3Array = PoolVector3Array()
    ve.push_back(Vector3(0, 0, 0))
    ve.push_back(Vector3(10, 0, 0))
    ve.push_back(Vector3(0, 0, 10))
    ve.push_back(Vector3(10, 0, 10))
        
    var uv: PoolVector2Array = PoolVector2Array()
    uv.push_back(Vector2(0, 0))
    uv.push_back(Vector2(1, 0))
    uv.push_back(Vector2(0, 1))
    uv.push_back(Vector2(1, 1))
    
    var co : PoolColorArray = PoolColorArray()
    co.push_back(Color(1, 1, 1))
    co.push_back(Color(1, 1, 1))
    co.push_back(Color(1, 1, 1))
    co.push_back(Color(1, 1, 1))
        
    var id: PoolIntArray = PoolIntArray()
    id.push_back(0)
    id.push_back(2)
    id.push_back(1)
    id.push_back(1)
    id.push_back(2)
    id.push_back(3)
        
    var no: PoolVector3Array = PoolVector3Array()
    no.push_back(Vector3(0, 1, 0))
    no.push_back(Vector3(0, 1, 0))
    no.push_back(Vector3(0, 1, 0))
    no.push_back(Vector3(0, 1, 0))
    
    var array_mesh = ArrayMesh.new()
    var arrays = []
    arrays.resize(ArrayMesh.ARRAY_MAX)
    arrays[ArrayMesh.ARRAY_VERTEX] = ve
    arrays[ArrayMesh.ARRAY_COLOR] = co
    arrays[ArrayMesh.ARRAY_INDEX] = id
    arrays[ArrayMesh.ARRAY_NORMAL] = no
    arrays[ArrayMesh.ARRAY_TEX_UV] = uv
    
    var m_i = MeshInstance.new()
    array_mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, arrays)
    m_i.mesh = array_mesh
    add_child(m_i)

Now with wireframe debug mode on I see the following:

wireframe

Otherwise, I can't see anything. If I add an albedo_color to the material_override then the lines will change to be that color. I'm pretty new into procedural generation but I've found that a similar thing will work in Unity so I'm not sure what I'm missing here.

\$\endgroup\$
5
  • \$\begingroup\$ What happens if you look at the mesh from the other side? Or if you swap your IDs from (0, 2, 1, 1, 2, 3) to (0, 1, 2, 3, 2, 1)? \$\endgroup\$
    – DMGregory
    Commented Jun 22, 2020 at 17:39
  • \$\begingroup\$ @DMGregory Reversing the indices does indeed work. Very strange as I'm adapting this from a Unity tutorial and I read that Unity, like Godot, expects vertices to be defined clockwise so I didn't even think to question the order. \$\endgroup\$ Commented Jun 22, 2020 at 17:45
  • \$\begingroup\$ Could be that your coordinate system is not exactly what you expected, or your camera is viewing your mesh from the opposite side as you expected. If this has solved your problem, please share your solution as an Answer below. \$\endgroup\$
    – DMGregory
    Commented Jun 22, 2020 at 17:52
  • \$\begingroup\$ I'm not sure I can really answer it though. I get that reversing the indices solves the problem but I can't explain why. The camera is "looking down" at the mesh at the mesh which should be facing up towards the camera. Here's an image of it if that helps. \$\endgroup\$ Commented Jun 22, 2020 at 18:40
  • \$\begingroup\$ @DMGregory in Unity forward is positive Z, in Godot forward is negative Z. \$\endgroup\$
    – Theraot
    Commented Sep 28, 2020 at 10:27

0

You must log in to answer this question.

Browse other questions tagged .