Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //------------------------------------------------------------------------
- // Light settings
- //------------------------------------------------------------------------
- #define MAX_LIGHTS 8
- #define LIGHT_DIR 0
- #define LIGHT_PNT 1
- #define LIGHT_SPT 2
- struct Light
- {
- int m_type;
- float3 m_pos;
- float4 m_ambient;
- float4 m_diffuse;
- float4 m_specular;
- float m_radius;
- };
- struct Material
- {
- float4 m_ambient;
- float4 m_diffuse;
- float4 m_emissive;
- float4 m_specular;
- float m_specularPower;
- };
- //------------------------------------------------------------------------
- // Globals
- //------------------------------------------------------------------------
- uniform extern matrix g_world;
- uniform extern matrix g_worldInvTrans;
- uniform extern matrix g_view;
- uniform extern matrix g_proj;
- uniform extern float3 g_cameraPos;
- uniform extern float4 g_globalAmbient;
- uniform extern int g_numLights;
- uniform extern matrix g_boneMatrices[40];
- uniform extern Light g_lights[MAX_LIGHTS];
- uniform extern Material g_material;
- //------------------------------------------------------------------------
- // Textures
- //------------------------------------------------------------------------
- uniform extern texture g_tex;
- sampler g_texSampler = sampler_state
- {
- Texture = <g_tex>;
- MinFilter = Anisotropic;
- MagFilter = Linear;
- MipFilter = Linear;
- MaxAnisotropy = 8;
- };
- //------------------------------------------------------------------------
- // Vertex shader
- //------------------------------------------------------------------------
- struct InputVS
- {
- float3 m_pos : POSITION0;
- int4 m_boneIndices : BLENDINDICES0;
- float4 m_weights : BLENDWEIGHT0;
- float3 m_normal : NORMAL;
- float2 m_tex0 : TEXCOORD0;
- };
- struct OutputVS
- {
- float4 m_posH : POSITION0;
- float3 m_posW : POSITION1;
- float3 m_normalW : NORMAL;
- float2 m_tex0 : TEXCOORD0;
- };
- float4 blendWeights(float4 t_pos, float4 t_weights, int4 t_bones)
- {
- float4 l_result = float4(0.0f, 0.0f, 0.0f, 0.0f);
- [unroll]
- for (int i = 0; i < 4; ++i) {
- if (t_weights[i] != 0.0f) {
- l_result += t_weights[i] * mul(t_pos, g_boneMatrices[t_bones[i]]);
- }
- }
- return l_result;
- }
- //------------------------------------------------------------------------
- // Vertex Shader
- //------------------------------------------------------------------------
- OutputVS VS(InputVS t_input) {
- OutputVS l_result;
- float4 l_pos = blendWeights(float4(t_input.m_pos, 1.0f), t_input.m_weights, t_input.m_boneIndices);
- l_pos.w = 1.0f;
- float4x4 l_wvp = mul(g_world, mul(g_view, g_proj));
- l_result.m_posH = mul(l_pos, l_wvp);
- l_result.m_posW = mul(l_pos, g_world).xyz;
- l_result.m_tex0 = t_input.m_tex0;
- float4 l_normal = blendWeights(float4(t_input.m_normal, 1.0f), t_input.m_weights, t_input.m_boneIndices);
- l_normal.w = 0.0f;
- l_result.m_normalW = mul(l_normal, (float3x3)g_worldInvTrans).xyz;
- return l_result;
- }
- //------------------------------------------------------------------------
- // Pixel Shader
- //------------------------------------------------------------------------
- float4 PS(OutputVS l_input) : COLOR
- {
- float3 l_n = normalize(l_input.m_normalW);
- float3 l_v = normalize(g_cameraPos - l_input.m_posW);
- float3 l_lightDir = float3(0.0f, 0.0f, 0.0f);
- float3 l_h = float3(0.0f, 0.0f, 0.0f);
- float l_atten = 0.0f;
- float l_nDotl = 0.0f;
- float l_nDoth = 0.0f;
- float l_power = 0.0f;
- float4 l_colour = float4(0.0f, 0.0f, 0.0f, 0.0f);
- [unroll]
- for (int i = 0; i < g_numLights; ++i) {
- l_lightDir = (g_lights[i].m_pos - l_input.m_posW) / g_lights[i].m_radius;
- l_atten = saturate(1.0f - dot(l_lightDir, l_lightDir));
- l_lightDir = normalize(l_lightDir);
- l_h = normalize(l_lightDir + l_v);
- l_nDotl = saturate(dot(l_n, l_lightDir));
- l_nDoth = saturate(dot(l_n, l_h));
- l_power = (l_nDotl == 0.0f) ? 0.0f : pow(l_nDoth, g_material.m_specularPower);
- l_colour += (g_material.m_ambient * (g_globalAmbient + (l_atten * g_lights[i].m_ambient))) +
- (g_material.m_diffuse * g_lights[i].m_diffuse * l_nDotl * l_atten) +
- (g_material.m_specular * g_lights[i].m_specular * l_power * l_atten);
- }
- return l_colour * tex2D(g_texSampler, l_input.m_tex0);
- }
- //------------------------------------------------------------------------
- // Techniques
- //------------------------------------------------------------------------
- technique draw
- {
- pass P0
- {
- vertexShader = compile vs_3_0 VS();
- pixelShader = compile ps_3_0 PS();
- ZEnable = 1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement