1/*---------------------------------------------------------------------------*
2
3  Copyright 2010-2012 Nintendo.  All rights reserved.
4
5  These coded instructions, statements, and computer programs contain
6  proprietary information of Nintendo of America Inc. and/or Nintendo
7  Company Ltd., and are protected by Federal copyright law.  They may
8  not be disclosed to third parties or copied or duplicated in any form,
9  in whole or in part, without the prior written consent of Nintendo.
10
11 *---------------------------------------------------------------------------*/
12
13///////////////////////////////////////////////////////////////////////////////////////////////////////
14// G-Buffer Pass //////////////////////////////////////////////////////////////////////////////////////
15///////////////////////////////////////////////////////////////////////////////////////////////////////
16
17float4x4 u_modelMtx : WORLD : register(c0);
18float4x4 u_viewprojMtx : VIEWPROJ : register(c4);
19
20float4 u_color : COLOR : register(c8);
21
22struct VS_INPUT_SETUP
23{
24    float4 position : POSITION0;
25    float3 normal   : NORMAL0;
26};
27
28struct PS_INPUT_SETUP
29{
30    float4 vPos      : POSITION0;
31    float4 vWorldPos : TEXCOORD1;
32    float4 vWorldNrm : TEXCOORD2;
33};
34
35struct PS_OUTPUT_SETUP
36{
37    float4 vMaterial    : COLOR0;
38    float4 vWorldNrm    : COLOR1;
39    float4 vWorldPosXYZ : COLOR2;
40};
41
42PS_INPUT_SETUP vertexSetup(VS_INPUT_SETUP input)
43{
44    PS_INPUT_SETUP output;
45
46    output.vWorldPos = mul(input.position, u_modelMtx);
47    output.vPos = mul(output.vWorldPos, u_viewprojMtx);
48    output.vWorldNrm = normalize(mul(float4(input.normal, 0.0), u_modelMtx));
49
50    return output;
51}
52
53PS_OUTPUT_SETUP pixelSetup(PS_INPUT_SETUP input)
54{
55    PS_OUTPUT_SETUP output;
56
57    output.vMaterial = u_color;
58    output.vWorldNrm = float4(input.vWorldNrm.xyz, 1.0);
59    output.vWorldPosXYZ = input.vWorldPos;
60
61    return output;
62}
63
64technique setup
65{
66    pass p0
67    {
68        VertexShader = compile vs_1_1 vertexSetup();
69        PixelShader = compile ps_2_0 pixelSetup();
70
71        CullMode = cw;
72        Zenable = true;
73        ZWriteEnable = true;
74        ZFunc = less;
75        StencilEnable = false;
76        AlphaBlendEnable = false;
77        AlphaTestEnable = false;
78    }
79}
80
81///////////////////////////////////////////////////////////////////////////////////////////////////////
82// Lighting Pass //////////////////////////////////////////////////////////////////////////////////////
83///////////////////////////////////////////////////////////////////////////////////////////////////////
84
85float3 u_lightPosition : LIGHTPOS : register(c0);
86float3 u_ambientColor : AMBIENTCOLOR : register(c1);
87float4 u_lightColor : LIGHTCOLOR : register(c2);
88float3 u_cameraPosition : CAMERAPOS : register(c3);
89
90texture2D t_diffuse;
91sampler s_diffuse : register(s0) = sampler_state
92{
93    Texture = <t_diffuse>;
94    MagFilter = Point;
95    MinFilter = Point;
96};
97
98texture2D t_normal;
99sampler s_normal : register(s1) = sampler_state
100{
101    Texture = <t_normal>;
102    MagFilter = Point;
103    MinFilter = Point;
104};
105
106texture2D t_position;
107sampler s_position : register(s2) = sampler_state
108{
109    Texture = <t_position>;
110    MagFilter = Point;
111    MinFilter = Point;
112};
113
114struct VS_INPUT_LIGHTING
115{
116    float4 position  : POSITION0;
117    float2 texCoord : TEXCOORD0;
118};
119
120struct PS_INPUT_LIGHTING
121{
122    float4 vPos  : POSITION0;
123    float2 vTex0 : TEXCOORD0;
124};
125
126PS_INPUT_LIGHTING vertexLighting(VS_INPUT_LIGHTING input)
127{
128    PS_INPUT_LIGHTING output;
129
130    output.vPos = input.position;
131    output.vTex0 = input.texCoord;
132
133    return output;
134}
135
136float4 pixelLighting(PS_INPUT_LIGHTING input) : COLOR0
137{
138    float4 diffuse = tex2D(s_diffuse, input.vTex0);
139    float4 norm = tex2D(s_normal, input.vTex0);
140    float3 pos = tex2D(s_position, input.vTex0).xyz;
141
142    if(norm.w == 0.0)
143    {
144        discard;
145    }
146    float3 normal = normalize(norm.xyz);
147
148	float3 lightDir = normalize(u_lightPosition - pos);
149
150    float3 ambientCol = u_ambientColor * diffuse.rgb;
151
152    float strength = u_lightColor.a * 255.0 / distance(pos, u_lightPosition);
153	float3 diffuseCol = u_lightColor.rgb * strength * diffuse.rgb;
154
155    float3 specularCol = diffuse.a * u_lightColor.rgb;
156    float3 refl = reflect(lightDir, normal);
157    float3 toViewer = normalize(pos - u_cameraPosition);
158    float shininess = diffuse.a * 255.0;
159
160	return float4(ambientCol +
161                  diffuseCol * max(0.0, dot(normal, lightDir)) +
162                  specularCol * pow(max(0.0, dot(refl, toViewer)), shininess), 1.0);
163}
164
165technique lighting
166{
167    pass p0
168    {
169        VertexShader = compile vs_1_1 vertexLighting();
170        PixelShader = compile ps_2_0 pixelLighting();
171
172        CullMode = none;
173        Zenable = false;
174        StencilEnable = false;
175        AlphaBlendEnable = false;
176        Srcblend = One;
177        Destblend = One;
178        AlphaTestEnable = false;
179    }
180}
181