【问题标题】:How to implement a shader to create highlights/shadows?如何实现着色器来创建高光/阴影?
【发布时间】:2018-06-26 12:00:27
【问题描述】:

如何实现着色器来创建 highlights/shadows ?我为 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);
}

【问题讨论】:

  • 您是指物体暗面的阴影还是物体投射的阴影?第一个很简单,你只需要使用片段的法线,如果 cos(norma, lightDIr)
  • @florentteppe 我的意思是物体阴暗面的阴影......

标签: opengl glsl shader pixel-shader


【解决方案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;

}

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-03
    • 1970-01-01
    • 2013-08-23
    相关资源
    最近更新 更多