Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Cel Shading/Toon Shader
- Cell shader that uses diffuse texture to draw lines, controllable lighting LOD.
- Next step - add controls to control edge color, shadow color, and diffuse color
- date: 10/2016
- */
- // WorldviewProjection establishes the camera in 3D space
- float4x4 WorldViewProj : WorldViewProjection ;// < string UIWidget="None"; >;
- //float4x4 View : ViewInverse < string UIWidget="None"; >;
- float4x4 World : World < string UIWidget="None"; >;
- //float4x4 WorldIT : WorldInverseTranspose < string UIWidget="None"; >;
- float4 cameraPosition : CAMERAPOSITION < string UIWidget="None";>;
- float time : Time < string UIWidget="None";>;
- //______________ Lights _________________/
- float4 ambientColor : AMBIENT <
- string UIName = "Ambient Light";
- string UIWidget = " Ambient Color";
- > = {1.0f , 1.0f, 1.0f, 0.0f};
- float4 lightColor : COLOR <
- string Object = "LightColor0";
- string UIName = "Light Color";
- string UIWidget = "Color";
- > = {1.0f, 1.0f, 1.0f, 0.0f};
- float3 lightDirection : DIRECTION <
- string Object = "DirectionalLight 0"; //object for binding
- string UIWidget = "Light Direction";
- string Space = "World";
- > = {0.0f, 0.0f, -1.0f};
- //____________________ TEX MAPS_______________________//
- //Setup texture globals
- // Grayscale texture that will be remapped
- texture diffuseTexture <
- string UIName = "Diffuse Texture";
- string ResourceType = "2D";>;
- SamplerState diffuseSampler {
- Texture = <diffuseTexture>;
- //Filter = MIN_MAG_MIP_LINEAR;
- AddressU = Wrap;
- AddressV = Wrap;
- };
- //______________Blend States________________//
- //Blend states allow for alpha blending
- BlendState AlphaBlendingOn
- {
- BlendEnable[0] = TRUE;
- DestBlend = INV_SRC_ALPHA;
- SrcBlend = SRC_ALPHA;
- };
- //_____________User Defined Shader globals___________________//
- float4 specColor : SPECULAR <
- string UIName = "Specular Color";
- string UIWidget = "Color";
- > = {1.0f, 1.0f, 1.0f,1.0f};
- float specExponent : SPECULARPOWER <
- string UIName = "Specular Power";
- string UIWidget = "slider";
- float UIMin = 0;
- float UIMax = 50.0;
- float UIStep = 1.0;
- > = 8.0;
- float levelNum <
- string UIName = "lighting LOD";
- string UIWidget = "slider";
- float UIMin = 0;
- float UIMax = 20.0;
- float UIStep = 1;
- > = 8;
- float diffLevelNum<
- string UIName = "Diffuse LOD";
- string UIWidget = "slider";
- float UIMin = 0;
- float UIMax = 20.0;
- float UIStep = 1;
- > = 8;
- float threshold <
- string UIName = "Edge Threshold";
- string UIWidget = "slider";
- float UIMin = 0;
- float UIMax = 3.0;
- float UIStep = 0.1;
- > = .1;
- float edgeSize <
- string UIName = "Edge Thickness";
- string UIWidget = "slider";
- float UIMin = 0;
- float UIMax = .1;
- float UIStep = 0.001;
- > = .001;
- //_______________________STRUCTS_________________________//
- // Vertex input struct
- struct VS_input{
- float4 position : POSITION;
- float3 normal: NORMAL;
- float3 tangent : TANGENT; //<- Tangent and binormal vectors used for bump maps
- float3 binormal : BINORMAL;
- float2 texCoord : TEXCOORD0;
- };
- // Pixel input struct
- struct VS_output{
- float4 position : SV_POSITION;
- float2 texCoord : TEXCOORD1;
- float3 normal: NORMAL;
- float3 view : TEXCOORD2;
- float3 lightVec : TEXCOORD3;
- };
- //______________________VERTEX SHADER___________________//
- VS_output mainVS(VS_input IN){
- VS_output OUT;
- OUT.position = mul(IN.position, WorldViewProj);
- OUT.texCoord = float2 (IN.texCoord.x, (1.0 - IN.texCoord.y)) ;
- OUT.normal = normalize( mul( float4(IN.normal,0), World));
- float3 pw = mul(IN.position, World).xyz;
- OUT.view = normalize( cameraPosition - pw);
- OUT.lightVec = -normalize(lightDirection);
- return OUT;
- }
- //___________PIXEL SHADER____________//
- float4 mainPS( VS_output IN) : SV_TARGET{
- float ss = edgeSize; // sampling size for matrix
- static const float2 c[9] = {
- float2(-ss, ss ),
- float2(0, ss),
- float2(ss, ss),
- float2(-ss, 0),
- float2(0, 0),
- float2(ss, ss),
- float2(-ss, -ss),
- float2( 0, -ss),
- float2( ss, -ss)
- };
- float3 col[9];
- [unroll(9)]
- for(int i = 0; i < 9; i++){
- col[i] = tex2D( diffuseSampler,IN.texCoord + c[i]);
- col[i] = dot(col[i], float3(.3,.59,.11)) ;
- };
- float x = col[2] + col[8] + (2*col[5]) - col[0] - (2*col[3]) - col[6];
- float y = col[6] + (2*col[7]) + col[8] - col[0] - (2*col[1]) - col[2];
- float edge = lerp(1, 0, (x*x + y*y)*threshold);
- //lighting calculations
- // determine light and surface vectors
- float3 L = normalize(IN.lightVec);
- float3 N = normalize(IN.normal);
- float3 V = normalize(IN.view);
- float3 NdotL = dot(N,L);
- float3 H = normalize( V + L);
- float facing = (dot(N,L) <= 0 ? 0 : 1);
- float3 diffuse = tex2D( diffuseSampler,IN.texCoord).rgb;
- //broken- trying to simplify the rgb channels to be LOD colors
- /*
- float diffLevel = 1/diffLevelNum;
- diffuse = round(diffuse * diffLevel);
- */
- // lighting levels
- float level= 1/levelNum;
- float kdLevels = round(max(NdotL, 0)/level)*level;
- float ksLevels = round((pow(dot(N,H),specExponent))/level)*level;
- float3 Kd = lightColor * kdLevels; // * diffuse;
- float3 Ks = lightColor * specColor * ksLevels;
- float3 surfaceColor = ( Kd ) * edge;
- return float4( surfaceColor, 1.0);
- }
- technique technique0 {
- pass p0 {
- CullMode = None;
- AlphaBlendEnable = true;
- SrcBlend = SRCALPHA;
- DestBlend = INVSRCALPHA;
- SetVertexShader(CompileShader(vs_3_0, mainVS()));
- SetPixelShader(CompileShader(ps_3_0, mainPS()));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement