crt.frag 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #version 120
  2. varying vec3 VNormal;
  3. varying vec3 vViewVec;
  4. varying vec3 reflVec;
  5. uniform float osg_SimulationTime;
  6. uniform sampler2D BaseTex;
  7. uniform sampler2D DirtTex;
  8. uniform sampler2D Scanlines;
  9. uniform samplerCube Environment;
  10. uniform int display_enabled;
  11. uniform float frame_factor;
  12. uniform float test_value;
  13. uniform float distortion;
  14. uniform float flicker_factor;
  15. uniform float view_y;
  16. float SCANTHICK = 2.0;
  17. float INTENSITY = 0.15;
  18. float BRIGHTBOOST = 0.15;
  19. //float DISTORTION = 0.03;
  20. float THRESHOLD = 0.05;
  21. vec2 TextureSize = vec2(800, 950);
  22. vec4 bloomTexture2D(sampler2D texture, vec2 texCoords)
  23. {
  24. vec4 texel = vec4(0, 0, 0, 0);
  25. float blur = 0.4;
  26. int size = 3;
  27. for(int x = -size; x <= size; x++)
  28. {
  29. for(int y = -size; y <= size; y++)
  30. {
  31. texel += texture2D(texture, texCoords + vec2(x/TextureSize.x, y/TextureSize.y));
  32. }
  33. }
  34. return (blur*texel/pow((2*size)+1, 2) + (1.0-blur)*texture2D(texture, texCoords));
  35. }
  36. vec2 distort(vec2 position)
  37. {
  38. position = vec2(2.0 * position - 1.0);
  39. position = position /(1.0 - distortion * length(position));
  40. position =(position + 1.0) * 0.5;
  41. return position;
  42. }
  43. vec3 frame(vec2 position, vec3 color)
  44. {
  45. // intersection between two ellipses and four circles
  46. position -= vec2(0.5);
  47. position *= vec2(2.0);
  48. float x2 = position.x * position.x;
  49. float y2 = position.y * position.y;
  50. if((x2/frame_factor) +(y2/20) > 1 || (x2/40) +(y2/frame_factor+0.03) > 1) {
  51. color = vec3(0.0, 0.0, 0.0);
  52. }
  53. // && length(abs(position)-vec2(frame_factor-0.08, frame_factor-0.06)) > 0.1
  54. float val = 0.6293*frame_factor + 0.2869;
  55. if( length(position) > frame_factor + 0.4 && length(abs(position)-vec2(val,val)) > 0.1 ) {
  56. color = vec3(0.0, 0.0, 0.0);
  57. }
  58. return color;
  59. }
  60. vec3 scanline(vec3 texel)
  61. {
  62. vec3 scanlines = texture2D(Scanlines, vec2(150, 300)*gl_TexCoord[0].xy).rgb;
  63. texel *= 1.5*scanlines;
  64. return texel;
  65. }
  66. vec3 backlight(vec3 color)
  67. {
  68. if(color.r < THRESHOLD && color.g < THRESHOLD && color.b < THRESHOLD) {
  69. color = vec3(THRESHOLD, THRESHOLD, THRESHOLD);
  70. }
  71. return color;
  72. }
  73. vec3 flickering(vec2 position, vec3 texel)
  74. {
  75. texel *= 0.95+flicker_factor*(1-mod(5*osg_SimulationTime+position.y, 1.0));
  76. return texel;
  77. }
  78. vec3 reflection()
  79. {
  80. vec3 coords;
  81. coords.x = reflVec.x; // distance
  82. coords.y = 1.5*reflVec.y + 0.5*view_y;
  83. coords.z = 1.5*reflVec.z - 0.25;
  84. vec3 reflection = 0.1*textureCube(Environment, coords).rgb;
  85. float NdotL;
  86. vec3 n = normalize(VNormal);
  87. vec3 lightDir = gl_LightSource[0].position.xyz;
  88. NdotL = max(dot(n, lightDir), 0.0);
  89. return NdotL*reflection;
  90. }
  91. void main()
  92. {
  93. vec3 texel = vec3(0.0, 0.0, 0.0);
  94. vec3 dirt = 0.1*texture2D(DirtTex, gl_TexCoord[0].xy).rgb;
  95. // crt-effect
  96. if(display_enabled > 0) {
  97. vec2 position = distort(gl_TexCoord[0].xy);
  98. if(position.x > 0.0 && position.y > 0.0 && position.x < 1.0 && position.y < 1.0) {
  99. // texel = texture2D(BaseTex, position).rgb;
  100. texel = bloomTexture2D(BaseTex, position).rgb;
  101. texel = backlight(texel);
  102. texel = scanline(texel);
  103. texel = flickering(position, texel);
  104. }
  105. texel = frame(position, texel);
  106. }
  107. texel += dirt;
  108. texel += reflection();
  109. texel = clamp(texel, 0.0, 1.0);
  110. gl_FragColor = vec4(texel, 1.0);
  111. }