FastGlow.qml 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2017 The Qt Company Ltd.
  4. ** Contact: https://www.qt.io/licensing/
  5. **
  6. ** This file is part of the Qt Graphical Effects module.
  7. **
  8. ** $QT_BEGIN_LICENSE:LGPL$
  9. ** Commercial License Usage
  10. ** Licensees holding valid commercial Qt licenses may use this file in
  11. ** accordance with the commercial license agreement provided with the
  12. ** Software or, alternatively, in accordance with the terms contained in
  13. ** a written agreement between you and The Qt Company. For licensing terms
  14. ** and conditions see https://www.qt.io/terms-conditions. For further
  15. ** information use the contact form at https://www.qt.io/contact-us.
  16. **
  17. ** GNU Lesser General Public License Usage
  18. ** Alternatively, this file may be used under the terms of the GNU Lesser
  19. ** General Public License version 3 as published by the Free Software
  20. ** Foundation and appearing in the file LICENSE.LGPL3 included in the
  21. ** packaging of this file. Please review the following information to
  22. ** ensure the GNU Lesser General Public License version 3 requirements
  23. ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
  24. **
  25. ** GNU General Public License Usage
  26. ** Alternatively, this file may be used under the terms of the GNU
  27. ** General Public License version 2.0 or (at your option) the GNU General
  28. ** Public license version 3 or any later version approved by the KDE Free
  29. ** Qt Foundation. The licenses are as published by the Free Software
  30. ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
  31. ** included in the packaging of this file. Please review the following
  32. ** information to ensure the GNU General Public License requirements will
  33. ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
  34. ** https://www.gnu.org/licenses/gpl-3.0.html.
  35. **
  36. ** $QT_END_LICENSE$
  37. **
  38. ****************************************************************************/
  39. import QtQuick 2.12
  40. import QtGraphicalEffects.private 1.12
  41. Item {
  42. id: rootItem
  43. property variant source
  44. property real spread: 0.0
  45. property real blur: 0.0
  46. property color color: "white"
  47. property bool transparentBorder: false
  48. property bool cached: false
  49. SourceProxy {
  50. id: sourceProxy
  51. input: rootItem.source
  52. }
  53. ShaderEffectSource {
  54. id: cacheItem
  55. anchors.fill: shaderItem
  56. visible: rootItem.cached
  57. smooth: true
  58. sourceItem: shaderItem
  59. live: true
  60. hideSource: visible
  61. }
  62. property string __internalBlurVertexShader: "qrc:/qt-project.org/imports/QtGraphicalEffects/shaders/fastblur_internal.vert"
  63. property string __internalBlurFragmentShader: "qrc:/qt-project.org/imports/QtGraphicalEffects/shaders/fastblur_internal.frag"
  64. ShaderEffect {
  65. id: level0
  66. property variant source: sourceProxy.output
  67. anchors.fill: parent
  68. visible: false
  69. smooth: true
  70. }
  71. ShaderEffectSource {
  72. id: level1
  73. width: Math.ceil(shaderItem.width / 32) * 32
  74. height: Math.ceil(shaderItem.height / 32) * 32
  75. sourceItem: level0
  76. hideSource: rootItem.visible
  77. sourceRect: transparentBorder ? Qt.rect(-64, -64, shaderItem.width, shaderItem.height) : Qt.rect(0,0,0,0)
  78. smooth: true
  79. visible: false
  80. }
  81. ShaderEffect {
  82. id: effect1
  83. property variant source: level1
  84. property real yStep: 1/height
  85. property real xStep: 1/width
  86. anchors.fill: level2
  87. visible: false
  88. smooth: true
  89. vertexShader: __internalBlurVertexShader
  90. fragmentShader: __internalBlurFragmentShader
  91. }
  92. ShaderEffectSource {
  93. id: level2
  94. width: level1.width / 2
  95. height: level1.height / 2
  96. sourceItem: effect1
  97. hideSource: rootItem.visible
  98. visible: false
  99. smooth: true
  100. }
  101. ShaderEffect {
  102. id: effect2
  103. property variant source: level2
  104. property real yStep: 1/height
  105. property real xStep: 1/width
  106. anchors.fill: level3
  107. visible: false
  108. smooth: true
  109. vertexShader: __internalBlurVertexShader
  110. fragmentShader: __internalBlurFragmentShader
  111. }
  112. ShaderEffectSource {
  113. id: level3
  114. width: level2.width / 2
  115. height: level2.height / 2
  116. sourceItem: effect2
  117. hideSource: rootItem.visible
  118. visible: false
  119. smooth: true
  120. }
  121. ShaderEffect {
  122. id: effect3
  123. property variant source: level3
  124. property real yStep: 1/height
  125. property real xStep: 1/width
  126. anchors.fill: level4
  127. visible: false
  128. smooth: true
  129. vertexShader: __internalBlurVertexShader
  130. fragmentShader: __internalBlurFragmentShader
  131. }
  132. ShaderEffectSource {
  133. id: level4
  134. width: level3.width / 2
  135. height: level3.height / 2
  136. sourceItem: effect3
  137. hideSource: rootItem.visible
  138. visible: false
  139. smooth: true
  140. }
  141. ShaderEffect {
  142. id: effect4
  143. property variant source: level4
  144. property real yStep: 1/height
  145. property real xStep: 1/width
  146. anchors.fill: level5
  147. visible: false
  148. smooth: true
  149. vertexShader: __internalBlurVertexShader
  150. fragmentShader: __internalBlurFragmentShader
  151. }
  152. ShaderEffectSource {
  153. id: level5
  154. width: level4.width / 2
  155. height: level4.height / 2
  156. sourceItem: effect4
  157. hideSource: rootItem.visible
  158. visible: false
  159. smooth: true
  160. }
  161. ShaderEffect {
  162. id: effect5
  163. property variant source: level5
  164. property real yStep: 1/height
  165. property real xStep: 1/width
  166. anchors.fill: level6
  167. visible: false
  168. smooth: true
  169. vertexShader: __internalBlurVertexShader
  170. fragmentShader: __internalBlurFragmentShader
  171. }
  172. ShaderEffectSource {
  173. id: level6
  174. width: level5.width / 2
  175. height: level5.height / 2
  176. sourceItem: effect5
  177. hideSource: rootItem.visible
  178. visible: false
  179. smooth: true
  180. }
  181. Item {
  182. id: dummysource
  183. width: 1
  184. height: 1
  185. visible: false
  186. }
  187. ShaderEffectSource {
  188. id: dummy
  189. width: 1
  190. height: 1
  191. sourceItem: dummysource
  192. visible: false
  193. smooth: false
  194. live: false
  195. }
  196. ShaderEffect {
  197. id: shaderItem
  198. x: transparentBorder ? -64 : 0
  199. y: transparentBorder ? -64 : 0
  200. width: transparentBorder ? parent.width + 128 : parent.width
  201. height: transparentBorder ? parent.height + 128 : parent.height
  202. property variant source1: level1
  203. property variant source2: level2
  204. property variant source3: level3
  205. property variant source4: level4
  206. property variant source5: level5
  207. property variant source6: level6
  208. property real lod: rootItem.blur
  209. property real weight1;
  210. property real weight2;
  211. property real weight3;
  212. property real weight4;
  213. property real weight5;
  214. property real weight6;
  215. property real spread: 1.0 - (rootItem.spread * 0.98)
  216. property alias color: rootItem.color
  217. function weight(v) {
  218. if (v <= 0.0)
  219. return 1
  220. if (v >= 0.5)
  221. return 0
  222. return 1.0 - v / 0.5
  223. }
  224. function calculateWeights() {
  225. var w1 = weight(Math.abs(lod - 0.100))
  226. var w2 = weight(Math.abs(lod - 0.300))
  227. var w3 = weight(Math.abs(lod - 0.500))
  228. var w4 = weight(Math.abs(lod - 0.700))
  229. var w5 = weight(Math.abs(lod - 0.900))
  230. var w6 = weight(Math.abs(lod - 1.100))
  231. var sum = w1 + w2 + w3 + w4 + w5 + w6;
  232. weight1 = w1 / sum;
  233. weight2 = w2 / sum;
  234. weight3 = w3 / sum;
  235. weight4 = w4 / sum;
  236. weight5 = w5 / sum;
  237. weight6 = w6 / sum;
  238. upateSources()
  239. }
  240. function upateSources() {
  241. var sources = new Array();
  242. var weights = new Array();
  243. if (weight1 > 0) {
  244. sources.push(level1)
  245. weights.push(weight1)
  246. }
  247. if (weight2 > 0) {
  248. sources.push(level2)
  249. weights.push(weight2)
  250. }
  251. if (weight3 > 0) {
  252. sources.push(level3)
  253. weights.push(weight3)
  254. }
  255. if (weight4 > 0) {
  256. sources.push(level4)
  257. weights.push(weight4)
  258. }
  259. if (weight5 > 0) {
  260. sources.push(level5)
  261. weights.push(weight5)
  262. }
  263. if (weight6 > 0) {
  264. sources.push(level6)
  265. weights.push(weight6)
  266. }
  267. for (var j = sources.length; j < 6; j++) {
  268. sources.push(dummy)
  269. weights.push(0.0)
  270. }
  271. source1 = sources[0]
  272. source2 = sources[1]
  273. source3 = sources[2]
  274. source4 = sources[3]
  275. source5 = sources[4]
  276. source6 = sources[5]
  277. weight1 = weights[0]
  278. weight2 = weights[1]
  279. weight3 = weights[2]
  280. weight4 = weights[3]
  281. weight5 = weights[4]
  282. weight6 = weights[5]
  283. }
  284. Component.onCompleted: calculateWeights()
  285. onLodChanged: calculateWeights()
  286. fragmentShader: "qrc:/qt-project.org/imports/QtGraphicalEffects/shaders/fastglow.frag"
  287. }
  288. }