如何计算聚光灯的距离?

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

我希望我的聚光灯具有最大范围。

目前,着色器看起来像这样:

#version 460 core

layout(location = 0) out vec4 f_color;

in vec2 v_position;
in vec2 v_uv;
in vec4 v_color;

uniform sampler2D u_texture;

void main()
{ 
    vec4 texColor = texture(u_texture, v_uv);
    vec4 objectColor = texColor * v_color;
    vec3 objectColorRGB = objectColor.rgb;

    vec3 lightPosition = vec3(150.0f, 150.0f, 5.0f);

    vec3 normal = normalize(vec3(0.0f, 0.0f, 1.0f));

    vec3 lightDir = normalize(lightPosition - vec3(v_position, 0.0f));
    float diff = max(dot(normal, lightDir), 0.0);

    float distance = length(lightPosition - vec3(v_position, 0.0f));
    float attenuation = 1.0 / distance * distance;    

    vec3 ambient = vec3(0.1f) * objectColorRGB;
    vec3 diffuse = vec3(0.8f) * diff * objectColorRGB; 

    ambient *= attenuation;
    diffuse *= attenuation;

    vec2 lightDir2 = normalize(lightPosition.xy - v_position);

    vec2 dir = { 1.0f, 0.707f };
    float theta = dot(lightDir2, normalize(-dir));

    float cutoff = cos(radians(12.5f));
    float outercutoff = cos(radians(17.5f));

    float epsilon   = cutoff - outercutoff;
    float intensity = clamp((theta - outercutoff) / (epsilon * 0.5f), 0.0, 5.0);   

    //ambient *= intensity;
    diffuse *= intensity;

    vec3 result = ambient + diffuse;

    f_color = vec4(result, 1.0f);
}

相机是正交的,左侧= 0.0f;正确= 1600.0f;顶部= 900.0f;底部= 0.0f

场景现在的样子:

enter image description here

我希望这样:

enter image description here

我如何实现?

opengl glsl 2d shader light
1个回答
0
投票

从光源到当前处理的片段的距离可以通过length来计算,例如:

length

如果超过某个float light_dist = length(lightPosition.xyz - v_position.xyz); ,则将intensity设置为0.0:(如果max_distance返回,则step返回0.0,否则返回1.0)

step
© www.soinside.com 2019 - 2024. All rights reserved.