How can I update the shadow of a changed mesh?

Hi
I want to change single vertices of a mesh while the game is running. All works fine, I can move single vertices, but my problem is that the shadow stays the same. If I start with a subdivided Plane and move single vertices within the game, the object has still the shading of the flat plane. How can I fix that?

The collision box stays the same too, but i figured out that I can fix that with reinstancePhysicsMesh, is there a similar solution for the shadow problem?

Hmm, the shadows should automatically update when you are in GLSL mode. I’m not sure what
kind of light source you are using though - sun and spotlight are the light sources which by
default cast shadows.

Here is an example: VertexShadowsUpdate.blend (484 KB)

If you ‘baked’ a shadow onto a surface (meaning you made it a static shadow), I don’t think
there’s a way to have that update.

Thanks for the reply.
I am in GLSL mode, I’m using a point lamp, but with any other lamp the problem is the same. I didn’t bake the shadow.
I wasn’t sure what you are trying to tell me with your blend file.
In the picture you can see my problem (it’s a screenshot of the running game): the mesh in the background is a normal mesh wich I made before the game starts, the shading is correct. The mesh in the foreground is a subdivided plane before the game and when the game starts, I move the vertices so that it is like the mesh in the background. As you can see, the shading is completely different.
they both have the same material.


Have you touched their object color at all (as in you ticked ‘object color’ for the materials and maybe set one of the objects to have a gray object color)? If not, you can fix that.
And does your point lamp have a large enough distance to cover lighting for both meshes adequately?

Also, sorry I wasn’t clear about that file I sent.

This is a modified version of the file, with a point lamp in the scene and two identical meshes. These meshes constantly update their vertex positions every logic tic. You should be able to see both of these meshes’ shading (and shadows) update as well: VertexShadowsUpdate2.blend (449 KB)

What I’m trying to show with this updated file is basically the shading (and shadows) should be the same for both meshes if their materials (and colors) are the same, and the lighting covers enough distance. Beyond that, I think I would have to look at your blend file (or a demo file illustrating your problem).

I didn’t touche the object color button.
In your blend file, it seems to work, but you don’t rotate it, it’s not smooth, it’s a bit different.
However, here is my blend file.
With W, A, S, D you can move the cube around and with up and down, you can move the vertice at the position of the cube up and down. The mesh in the background is only there to see how the shadow is supposed to look like.
I hope you can help me.

Attachments

Shadingproblem.blend (541 KB)

Ah, thank you so much. Now I see what you mean. Sorry I didn’t catch on to this!

Currently, I have no solution for that problem.

You have to regenerate the vertex normals.

Why This Happens:
It is obvious that each face has a front and a back. It is less obvious that each vertex also has a direction. This direction is used to do that shading. A ‘flat’ shaded face will have the vertex normals facing the same way as the face normal. A smooth shaded face will have the vertex normals being the average of the faces it connects to.

When you move a vertex’s location, you set just that: it’s location. To update it’s shading you also need to update it’s normal direction.

The Solution:

def recalc_normals(cont, material_name):
    mesh = cont.owner.meshes[0]
    mat_id = bge.texture.materialID(cont.owner, "MA" + material_name)
    for poly_id in range(mesh.numPolygons):
        poly = mesh.getPolygon(poly_id)
        v1 = mesh.getVertex(mat_id, poly.v1)
        v2 = mesh.getVertex(mat_id, poly.v2)
        v3 = mesh.getVertex(mat_id, poly.v3)
        if poly.v4 == 0:
            normal = mathutils.geometry.normal(v1.getXYZ(), v2.getXYZ(), v3.getXYZ())
            v1.setNormal(normal)
            v2.setNormal(normal)
            v3.setNormal(normal)
        else:
            v4 = mesh.getVertex(mat_id, poly.v4)
            normal = mathutils.geometry.normal(v1.getXYZ(), v2.getXYZ(), v3.getXYZ(), v4.getXYZ())

THIS ASSUMES A TRIANGLE-ONLY MESH. IT MAY NOT WORK PROPERLY ON QUAD MESHES
Be aware that this is not the fastest thing, so (probably) don’t run it every frame.

Ah, that’s it. Thanks!