Opengl 屏幕空间反射伪影
当我实现SSR时,我遇到了工件的问题。下面我展示代码和屏幕截图 片段SSR着色器:Opengl 屏幕空间反射伪影,opengl,glsl,raytracing,raycasting,post-processing,Opengl,Glsl,Raytracing,Raycasting,Post Processing,当我实现SSR时,我遇到了工件的问题。下面我展示代码和屏幕截图 片段SSR着色器: #version 330 core uniform sampler2D normalMap; // in view space uniform sampler2D colorMap; uniform sampler2D reflectionStrengthMap; uniform sampler2D positionMap; // in view space uniform mat4 projection; u
#version 330 core
uniform sampler2D normalMap; // in view space
uniform sampler2D colorMap;
uniform sampler2D reflectionStrengthMap;
uniform sampler2D positionMap; // in view space
uniform mat4 projection;
uniform vec3 skyColor = vec3(0.1, 0, 0.5);
in vec2 texCoord;
layout (location = 0) out vec4 fragColor;
const int binarySearchCount = 10;
const int rayMarchCount = 30;
const float step = 0.05;
const float LLimiter = 0.2;
const float minRayStep = 0.2;
vec3 getPosition(in vec2 texCoord) {
return texture(positionMap, texCoord).xyz;
}
vec2 binarySearch(inout vec3 dir, inout vec3 hitCoord, inout float dDepth) {
float depth;
vec4 projectedCoord;
for(int i = 0; i < binarySearchCount; i++) {
projectedCoord = projection * vec4(hitCoord, 1.0);
projectedCoord.xy /= projectedCoord.w;
projectedCoord.xy = projectedCoord.xy * 0.5 + 0.5;
depth = getPosition(projectedCoord.xy).z;
dDepth = hitCoord.z - depth;
dir *= 0.5;
if(dDepth > 0.0)
hitCoord += dir;
else
hitCoord -= dir;
}
projectedCoord = projection * vec4(hitCoord, 1.0);
projectedCoord.xy /= projectedCoord.w;
projectedCoord.xy = projectedCoord.xy * 0.5 + 0.5;
return vec2(projectedCoord.xy);
}
vec2 rayCast(vec3 dir, inout vec3 hitCoord, out float dDepth) {
dir *= step;
for (int i = 0; i < rayMarchCount; i++) {
hitCoord += dir;
vec4 projectedCoord = projection * vec4(hitCoord, 1.0);
projectedCoord.xy /= projectedCoord.w;
projectedCoord.xy = projectedCoord.xy * 0.5 + 0.5;
float depth = getPosition(projectedCoord.xy).z;
dDepth = hitCoord.z - depth;
if((dir.z - dDepth) < 1.2 && dDepth <= 0.0) return binarySearch(dir, hitCoord, dDepth);
}
return vec2(-1.0);
}
void main() {
float reflectionStrength = texture(reflectionStrengthMap, texCoord).r;
if (reflectionStrength == 0) {
fragColor = texture(colorMap, texCoord);
return;
}
vec3 normal = texture(normalMap, texCoord).xyz;
vec3 viewPos = getPosition(texCoord);
// Reflection vector
vec3 reflected = normalize(reflect(normalize(viewPos), normalize(normal)));
// Ray cast
vec3 hitPos = viewPos;
float dDepth;
vec2 coords = rayCast(reflected * max(-viewPos.z, minRayStep), hitPos, dDepth);
float L = length(getPosition(coords) - viewPos);
L = clamp(L * LLimiter, 0, 1);
float error = 1 - L;
vec3 color = texture(colorMap, coords.xy).rgb * error;
if (coords.xy != vec2(-1.0)) {
fragColor = mix(texture(colorMap, texCoord), vec4(color, 1.0), reflectionStrength);
return;
}
fragColor = mix(texture(colorMap, texCoord), vec4(skyColor, 1.0), reflectionStrength);
}
#版本330核心
均匀采样二维法线贴图;//视野空间
均匀采样二维彩色地图;
均匀采样2D反射强度图;
均匀采样二维位置图;//视野空间
均匀mat4投影;
均匀vec3天空颜色=vec3(0.1,0,0.5);
在vec2 texCoord;
布局(位置=0)输出vec4 fragColor;
const int binarySearchCount=10;
常数int rayMarchCount=30;
常数浮动步长=0.05;
常量浮点LLimiter=0.2;
常量float minRayStep=0.2;
vec3获取位置(在vec2 texCoord中){
返回纹理(positionMap,texCoord).xyz;
}
vec2二进制搜索(inout vec3 dir、inout vec3 hitCoord、inout float dDepth){
漂浮深度;
vec4项目合作;
for(int i=0;i0.0)
hitCoord+=dir;
其他的
hitCoord-=dir;
}
projectedCoord=projection*vec4(hitCoord,1.0);
projectedCoord.xy/=projectedCoord.w;
projectedCoord.xy=projectedCoord.xy*0.5+0.5;
返回vec2(projectedCoord.xy);
}
vec2光线投射(vec3直达、inout vec3 hitCoord、out float dDepth){
dir*=步进;
对于(int i=0;i 如果((dir.z-dDepth)<1.2&&dDepth我做了这里描述的菲涅耳:。人工制品的问题仍然悬而未决