如何实现着色器来创建高光/阴影?

问题描述 投票:0回答:2

如何实现着色器来创建高光/阴影?我找到了highlights部分的代码(取自https://gitlab.bestminr.com/bestminr/FrontShaders/blob/master/shaders/),但我找不到shadows

的等效代码
varying vec4 coord;

uniform sampler2D texture;
uniform float highlights;

const float a = 1.357697966704323E-01;
const float b = 1.006045552016985E+00;
const float c = 4.674339906510876E-01;
const float d = 8.029414702292208E-01;
const float e = 1.127806558508491E-01;

void main() {
    vec4 color = texture2D(texture, coord.xy);
    float maxx = max(color.r, max(color.g, color.b));
    float minx = min(color.r, min(color.g, color.b));
    float lum = (maxx+minx)/2.0;
    float x1 = abs(highlights);
    float x2 = lum;
    float lum_new =  lum < 0.5 ? lum : lum+ a * sign(highlights) * exp(-0.5 * (((x1-b)/c)*((x1-b)/c) + ((x2-d)/e)*((x2-d)/e)));
    // gl_FragColor = color * lum_new / lum;
    gl_FragColor = vec4(color * lum_new / lum);
}
opengl glsl shader pixel-shader
2个回答
1
投票

要使物体的暗面真正变暗,您需要以下 3 个东西: -你的片段的法线 -你的片段的位置 -你的灯的位置 - 片段衰减后的光强度

cos() 计算的总体思路是,如果 cos>0 则表示光线照射到片段的前面,但如果为负数,则表示 -lightDir 的方向与法线相反,这意味着 lightDir 实际上照射到片段的前面后面的片段。

in vec3 fragPos;
in vec3 fragNormal;

uniform vec3 lightPos

vec3 lightIntensity;


   vec3 lightShadowCalc(vec3 fragPos, vec3 fragNormal, vec3 lightPos, vec3 lightIntensity)
{
   vec3 lightDir = normalize(fragPos - lightPos);
   vec3 newIntensity = lightIntensity * cos(fragNormal, -lightDir)
   newIntensity = max(vec3(0), newIntensity);

   return newIntensity;

}

这应该是这样的希望有帮助。


0
投票

老问题,但就是这样,与高光相同,但阴影相同。您可以使用 ShadowMidRange 来平滑阴影和中频之间的剪切。

varying vec2 uv;

uniform sampler2D t;
uniform float adjustments; // Positive for highlights, negative for shadows

void main() {
  vec4 color = texture2D(t, uv);
  
  // Calculate average luminance
  float maxx = max(color.r, max(color.g, color.b));
  float minx = min(color.r, min(color.g, color.b));
  float lum = (maxx + minx) / 2.0;
  
  // Define the range for the transition between shadows and midtones
  float shadowMidRange = 0.75; // Adjust this value as needed
  
  // Calculate the adjustment based on positive or negative adjustments
  float x1 = abs(adjustments);
  float x2 = lum;
  float lum_new = lum;
  if (adjustments > 0.0) {
    lum_new = mix(lum, lum + 0.5 * x1, smoothstep(shadowMidRange, 0.6, lum));
  } else if (adjustments < 0.0) {
    lum_new = mix(lum, lum - 0.5 * x1, smoothstep(0.4, shadowMidRange, 1.0 - lum));
  }
  
  // Apply the adjustment to the color
  color.rgb *= lum_new / lum;
  
  gl_FragColor = color;
}
© www.soinside.com 2019 - 2024. All rights reserved.