[英]Parentheses in HLSL cause light attenuation function to not work correctly?
I have a diffuse+specular equation in my pixel shader, and it works pretty well except for this one issue: 我的像素着色器中有一个diffuse + specular方程,除了这个问题外,它工作得很好:
When I change this: float attenuation = 1.0f / d*d; 当我改变它时:浮点衰减= 1.0f / d * d;
To this: float attenuation = 1.0f / ( d*d ); 为此:浮点衰减= 1.0f /(d * d);
My model is no longer lit, and is instead the color of my ambient intensity. 我的模型不再点亮,而是环境强度的颜色。 I find this extremely strange.
我觉得这很奇怪。 The reason I want parentheses is so I can use a different attenuation function such as ( 1 + 0.045*d + 0.0075*d*d ).
我想要括号的原因是可以使用其他衰减函数,例如(1 + 0.045 * d + 0.0075 * d * d)。
Here is my entire pixel shader: 这是我的整个像素着色器:
void ps( in v2p input, out float4 final_color : SV_TARGET )
{
float3 ambient_intensity = float3( 0.3f, 0.3f, 0.3f );
float3 diffuse_color = float3( 0.8f, 0.8f, 0.8f);
float3 specular_color = float3( 1.0f, 1.0f , 1.0f );
float3 tmp_light;
tmp_light.x = light_vector.x;
tmp_light.y = light_vector.y;
tmp_light.z = light_vector.z;
float3 norm_light = normalize( tmp_light );
float3 tmp_pos;
tmp_pos.x = input.pos.x;
tmp_pos.y = input.pos.y;
tmp_pos.z = input.pos.z;
float3 tmp_norm;
tmp_norm.x = input.norm.x;
tmp_norm.y = input.norm.y;
tmp_norm.z = input.norm.z;
float3 tmp_cam = float3( 0.0f, 0.0f, -20.0f ); // todo: make this stuff work right in cbuffer
// light intensity
float d = distance( tmp_light, tmp_pos );
float attenuation = 1.0f / d*d;
float3 pointlight = attenuation*light_color;
// diffuse lighting
float diffuse = max( dot( tmp_norm, norm_light) , 0.0f );
float3 diffuse_final = diffuse_color*ambient_intensity + diffuse_color*pointlight*diffuse;
// specular lighting
float3 reflect_vect = 2*dot( tmp_norm, norm_light )*tmp_norm - norm_light;
float ref_max = max( dot( reflect_vect, normalize(tmp_cam) ), 0.0f );
float spec_exponent = pow ( ref_max, 1.0f );
float3 spec_final;
if( dot( tmp_norm, norm_light ) <= 0 )
{
spec_final = float3( 0.0f, 0.0f, 0.0f );
}
if( dot( tmp_norm, norm_light ) > 0 )
{
spec_final = specular_color*pointlight*spec_exponent;
}
final_color = float4( diffuse_final + spec_final, 1.0f );
}
Without parentheses: http://i48.tinypic.com/357rmnq.png 不带括号: http : //i48.tinypic.com/357rmnq.png
With parentheses: http://i45.tinypic.com/70jscy.png 带括号: http : //i45.tinypic.com/70jscy.png
The line float attenuation = 1.0f / d*d;
线路
float attenuation = 1.0f / d*d;
is equal to float attenuation = 1.0f;
等于
float attenuation = 1.0f;
and that's why you get shaded object with no attenuation. 这就是为什么您获得没有衰减的阴影对象的原因。
Assuming your object is placed near the origin and is small compared to its distance to the camera, you get an attenuation close to zero when using float attenuation = 1.0f / (d*d);
假设您的物体放置在原点附近,并且与物体到相机的距离相比较小,那么使用
float attenuation = 1.0f / (d*d);
时,衰减将接近零float attenuation = 1.0f / (d*d);
. 。 That's because the
d
is around 20.0
and the attenuation
is 1.0 / 400.0 = 0.0025
. 这是因为
d
约为20.0
, attenuation
为1.0 / 400.0 = 0.0025
。 So the only visible light on the model is the ambient one. 因此,模型上唯一的可见光是环境光。 In fact, this is correct - the point light has just a very high attenuation and thus has got almost no impact on the object.
实际上,这是正确的-点光源仅具有非常高的衰减,因此几乎没有对物体产生影响。 Try some other attenuation function, for example
1.0f / (0.003*d*d)
, and you'll see the difference. 尝试其他衰减函数,例如
1.0f / (0.003*d*d)
,您将看到差异。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.