0 votes

This is the code I'm using to create a simple two triangle mesh:

func createMesh(size):
var st = SurfaceTool.new()
st.begin(Mesh.PRIMITIVE_TRIANGLES)
st.add_color(Color(1, 0, 0))
st.add_uv(Vector2(0, 0))
st.add_normal(Vector3( 0,  0,  size))
st.add_vertex(Vector3(-size, -size,  0))
st.add_vertex(Vector3( size,  size,  0))
st.add_vertex(Vector3( size, -size,  0))
st.add_vertex(Vector3(-size, -size,  0))
st.add_vertex(Vector3(-size,  size,  0))
st.add_vertex(Vector3( size,  size,  0))
var mesh = st.commit()
meshInstance.set_mesh(mesh)

I'm guessing it has to do with add_uv?

in Engine by (232 points)

3 Answers

0 votes
Best answer

I found if I set the override material on the mesh instance, I could colour the mesh. Not sure why add_color had no effect, even with UV set.

My code has become this:

    var st = SurfaceTool.new()
st.begin(Mesh.PRIMITIVE_TRIANGLES)
st.add_normal(Vector3( 0, 0, 1))
st.add_vertex(Vector3(-size, -size,  0))
st.add_vertex(Vector3( size,  size,  0))
st.add_vertex(Vector3( size, -size,  0))
st.add_vertex(Vector3(-size, -size,  0))
st.add_vertex(Vector3(-size,  size,  0))
st.add_vertex(Vector3( size,  size,  0))
var mesh = st.commit()
meshInstance.set_mesh(mesh)

Setting the mesh instance override gets the colour if I used FixedMaterial

by (232 points)

It needs a material and there is an option on the materials to use the vertex color (use_color_array).

0 votes

out of my head: you need to add uv coorinates to every vertex.

there may be an better solution but i didnt do mesh building for quite a while.

by (289 points)
0 votes

The code is correct, what you need is to add a DirectionalLight to provide light on your mesh. Otherwise, it will indeed be black.

by (27,170 points)

I added an Omni light source, and I get a white spot on the surface, the rest is still black.

I tried a world environment which made it the same colour as the environment. I've tried add_uv but it doesn't seem to make any difference.

func createMesh(size):
var st = SurfaceTool.new()
st.begin(Mesh.PRIMITIVE_TRIANGLES)
st.add_color(Color(1, 0, 0))
st.add_normal(Vector3( 0, 0, 1))
st.add_uv(Vector2(0, 0))
st.add_vertex(Vector3(-size, -size,  0))
st.add_uv(Vector2(1, 1))
st.add_vertex(Vector3( size,  size,  0))
st.add_uv(Vector2(1, 0))
st.add_vertex(Vector3( size, -size,  0))
st.add_uv(Vector2(0, 0))
st.add_vertex(Vector3(-size, -size,  0))
st.add_uv(Vector2(0, 1))
st.add_vertex(Vector3(-size,  size,  0))
st.add_uv(Vector2(1, 1))
st.add_vertex(Vector3( size,  size,  0))
var mesh = st.commit()
meshInstance.set_mesh(mesh)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.

Categories