I created a new CSGMesh Node in my 3D godot scene with the following Script attached:


extends CSGMesh

func _ready():

	var material = SpatialMaterial.new()
	material.vertex_color_use_as_albedo = true
	#material.albedo_color = Color.green
	
	self.material_override = material
	self.material = material
	
	var surfacetool = SurfaceTool.new()
	
	surfacetool.begin(Mesh.PRIMITIVE_TRIANGLES)
	
	for i in [[-1, 1], [-1, -1], [1, -1]]:
		surfacetool.add_normal(Vector3(0, 1, 0))
		surfacetool.add_color(Color.red)
		surfacetool.add_uv(Vector2(0, 0))
		surfacetool.add_vertex(Vector3(i[0], 0, i[1]))

	self.mesh = surfacetool.commit()

code with syntax highlighting

In line 6 I order the material to use Vertex Colors . material.vertex_color_use_as_albedo = true

And use that spatial Material on the mesh in line 9 and 10

In line 18 I set all the Vertex Colors to be red: surfacetool.add_color(Color.red)

what I get is this triangle: why is it white? All the vertices should have a red color... How do I tell the SpatialMaterial shader to use the vertexcolors .. the flag material.vertex_color_use_as_albedo is already set to true ?

Why is it white and not red as the Vertexcolor set in the code?

If you are making that mesh into a CSG then it's the CSG node that you need to add the material to. I suggest you play around with the CSG nodes in the editor manually first to get to know them better.

You also need to enable using vertex colors in the SpatialMaterial. I do not remember the name right off, but I believe it is in the flags and is a checkbox called something like "use vertex color as srgb" or similar.

Edit: never mind, I see the flag is enabled in the screenshot. :sweat_smile:

Can you reproduce this when your script extends MeshInstance instead of CSGMesh?

I'm not sure if CSG preserves vertex colors. It was likely not designed for that purpose.

a year later