Advertisement
eoverfelt

Cel Shader - Black and white

Oct 28th, 2016
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.60 KB | None | 0 0
  1. /*
  2. Cel Shading/Toon Shader
  3.  
  4. Cell shader that uses diffuse texture to draw lines, controllable lighting LOD.
  5. Next step - add controls to control edge color, shadow color, and diffuse color
  6. date: 10/2016
  7.  
  8. */
  9.  
  10. // WorldviewProjection establishes the camera in 3D space
  11.  
  12. float4x4 WorldViewProj : WorldViewProjection ;// < string UIWidget="None"; >;
  13. //float4x4 View : ViewInverse < string UIWidget="None"; >;
  14. float4x4 World : World < string UIWidget="None"; >;
  15. //float4x4 WorldIT : WorldInverseTranspose < string UIWidget="None"; >;
  16. float4 cameraPosition : CAMERAPOSITION < string UIWidget="None";>;
  17. float time : Time < string UIWidget="None";>;
  18.  
  19. //______________ Lights _________________/
  20.  
  21. float4 ambientColor : AMBIENT <
  22. string UIName = "Ambient Light";
  23. string UIWidget = " Ambient Color";
  24. > = {1.0f , 1.0f, 1.0f, 0.0f};
  25.  
  26. float4 lightColor : COLOR <
  27. string Object = "LightColor0";
  28. string UIName = "Light Color";
  29. string UIWidget = "Color";
  30. > = {1.0f, 1.0f, 1.0f, 0.0f};
  31.  
  32. float3 lightDirection : DIRECTION <
  33. string Object = "DirectionalLight 0"; //object for binding
  34. string UIWidget = "Light Direction";
  35. string Space = "World";
  36. > = {0.0f, 0.0f, -1.0f};
  37.  
  38.  
  39. //____________________ TEX MAPS_______________________//
  40.  
  41.  
  42. //Setup texture globals
  43.  
  44. // Grayscale texture that will be remapped
  45. texture diffuseTexture <
  46. string UIName = "Diffuse Texture";
  47. string ResourceType = "2D";>;
  48.  
  49. SamplerState diffuseSampler {
  50. Texture = <diffuseTexture>;
  51. //Filter = MIN_MAG_MIP_LINEAR;
  52. AddressU = Wrap;
  53. AddressV = Wrap;
  54. };
  55.  
  56.  
  57.  
  58. //______________Blend States________________//
  59.  
  60. //Blend states allow for alpha blending
  61.  
  62. BlendState AlphaBlendingOn
  63. {
  64. BlendEnable[0] = TRUE;
  65. DestBlend = INV_SRC_ALPHA;
  66. SrcBlend = SRC_ALPHA;
  67. };
  68.  
  69.  
  70. //_____________User Defined Shader globals___________________//
  71.  
  72. float4 specColor : SPECULAR <
  73. string UIName = "Specular Color";
  74. string UIWidget = "Color";
  75. > = {1.0f, 1.0f, 1.0f,1.0f};
  76.  
  77. float specExponent : SPECULARPOWER <
  78. string UIName = "Specular Power";
  79. string UIWidget = "slider";
  80. float UIMin = 0;
  81. float UIMax = 50.0;
  82. float UIStep = 1.0;
  83. > = 8.0;
  84.  
  85. float levelNum <
  86. string UIName = "lighting LOD";
  87. string UIWidget = "slider";
  88. float UIMin = 0;
  89. float UIMax = 20.0;
  90. float UIStep = 1;
  91. > = 8;
  92.  
  93. float diffLevelNum<
  94. string UIName = "Diffuse LOD";
  95. string UIWidget = "slider";
  96. float UIMin = 0;
  97. float UIMax = 20.0;
  98. float UIStep = 1;
  99. > = 8;
  100.  
  101. float threshold <
  102. string UIName = "Edge Threshold";
  103. string UIWidget = "slider";
  104. float UIMin = 0;
  105. float UIMax = 3.0;
  106. float UIStep = 0.1;
  107. > = .1;
  108.  
  109. float edgeSize <
  110. string UIName = "Edge Thickness";
  111. string UIWidget = "slider";
  112. float UIMin = 0;
  113. float UIMax = .1;
  114. float UIStep = 0.001;
  115. > = .001;
  116.  
  117. //_______________________STRUCTS_________________________//
  118. // Vertex input struct
  119. struct VS_input{
  120. float4 position : POSITION;
  121. float3 normal: NORMAL;
  122. float3 tangent : TANGENT; //<- Tangent and binormal vectors used for bump maps
  123. float3 binormal : BINORMAL;
  124. float2 texCoord : TEXCOORD0;
  125. };
  126.  
  127.  
  128. // Pixel input struct
  129. struct VS_output{
  130. float4 position : SV_POSITION;
  131. float2 texCoord : TEXCOORD1;
  132. float3 normal: NORMAL;
  133. float3 view : TEXCOORD2;
  134. float3 lightVec : TEXCOORD3;
  135. };
  136.  
  137. //______________________VERTEX SHADER___________________//
  138. VS_output mainVS(VS_input IN){
  139.  
  140. VS_output OUT;
  141.  
  142. OUT.position = mul(IN.position, WorldViewProj);
  143. OUT.texCoord = float2 (IN.texCoord.x, (1.0 - IN.texCoord.y)) ;
  144. OUT.normal = normalize( mul( float4(IN.normal,0), World));
  145.  
  146. float3 pw = mul(IN.position, World).xyz;
  147. OUT.view = normalize( cameraPosition - pw);
  148. OUT.lightVec = -normalize(lightDirection);
  149.  
  150. return OUT;
  151. }
  152.  
  153. //___________PIXEL SHADER____________//
  154. float4 mainPS( VS_output IN) : SV_TARGET{
  155.  
  156.  
  157. float ss = edgeSize; // sampling size for matrix
  158. static const float2 c[9] = {
  159. float2(-ss, ss ),
  160. float2(0, ss),
  161. float2(ss, ss),
  162. float2(-ss, 0),
  163. float2(0, 0),
  164. float2(ss, ss),
  165. float2(-ss, -ss),
  166. float2( 0, -ss),
  167. float2( ss, -ss)
  168. };
  169.  
  170. float3 col[9];
  171. [unroll(9)]
  172. for(int i = 0; i < 9; i++){
  173. col[i] = tex2D( diffuseSampler,IN.texCoord + c[i]);
  174. col[i] = dot(col[i], float3(.3,.59,.11)) ;
  175. };
  176.  
  177. float x = col[2] + col[8] + (2*col[5]) - col[0] - (2*col[3]) - col[6];
  178. float y = col[6] + (2*col[7]) + col[8] - col[0] - (2*col[1]) - col[2];
  179.  
  180. float edge = lerp(1, 0, (x*x + y*y)*threshold);
  181.  
  182. //lighting calculations
  183. // determine light and surface vectors
  184. float3 L = normalize(IN.lightVec);
  185. float3 N = normalize(IN.normal);
  186. float3 V = normalize(IN.view);
  187.  
  188. float3 NdotL = dot(N,L);
  189. float3 H = normalize( V + L);
  190. float facing = (dot(N,L) <= 0 ? 0 : 1);
  191.  
  192. float3 diffuse = tex2D( diffuseSampler,IN.texCoord).rgb;
  193.  
  194. //broken- trying to simplify the rgb channels to be LOD colors
  195.  
  196. /*
  197. float diffLevel = 1/diffLevelNum;
  198. diffuse = round(diffuse * diffLevel);
  199. */
  200.  
  201. // lighting levels
  202.  
  203.  
  204. float level= 1/levelNum;
  205.  
  206. float kdLevels = round(max(NdotL, 0)/level)*level;
  207. float ksLevels = round((pow(dot(N,H),specExponent))/level)*level;
  208.  
  209.  
  210.  
  211. float3 Kd = lightColor * kdLevels; // * diffuse;
  212. float3 Ks = lightColor * specColor * ksLevels;
  213.  
  214.  
  215.  
  216. float3 surfaceColor = ( Kd ) * edge;
  217.  
  218.  
  219. return float4( surfaceColor, 1.0);
  220. }
  221.  
  222. technique technique0 {
  223. pass p0 {
  224. CullMode = None;
  225. AlphaBlendEnable = true;
  226. SrcBlend = SRCALPHA;
  227. DestBlend = INVSRCALPHA;
  228.  
  229. SetVertexShader(CompileShader(vs_3_0, mainVS()));
  230. SetPixelShader(CompileShader(ps_3_0, mainPS()));
  231.  
  232. }
  233. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement