XNA 多光源镜面反射

 

     每盏灯都可以有镜面反射,最多支持三盏灯,再多就不能使用ps2_0,附上HLSL代码和执行文件,自己玩吧^_^

 

 

 

 1float4x4 World;
 2float4x4 View;
 3float4x4 Projection;
 4float4x4 WorldViewProjection;
 5float3 EyePosition;
 6
 7#define MaxLights 3
 8
 9float3 LightDirs[MaxLights];
10float4 LightColors[MaxLights];
11int LightCount;
12
13float4 AmbientColor = float4(0.05,0.05,0.05,1);
14float SpecularPower = 16;
15
16texture Texture;
17sampler TextureSampler = sampler_state
18{
19    Texture = (Texture);
20    AddressU  = Wrap;
21    AddressV  = Wrap;
22    AddressW  = Wrap;
23    MinFilter = Linear;
24    MagFilter = Linear;
25    MipFilter = Linear;
26}
;
27
28struct VertexShaderInput
29{
30    float4 pos : POSITION0;
31    float2 texCoord : TEXCOORD0;
32    float3 normal   : NORMAL;
33    float3 tangent  : TANGENT;
34}
;
35
36struct VertexShaderOutput
37{
38    float4 pos : POSITION0;
39    float2 texCoord : TEXCOORD0;
40    float3 normal : TEXCOORD1;
41    float3 view : TEXCOORD2;
42}
;
43
44VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
45{
46    VertexShaderOutput output;
47
48    WorldViewProjection = mul(mul(World, View), Projection);
49    output.pos = mul(input.pos, WorldViewProjection);
50    output.texCoord = input.texCoord;
51    output.normal = mul(input.normal, (float3x3)World);
52    output.view = EyePosition - mul(input.pos, World);
53    
54    return output;
55}

56
57float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
58{
59    float4 diffuseSum = 0;
60    float4 specularSum = 0;
61    for(int i = 0; i < LightCount; i++)
62    {
63        float3 L = normalize(-LightDirs[i]);
64        float3 N = normalize(input.normal);
65        float3 R = normalize(reflect(LightDirs[i], N));
66        float3 V = normalize(input.view);
67
68        diffuseSum += saturate(dot(N, L)) * LightColors[i];
69        specularSum += pow(saturate(dot(R, V)), SpecularPower);
70    }

71    
72    float4 textureColor = tex2D(TextureSampler, input.texCoord);
73
74    float4 final = AmbientColor + textureColor * diffuseSum + specularSum;
75
76    return final;
77}

78
79technique Technique1
80{
81    pass Pass1
82    {
83        VertexShader = compile vs_2_0 VertexShaderFunction();
84        PixelShader = compile ps_2_0 PixelShaderFunction();
85    }

86}

87

 

转载请注明出处:

作者:gogoplayer

E-mail : gogoplayer@163.com

QQ : 78939328

http://www.gogoplayer.com.cn

posted on 2008-11-13 16:09  gogoplayer  阅读(1855)  评论(6编辑  收藏  举报

导航