from xuwzen2024@programming.dev to technology@lemmy.world on 02 Nov 2024 07:44
https://programming.dev/post/21255928
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::Refgodot::ImageTexture createRandomTexture( int width, int height ) { using namespace godot; Refgodot::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; }
`
This issue has been solved: Refgodot::ImageTexture texture = godot::ImageTexture::create_from_image( image ); return texture;
threaded - newest