I'm creating a mesh on the fly and apply a material to it, except all I get is a white coloured surface. The code I'm using is as follows:

extends Spatial

onready var meshInstance = $Mesh

var angle = 0

func _ready():
	var texture = load("res://junk.png")
	var material = SpatialMaterial.new()
	material.albedo_texture = texture
	createMesh(2, material)
	
func createMesh(size, material):
	var surfaceTool = SurfaceTool.new()
	surfaceTool.begin(Mesh.PRIMITIVE_TRIANGLES)
	
	surfaceTool.set_material(material)

	surfaceTool.add_vertex(Vector3(-size, -size,  0))
	surfaceTool.add_vertex(Vector3( size,  size,  0))
	surfaceTool.add_vertex(Vector3( size, -size,  0))
	surfaceTool.add_vertex(Vector3(-size, -size,  0))
	surfaceTool.add_vertex(Vector3(-size,  size,  0))
	surfaceTool.add_vertex(Vector3( size,  size,  0))
	
	surfaceTool.generate_normals()
	
	var mesh = surfaceTool.commit()
	meshInstance.mesh = mesh
	
func _process(delta):
	angle += delta * 30
	meshInstance.rotation_degrees = Vector3(0, 0, angle)

Probably need to give values to other parameters too, such as roughness for an example, but I'm just guessing off the top pf my head right now.

For debugging purposes, I wonder how it would work out if you created a spatial material saved to disk before hand and read from that?

That did cross my mind, I assumed the defaults would be enough?

Maybe try using set_surface_material and see if that works? I think the problem is that the surface tool isn't properly assigning the material, so using set_surface_material may work as it sets the material on the MaterialInstance (as opposed to the surface tool)

Also, I don't think you need to worry about setting the other parameters, or at least it doesn't seem to matter in the test I did.

It's that the one with an index? I tried that, same results.

@Megalomaniak said: For debugging purposes, I wonder how it would work out if you created a spatial material saved to disk before hand and read from that?

Tried that, got the same result.

DERP. Right. You should probably create UV's too, don't you think? ;)

func createMaterial(): var texture = load("res://junk.png") var material = SpatialMaterial.new() material.albedo_texture = texture material.params_diffuse_mode = SpatialMaterial.DIFFUSE_BURLEY material.params_specular_mode = SpatialMaterial.SPECULAR_SCHLICK_GGX material.params_blend_mode = SpatialMaterial.BLEND_MODE_MIX material.params_cull_mode = SpatialMaterial.CULL_BACK material.params_depth_draw_mode = SpatialMaterial.DEPTH_DRAW_OPAQUE_ONLY material.params_line_width = 1 material.params_point_size = 1 material.metallic = 0 material.metallic_specular = 0.5 material.metallic_texture_channel = SpatialMaterial.TEXTURE_CHANNEL_RED material.uv1_scale = Vector3(1,1,1) material.uv2_scale = Vector3(1,1,1) return material

Need to define UVs in your mesh, I meant.

http://docs.godotengine.org/en/3.0/classes/class_surfacetool.html#description


var st = SurfaceTool.new()
st.begin(Mesh.PRIMITIVE_TRIANGLES)
st.add_color(Color(1, 0, 0))
st.add_uv(Vector2(0, 0))
st.add_vertex(Vector3(0, 0, 0))

I can confirm that this code works (I tested it on a MeshInstance): extends MeshInstance func _ready(): var mat = SpatialMaterial.new(); mat.flags_unshaded = true; mat.albedo_texture = load("res://icon.png"); var size = 2; var surfaceTool = SurfaceTool.new(); surfaceTool.begin(Mesh.PRIMITIVE_TRIANGLES) surfaceTool.set_material(mat) surfaceTool.add_uv(Vector2(0, 0)); surfaceTool.add_vertex(Vector3(-size, -size, 0)) surfaceTool.add_uv(Vector2(1, 1)); surfaceTool.add_vertex(Vector3( size, size, 0)) surfaceTool.add_uv(Vector2(1, 0)); surfaceTool.add_vertex(Vector3( size, -size, 0)) surfaceTool.add_uv(Vector2(0, 0)); surfaceTool.add_vertex(Vector3(-size, -size, 0)) surfaceTool.add_uv(Vector2(0, 1)); surfaceTool.add_vertex(Vector3(-size, size, 0)) surfaceTool.add_uv(Vector2(1, 1)); surfaceTool.add_vertex(Vector3( size, size, 0)) surfaceTool.generate_normals() var new_mesh = surfaceTool.commit() self.mesh = new_mesh;

Hopefully it will help.

@TwistedTwigleg said: I can confirm that this code works (I tested it on a MeshInstance): extends MeshInstance func _ready(): var mat = SpatialMaterial.new(); mat.flags_unshaded = true; mat.albedo_texture = load("res://icon.png"); var size = 2; var surfaceTool = SurfaceTool.new(); surfaceTool.begin(Mesh.PRIMITIVE_TRIANGLES) surfaceTool.set_material(mat) surfaceTool.add_uv(Vector2(0, 0)); surfaceTool.add_vertex(Vector3(-size, -size, 0)) surfaceTool.add_uv(Vector2(1, 1)); surfaceTool.add_vertex(Vector3( size, size, 0)) surfaceTool.add_uv(Vector2(1, 0)); surfaceTool.add_vertex(Vector3( size, -size, 0)) surfaceTool.add_uv(Vector2(0, 0)); surfaceTool.add_vertex(Vector3(-size, -size, 0)) surfaceTool.add_uv(Vector2(0, 1)); surfaceTool.add_vertex(Vector3(-size, size, 0)) surfaceTool.add_uv(Vector2(1, 1)); surfaceTool.add_vertex(Vector3( size, size, 0)) surfaceTool.generate_normals() var new_mesh = surfaceTool.commit() self.mesh = new_mesh;

Hopefully it will help.

I think I need your scene file too, I tried the above, same result.

Here it is!

(The texture is drawn upside down... :sweat_smile: You could fix it by changing the UV order though)

Your solution works, the only difference is you don't have a light source. So I removed mine. I get a completely dark picture.

GOT IT! I noticed my mesh showed up a white even with a mesh and material applied through the editor. I delete the mesh instance, added a new one, set it's shape to cube and colour to red. Ran the game and now I get my weird textured applied as expected.

Something odd about the mesh in my scene. I did call it Mesh, this time I left it as MeshInstance.

5 years later