FastGlow.qml 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. // Copyright (C) 2022 The Qt Company Ltd.
  2. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
  3. // Qt-Security score:significant reason:default
  4. import QtQuick
  5. import Qt5Compat.GraphicalEffects.private
  6. Item {
  7. id: rootItem
  8. property variant source
  9. property real spread: 0.0
  10. property real blur: 0.0
  11. property color color: "white"
  12. property bool transparentBorder: false
  13. property bool cached: false
  14. SourceProxy {
  15. id: sourceProxy
  16. input: rootItem.source
  17. }
  18. ShaderEffectSource {
  19. id: cacheItem
  20. anchors.fill: shaderItem
  21. visible: rootItem.cached
  22. smooth: true
  23. sourceItem: shaderItem
  24. live: true
  25. hideSource: visible
  26. }
  27. property string __internalBlurVertexShader: "qrc:/qt-project.org/imports/Qt5Compat/GraphicalEffects/shaders_ng/fastblur_internal.vert.qsb"
  28. property string __internalBlurFragmentShader: "qrc:/qt-project.org/imports/Qt5Compat/GraphicalEffects/shaders_ng/fastblur_internal.frag.qsb"
  29. ShaderEffect {
  30. id: level0
  31. property variant source: sourceProxy.output
  32. anchors.fill: parent
  33. visible: false
  34. smooth: true
  35. }
  36. ShaderEffectSource {
  37. id: level1
  38. width: Math.ceil(shaderItem.width / 32) * 32
  39. height: Math.ceil(shaderItem.height / 32) * 32
  40. sourceItem: level0
  41. hideSource: rootItem.visible
  42. sourceRect: transparentBorder ? Qt.rect(-64, -64, shaderItem.width, shaderItem.height) : Qt.rect(0,0,0,0)
  43. smooth: true
  44. visible: false
  45. }
  46. ShaderEffect {
  47. id: effect1
  48. property variant source: level1
  49. property real yStep: 1/height
  50. property real xStep: 1/width
  51. anchors.fill: level2
  52. visible: false
  53. smooth: true
  54. vertexShader: __internalBlurVertexShader
  55. fragmentShader: __internalBlurFragmentShader
  56. }
  57. ShaderEffectSource {
  58. id: level2
  59. width: level1.width / 2
  60. height: level1.height / 2
  61. sourceItem: effect1
  62. hideSource: rootItem.visible
  63. visible: false
  64. smooth: true
  65. }
  66. ShaderEffect {
  67. id: effect2
  68. property variant source: level2
  69. property real yStep: 1/height
  70. property real xStep: 1/width
  71. anchors.fill: level3
  72. visible: false
  73. smooth: true
  74. vertexShader: __internalBlurVertexShader
  75. fragmentShader: __internalBlurFragmentShader
  76. }
  77. ShaderEffectSource {
  78. id: level3
  79. width: level2.width / 2
  80. height: level2.height / 2
  81. sourceItem: effect2
  82. hideSource: rootItem.visible
  83. visible: false
  84. smooth: true
  85. }
  86. ShaderEffect {
  87. id: effect3
  88. property variant source: level3
  89. property real yStep: 1/height
  90. property real xStep: 1/width
  91. anchors.fill: level4
  92. visible: false
  93. smooth: true
  94. vertexShader: __internalBlurVertexShader
  95. fragmentShader: __internalBlurFragmentShader
  96. }
  97. ShaderEffectSource {
  98. id: level4
  99. width: level3.width / 2
  100. height: level3.height / 2
  101. sourceItem: effect3
  102. hideSource: rootItem.visible
  103. visible: false
  104. smooth: true
  105. }
  106. ShaderEffect {
  107. id: effect4
  108. property variant source: level4
  109. property real yStep: 1/height
  110. property real xStep: 1/width
  111. anchors.fill: level5
  112. visible: false
  113. smooth: true
  114. vertexShader: __internalBlurVertexShader
  115. fragmentShader: __internalBlurFragmentShader
  116. }
  117. ShaderEffectSource {
  118. id: level5
  119. width: level4.width / 2
  120. height: level4.height / 2
  121. sourceItem: effect4
  122. hideSource: rootItem.visible
  123. visible: false
  124. smooth: true
  125. }
  126. ShaderEffect {
  127. id: effect5
  128. property variant source: level5
  129. property real yStep: 1/height
  130. property real xStep: 1/width
  131. anchors.fill: level6
  132. visible: false
  133. smooth: true
  134. vertexShader: __internalBlurVertexShader
  135. fragmentShader: __internalBlurFragmentShader
  136. }
  137. ShaderEffectSource {
  138. id: level6
  139. width: level5.width / 2
  140. height: level5.height / 2
  141. sourceItem: effect5
  142. hideSource: rootItem.visible
  143. visible: false
  144. smooth: true
  145. }
  146. Item {
  147. id: dummysource
  148. width: 1
  149. height: 1
  150. visible: false
  151. }
  152. ShaderEffectSource {
  153. id: dummy
  154. width: 1
  155. height: 1
  156. sourceItem: dummysource
  157. visible: false
  158. smooth: false
  159. live: false
  160. }
  161. ShaderEffect {
  162. id: shaderItem
  163. x: transparentBorder ? -64 : 0
  164. y: transparentBorder ? -64 : 0
  165. width: transparentBorder ? parent.width + 128 : parent.width
  166. height: transparentBorder ? parent.height + 128 : parent.height
  167. property variant source1: level1
  168. property variant source2: level2
  169. property variant source3: level3
  170. property variant source4: level4
  171. property variant source5: level5
  172. property variant source6: level6
  173. property real lod: rootItem.blur
  174. property real weight1;
  175. property real weight2;
  176. property real weight3;
  177. property real weight4;
  178. property real weight5;
  179. property real weight6;
  180. property real spread: 1.0 - (rootItem.spread * 0.98)
  181. property alias color: rootItem.color
  182. function weight(v) {
  183. if (v <= 0.0)
  184. return 1
  185. if (v >= 0.5)
  186. return 0
  187. return 1.0 - v / 0.5
  188. }
  189. function calculateWeights() {
  190. var w1 = weight(Math.abs(lod - 0.100))
  191. var w2 = weight(Math.abs(lod - 0.300))
  192. var w3 = weight(Math.abs(lod - 0.500))
  193. var w4 = weight(Math.abs(lod - 0.700))
  194. var w5 = weight(Math.abs(lod - 0.900))
  195. var w6 = weight(Math.abs(lod - 1.100))
  196. var sum = w1 + w2 + w3 + w4 + w5 + w6;
  197. weight1 = w1 / sum;
  198. weight2 = w2 / sum;
  199. weight3 = w3 / sum;
  200. weight4 = w4 / sum;
  201. weight5 = w5 / sum;
  202. weight6 = w6 / sum;
  203. upateSources()
  204. }
  205. function upateSources() {
  206. var sources = new Array();
  207. var weights = new Array();
  208. if (weight1 > 0) {
  209. sources.push(level1)
  210. weights.push(weight1)
  211. }
  212. if (weight2 > 0) {
  213. sources.push(level2)
  214. weights.push(weight2)
  215. }
  216. if (weight3 > 0) {
  217. sources.push(level3)
  218. weights.push(weight3)
  219. }
  220. if (weight4 > 0) {
  221. sources.push(level4)
  222. weights.push(weight4)
  223. }
  224. if (weight5 > 0) {
  225. sources.push(level5)
  226. weights.push(weight5)
  227. }
  228. if (weight6 > 0) {
  229. sources.push(level6)
  230. weights.push(weight6)
  231. }
  232. for (var j = sources.length; j < 6; j++) {
  233. sources.push(dummy)
  234. weights.push(0.0)
  235. }
  236. source1 = sources[0]
  237. source2 = sources[1]
  238. source3 = sources[2]
  239. source4 = sources[3]
  240. source5 = sources[4]
  241. source6 = sources[5]
  242. weight1 = weights[0]
  243. weight2 = weights[1]
  244. weight3 = weights[2]
  245. weight4 = weights[3]
  246. weight5 = weights[4]
  247. weight6 = weights[5]
  248. }
  249. Component.onCompleted: calculateWeights()
  250. onLodChanged: calculateWeights()
  251. fragmentShader: "qrc:/qt-project.org/imports/Qt5Compat/GraphicalEffects/shaders_ng/fastglow.frag.qsb"
  252. }
  253. }