How can I apply a texture to a mesh at runtime?

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

*Well I guess that’s the last straw for Godot. It’s nearly impossible to do simple things like instantiate objects and texture them in Gdscript. If I wanted to make a basic game with minimal features I wouldn’t use this anyway. So you’re either powerful or easy, but you’re not powerful because nothing is documented or has any examples beyond what can be farmed from the code itself. Lame. LAME!

  1. Why not just expose real Python by default? What would that hurt?
  2. Why does ABSOLUTELY NOTHING have examples in the documentation?
  3. Why is it so damn hard to generate a mesh and render it with a material? This should be a snap, but there are NO good answers on how to do it. I can do this in Unity in minutes. With Godot I have to pray someone else has already yelled and screamed and cried about whatever it is I want to do because NOTHING HAS EXAMPLES.
  4. You already have the same problem Unity does with out-of-date reference material all over the internet. The reason for this is that you didn’t give examples for anything at all, so 3rd parties created them instead, and now you have no control over that.
  5. No live debugger (command shell). Fail. It would be 1000x easier to learn if it had that.
  6. Editor missing most basic QOL features.
  7. Editor window does not update live while game is running, so you can’t see it as it happens like other game engines.

Such fail. If you are a developer already it is 1000x easier to learn C# and use Unity than it is to arse with GDscript.

I invite anyone that got this far to see how easy this should be;
How can I assign materials using C# code? - Questions & Answers - Unity Discussions
Also this… see how the documentation is almost entirely CODE???
Unity - Scripting API: GameObject.CreatePrimitive

I am trying to load a texture onto a mesh. I downloaded the diamond plate texture from the asset store. I just get a black cube instead of a textured cube. What do I need to do so that the texture will display on the mesh?

var c = CubeMesh.new()
c.size = Vector3(100, 1, 100)
var arr_mesh = ArrayMesh.new()
arr_mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, c.get_mesh_arrays())
var m = MeshInstance.new()
m.mesh = arr_mesh
m.transform.origin = Vector3(0,0,0)
m.create_trimesh_collision() # adds a child collider

var new_texture = ImageTexture.new()
new_texture.load("res://materials/diamonplate_tex.png")
m.material_override = SpatialMaterial.new()
m.material_override.albedo_texture = new_texture

add_child(m)

also tried

var matVar = load("res://materials/diamondPlateAluminum/diamondAluminum.tres")
m.material_override = SpatialMaterial.new()
m.material_override.albedo_texture = matVar
:bust_in_silhouette: Reply From: Ducku

Do you have your png imported as an ImageTexture resource? The default is StreamTexture so you either want to reimport it or load it in as a StreamTexture.