首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在暗环境灯上应用聚光灯- HLSL -单色灯

在暗环境灯上应用聚光灯- HLSL -单色灯
EN

Stack Overflow用户
提问于 2019-07-12 19:41:58
回答 1查看 338关注 0票数 0

我为我的Monogame项目编写了一个HLSL着色器,它使用环境照明来创建一个昼夜周期。

代码语言:javascript
复制
#if OPENGL
    #define SV_POSITION POSITION
    #define VS_SHADERMODEL vs_3_0
    #define PS_SHADERMODEL ps_3_0
#else
    #define VS_SHADERMODEL vs_4_0_level_9_1
    #define PS_SHADERMODEL ps_4_0_level_9_1
#endif

sampler s0;

struct VertexShaderOutput
{
    float4 Position : SV_POSITION;
    float4 Color : COLOR0;
    float2 TextureCoordinates : TEXCOORD0;
};


float ambient = 1.0f;
float percentThroughDay = 0.0f;

float4 MainPS(VertexShaderOutput input) : COLOR
{
    float4 pixelColor = tex2D(s0, input.TextureCoordinates);
    float4 outputColor = pixelColor;

    // lighting intensity is gradient of pixel position
    float Intensity = 1 + (1  - input.TextureCoordinates.y) * 1.3;
    outputColor.r = outputColor.r / ambient * Intensity;
    outputColor.g = outputColor.g / ambient * Intensity;
    outputColor.b = outputColor.b / ambient * Intensity;

    // sun set/rise blending 
    float exposeRed = (1 + (.39 - input.TextureCoordinates.y) * 8); // overexpose red
    float exposeGreen = (1 + (.39 - input.TextureCoordinates.y) * 2); // some extra green for the blue pixels
    float exposeBlue = (1 + (.39 - input.TextureCoordinates.y) * 6); // some extra blue 

    // happens over full screen
    if (input.TextureCoordinates.y < 1.0f) {

        float redAdder = max(1, (exposeRed * (percentThroughDay/0.25f))); // be at full exposure at 25% of day gone
        float greenAdder = max(1, (exposeGreen * (percentThroughDay/0.25f))); // be at full exposure at 25% of day gone
        float blueAdder = max(1, (exposeBlue * (percentThroughDay/0.25f))); // be at full exposure at 25% of day gone

        // begin reducing adders
        if (percentThroughDay >= 0.25f && percentThroughDay < 0.50f) {
            redAdder = max(1, (exposeRed * (1-(percentThroughDay - 0.25f)/0.25f)));
            greenAdder = max(1, (exposeGreen * (1-(percentThroughDay - 0.25f)/0.25f)));
            blueAdder = max(1, (exposeGreen * (1-(percentThroughDay - 0.25f)/0.25f)));
        }

        //mid day
        else if (percentThroughDay >= 0.50f && percentThroughDay < 0.75f) {
            redAdder = 1;
            greenAdder = 1;
            blueAdder = 1;
        }

        // add adders back for sunset
        else if (percentThroughDay >= 0.75f && percentThroughDay < 0.85f) {
            redAdder = max(1, (exposeRed * ((percentThroughDay - 0.75f)/0.10f)));
            greenAdder = max(1, (exposeGreen * ((percentThroughDay - 0.75f)/0.10f)));
            blueAdder = max(1, (exposeBlue * ((percentThroughDay - 0.75f)/0.10f)));
        }

        // begin reducing adders
        else if (percentThroughDay >= 0.85f) {
            redAdder = max(1, (exposeRed * (1-(percentThroughDay - 0.85f)/0.15f)));
            greenAdder = max(1, (exposeGreen * (1-(percentThroughDay - 0.85f)/0.15f)));
            blueAdder = max(1, (exposeBlue * (1-(percentThroughDay - 0.85f)/0.15f)));
        }

        outputColor.r = outputColor.r * redAdder;
        outputColor.g = outputColor.g * greenAdder;
        outputColor.b = outputColor.b * blueAdder;
    }

    return outputColor;
}

technique ambientLightDayNight
{
    pass P0
    {
        PixelShader = compile ps_2_0 MainPS();
    }
};

这在很大程度上是我想要的(它肯定需要一些计算优化)。

然而,我现在正在考虑增加聚光灯在我的游戏中,供玩家使用。我跟着这种方法一起工作,我独立于ambientLight着色器工作。这是一个非常简单的着色器,使用lightMask。

代码语言:javascript
复制
sampler s0;  

texture lightMask;  
sampler lightSampler = sampler_state{Texture = lightMask;};  

float4 PixelShaderLight(float2 coords: TEXCOORD0) : COLOR0  
{  
    float4 color = tex2D(s0, coords);  
    float4 lightColor = tex2D(lightSampler, coords);  
    return color * lightColor;  
}  


technique Technique1  
{  
    pass Pass1  
    {  
        PixelShader = compile ps_2_0 PixelShaderLight();  
    }  
}  

我的问题是同时使用这两种着色器。当前的方法是将我的游戏场景绘制到渲染目标,应用环境光着色器,然后在应用聚光灯着色器时将游戏场景绘制到客户端屏幕上。

这就引出了多个问题:

  • 应用聚光灯着色器后,周围的光线完全黑掉周围的任何光线,当在现实中,周围的光线应该是周围的光线。
  • 在聚光灯着色器中计算的光强度(光的亮度)在“夜晚”时太暗了,因为它是根据环境光着色器的输出来计算光色的。

我试着在聚光灯着色器之后应用环境光着色器,但是这只会使大部分的东西都变黑,因为环境光是根据大部分黑色背景来计算的。

我已经尝试添加一些代码到聚光灯着色器中,将黑色像素涂成白色,以便显示周围的光线背景,但是光线强度仍在根据较暗的环境光进行计算--结果是非常暗的光。

另一个想法是,只是修改我的环境光着色器,以lightMask作为一个参数,只是不应用环境光的灯标记上的光面具。然后我就可以使用聚光灯着色器来应用光线的灰度,并修改颜色。但我不确定我是否应该把这两个看似独立的光效果塞进一个像素着色器。当我尝试这个,我的着色器也没有编译,因为有太多的算术运算。

所以我对每个人的问题是:

  • 我应该避免把多个效果塞进一个像素着色器吗?
  • 一般来说,我如何应用光斑照明的环境光的影响,可以是“黑暗”?

编辑

我的解决方案

代码语言:javascript
复制
float4 MainPS(VertexShaderOutput input) : COLOR
{

    float4 constant = 1.5f;
    float4 pixelColor = tex2D(s0, input.TextureCoordinates);
    float4 outputColor = pixelColor;

    // lighting intensity is gradient of pixel position
    float Intensity = 1 + (1  - input.TextureCoordinates.y) * 1.05;
    outputColor.r = outputColor.r / ambient * Intensity;
    outputColor.g = outputColor.g / ambient * Intensity;
    outputColor.b = outputColor.b / ambient * Intensity;

    // sun set/rise blending  
    float gval = (1 - input.TextureCoordinates.y); // replace 1 with .39 to lock to 39 percent of screen (this is how it was before)
    float exposeRed = (1 + gval * 8); // overexpose red
    float exposeGreen = (1 + gval * 2); // some extra green
    float exposeBlue = (1 + gval * 4); // some extra blue 

    float quarterDayPercent = (percentThroughDay/0.25f);
    float redAdder = max(1, (exposeRed * quarterDayPercent)); // be at full exposure at 25% of day gone
    float greenAdder = max(1, (exposeGreen * quarterDayPercent)); // be at full exposure at 25% of day gone
    float blueAdder = max(1, (exposeBlue * quarterDayPercent)); // be at full exposure at 25% of day gone

    // begin reducing adders
    if (percentThroughDay >= 0.25f ) {
        float gradientVal1 = (1-(percentThroughDay - 0.25f)/0.25f);
        redAdder = max(1, (exposeRed * gradientVal1));
        greenAdder = max(1, (exposeGreen * gradientVal1));
        blueAdder = max(1, (exposeGreen * gradientVal1));
    }

    //mid day
    if (percentThroughDay >= 0.50f) {
        redAdder = 1;
        greenAdder = 1;
        blueAdder = 1;
    }

    // add adders back for sunset
    if (percentThroughDay >= 0.75f) {
        float gradientVal2 = ((percentThroughDay - 0.75f)/0.10f);
        redAdder = max(1, (exposeRed * gradientVal2));
        greenAdder = max(1, (exposeGreen * gradientVal2));
        blueAdder = max(1, (exposeBlue * gradientVal2));
    }

    // begin reducing adders
    if (percentThroughDay >= 0.85f) {

        float gradientVal3 = (1-(percentThroughDay - 0.85f)/0.15f);
        redAdder = max(1, (exposeRed * gradientVal3));
        greenAdder = max(1, (exposeGreen * gradientVal3));
        blueAdder = max(1, (exposeBlue * gradientVal3));
    }

    outputColor.r = outputColor.r * redAdder;
    outputColor.g = outputColor.g * greenAdder;
    outputColor.b = outputColor.b * blueAdder;

    // first check if we are in a lightMask light
    float4 lightMaskColor = tex2D(lightSampler, input.TextureCoordinates);
    if (lightMaskColor.r != 0.0f || lightMaskColor.g != 0.0f || lightMaskColor.b != 0.0f) 
    {
        // we are in the light so don't apply ambient light
        return pixelColor * (lightMaskColor + outputColor) * constant; // have to offset by outputColor here because the lightMask is pure black
    }

    return outputColor * pixelColor * constant; // must multiply by pixelColor here to offset the lightMask bounds. TODO: could try to restore original color by removing this multiplaction and factoring in more of an offset on ln 91
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-07-15 11:40:45

要想像你想要的那样锁链,你需要一种不同的方法。正如你已经遇到的,把灯链在颜色上是行不通的,因为一旦颜色变黑,就不能再高亮显示了。为了处理多个光线,有两种典型的方法:向前阴影和延迟阴影。每一个都有它的优点和缺点,所以你需要找出哪个更适合你的情况。

正向阴影

这是一个方法,你测试的填充所有照明计算在一个单一的阴影通行证。你把所有的光强度加到最后的光强上,然后把它与颜色相乘。

优点是性能和简单,Cons是在数量上的限制和更复杂的着色代码。

延后遮阳

这种方法将单个灯光相互分离,并可用来绘制有很多灯光的场景。每个光都需要原始场景颜色(反照率)来计算其部分的最终图像。因此,您首先渲染您的场景没有任何照明纹理(通常称为颜色缓冲区或反照率缓冲区)。然后,你可以用反照率和反照率分别渲染每个光,然后把它加到最后的图像中。所以即使在黑暗的部分,原来的颜色也会带着光回来。

优点是更清洁的结构和可能使用许多灯,即使是不同的形状。缺点是额外的缓冲区和抽签电话,必须作出。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57013180

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档