I am trying to do blinn-phong lighting with multiple lights. For this i need to calculate the tangent position of the lights to my vertex. But wgpu does not allow me to use an array of vecs in my return struct. Is there a way around?
code:
struct VertexOutput {
@builtin(position) clip_position: vec4<f32>,
@location(0) tex_coords: vec2<f32>,
@location(1) tangent_position: vec3<f32>,
@location(2) tangent_view_position: vec3<f32>,
@location(3) tangent_light_position: array<vec3<f32>,MAX_LIGHTS>,};
@vertex
fn vs_main(
model: VertexInput,
instance: InstanceInput,
) -> VertexOutput {
let model_matrix = mat4x4<f32>(
instance.model_matrix_0,
instance.model_matrix_1,
instance.model_matrix_2,
instance.model_matrix_3,
);
let normal_matrix = mat3x3<f32>(
instance.normal_matrix_0,
instance.normal_matrix_1,
instance.normal_matrix_2,
);
var light_position = array<vec3<f32>,MAX_LIGHTS>();
var light_color = array<vec3<f32>,MAX_LIGHTS>();
for (var i: u32 = 0; i < light.num_lights; i = i + 1) {
light_position[i] = vec3<f32>(light.position[i][0], light.position[i][1], light.position[i][2]);
light_color[i] = vec3<f32>(light.color[i][0], light.color[i][1], light.color[i][2]);
}
let world_normal = normalize(normal_matrix * model.normal);
let world_tangent = normalize(normal_matrix * model.tangent);
let world_bitangent = normalize(normal_matrix * model.bitangent);
let tangent_matrix = transpose(mat3x3<f32>(
world_tangent,
world_bitangent,
world_normal,
));
let world_position = model_matrix * vec4<f32>(model.position, 1.0);
var out: VertexOutput;
out.clip_position = camera.view_proj * world_position;
out.tex_coords = model.tex_coords;
out.tangent_position = tangent_matrix * world_position.xyz;
out.tangent_view_position = tangent_matrix * camera.view_pos.xyz;
var tlws = array<vec3<f32>,MAX_LIGHTS >();
for (var i: u32 = 0; i < light.num_lights; i = i + 1) {
//TODO: erst Variable erstellen, dann nach Schleife zuweischen
tlws[i] = tangent_matrix * light_position[i];
}
out.tangent_light_position = tlws;
return out;}
error:
Shader validation error: Entry point vs_main at Vertex is invalid = The type [6] cannot be used for user-defined entry point inputs or outputs