Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: data/contentcreation/pps/MarkusWegmann/Godray Shader Proof of Concept/Blinn.cgfx @ 10256

Last change on this file since 10256 was 8708, checked in by marwegma, 14 years ago

My Stuff

File size: 7.6 KB
Line 
1/*********************************************************************NVMH3****
2$Revision$
3
4Copyright NVIDIA Corporation 2007
5TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, THIS SOFTWARE IS PROVIDED
6*AS IS* AND NVIDIA AND ITS SUPPLIERS DISCLAIM ALL WARRANTIES, EITHER EXPRESS
7OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY
8AND FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL NVIDIA OR ITS SUPPLIERS
9BE LIABLE FOR ANY SPECIAL, INCIDENTAL, INDIRECT, OR CONSEQUENTIAL DAMAGES
10WHATSOEVER (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS,
11BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION, OR ANY OTHER PECUNIARY
12LOSS) ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
13NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
14
15
16To learn more about shading, shaders, and to bounce ideas off other shader
17    authors and users, visit the NVIDIA Shader Library Forums at:
18
19    http://developer.nvidia.com/forums/
20
21******************************************************************************/
22
23// #define FLIP_TEXTURE_Y
24
25float Script : STANDARDSGLOBAL <
26    string UIWidget = "none";
27    string ScriptClass = "object";
28    string ScriptOrder = "standard";
29    string ScriptOutput = "color";
30    string Script = "Technique=Main;";
31> = 0.8;
32
33//// UN-TWEAKABLES - AUTOMATICALLY-TRACKED TRANSFORMS ////////////////
34
35float4x4 WorldITXf : WorldInverseTranspose < string UIWidget="None"; >;
36float4x4 WvpXf : WorldViewProjection < string UIWidget="None"; >;
37float4x4 WorldXf : World < string UIWidget="None"; >;
38float4x4 ViewIXf : ViewInverse < string UIWidget="None"; >;
39
40//// TWEAKABLE PARAMETERS ////////////////////
41
42/// Point Lamp 0 ////////////
43float3 Lamp0Pos : Position <
44    string Object = "PointLight0";
45    string UIName =  "Lamp 0 Position";
46    string Space = "World";
47> = {-0.5f,2.0f,1.25f};
48float3 Lamp0Color : Specular <
49    string UIName =  "Lamp 0";
50    string Object = "Pointlight0";
51    string UIWidget = "Color";
52> = {1.0f,1.0f,1.0f};
53
54// Ambient Light
55float3 AmbiColor : Ambient <
56    string UIName =  "Ambient Light";
57    string UIWidget = "Color";
58> = {0.07f,0.07f,0.07f};
59
60float Ks <
61    string UIWidget = "slider";
62    float UIMin = 0.0;
63    float UIMax = 1.0;
64    float UIStep = 0.05;
65    string UIName =  "Specular";
66> = 0.4;
67
68float Eccentricity <
69    string UIWidget = "slider";
70    float UIMin = 0.0;
71    float UIMax = 1.0;
72    float UIStep = 0.0001;
73    string UIName =  "Highlight Eccentricity";
74> = 0.3;
75 
76
77//////// COLOR & TEXTURE /////////////////////
78
79texture ColorTexture  <
80    string ResourceName = "default_color.dds";
81    string UIName =  "Diffuse Texture";
82    string ResourceType = "2D";
83>;
84
85sampler2D ColorSampler = sampler_state {
86    Texture = <ColorTexture>;
87    MinFilter = LinearMipMapLinear;
88    MagFilter = Linear;
89    WrapS = Repeat;
90    WrapT = Repeat;
91}; 
92
93//
94// #define this macro to permit the import and use of shared shadow
95//  maps created by COLLADA-FX. Make sure that the macro is defined
96//  and the code recompile *before* executing "Convert to Collada-FX"!
97//
98// #define USE_SHARED_SHADOW
99
100#ifdef USE_SHARED_SHADOW
101#include "include/shadowMap.cgh"
102DECLARE_SHADOW_XFORMS("SpotLight0",LampViewXf,LampProjXf,ShadowViewProjXf)
103DECLARE_SHADOW_BIAS
104DECLARE_SHADOW_MAPS(ColorShadTarget,ColorShadSampler,DepthShadTarget,DepthShadSampler)
105
106float ShadDens <
107    string UIWidget = "slider";
108    float UIMin = 0.0;
109    float UIMax = 1.0;
110    float UIStep = 0.01;
111    string UIName =  "Shadow Density";
112> = 0.7;
113#endif /* USE_SHARED_SHADOW */
114
115
116//////// CONNECTOR DATA STRUCTURES ///////////
117
118/* data from application vertex buffer */
119struct appdata {
120    float3 Position     : POSITION;
121    float4 UV           : TEXCOORD0;
122    float4 Normal       : NORMAL;
123    float4 Tangent      : TANGENT0;
124    float4 Binormal     : BINORMAL0;
125};
126
127/* data passed from vertex shader to pixel shader */
128struct vertexOutput {
129    float4 HPosition    : POSITION;
130    float2 UV           : TEXCOORD0;
131    // The following values are passed in "World" coordinates since
132    //   it tends to be the most flexible and easy for handling
133    //   reflections, sky lighting, and other "global" effects.
134    float3 LightVec     : TEXCOORD1;
135    float3 WorldNormal  : TEXCOORD2;
136    float3 WorldTangent : TEXCOORD3;
137    float3 WorldBinormal : TEXCOORD4;
138    float3 WorldView    : TEXCOORD5;
139#ifdef USE_SHARED_SHADOW
140    // This optional value expresses the current location in "light"
141    //   coordinates for use with shadow mapping.
142    float4 LProj        : LPROJ_COORD;
143#endif /* USE_SHARED_SHADOW */
144};
145 
146///////// VERTEX SHADING /////////////////////
147
148/*********** Generic Vertex Shader ******/
149
150vertexOutput std_VS(appdata IN) {
151    vertexOutput OUT = (vertexOutput)0;
152    OUT.WorldNormal = mul(WorldITXf,IN.Normal).xyz;
153    OUT.WorldTangent = mul(WorldITXf,IN.Tangent).xyz;
154    OUT.WorldBinormal = mul(WorldITXf,IN.Binormal).xyz;
155    float4 Po = float4(IN.Position.xyz,1);
156    float3 Pw = mul(WorldXf,Po).xyz;
157    OUT.LightVec = (Lamp0Pos - Pw);
158#ifdef FLIP_TEXTURE_Y
159    OUT.UV = float2(IN.UV.x,(1.0-IN.UV.y));
160#else /* !FLIP_TEXTURE_Y */
161    OUT.UV = IN.UV.xy;
162#endif /* !FLIP_TEXTURE_Y */
163#ifdef USE_SHARED_SHADOW
164    float4 Pl = mul(ShadowViewProjXf,Pw);  // "P" in light coords
165    float4x4 BiasXf = make_bias_mat(ShadBias);
166    OUT.LProj = mul(BiasXf,Pl);         // bias to make texcoord
167#endif /* USE_SHARED_SHADOW */
168    OUT.WorldView = normalize(float3(ViewIXf[0].w,ViewIXf[1].w,ViewIXf[2].w) - Pw);
169    OUT.HPosition = mul(WvpXf,Po);
170    return OUT;
171}
172
173///////// PIXEL SHADING //////////////////////
174
175// Utility function for blinn shading
176
177void blinn_shading(vertexOutput IN,
178                    float3 LightColor,
179                    float3 Nn,
180                    float3 Ln,
181                    float3 Vn,
182                    out float3 DiffuseContrib,
183                    out float3 SpecularContrib)
184{
185    float3 Hn = normalize(Vn + Ln);
186    float hdn = dot(Hn,Nn);
187    float3 R = reflect(-Ln,Nn);
188    float rdv = dot(R,Vn);
189    rdv = max(rdv,0.001);
190    float ldn=dot(Ln,Nn);
191    ldn = max(ldn,0.0);
192    float ndv = dot(Nn,Vn);
193    float hdv = dot(Hn,Vn);
194    float eSq = Eccentricity*Eccentricity;
195    float distrib = eSq / (rdv * rdv * (eSq - 1.0) + 1.0);
196    distrib = distrib * distrib;
197    float Gb = 2.0 * hdn * ndv / hdv;
198    float Gc = 2.0 * hdn * ldn / hdv;
199    float Ga = min(1.0,min(Gb,Gc));
200    float fresnelHack = 1.0 - pow(ndv,5.0);
201    hdn = distrib * Ga * fresnelHack / ndv;
202    DiffuseContrib = ldn * LightColor;
203    SpecularContrib = hdn * Ks * LightColor;
204}
205
206float4 std_PS(vertexOutput IN) : COLOR {
207    float3 diffContrib;
208    float3 specContrib;
209    float3 Ln = normalize(IN.LightVec);
210    float3 Vn = normalize(IN.WorldView);
211    float3 Nn = normalize(IN.WorldNormal);
212        blinn_shading(IN,Lamp0Color,Nn,Ln,Vn,diffContrib,specContrib);
213    float3 diffuseColor = tex2D(ColorSampler,IN.UV).rgb;
214#ifdef USE_SHARED_SHADOW
215    float shadowed = tex2Dproj(DepthShadSampler,IN.LProj).x;
216        float faded = 1.0-(ShadDens*(1.0-shadowed));
217        diffContrib *= faded;
218        specContrib *= shadowed;
219#endif /* USE_SHARED_SHADOW */
220    float3 result = specContrib+(diffuseColor*(diffContrib+AmbiColor));
221    // return as float4
222    return float4(result,1);
223}
224
225///// TECHNIQUES /////////////////////////////
226
227technique Main <
228        string Script = "Pass=p0;";
229> {
230    pass p0 <
231        string Script = "Draw=geometry;";
232    > {
233        VertexProgram = compile vp40 std_VS();
234                DepthTestEnable = true;
235                DepthMask = true;
236                CullFaceEnable = false;
237                BlendEnable = false;
238                DepthFunc = LEqual;
239        FragmentProgram = compile fp40 std_PS();
240    }
241}
242
243/////////////////////////////////////// eof //
Note: See TracBrowser for help on using the repository browser.