Gary_Keen27

Cel Shading

Oct 25th, 2016
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /////////////////////////////////////////////////////
  2. ////////////////////VERTEX SHADER////////////////////
  3. /////////////////////////////////////////////////////
  4. #version 330
  5.  
  6. layout(location = 0) in vec3 inPosition;
  7. layout(location = 1) in vec3 inNormal;
  8. layout(location = 2) in vec2 inTexCoord;
  9. layout(location = 3) in vec3 inTangent;
  10. layout(location = 4) in vec3 inBitangent;
  11.  
  12. out vec2 texCoord;
  13. out vec3 camDir;
  14. out vec3 lightDir;
  15. out float attenuation;
  16.  
  17. uniform mat4 viewMatrix;
  18. uniform mat4 modelMatrix;
  19. uniform mat4 projectionMatrix;
  20. uniform mat3 normalMatrix;
  21. uniform vec3 camPosIn;
  22. uniform vec3 lightPosIn;
  23.  
  24. void main()
  25. {
  26.     vec3 N = normalize(normalMatrix * inNormal);
  27.     vec3 T = normalize(normalMatrix * inTangent);
  28.          T = normalize(T - (dot(N, T) * N));
  29.     vec3 B = normalize(normalMatrix * inBitangent);
  30.  
  31.     if (dot(cross(N, T), B) < 0.0)
  32.         T = T * -1.0;
  33.  
  34.     mat3 TBNMatrix = transpose(mat3(T, B, N));
  35.  
  36.     vec3 position = vec3((viewMatrix * modelMatrix) * vec4(inPosition, 1.0)); //Vector is a position so W = 1.0. If it were a direction w = 0.0.
  37.     camDir = TBNMatrix * (normalize(camPosIn - position));
  38.     lightDir = TBNMatrix * (normalize(lightPosIn - position));
  39.     texCoord = inTexCoord;
  40.  
  41.     float radius = 50.0;
  42.     float dist = length(vec3((viewMatrix * modelMatrix) * vec4(lightPosIn, 1.0)) - position);
  43.     attenuation = 1.0 / (1.0 + ((2.0 / radius) * dist) + ((1.0 / (radius * radius)) * (dist * dist)));
  44.  
  45.     gl_Position = (projectionMatrix * (viewMatrix * modelMatrix)) * vec4(inPosition, 1.0);
  46. }
  47.  
  48. ///////////////////////////////////////////////////////
  49. ////////////////////FRAGMENT SHADER////////////////////
  50. ///////////////////////////////////////////////////////
  51. #version 330
  52. precision mediump float;
  53.  
  54. in vec2 texCoord;
  55. in vec3 camDir;
  56. in vec3 lightDir;
  57. in float attenuation;
  58.  
  59. layout (location = 0) out vec4 FragColour;
  60.  
  61. uniform sampler2D textureMap;
  62. uniform sampler2D normalMap;
  63. uniform sampler2D AOMap;
  64. uniform sampler2D specMap;
  65. uniform sampler2D roughMap;
  66. uniform vec3 lightColour;
  67.  
  68. void main()
  69. {
  70.     vec3 albedo = texture(textureMap, texCoord).rgb;
  71.     vec3 normals = normalize(texture(normalMap, texCoord).rgb * 2.0 - 1.0);
  72.    
  73.     vec3 cel = vec3(0.0);
  74.  
  75.     for (int i = 0; i < 1; i++)
  76.     {
  77.         float NdotL = clamp(dot(normals, lightDir), 0.0, 1.0);
  78.        
  79.         vec3 D = vec3(0.0);
  80.  
  81.         if (NdotL > 0.95)
  82.             D = lightColour * 1.0;
  83.         else if (NdotL > 0.5)
  84.             D = lightColour * 0.6;
  85.         else if (NdotL > 0.25)
  86.             D = lightColour * 0.4;
  87.         else
  88.             D = lightColour * 0.2;
  89.  
  90.         cel += D;
  91.     }
  92.  
  93.     cel = pow(cel, vec3(2.2));
  94.     vec3 final = albedo * cel;
  95.     final = pow(final, vec3(1.0 / 2.2));
  96.     FragColour = vec4(final, 1.0);
  97. }
Advertisement
Add Comment
Please, Sign In to add comment