NMapEffect.qml 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import QtQuick 2.0
  2. /* Simple normal mapping shader */
  3. ShaderEffect {
  4. id: root
  5. // Original image
  6. property string sourceImage
  7. // Normal mapped image
  8. property string normalsImage
  9. // Lightsource which defines the position of light
  10. property NMapLightSource lightSource
  11. // Boost diffuse effect of this item
  12. property real diffuseBoost: 0.0
  13. // Light intensity from source or alternatively custom intensity for this item
  14. property real lightIntensity: lightSource.lightIntensity
  15. // Switch x-coordinate of normal mapped image
  16. property bool switchX: false
  17. // Switch y-coordinate of normal mapped image
  18. property bool switchY: false
  19. // Set these in case this item isn't directly under main area
  20. property real elementPositionX: root.x
  21. property real elementPositionY: root.y
  22. // Optional 'colorize' effect to apply for the item, can be used for fog effect
  23. property color colorizeColor: "#404040"
  24. property real colorizeAmount: 0.0
  25. // Sizes of the original pixmap
  26. property real originalWidth: sourceImageItem.sourceSize.width
  27. property real originalHeight: sourceImageItem.sourceSize.height
  28. /* Private */
  29. property real _lightPosX: lightSource.lightPosX / lightSource.width * (lightSource.width/root.width) - elementPositionX/root.width
  30. property real _lightPosY: lightSource.lightPosY / lightSource.height * (lightSource.height/root.height) - elementPositionY/root.height
  31. property variant _source: ShaderEffectSource { sourceItem: sourceImageItem; hideSource: true }
  32. property variant _source2: ShaderEffectSource { sourceItem: normalsourceImageItem; hideSource: true }
  33. width: sourceImageItem.width
  34. height: sourceImageItem.height
  35. Image {
  36. id: sourceImageItem
  37. source: sourceImage
  38. visible: false
  39. }
  40. Image {
  41. id: normalsourceImageItem
  42. source: normalsImage
  43. visible: false
  44. }
  45. fragmentShader: "
  46. varying highp vec2 qt_TexCoord0;
  47. uniform highp float qt_Opacity;
  48. uniform sampler2D _source;
  49. uniform sampler2D _source2;
  50. uniform highp float _lightPosX;
  51. uniform highp float _lightPosY;
  52. uniform highp float diffuseBoost;
  53. uniform highp float lightIntensity;
  54. uniform highp float colorizeAmount;
  55. uniform highp vec4 colorizeColor;
  56. uniform bool switchX;
  57. uniform bool switchY;
  58. void main(void)
  59. {
  60. highp vec2 pixPos = qt_TexCoord0;
  61. highp vec4 pix = texture2D(_source, pixPos.st);
  62. highp vec4 pix2 = texture2D(_source2, pixPos.st);
  63. highp vec3 normal = normalize(pix2.rgb * 2.0 - 1.0);
  64. highp float xp = float(switchX) * (_lightPosX - pixPos.x) + float(!switchX) * (pixPos.x - _lightPosX);
  65. highp float yp = float(switchY) * (_lightPosY - pixPos.y) + float(!switchY) * (pixPos.y - _lightPosY);
  66. highp vec3 light_pos = normalize(vec3(xp, yp, lightIntensity));
  67. highp float diffuse = max(dot(normal, light_pos), 0.2);
  68. diffuse *= (1.0 + diffuseBoost);
  69. highp vec4 color = vec4(diffuse * pix.rgb, pix.a);
  70. color = mix(color, color.a * colorizeColor, colorizeAmount);
  71. gl_FragColor = color * qt_Opacity;
  72. }
  73. "
  74. }