/*********************************************************************NVMH3**** Path: NVSDK\Common\media\cgfx File: $Id: //sw/devtools/FXComposer2/Alpha4+/SDK/MEDIA/CgFX1.4/durer.cgfx#1 $ Copyright NVIDIA Corporation 2002 TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, THIS SOFTWARE IS PROVIDED *AS IS* AND NVIDIA AND ITS SUPPLIERS DISCLAIM ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL NVIDIA OR ITS SUPPLIERS BE LIABLE FOR ANY SPECIAL, INCIDENTAL, INDIRECT, OR CONSEQUENTIAL DAMAGES WHATSOEVER (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION, OR ANY OTHER PECUNIARY LOSS) ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. Comments: * Emulation of Durer-style split-tone technique. * Lines are drawn in black for shadow, then light-tone opaque lines are overlaid in a different direction for the highlights. ******************************************************************************/ string Category = "Effects\\Cg\\BRDF"; string description = "Analytic Anti-aliasing"; /************* UNTWEAKABLES **************/ float4x4 WorldITXf : WorldInverseTranspose; float4x4 WorldViewProjXf : WorldViewProjection; float4x4 WorldXf : World; float4x4 ViewIXf : ViewInverse; /** tweakables **********************************************/ float4 lightPos : Position < string Object = "PointLight"; string Space = "World"; > = {100.0f, 100.0f, 100.0f, 0.0f}; float3 SurfColor : Diffuse < string UIName = "Base Surface"; > = {1.0f, 1.0f, 1.0f}; float3 StrokeColor : Diffuse < string UIName = "Dark Stroke"; > = {0.0f, 0.0f, 0.0f}; float3 SpecColor : Specular < string UIName = "Hilight"; > = {1.0f, 1.0f, 1.0f}; float Kd < string UIWidget = "slider"; float uimin = 0.0; float uimax = 1.0; float uistep = 0.001; string UIName = "specular"; > = 1.0; float Ks < string UIWidget = "slider"; float uimin = 0.0; float uimax = 2.0; float uistep = 0.01; string UIName = "specular"; > = 0.5; float SpecExpon : Power < string UIWidget = "slider"; float uimin = 1.0; float uimax = 128.0; float uistep = 1.0; string UIName = "specular power"; > = 25.0; float SWidth < string UIWidget = "slider"; float uimin = 0.0; float uimax = 20.0; float uistep = 0.0001; string UIName = "s width"; > = 1.0; float PeriodD < string UIWidget = "slider"; float uimin = 0.0; float uimax = 0.3; float uistep = 0.001; string UIName = "Diff Period"; > = 0.02; float PeriodS < string UIWidget = "slider"; float uimin = 0.0; float uimax = 0.2; float uistep = 0.001; string UIName = "Spec Period"; > = 0.012; float SAng < string UIWidget = "slider"; float uimin = -90.0; float uimax = 90.0; float uistep = 0.001; string UIName = "Stroke Angle"; > = 0.0; static float Rotate = (SAng*3.141592/180.0); /************* DATA STRUCTS **************/ /* data from application vertex buffer */ struct appdata { float3 Position : POSITION; float4 UV : TEXCOORD0; float4 Normal : NORMAL; float4 Tangent : TEXCOORD1; float4 Binormal : TEXCOORD2; }; /* data passed from vertex shader to pixel shader */ struct vertexOutput { float4 HPosition : POSITION; float4 UV : TEXCOORD0; float3 LightVec : TEXCOORD1; float3 WorldNormal : TEXCOORD2; float3 WorldPos : TEXCOORD3; float3 WorldEyeVec : TEXCOORD4; float3 WorldTangent : TEXCOORD5; float3 WorldBinorm : TEXCOORD6; float4 HPos2 : TEXCOORD7; }; /*********** vertex shader ******/ vertexOutput mainVS(appdata IN,uniform float3 Lw) { vertexOutput OUT; OUT.WorldNormal = mul(WorldITXf, IN.Normal).xyz; OUT.WorldTangent = mul(WorldITXf, IN.Tangent).xyz; OUT.WorldBinorm = mul(WorldITXf, IN.Binormal).xyz; float4 Po = float4(IN.Position.xyz,1.0f); float3 Pw = mul(WorldXf, Po).xyz; OUT.WorldPos = Pw; OUT.LightVec = Lw - Pw; OUT.UV = IN.UV; OUT.WorldEyeVec = float3(ViewIXf[0].w,ViewIXf[1].w,ViewIXf[2].w) - Pw; float4 hpos = mul(WorldViewProjXf, Po); OUT.HPosition = hpos; float cr = cos(Rotate); float sr = sin(Rotate); OUT.HPos2 = float4((hpos.x*cr - hpos.y*sr),(hpos.x*sr + hpos.y*cr),hpos.zw); return OUT; } /********* pixel shader ********/ float4 basic_lighting(vertexOutput IN) { float3 Ln = normalize(IN.LightVec); float3 Nn = normalize(IN.WorldNormal); float3 Vn = normalize(IN.WorldEyeVec); float3 Hn = normalize(Vn + Ln); float ldn = dot(Ln,Nn); float hdn = dot(Hn,Nn); return lit(ldn,hdn,SpecExpon) * float4(1.0f,Kd,Ks,1.0f); } float numeric_stroke(float val,float Period,float Bal,float SampleWidth) { float edge = Period*Bal; float width = abs(ddx(val)) + abs(ddy(val)); float w = width*SampleWidth/Period; float x0 = val/Period - (w/2.0); float x1 = x0 + w; float nedge = edge/Period; float i0 = (1.0-nedge)*floor(x0) + max(0.0, frac(x0)-nedge); float i1 = (1.0-nedge)*floor(x1) + max(0.0, frac(x1)-nedge); float s = (i1 - i0)/w; s = min(1.0,max(0.0,s)); return s; } float4 durer_core(vertexOutput IN,float2 StrokeVal) { float4 litV = basic_lighting(IN); float s = numeric_stroke(StrokeVal.x,PeriodD,litV.y,SWidth); float3 diffContrib = lerp(SurfColor,StrokeColor,s); s = numeric_stroke(StrokeVal.y,PeriodS,litV.z,SWidth); float3 result = lerp(SpecColor,diffContrib,s); // hilight is OPAQUE -- just like Durer drawing return float4(result,1.0f); } /********* pixel shader ********/ float4 durerScreenPS(vertexOutput IN) : COLOR { return durer_core(IN,float2((IN.HPos2.x/IN.HPos2.z),(IN.HPos2.x/IN.HPos2.z))); } float4 durerUVPS(vertexOutput IN) : COLOR { return durer_core(IN,IN.UV.xy); } float4 durerM1PS(vertexOutput IN) : COLOR { return durer_core(IN,float2((IN.HPos2.x/IN.HPos2.z),IN.UV.y)); } float4 durerM2PS(vertexOutput IN) : COLOR { return durer_core(IN,float2(IN.UV.x,(IN.HPos2.x/IN.HPos2.z))); } /*************/ technique screenStroke { pass p0 { VertexProgram = compile vp30 mainVS(lightPos); DepthTestEnable = true; DepthMask = true; CullFaceEnable = false; FragmentProgram = compile fp30 durerScreenPS(); } } technique uvStroke { pass p0 { VertexProgram = compile vp30 mainVS(lightPos); DepthTestEnable = true; DepthMask = true; CullFaceEnable = false; FragmentProgram = compile fp30 durerUVPS(); } } technique mix1 { pass p0 { VertexProgram = compile vp30 mainVS(lightPos); DepthTestEnable = true; DepthMask = true; CullFaceEnable = false; FragmentProgram = compile fp30 durerM1PS(); } } technique mix2 { pass p0 { VertexProgram = compile vp30 mainVS(lightPos); DepthTestEnable = true; DepthMask = true; CullFaceEnable = false; FragmentProgram = compile fp30 durerM2PS(); } } /***************************** eof ***/