import msp_interface; import shadow; struct AlphaCutoffParams { float cutoff; float feather; }; layout(set=1) uniform sampler2D normal_map; layout(constant_id=auto) const bool use_instancing = false; layout(constant_id=auto) const bool use_normal_map = false; layout(constant_id=auto) const bool use_alpha_cutoff = false; #pragma MSP stage(vertex) virtual vec4 get_vertex_position() { return vertex; } virtual vec3 get_vertex_normal() { return normal; } virtual mat4 get_vertex_transform() { if(use_instancing) return transpose(mat4(instance_transform[0], instance_transform[1], instance_transform[2], vec4(0.0, 0.0, 0.0, 1.0))); else return world_obj_matrix; } virtual mat3 get_normal_transform() { if(use_instancing) return transpose(mat3(instance_transform[0].xyz, instance_transform[1].xyz, instance_transform[2].xyz)); else return world_obj_normal_matrix; } void standard_transform() { mat4 vertex_tf = get_vertex_transform(); mat3 normal_tf = get_normal_transform(); out vec4 world_vertex = vertex_tf*get_vertex_position(); vec4 eye_vertex = eye_world_matrix*world_vertex; gl_Position = clip_eye_matrix*eye_vertex; out vec3 world_normal = normalize(normal_tf*get_vertex_normal()); if(use_normal_map) { vec3 world_tangent = normalize(normal_tf*tangent); vec3 world_binormal = normalize(cross(world_normal, world_tangent)); out mat3 world_tbn_matrix = mat3(world_tangent, world_binormal, world_normal); } vec3 eye_pos = world_eye_matrix[3].xyz; out vec3 world_look_dir = normalize(world_vertex.xyz-eye_pos); out float fog_coord = eye_vertex.z; } virtual void custom_transform() { } void main() { standard_transform(); custom_transform(); passthrough; } #pragma MSP stage(fragment) struct IncomingLight { vec3 direction; vec3 color; }; layout(location=0) out vec4 frag_color; virtual vec3 get_fragment_normal() { vec3 normal; float sgn = (gl_FrontFacing ? 1.0 : -1.0); if(use_normal_map) return sgn*normalize(world_tbn_matrix*(texture(normal_map, texcoord.xy).xyz*2.0-1.0)); else return sgn*normalize(world_normal); } virtual IncomingLight get_incoming_light(int index, vec3 world_pos) { vec4 light_pos = light_sources[index].position; vec3 rel_pos = light_pos.xyz-world_pos*light_pos.w; float d = length(rel_pos); float attenuation = 1.0/dot(vec3(1.0, d, d*d), light_sources[index].attenuation); return IncomingLight(rel_pos/d, light_sources[index].color*attenuation); } float apply_alpha_cutoff(float alpha, AlphaCutoffParams params) { if(use_alpha_cutoff) { if(alpha