2023-08-10 21:53:51 +00:00
|
|
|
#ifndef MAX_BONES
|
|
|
|
#define MAX_BONES 110
|
|
|
|
#endif
|
|
|
|
uniform mat3x4 vsBoneMatrix[MAX_BONES];
|
|
|
|
uniform bool SKINNED = false;
|
2023-09-19 04:50:53 +00:00
|
|
|
uniform mat4 M; // RIM
|
2023-08-10 21:53:51 +00:00
|
|
|
uniform mat4 VP;
|
2023-11-06 08:30:19 +00:00
|
|
|
uniform mat4 P;
|
|
|
|
uniform int u_billboard;
|
2023-09-10 14:46:07 +00:00
|
|
|
|
2023-08-10 21:53:51 +00:00
|
|
|
#if 0
|
2023-08-10 22:46:04 +00:00
|
|
|
// Fetch blend channels from all attached blend deformers.
|
2023-08-10 21:53:51 +00:00
|
|
|
for (size_t di = 0; di < mesh->blend_deformers.count; di++) {
|
|
|
|
ufbx_blend_deformer *deformer = mesh->blend_deformers.data[di];
|
|
|
|
for (size_t ci = 0; ci < deformer->channels.count; ci++) {
|
|
|
|
ufbx_blend_channel *chan = deformer->channels.data[ci];
|
|
|
|
if (chan->keyframes.count == 0) continue;
|
|
|
|
if (num_blend_shapes < MAX_BLEND_SHAPES) {
|
|
|
|
blend_channels[num_blend_shapes] = chan;
|
|
|
|
vmesh->blend_channel_indices[num_blend_shapes] = (int32_t)chan->typed_id;
|
|
|
|
num_blend_shapes++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (num_blend_shapes > 0) {
|
|
|
|
vmesh->blend_shape_image = pack_blend_channels_to_image(mesh, blend_channels, num_blend_shapes);
|
|
|
|
vmesh->num_blend_shapes = num_blend_shapes;
|
|
|
|
}
|
|
|
|
|
2023-09-10 14:46:07 +00:00
|
|
|
|
2023-08-10 21:53:51 +00:00
|
|
|
ubo.f_num_blend_shapes = (float)mesh->num_blend_shapes;
|
|
|
|
for (size_t i = 0; i < mesh->num_blend_shapes; i++) {
|
|
|
|
ubo.blend_weights[i] = view->scene.blend_channels[mesh->blend_channel_indices[i]].weight;
|
|
|
|
}
|
|
|
|
|
2023-09-10 14:46:07 +00:00
|
|
|
|
2023-08-10 21:53:51 +00:00
|
|
|
sg_image blend_shapes = mesh->num_blend_shapes > 0 ? mesh->blend_shape_image : view->empty_blend_shape_image;
|
|
|
|
#endif
|
|
|
|
|
2023-09-10 14:46:07 +00:00
|
|
|
|
2023-08-10 22:46:04 +00:00
|
|
|
// for blendshapes
|
2023-08-10 21:53:51 +00:00
|
|
|
#ifndef MAX_BLENDSHAPES
|
|
|
|
#define MAX_BLENDSHAPES 16
|
|
|
|
#endif
|
2023-08-10 22:46:04 +00:00
|
|
|
uniform vec4 blend_weights[MAX_BLENDSHAPES]; // @todo: implement me
|
|
|
|
uniform float f_num_blend_shapes; // @todo: implement me
|
|
|
|
uniform sampler2DArray blend_shapes; // @todo: implement me
|
2023-08-10 21:53:51 +00:00
|
|
|
|
2023-09-10 14:46:07 +00:00
|
|
|
|
2023-08-10 22:46:04 +00:00
|
|
|
in vec3 att_position; // @todo: reorder ass2iqe to emit p3 n3 u2 t3 b3 c4B i4 w4 instead
|
2023-08-10 21:53:51 +00:00
|
|
|
in vec2 att_texcoord;
|
|
|
|
in vec3 att_normal;
|
2023-08-10 22:46:04 +00:00
|
|
|
in vec4 att_tangent; // vec3 + bi sign
|
|
|
|
in mat4 att_instanced_matrix; // for instanced rendering
|
|
|
|
in vec4 att_indexes; // @fixme: gles might use ivec4 instead?
|
|
|
|
in vec4 att_weights; // @todo: downgrade from float to byte
|
|
|
|
in float att_vertexindex; // for blendshapes
|
2023-08-10 21:53:51 +00:00
|
|
|
in vec4 att_color;
|
2023-12-01 15:05:38 +00:00
|
|
|
in vec2 att_texcoord2;
|
2023-12-02 09:58:18 +00:00
|
|
|
in vec3 att_bitangent; // @todo: remove? also, ass2iqe might output this
|
2023-08-10 21:53:51 +00:00
|
|
|
out vec4 v_color;
|
2023-09-23 18:53:25 +00:00
|
|
|
out vec3 v_position, v_position_ws;
|
2023-08-10 21:53:51 +00:00
|
|
|
out vec3 v_normal, v_normal_ws;
|
2023-12-01 15:05:38 +00:00
|
|
|
out vec2 v_texcoord, v_texcoord2;
|
2023-08-10 21:53:51 +00:00
|
|
|
|
|
|
|
|
2023-09-10 14:46:07 +00:00
|
|
|
|
|
|
|
|
2023-08-10 22:46:04 +00:00
|
|
|
// shadow
|
2023-08-10 21:53:51 +00:00
|
|
|
uniform mat4 model, view;
|
|
|
|
uniform mat4 cameraToShadowProjector;
|
|
|
|
out vec4 vneye;
|
|
|
|
out vec4 vpeye;
|
|
|
|
out vec4 sc;
|
|
|
|
void do_shadow() {
|
|
|
|
vneye = view * model * vec4(att_normal, 0.0f);
|
|
|
|
vpeye = view * model * vec4(att_position, 1.0);
|
|
|
|
sc = cameraToShadowProjector * model * vec4(att_position, 1.0f);
|
|
|
|
}
|
|
|
|
|
2023-09-10 14:46:07 +00:00
|
|
|
|
2023-08-10 22:46:04 +00:00
|
|
|
// blendshapes
|
2023-08-10 21:53:51 +00:00
|
|
|
vec3 evaluate_blend_shape(int vertex_index) {
|
|
|
|
ivec2 coord = ivec2(vertex_index & (2048 - 1), vertex_index >> 11);
|
|
|
|
int num_blend_shapes = int(f_num_blend_shapes);
|
|
|
|
vec3 offset = vec3(0.0);
|
|
|
|
for (int i = 0; i < num_blend_shapes; i++) {
|
|
|
|
vec4 packedw = blend_weights[i >> 2];
|
|
|
|
float weight = packedw[i & 3];
|
|
|
|
offset += weight * texelFetch(blend_shapes, ivec3(coord, i), 0).xyz;
|
|
|
|
}
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
2023-09-10 14:46:07 +00:00
|
|
|
|
2023-08-10 21:53:51 +00:00
|
|
|
void main() {
|
|
|
|
vec3 objPos;
|
|
|
|
if(!SKINNED) {
|
|
|
|
objPos = att_position;
|
|
|
|
v_normal = att_normal;
|
2023-09-21 10:10:36 +00:00
|
|
|
} else {
|
|
|
|
mat3x4 m = vsBoneMatrix[int(att_indexes.x)] * att_weights.x;
|
|
|
|
m += vsBoneMatrix[int(att_indexes.y)] * att_weights.y;
|
|
|
|
m += vsBoneMatrix[int(att_indexes.z)] * att_weights.z;
|
|
|
|
m += vsBoneMatrix[int(att_indexes.w)] * att_weights.w;
|
|
|
|
objPos = vec4(att_position, 1.0) * m;
|
|
|
|
|
|
|
|
// blendshapes
|
|
|
|
// objPos += evaluate_blend_shape(int(att_vertexindex));
|
|
|
|
|
|
|
|
v_normal = vec4(att_normal, 0.0) * m;
|
|
|
|
//@todo: tangents
|
|
|
|
}
|
2023-11-06 08:30:19 +00:00
|
|
|
|
|
|
|
// vec3 tangent = att_tangent.xyz;
|
|
|
|
// vec3 bitangent = cross(att_normal, att_tangent.xyz) * att_tangent.w;
|
|
|
|
v_normal_ws = normalize(vec3(model * vec4(v_normal, 0.))); // normal to world/model space
|
|
|
|
v_normal = normalize(v_normal);
|
|
|
|
v_position = att_position;
|
|
|
|
v_texcoord = att_texcoord;
|
2023-12-01 15:05:38 +00:00
|
|
|
v_texcoord2 = att_texcoord2;
|
2023-11-06 08:30:19 +00:00
|
|
|
v_color = att_color;
|
2023-11-06 12:51:17 +00:00
|
|
|
mat4 modelView = view * att_instanced_matrix;
|
|
|
|
mat4 l_model = att_instanced_matrix;
|
|
|
|
v_position_ws = (l_model * vec4( objPos, 1.0 )).xyz;
|
|
|
|
|
|
|
|
if(u_billboard > 0) {
|
|
|
|
vec3 cameraPosition = -transpose(mat3(view)) * view[3].xyz;
|
|
|
|
vec3 lookDir = normalize(cameraPosition - v_position_ws);
|
|
|
|
|
2023-11-07 09:54:37 +00:00
|
|
|
vec3 up = vec3(modelView[0][1], modelView[1][1], modelView[2][1]);
|
2023-11-06 12:51:17 +00:00
|
|
|
vec3 right = normalize(cross(up, lookDir));
|
|
|
|
up = cross(lookDir, right);
|
|
|
|
|
|
|
|
vec3 scale;
|
2023-11-07 09:54:37 +00:00
|
|
|
scale.x = length(vec3(l_model[0]));
|
|
|
|
scale.y = length(vec3(l_model[1]));
|
|
|
|
scale.z = length(vec3(l_model[2]));
|
|
|
|
// scale.x *= sign(l_model[0][0]);
|
|
|
|
// scale.y *= sign(l_model[1][1]);
|
|
|
|
// scale.z *= sign(l_model[2][2]);
|
2023-11-06 12:51:17 +00:00
|
|
|
|
|
|
|
mat4 billboardRotation = mat4(
|
|
|
|
vec4(right * scale.x, 0.0),
|
|
|
|
vec4(-up * scale.y, 0.0),
|
|
|
|
vec4(-lookDir * scale.z, 0.0),
|
|
|
|
vec4(0.0, 0.0, 0.0, 1.0)
|
|
|
|
);
|
|
|
|
|
|
|
|
if((u_billboard & 0x4) != 0) l_model[0] = billboardRotation[0];
|
|
|
|
if((u_billboard & 0x2) != 0) l_model[1] = billboardRotation[1];
|
|
|
|
if((u_billboard & 0x1) != 0) l_model[2] = billboardRotation[2];
|
|
|
|
modelView = view * l_model;
|
|
|
|
}
|
|
|
|
v_position_ws = (l_model * vec4( objPos, 1.0 )).xyz;
|
2023-11-06 08:30:19 +00:00
|
|
|
gl_Position = P * modelView * vec4( objPos, 1.0 );
|
|
|
|
do_shadow();
|
|
|
|
}
|