Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ====== rsm.vs =============================
- #version 330 core
- uniform mat4 model;
- uniform mat4 lightspace;
- layout(location=0) in vec3 invertex;
- layout(location=1) in vec2 intexcoord;
- layout(location=2) in vec3 innormal;
- out vec3 wsposition;
- out vec3 wsnormal;
- out vec2 texcoord;
- void main(){
- vec4 invertex4 = vec4(invertex, 1.0);
- wsposition = (model * invertex4).xyz;
- wsnormal = normalize(transpose(inverse(mat3(model))) * innormal);
- texcoord = intexcoord;
- gl_Position = lightspace * model * invertex4;
- }
- // ======= rsm.fs ===============================
- #version 330 core
- uniform sampler2D tex_color;
- uniform vec4 basecolor;
- in vec3 wsposition;
- in vec3 wsnormal;
- in vec2 texcoord;
- layout(location = 0) out vec4 out_position;
- layout(location = 1) out vec4 out_normal;
- layout(location = 2) out vec4 out_flux;
- void main(void) {
- out_position = vec4(wsposition, 1.0);
- out_normal = vec4(normalize(wsnormal) * 0.5 + 0.5, 1.0);
- out_flux = basecolor * texture(tex_color, texcoord);
- }
- // ======= rsm_render.vs ========================
- #version 330 core
- uniform vec3 viewposition;
- uniform mat4 lightspace;
- uniform mat4 model;
- uniform mat4 MVP;
- layout(location=0) in vec3 invertex;
- layout(location=1) in vec2 intexcoord;
- layout(location=2) in vec3 innormal;
- out vec2 texcoord;
- out vec3 viewvec;
- out vec3 normalvec;
- out vec3 wsposition;
- out vec4 lsposition;
- void main(){
- vec4 invertex4 = vec4(invertex, 1.0);
- wsposition = (model * invertex4).xyz;
- lsposition = lightspace * vec4(wsposition, 1.0);
- viewvec = wsposition.xyz-viewposition;
- normalvec = normalize(transpose(inverse(mat3(model))) * innormal);
- texcoord = intexcoord;
- gl_Position = MVP * invertex4;
- }
- // ======= rsm_render.fs ========================
- #version 330 core
- uniform sampler2D tex_color;
- uniform sampler2D tex_depth;
- uniform sampler2D tex_wspos;
- uniform sampler2D tex_normal;
- uniform sampler2D tex_flux;
- uniform sampler1D tex_rand;
- uniform int samples; //64
- uniform float sample_radius; //0.5
- uniform vec4 basecolor;
- uniform vec3 lightdirection;
- in vec2 texcoord;
- in vec3 viewvec;
- in vec3 normalvec;
- in vec3 wsposition;
- in vec4 lsposition;
- out vec4 glfragcolor;
- #define M_PI 3.1415926
- float ShadowCalculation(vec4 fragPosLightSpace){
- vec3 projCoords = fragPosLightSpace.xyz / fragPosLightSpace.w;
- projCoords = projCoords * 0.5 + 0.5;
- float closestDepth = texture(tex_depth, projCoords.xy).r;
- float currentDepth = projCoords.z;
- vec3 normal = normalize(normalvec);
- float bias = max(0.05 * (1.0 - dot(normalvec, lightdirection)), 0.005);
- float shadow = 0.0;
- vec2 texelSize = 1.0 / textureSize(tex_depth, 0);
- for(int x = -1; x <= 1; ++x){
- for(int y = -1; y <= 1; ++y){
- float pcfDepth = texture(tex_depth, projCoords.xy + vec2(x, y) * texelSize).r;
- shadow += (currentDepth - bias > pcfDepth) ? 1.0 : 0.0;
- }
- }
- shadow /= 9.0;
- if(projCoords.z > 1.0)
- shadow = 0.0;
- return shadow;
- }
- void main(){
- vec4 albedo = texture(tex_color, texcoord) * basecolor;
- vec3 indirect = vec3(0.0, 0.0, 0.0);
- vec2 ls_uv = (lsposition.xy / lsposition.w) * 0.5 + 0.5;
- for (int i = 0; i < samples; i++) {
- float s_dist = sample_radius * texture(tex_rand, 0.5 * (i * 2) / samples).x;
- float s_angle = 2.0 * M_PI * texture(tex_rand, 0.5 * (i * 2 + 1) / samples).x;
- vec2 offset = s_dist * vec2(cos(s_angle), sin(s_angle));
- vec2 uv = ls_uv + offset;
- vec3 pos = texture(tex_wspos, uv).xyz;
- vec3 nrm = normalize(texture(tex_normal, uv).xyz);
- vec3 flux = texture(tex_flux, uv).rgb;
- float dot1 = max(0.0, dot(pos - wsposition, normalize(normalvec)));
- float dot2 = max(0.0, -dot(wsposition - pos, nrm));
- float dist = length(pos - wsposition);
- indirect += diff * (dot1 * dot2) / pow(dist, 4);
- //indirect += flux * ( (max(0.0, dot(nrm, pos - wsposition))) ) / pow( length(wsposition - pos), 4);
- }
- indirect = 4.0 * M_PI * indirect / samples;
- float shadow = ShadowCalculation(lsposition);
- float diff = max(dot(lightdirection, normalvec), 0.0);
- vec3 lighting = (1.0 - shadow) * (albedo.rgb * diff + albedo.rgb * indirect);
- glfragcolor = vec4(lighting, albedo.a);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement