2

ピクセルシェーダーに拡散反射+鏡面反射方程式があり、この1つの問題を除いて、かなりうまく機能します。

これを変更すると、フロート減衰= 1.0f / d * d;

これに対して:フロート減衰= 1.0f /(d * d);

モデルは点灯しなくなり、代わりに周囲の強度の色になります。これは非常に奇妙だと思います。括弧が必要な理由は、(1 + 0.045 * d + 0.0075 * d * d)などの別の減衰関数を使用できるようにするためです。

これが私のピクセルシェーダー全体です。

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 );
}

括弧なし:http://i48.tinypic.com/357rmnq.png

括弧付き:http://i45.tinypic.com/70jscy.png

4

1 に答える 1

0

float attenuation = 1.0f / d*d;は等しいためfloat attenuation = 1.0f;、減衰のないシェーディング オブジェクトが得られます。

オブジェクトが原点の近くに配置され、カメラまでの距離に比べて小さいと仮定すると、 を使用するとゼロに近い減衰が得られますfloat attenuation = 1.0f / (d*d);。それは、dが周りに20.0あり、が であるからattenuationです1.0 / 400.0 = 0.0025。そのため、モデルの可視光は周囲光のみです。実際、これは正しいです。ポイント ライトは減衰が非常に高いため、オブジェクトにはほとんど影響がありません。などの他の減衰関数を試してみる1.0f / (0.003*d*d)と、違いがわかります。

于 2012-09-16T15:46:25.490 に答える