Advertisement
Guest User

Untitled

a guest
Feb 11th, 2021
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.49 KB | None | 0 0
  1. //--------------------------- BASIC PROPERTIES ------------------------------
  2. // The world transformation
  3. float4x4 World;
  4.  
  5. // The view transformation
  6. float4x4 View;
  7.  
  8. // The projection transformation
  9. float4x4 Projection;
  10.  
  11. // The transpose of the inverse of the world transformation,
  12. // used for transforming the vertex's normal
  13. float4x4 WorldInverseTranspose;
  14.  
  15. //--------------------------- DIFFUSE LIGHT PROPERTIES ------------------------------
  16. // The direction of the diffuse light
  17. float3 DiffuseLightDirection = float3(1, 0, 0);
  18.  
  19. // The color of the diffuse light
  20. float4 DiffuseColor = float4(1, 1, 1, 1);
  21.  
  22. // The intensity of the diffuse light
  23. float DiffuseIntensity = 1.0;
  24.  
  25. //--------------------------- TOON SHADER PROPERTIES ------------------------------
  26. // The color to draw the lines in. Black is a good default.
  27. float4 LineColor = float4(0, 0, 0, 1);
  28.  
  29. // The thickness of the lines. This may need to change, depending on the scale of
  30. // the objects you are drawing.
  31. float LineThickness = .03;
  32.  
  33. //--------------------------- TEXTURE PROPERTIES ------------------------------
  34. // The texture being used for the object
  35. texture Texture;
  36.  
  37. // The texture sampler, which will get the texture color
  38. sampler2D textureSampler = sampler_state
  39. {
  40. Texture = (Texture);
  41. MinFilter = Linear;
  42. MagFilter = Linear;
  43. AddressU = Clamp;
  44. AddressV = Clamp;
  45. };
  46.  
  47. //--------------------------- DATA STRUCTURES ------------------------------
  48. // The structure used to store information between the application and the
  49. // vertex shader
  50. struct AppToVertex
  51. {
  52. float4 Position : SV_POSITION0; // The position of the vertex
  53. float3 Normal : NORMAL0; // The vertex's normal
  54. float2 TextureCoordinate : TEXCOORD0; // The texture coordinate of the vertex
  55. };
  56.  
  57. // The structure used to store information between the vertex shader and the
  58. // pixel shader
  59. struct VertexToPixel
  60. {
  61. float4 Position : SV_POSITION0;
  62. float2 TextureCoordinate : TEXCOORD0;
  63. float3 Normal : TEXCOORD1;
  64. };
  65.  
  66. //--------------------------- SHADERS ------------------------------
  67. // The vertex shader that does cel shading.
  68. // It really only does the basic transformation of the vertex location,
  69. // and normal, and copies the texture coordinate over.
  70. VertexToPixel CelVertexShader(AppToVertex input)
  71. {
  72. VertexToPixel output;
  73.  
  74. // Transform the position
  75. float4 worldPosition = mul(input.Position, World);
  76. float4 viewPosition = mul(worldPosition, View);
  77. output.Position = mul(viewPosition, Projection);
  78.  
  79. // Transform the normal
  80. output.Normal = normalize(mul(input.Normal, WorldInverseTranspose));
  81.  
  82. // Copy over the texture coordinate
  83. output.TextureCoordinate = input.TextureCoordinate;
  84.  
  85. return output;
  86. }
  87.  
  88. // The pixel shader that does cel shading. Basically, it calculates
  89. // the color like is should, and then it discretizes the color into
  90. // one of four colors.
  91. float4 CelPixelShader(VertexToPixel input) : COLOR0
  92. {
  93. // Calculate diffuse light amount
  94. float intensity = dot(normalize(DiffuseLightDirection), input.Normal);
  95. if (intensity < 0)
  96. intensity = 0;
  97.  
  98. // Calculate what would normally be the final color, including texturing and diffuse lighting
  99. float4 color = tex2D(textureSampler, input.TextureCoordinate) * DiffuseColor * DiffuseIntensity;
  100. color.a = 1;
  101.  
  102. // Discretize the intensity, based on a few cutoff points
  103. if (intensity > 0.95)
  104. color = float4(1.0,1,1,1.0) * color;
  105. else if (intensity > 0.5)
  106. color = float4(0.7,0.7,0.7,1.0) * color;
  107. else if (intensity > 0.05)
  108. color = float4(0.35,0.35,0.35,1.0) * color;
  109. else
  110. color = float4(0.1,0.1,0.1,1.0) * color;
  111.  
  112. return color;
  113. }
  114.  
  115. // The vertex shader that does the outlines
  116. VertexToPixel OutlineVertexShader(AppToVertex input)
  117. {
  118. VertexToPixel output = (VertexToPixel)0;
  119.  
  120. // Calculate where the vertex ought to be. This line is equivalent
  121. // to the transformations in the CelVertexShader.
  122. float4 original = mul(mul(mul(input.Position, World), View), Projection);
  123.  
  124. // Calculates the normal of the vertex like it ought to be.
  125. float4 normal = mul(mul(mul(input.Normal, World), View), Projection);
  126.  
  127. // Take the correct "original" location and translate the vertex a little
  128. // bit in the direction of the normal to draw a slightly expanded object.
  129. // Later, we will draw over most of this with the right color, except the expanded
  130. // part, which will leave the outline that we want.
  131. output.Position = original + (mul(LineThickness, normal));
  132.  
  133. return output;
  134. }
  135.  
  136. // The pixel shader for the outline. It is pretty simple: draw everything with the
  137. // correct line color.
  138. float4 OutlinePixelShader(VertexToPixel input) : COLOR0
  139. {
  140. return LineColor;
  141. }
  142.  
  143. // The entire technique for doing toon shading
  144. technique Toon
  145. {
  146. // The first pass will go through and draw the back-facing triangles with the outline shader,
  147. // which will draw a slightly larger version of the model with the outline color. Later, the
  148. // model will get drawn normally, and draw over the top most of this, leaving only an outline.
  149. pass Pass1
  150. {
  151. VertexShader = compile vs_4_0_level_9_3 OutlineVertexShader();
  152. PixelShader = compile ps_4_0_level_9_3 OutlinePixelShader();
  153. CullMode = CW;
  154. }
  155.  
  156. // The second pass will draw the model like normal, but with the cel pixel shader, which will
  157. // color the model with certain colors, giving us the cel/toon effect that we are looking for.
  158. pass Pass2
  159. {
  160. VertexShader = compile vs_4_0_level_9_3 CelVertexShader();
  161. PixelShader = compile ps_4_0_level_9_3 CelVertexShader();
  162. CullMode = CCW;
  163. }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement