Wow ! I recompile vertex shader Sascha Willems for skeletal animation and bug !

Recently I saw a bug in my animation. And think its my bug.
But the same bug i get in example skeletal animation Sascha Willems

I recompile Sascha Willems vertex shader
[b]
#version 450

#extension GL_ARB_separate_shader_objects : enable
#extension GL_ARB_shading_language_420pack : enable

layout (location = 0) in vec3 inPos;
layout (location = 1) in vec3 inNormal;
layout (location = 2) in vec2 inUV;
layout (location = 3) in vec3 inColor;
layout (location = 4) in vec4 inBoneWeights;
layout (location = 5) in ivec4 inBoneIDs;

#define MAX_BONES 64

layout (binding = 0) uniform UBO
{
mat4 projection;
mat4 model;
mat4 bones[MAX_BONES];
vec4 lightPos;
vec4 viewPos;
} ubo;

layout (location = 0) out vec3 outNormal;
layout (location = 1) out vec3 outColor;
layout (location = 2) out vec2 outUV;
layout (location = 3) out vec3 outViewVec;
layout (location = 4) out vec3 outLightVec;

out gl_PerVertex
{
vec4 gl_Position;
};

void main()
{
mat4 boneTransform = ubo.bones[inBoneIDs[0]] * inBoneWeights[0];
boneTransform += ubo.bones[inBoneIDs[1]] * inBoneWeights[1];
boneTransform += ubo.bones[inBoneIDs[2]] * inBoneWeights[2];
boneTransform += ubo.bones[inBoneIDs[3]] * inBoneWeights[3];

outNormal = inNormal;
outColor = inColor;
outUV = inUV;

gl_Position = ubo.projection * ubo.model * boneTransform * vec4(inPos.xyz+22.0, 1.0);// i add 22.0 to inPos.xyz and animation look like after boom !

vec4 pos = ubo.model * vec4(inPos, 1.0);
outNormal = mat3(inverse(transpose(ubo.model))) * inNormal;
outLightVec = ubo.lightPos.xyz - pos.xyz;
outViewVec = ubo.viewPos.xyz - pos.xyz;		

}
[/b]
If i not added to inPos.xyz 22.0 then work fine !
This is mean i found vertex shader bug.

      • Updated - - -

I need add coords for moving animated object in 3d world.
Because doing this

i add 22.0 to inPos.xyz and animation look like after boom !

If i not added to inPos.xyz 22.0 then work fine !

It’s not clear if you’re saying that you put the +22.0 in and that made it work, or if you’re saying that the example had this spurious addition and removing it made it work.

In any case, that addition makes no sense. If you putting that there made your code work, then you’re not fixing a bug. Or rather, you have a bug elsewhere (probably in your matrices), and adding this number made things look like they work.

I need add coords for moving animated object in 3d world.
Because doing this

You seem to have stopped mid-sentence.

Alfonse Reinheart man, simple run Sascha Willems skeletal animation example.

Then simple open in notepad.exe file mesh.vert from https://github.com/SaschaWillems/Vulkan/tree/master/data/shaders/skeletalanimation
And simple change this gl_Position = ubo.projection * ubo.model * boneTransform * vec4(inPos.xyz, 1.0);
to this gl_Position = ubo.projection * ubo.model * boneTransform * vec4(inPos.xyz + 22.0, 1.0);
And simple compile this shader and run again Sascha Willems skeletal animation example and yourself will see this bug.
And your questions will disappear by themselves.

I showed example where is bug.
Because if we will give from CPU 3d coords 3d model to shader we get boom model.
It`s not nice.
How do we move a 3D model in a 3D world with this bug ?

How do we move a 3D model in a 3D world

The same way it is (almost) always done, by adjusting the model matrix.

And for the record this is not a bug. Vertex skinning only works when the input vertices are in the pose the bone animations were authored for. When you modify the input vertex positions you break this. Instead you need to modify the position after the boneTransform was applied.

This is not a bug. Please try to understand what the example does first and also please refrain from using my name in a public forum topic title.

As carsten said, vertex positions need to be in initial pose positions. If you want to move the mesh around, use it’s model view to do so instead of changing vertex positions in the shader. This works like a charm in my example.

[QUOTE=Ronniko;40852]Alfonse Reinheart man, simple run Sascha Willems skeletal animation example.

Then simple open in notepad.exe file mesh.vert from https://github.com/SaschaWillems/Vulkan/tree/master/data/shaders/skeletalanimation
And simple change this gl_Position = ubo.projection * ubo.model * boneTransform * vec4(inPos.xyz, 1.0);
to this gl_Position = ubo.projection * ubo.model * boneTransform * vec4(inPos.xyz + 22.0, 1.0);
And simple compile this shader and run again Sascha Willems skeletal animation example and yourself will see this bug.
And your questions will disappear by themselves.[/quote]

I’m sorry; I’m having a difficult time following your thinking.

You copy someone else’s code. That code works fine as-is. You then make an arbitrary change to this code. It no longer works fine with this change.

And the fact that the changed code no longer works means that the original code has a bug in it.

Generally speaking, it is very unwise to claim that someone’s code is broken when you made a change to it that caused it to become broken. A more productive question would be “I took Sascha Willems’s code, made this change, and it didn’t do what I expect. Why not?” That is, rather than placing the blame on someone else initially, first assume that you’ve probably misunderstood something.

I do this because i do this in HLSL for DX 11. And this work in HLSL.
Strange, how i expected vec4(inPos.xyz + 22.0, 1.0); must add value to input mesh vertexes and then multiply to matrix ubo.projection * ubo.model * boneTransform.
But why , if i add value to inPos.xyz i get boom mesh ! I do not understand this.
My method is based on the programing HLSL.
But not working for Vulkan.
Vulkan shader new to me.

And strange this. If I do without (boneTransform) then work like i expected gl_Position = ubo.projection * ubo.model * vec4(inPos.xyz + 22.0, 1.0);
Mesh look good and shifted to +22.0. But mesh not animated without boneTransform matrix.

And on this principle I moved in 3d world a simple(without animation) 3d mesh.
I simple do gl_Position = ubo.projection * ubo.model * vec4(inPos.xyz + pushConsts.lightPos[0].xyz, 1.0);
And this work fine.

I simple do gl_Position = ubo.projection * ubo.View * vec4(inPos.xyz + pushConsts.Pos[0].xyz, 1.0);
Its work fine for simple 3d mesh without animation.

[QUOTE=Ronniko;40857]I simple do gl_Position = ubo.projection * ubo.View * vec4(inPos.xyz + pushConsts.Pos[0].xyz, 1.0);
Its work fine for simple 3d mesh without animation.[/QUOTE]

Then you don’t understand how animation/gpu skinning works.

Each vertex gets a different matrix depending on the bone(s) it is part of. So if you want to translate the mesh after animation then you need to do

gl_Position = ubo.projection * ubo.View * (ubo.bones[inBone] * vec4(inPos.xyz , 1.0) + vec4(pushConsts.Pos[0].xyz, 1.0) );

appliy the translation after the animation

However in a typical application that translation will be part of the view matrix

[QUOTE=Ronniko;40856]But why , if i add value to inPos.xyz i get boom mesh ! I do not understand this.
[/QUOTE]

Read Carsten’s answer. If you’re using a rigged mesh with vertex skinning vertices have to be in initial bind position before applying bone transformations. Changing the vertex positions in the shader just won’t work as this will offset the vertices from the armature of the mesh, so your skeleton will be in another position as the vertices. This just can’t work.