FastInnerShadow.qml 7.9 KB

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