Teraka

Customizable shader

Dec 19th, 2015
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.67 KB | None | 0 0
  1. /*-------------------------------------------------------------.
  2. /                        Config Settings                      /
  3. '-----------------------------------------------------------*/
  4. //Adjust these values as you like:
  5. #define lineStrength 3 //How thick the borders are
  6. #define lineStrengthGradient 0 //Makes borders thicker (or thinner if negative) the closer they are
  7. #define polygonAmount 6 //Amount of detail (higher = less detail)
  8. #define bleedThrough 0.0 //Makes the world visible in transparency, 0-1
  9. //Color templates
  10. #define red float3(1.0, 0, 0)
  11. #define blue float3(0, 0, 1.0)
  12. #define green float3(0, 1.0, 0)
  13. #define magenta float3(1.0, 0, 1.0)
  14. #define cyan float3(0, 1.0, 1.0)
  15. #define yellow float3(1.0, 1.0, 0)
  16. #define white float3(1.0, 1.0, 1.0)
  17. #define black float3(0.0, 0.0, 0.0)
  18.  
  19. #define hex(x) float3(x>>16, (x>>8)&0xFF, x&0xFF) //Use hex(0xFFFFFF) to use hexadecimal color codes
  20. #define rgb(r, g, b) float3(r/255.0, g/255.0, b/255.0) //Use rgb(255, 255, 255) to use standard 0-255 rgb color codes
  21.  
  22. #define borderColor black //Base border color
  23. #define borderEndColor white //Defines the color for borders far away
  24. #define borderGradient 0 //Defines at which distance the end color is reached (higher=closer)
  25.  
  26. #define backgroundColor white //Works the same as the above, but for background colors
  27. #define backgroundEndColor black
  28. #define backgroundGradient 0
  29.  
  30. #define noiseIntensity 0.0 //Intensity of the noise, 0-1
  31.  
  32. /*-----------------------------------------------------------.
  33. /                        Shader Code                          /
  34. '-----------------------------------------------------------*/
  35.  
  36. float3 CustomPass(float4 position : SV_Position, float2 texcoord : TEXCOORD0) : SV_Target
  37. {
  38.     float depth = tex2D(depthSampler, texcoord).x;
  39.  
  40.     depth = (2.0 * Depth_z_near) / ( -(Depth_z_far - Depth_z_near) * depth + (Depth_z_far + Depth_z_near) );
  41.     float pixelWidth = float(lineStrength) / BUFFER_WIDTH;
  42.     float pixelHeight = float(lineStrength) / BUFFER_HEIGHT;
  43.  
  44.     if (lineStrengthGradient > 0)
  45.     {
  46.         pixelWidth *= (1-depth) * lineStrengthGradient;
  47.         pixelHeight *= (1-depth) * lineStrengthGradient;
  48.     }
  49.     else if (lineStrengthGradient < 0)
  50.     {
  51.         pixelWidth *= (depth) * abs(float(lineStrengthGradient));
  52.         pixelHeight *= (depth) * abs(float(lineStrengthGradient));
  53.     }
  54.     //Calculate depth
  55.     float surrounddepth = 0;
  56.     surrounddepth += tex2D(depthSampler, float2(texcoord.x + pixelWidth, texcoord.y)).x;
  57.     surrounddepth += tex2D(depthSampler, float2(texcoord.x - pixelWidth, texcoord.y)).x;
  58.     surrounddepth += tex2D(depthSampler, float2(texcoord.x, texcoord.y + pixelHeight)).x;
  59.     surrounddepth += tex2D(depthSampler, float2(texcoord.x, texcoord.y - pixelHeight)).x;
  60.     surrounddepth = surrounddepth / 4.0;
  61.     surrounddepth = (2.0 * Depth_z_near) / ( -(Depth_z_far - Depth_z_near) * surrounddepth + (Depth_z_far + Depth_z_near) );
  62.     //Calculate noise
  63.     float t = (timer * 0.0022337);
  64.     float seed = dot(texcoord, float2(12.9898, 78.233));
  65.     float noise = frac(sin(seed) * 43758.5453 + t);
  66.     float noiseColor = frac(cos(seed) * 53758.5453 - t);
  67.  
  68.     float3 finalColor;
  69.  
  70.     if (surrounddepth > depth * (1.0 + 0.00005 * polygonAmount))
  71.     { //Pixel is part of a border
  72.         float gradient = surrounddepth * borderGradient;
  73.         finalColor = lerp(borderColor, borderEndColor, gradient > 1 ? 1 : gradient);
  74.     }
  75.     else
  76.     { //Pixel is part of the background
  77.         float gradient = surrounddepth * backgroundGradient;
  78.         finalColor = lerp(backgroundColor, backgroundEndColor, gradient > 1 ? 1 : gradient);
  79.         finalColor = finalColor * (1-bleedThrough) + tex2D(s0, texcoord).rgb*bleedThrough;
  80.         finalColor = lerp(finalColor, noiseColor, noise*noiseIntensity);
  81.     }
  82.     return finalColor;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment