FastInnerShadow.qml 9.5 KB

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