Hello everyone,

I am encountering an issue where a triangle mesh created in Godot-cpp 4.2 is not rendering with a programmatically generated texture. I have followed the steps to create the texture, assign it to a material, and then apply the material to the mesh instance, but the triangle still appears without the texture.

Here is my code for generating the texture and creating the triangle mesh:
`
godot::MeshInstance3D* createTriangle()
{
using namespace godot;
PackedVector3Array verts;
PackedInt32Array indices;
PackedVector2Array uvs;
verts.push_back( Vector3( 0.0, 0.0, 0.0 ) );
verts.push_back( Vector3( 1.0, 0.0, 0.0 ) );
verts.push_back( Vector3( 1.0, 0.0, 1.0 ) );
indices.push_back( 0 );
indices.push_back( 1 );
indices.push_back( 2 );
uvs.push_back( Vector2( 0, 0 ) );
uvs.push_back( Vector2( 0, 1 ) );
uvs.push_back( Vector2( 1, 1 ) );

    Array surface_array;
    surface_array.resize( ArrayMesh::ARRAY_MAX );
    surface_array[ArrayMesh::ARRAY_VERTEX] = verts;
    surface_array[ArrayMesh::ARRAY_INDEX] = indices;
    surface_array[ArrayMesh::ARRAY_TEX_UV] = uvs;

    Ref<ArrayMesh> mesh;
    mesh.instantiate();
    mesh->add_surface_from_arrays( ArrayMesh::PRIMITIVE_TRIANGLES, surface_array );
    MeshInstance3D *meshInstance = memnew( MeshInstance3D );
    meshInstance->set_mesh( mesh );
    Ref<StandardMaterial3D> material;
    material.instantiate();
    Ref<godot::Texture> random_texture = createRandomTexture( 30, 30 );
    material->set_texture( StandardMaterial3D::TextureParam::TEXTURE_ALBEDO, random_texture );
    meshInstance->set_material_override( material );
    meshInstance->set_scale( Vector3( 10, 10, 10 ) );
    return meshInstance;

}

godot::Ref<godot::ImageTexture> createRandomTexture( int width, int height )
{
using namespace godot;
Ref<godot::Image> image =
godot::Image::create( width, height, false, godot::Image::FORMAT_RGBA8 );
godot::RandomNumberGenerator rng;
rng.set_seed( static_cast<uint32_t>( time( nullptr ) ) );

    for ( int y = 0; y < height; ++y )
    {
        for ( int x = 0; x < width; ++x )
        {
            // godot::Color color = godot::Color( 0.2549f, 0.9137f, 0.788f, 1.0 );
            godot::Color color = godot::Color( rng.randf(), rng.randf(), rng.randf(), 1.0 );
            image->set_pixel( x, y, color );
        }
    }

    Ref<godot::ImageTexture> texture =
        Ref<godot::ImageTexture>( memnew( godot::ImageTexture ) );
    texture->create_from_image( image );

    return texture;
}

`

Fixed:
Ref<godot::ImageTexture> texture = godot::ImageTexture::create_from_image( image );
return texture;

  • xyz replied to this.

    xuwzen2024 Try to assign a test texture loaded from file to see if the problem is with texture generation or with texture/material assignment.

    At first glance your code doesn't acquire the reference to the created image texture. ImageTexture::create_from_image() is a static function that returns that reference. So you should do:

    Ref<godot::ImageTexture> texture = godot::ImageTexture::create_from_image( image );
    return texture;

    Also shouldn't Ref<godot::Texture> random_texture = createRandomTexture( 30, 30 ); be Ref<godot::ImageTexture>... ? It does not really matter in this simple pointer/reference assignment case but may cause problems down the line.

      xuwzen2024 changed the title to (Solved)Triangle Mesh Not Rendering with Programmatically Generated Texture .