XNA 多光源镜面反射

 

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

 

 

XNA 多光源镜面反射_第1张图片 

 1 float4x4 World;
 2 float4x4 View;
 3 float4x4 Projection;
 4 float4x4 WorldViewProjection;
 5 float3 EyePosition;
 6
 7 #define  MaxLights 3
 8
 9 float3 LightDirs[MaxLights];
10 float4 LightColors[MaxLights];
11 int  LightCount;
12
13 float4 AmbientColor  =  float4( 0.05 , 0.05 , 0.05 , 1 );
14 float  SpecularPower  =   16 ;
15
16 texture Texture;
17 sampler 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
28 struct  VertexShaderInput
29 {
30    float4 pos : POSITION0;
31    float2 texCoord : TEXCOORD0;
32    float3 normal   : NORMAL;
33    float3 tangent  : TANGENT;
34}
;
35
36 struct  VertexShaderOutput
37 {
38    float4 pos : POSITION0;
39    float2 texCoord : TEXCOORD0;
40    float3 normal : TEXCOORD1;
41    float3 view : TEXCOORD2;
42}
;
43
44 VertexShaderOutput 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
57 float4 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
79 technique 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 : [email protected]

QQ : 78939328

http://www.gogoplayer.com.cn

你可能感兴趣的:(反射)