FastGlow.qml 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2016 The Qt Company Ltd.
  4. ** Contact: https://www.qt.io/licensing/
  5. **
  6. ** This file is part of the Qt Quick Controls module of the Qt Toolkit.
  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.4
  40. Item {
  41. id: rootItem
  42. property variant source
  43. property real spread: 0.0
  44. property real blur: 0.0
  45. property color color: "white"
  46. property bool transparentBorder: false
  47. property bool cached: false
  48. SourceProxy {
  49. id: sourceProxy
  50. input: rootItem.source
  51. }
  52. ShaderEffectSource {
  53. id: cacheItem
  54. anchors.fill: shaderItem
  55. visible: rootItem.cached
  56. smooth: true
  57. sourceItem: shaderItem
  58. live: true
  59. hideSource: visible
  60. }
  61. property string __internalBlurVertexShader: "qrc:/QtQuick/Controls/Shaders/blur.vert"
  62. property string __internalBlurFragmentShader: "qrc:/QtQuick/Controls/Shaders/blur.frag"
  63. ShaderEffect {
  64. id: level0
  65. property variant source: sourceProxy.output
  66. anchors.fill: parent
  67. visible: false
  68. smooth: true
  69. }
  70. ShaderEffectSource {
  71. id: level1
  72. width: Math.ceil(shaderItem.width / 32) * 32
  73. height: Math.ceil(shaderItem.height / 32) * 32
  74. sourceItem: level0
  75. hideSource: rootItem.visible
  76. sourceRect: transparentBorder ? Qt.rect(-64, -64, shaderItem.width, shaderItem.height) : Qt.rect(0,0,0,0)
  77. smooth: true
  78. visible: false
  79. }
  80. ShaderEffect {
  81. id: effect1
  82. property variant source: level1
  83. property real yStep: 1/height
  84. property real xStep: 1/width
  85. anchors.fill: level2
  86. visible: false
  87. smooth: true
  88. vertexShader: __internalBlurVertexShader
  89. fragmentShader: __internalBlurFragmentShader
  90. }
  91. ShaderEffectSource {
  92. id: level2
  93. width: level1.width / 2
  94. height: level1.height / 2
  95. sourceItem: effect1
  96. hideSource: rootItem.visible
  97. visible: false
  98. smooth: true
  99. }
  100. ShaderEffect {
  101. id: effect2
  102. property variant source: level2
  103. property real yStep: 1/height
  104. property real xStep: 1/width
  105. anchors.fill: level3
  106. visible: false
  107. smooth: true
  108. vertexShader: __internalBlurVertexShader
  109. fragmentShader: __internalBlurFragmentShader
  110. }
  111. ShaderEffectSource {
  112. id: level3
  113. width: level2.width / 2
  114. height: level2.height / 2
  115. sourceItem: effect2
  116. hideSource: rootItem.visible
  117. visible: false
  118. smooth: true
  119. }
  120. ShaderEffect {
  121. id: effect3
  122. property variant source: level3
  123. property real yStep: 1/height
  124. property real xStep: 1/width
  125. anchors.fill: level4
  126. visible: false
  127. smooth: true
  128. vertexShader: __internalBlurVertexShader
  129. fragmentShader: __internalBlurFragmentShader
  130. }
  131. ShaderEffectSource {
  132. id: level4
  133. width: level3.width / 2
  134. height: level3.height / 2
  135. sourceItem: effect3
  136. hideSource: rootItem.visible
  137. visible: false
  138. smooth: true
  139. }
  140. ShaderEffect {
  141. id: effect4
  142. property variant source: level4
  143. property real yStep: 1/height
  144. property real xStep: 1/width
  145. anchors.fill: level5
  146. visible: false
  147. smooth: true
  148. vertexShader: __internalBlurVertexShader
  149. fragmentShader: __internalBlurFragmentShader
  150. }
  151. ShaderEffectSource {
  152. id: level5
  153. width: level4.width / 2
  154. height: level4.height / 2
  155. sourceItem: effect4
  156. hideSource: rootItem.visible
  157. visible: false
  158. smooth: true
  159. }
  160. ShaderEffect {
  161. id: effect5
  162. property variant source: level5
  163. property real yStep: 1/height
  164. property real xStep: 1/width
  165. anchors.fill: level6
  166. visible: false
  167. smooth: true
  168. vertexShader: __internalBlurVertexShader
  169. fragmentShader: __internalBlurFragmentShader
  170. }
  171. ShaderEffectSource {
  172. id: level6
  173. width: level5.width / 2
  174. height: level5.height / 2
  175. sourceItem: effect5
  176. hideSource: rootItem.visible
  177. visible: false
  178. smooth: true
  179. }
  180. Item {
  181. id: dummysource
  182. width: 1
  183. height: 1
  184. visible: false
  185. }
  186. ShaderEffectSource {
  187. id: dummy
  188. width: 1
  189. height: 1
  190. sourceItem: dummysource
  191. visible: false
  192. smooth: false
  193. live: false
  194. }
  195. ShaderEffect {
  196. id: shaderItem
  197. x: transparentBorder ? -64 : 0
  198. y: transparentBorder ? -64 : 0
  199. width: transparentBorder ? parent.width + 128 : parent.width
  200. height: transparentBorder ? parent.height + 128 : parent.height
  201. property variant source1: level1
  202. property variant source2: level2
  203. property variant source3: level3
  204. property variant source4: level4
  205. property variant source5: level5
  206. property variant source6: level6
  207. property real lod: rootItem.blur
  208. property real weight1;
  209. property real weight2;
  210. property real weight3;
  211. property real weight4;
  212. property real weight5;
  213. property real weight6;
  214. property real spread: 1.0 - (rootItem.spread * 0.98)
  215. property alias color: rootItem.color
  216. function weight(v) {
  217. if (v <= 0.0)
  218. return 1
  219. if (v >= 0.5)
  220. return 0
  221. return 1.0 - v / 0.5
  222. }
  223. function calculateWeights() {
  224. var w1 = weight(Math.abs(lod - 0.100))
  225. var w2 = weight(Math.abs(lod - 0.300))
  226. var w3 = weight(Math.abs(lod - 0.500))
  227. var w4 = weight(Math.abs(lod - 0.700))
  228. var w5 = weight(Math.abs(lod - 0.900))
  229. var w6 = weight(Math.abs(lod - 1.100))
  230. var sum = w1 + w2 + w3 + w4 + w5 + w6;
  231. weight1 = w1 / sum;
  232. weight2 = w2 / sum;
  233. weight3 = w3 / sum;
  234. weight4 = w4 / sum;
  235. weight5 = w5 / sum;
  236. weight6 = w6 / sum;
  237. upateSources()
  238. }
  239. function upateSources() {
  240. var sources = new Array();
  241. var weights = new Array();
  242. if (weight1 > 0) {
  243. sources.push(level1)
  244. weights.push(weight1)
  245. }
  246. if (weight2 > 0) {
  247. sources.push(level2)
  248. weights.push(weight2)
  249. }
  250. if (weight3 > 0) {
  251. sources.push(level3)
  252. weights.push(weight3)
  253. }
  254. if (weight4 > 0) {
  255. sources.push(level4)
  256. weights.push(weight4)
  257. }
  258. if (weight5 > 0) {
  259. sources.push(level5)
  260. weights.push(weight5)
  261. }
  262. if (weight6 > 0) {
  263. sources.push(level6)
  264. weights.push(weight6)
  265. }
  266. for (var j = sources.length; j < 6; j++) {
  267. sources.push(dummy)
  268. weights.push(0.0)
  269. }
  270. source1 = sources[0]
  271. source2 = sources[1]
  272. source3 = sources[2]
  273. source4 = sources[3]
  274. source5 = sources[4]
  275. source6 = sources[5]
  276. weight1 = weights[0]
  277. weight2 = weights[1]
  278. weight3 = weights[2]
  279. weight4 = weights[3]
  280. weight5 = weights[4]
  281. weight6 = weights[5]
  282. }
  283. Component.onCompleted: calculateWeights()
  284. onLodChanged: calculateWeights()
  285. fragmentShader: "qrc:/QtQuick/Controls/Shaders/glow.frag"
  286. }
  287. }