You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

KNWebGLShader.js 47KB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362
  1. /*
  2. * KNWebGLShader.js
  3. * Keynote HTML Player
  4. *
  5. * Created by Tungwei Cheng
  6. * Copyright (c) 2016-2019 Apple Inc. All rights reserved.
  7. */
  8. var KNWebGLShader = {};
  9. KNWebGLShader.defaultTexture = {
  10. attribNames: ["Position", "TexCoord"],
  11. uniformNames: ["MVPMatrix", "Texture"],
  12. vertex: "\
  13. #ifdef GL_ES\n\
  14. precision highp float;\n\
  15. #endif\n\
  16. uniform mat4 MVPMatrix;\n\
  17. attribute vec4 Position;\n\
  18. attribute vec2 TexCoord;\n\
  19. varying vec2 v_TexCoord;\n\
  20. void main()\n\
  21. {\n\
  22. v_TexCoord = TexCoord;\n\
  23. gl_Position = (MVPMatrix * Position);\n\
  24. }\
  25. ",
  26. fragment: "\
  27. #ifdef GL_ES\n\
  28. precision mediump float;\n\
  29. #endif\n\
  30. uniform sampler2D Texture;\n\
  31. varying vec2 v_TexCoord;\n\
  32. void main()\n\
  33. {\n\
  34. gl_FragColor = texture2D(Texture, v_TexCoord);\n\
  35. }\
  36. "
  37. };
  38. KNWebGLShader.defaultTextureAndOpacity = {
  39. attribNames: ["Position", "TexCoord"],
  40. uniformNames: ["MVPMatrix", "Texture", "Opacity"],
  41. vertex: "\
  42. #ifdef GL_ES\n\
  43. precision highp float;\n\
  44. #endif\n\
  45. uniform mat4 MVPMatrix;\n\
  46. attribute vec4 Position;\n\
  47. attribute vec2 TexCoord;\n\
  48. varying vec2 v_TexCoord;\n\
  49. void main()\n\
  50. {\n\
  51. v_TexCoord = TexCoord;\n\
  52. gl_Position = (MVPMatrix * Position);\n\
  53. }\
  54. ",
  55. fragment: "\
  56. #ifdef GL_ES\n\
  57. precision mediump float;\n\
  58. #endif\n\
  59. uniform sampler2D Texture;\n\
  60. uniform float Opacity;\n\
  61. varying vec2 v_TexCoord;\n\
  62. void main()\n\
  63. {\n\
  64. vec4 texColor = texture2D(Texture, v_TexCoord);\n\
  65. gl_FragColor = vec4(Opacity) * texColor;\n\
  66. }\
  67. "
  68. };
  69. KNWebGLShader.contentsAndOpacity = {
  70. attribNames: ["Position", "TexCoord"],
  71. uniformNames: ["MVPMatrix", "Texture", "Texture2", "mixFactor", "Opacity"],
  72. vertex: "\
  73. #ifdef GL_ES\n\
  74. precision highp float;\n\
  75. #endif\n\
  76. uniform mat4 MVPMatrix;\n\
  77. attribute vec4 Position;\n\
  78. attribute vec2 TexCoord;\n\
  79. varying vec2 v_TexCoord;\n\
  80. void main()\n\
  81. {\n\
  82. v_TexCoord = TexCoord;\n\
  83. gl_Position = (MVPMatrix * Position);\n\
  84. }\
  85. ",
  86. fragment: "\
  87. #ifdef GL_ES\n\
  88. precision mediump float;\n\
  89. #endif\n\
  90. uniform sampler2D Texture;\n\
  91. uniform sampler2D Texture2;\n\
  92. uniform float mixFactor;\n\
  93. uniform float Opacity;\n\
  94. varying vec2 v_TexCoord;\n\
  95. void main()\n\
  96. {\n\
  97. vec4 outgoingColor = texture2D(Texture2, v_TexCoord);\n\
  98. vec4 incomingColor = texture2D(Texture, v_TexCoord);\n\
  99. vec4 result = mix(outgoingColor, incomingColor, mixFactor);\n\
  100. gl_FragColor = vec4(Opacity) * result;\n\
  101. }\
  102. "
  103. };
  104. KNWebGLShader.contents = {
  105. attribNames: ["Position", "TexCoord"],
  106. uniformNames: ["MVPMatrix", "Texture", "Texture2", "mixFactor"],
  107. vertex: "\
  108. #ifdef GL_ES\n\
  109. precision highp float;\n\
  110. #endif\n\
  111. uniform mat4 MVPMatrix;\n\
  112. attribute vec4 Position;\n\
  113. attribute vec2 TexCoord;\n\
  114. varying vec2 v_TexCoord;\n\
  115. void main()\n\
  116. {\n\
  117. v_TexCoord = TexCoord;\n\
  118. gl_Position = (MVPMatrix * Position);\n\
  119. }\
  120. ",
  121. fragment: "\
  122. #ifdef GL_ES\n\
  123. precision mediump float;\n\
  124. #endif\n\
  125. uniform sampler2D Texture;\n\
  126. uniform sampler2D Texture2;\n\
  127. uniform float mixFactor;\n\
  128. varying vec2 v_TexCoord;\n\
  129. void main()\n\
  130. {\n\
  131. vec4 outgoingColor = texture2D(Texture2, v_TexCoord);\n\
  132. vec4 incomingColor = texture2D(Texture, v_TexCoord);\n\
  133. vec4 result = mix(outgoingColor, incomingColor, mixFactor);\n\
  134. gl_FragColor = result;\n\
  135. }\
  136. "
  137. };
  138. KNWebGLShader.iris = {
  139. attribNames: ["Position", "TexCoord"],
  140. uniformNames: ["PercentForAlpha", "Scale", "Mix", "Texture", "MVPMatrix", "Opacity"],
  141. vertex: "\
  142. #ifdef GL_ES\n\
  143. precision highp float;\n\
  144. #endif\n\
  145. uniform mat4 MVPMatrix;\n\
  146. attribute vec4 Position;\n\
  147. attribute vec2 TexCoord;\n\
  148. varying vec2 v_TexCoord;\n\
  149. void main()\n\
  150. {\n\
  151. v_TexCoord = TexCoord;\n\
  152. gl_Position = MVPMatrix * Position;\n\
  153. }\
  154. ",
  155. fragment: "\
  156. #ifdef GL_ES\n\
  157. precision mediump float;\n\
  158. #endif\n\
  159. uniform sampler2D Texture;\n\
  160. uniform float Opacity;\n\
  161. uniform float PercentForAlpha;\n\
  162. uniform float Scale;\n\
  163. uniform float Mix;\n\
  164. varying vec2 v_TexCoord;\n\
  165. void main()\n\
  166. {\n\
  167. vec4 incomingTexColor = texture2D(Texture, v_TexCoord);\n\
  168. vec4 clear = vec4(0.0, 0.0, 0.0, 0.0);\n\
  169. float tolerance = PercentForAlpha/5.0;\n\
  170. vec2 powers = vec2((v_TexCoord.x - 0.5) * Scale,v_TexCoord.y - 0.5);\n\
  171. powers *= powers;\n\
  172. float radiusSqrd = PercentForAlpha * PercentForAlpha;\n\
  173. float dist = (powers.x+powers.y)/((0.5*Scale)*(0.5*Scale)+0.25);\n\
  174. float gradient = smoothstep(radiusSqrd, radiusSqrd+tolerance, dist);\n\
  175. gl_FragColor = vec4(Opacity) * mix(clear, incomingTexColor, abs(Mix - gradient));\n\
  176. }\
  177. "
  178. };
  179. KNWebGLShader.twist = {
  180. attribNames: ["Position", "TexCoord", "Normal"],
  181. uniformNames: ["TextureMatrix", "SpecularColor", "FlipNormals", "MVPMatrix", "Texture"],
  182. vertex: "\
  183. #ifdef GL_ES\n\
  184. precision highp float;\n\
  185. #endif\n\
  186. uniform mat4 MVPMatrix;\n\
  187. uniform mat3 TextureMatrix;\n\
  188. uniform float SpecularColor;\n\
  189. uniform mediump float FlipNormals;\n\
  190. attribute vec3 Position;\n\
  191. attribute vec3 Normal;\n\
  192. attribute vec2 TexCoord;\n\
  193. varying vec2 v_TexCoord;\n\
  194. varying vec3 v_DiffuseColor;\n\
  195. varying vec3 v_SpecularColor;\n\
  196. const vec3 c_AmbientColor = vec3(0.2);\n\
  197. const vec3 c_DiffuseColor = vec3(1);\n\
  198. const float c_LightExponent = 32.0;\n\
  199. const vec3 c_LightDirection = vec3(0.1580, +0.5925, 0.7900);\n\
  200. const vec3 c_LightHalfPlane = vec3(0.0835, +0.3131, 0.9460);\n\
  201. void main()\n\
  202. {\n\
  203. vec3 thisNormal = Normal * FlipNormals;\n\
  204. // Lighting\n\
  205. float ndotl = max(0.0, dot(thisNormal, c_LightDirection));\n\
  206. float ndoth = max(0.0, dot(thisNormal, c_LightHalfPlane));\n\
  207. v_DiffuseColor = (c_AmbientColor + ndotl * c_DiffuseColor);\n\
  208. v_SpecularColor = (ndoth <= 0.0) ? vec3(0) : (pow(ndoth, c_LightExponent) * vec3(SpecularColor));\n\
  209. gl_Position = MVPMatrix * vec4(Position, 1.0);\n\
  210. v_TexCoord = (TextureMatrix * vec3(TexCoord,1.0)).xy;\n\
  211. }\
  212. ",
  213. fragment: "\
  214. #ifdef GL_ES\n\
  215. precision mediump float;\n\
  216. #endif\n\
  217. uniform sampler2D Texture;\n\
  218. varying vec2 v_TexCoord;\n\
  219. varying vec3 v_DiffuseColor;\n\
  220. varying vec3 v_SpecularColor;\n\
  221. void main()\n\
  222. {\n\
  223. vec4 texColor = texture2D(Texture, v_TexCoord);\n\
  224. // Lighting\n\
  225. texColor.xyz = texColor.xyz * v_DiffuseColor + v_SpecularColor;\n\
  226. gl_FragColor = texColor;\n\
  227. }\
  228. "
  229. };
  230. KNWebGLShader.colorPlanes = {
  231. attribNames: ["Position", "TexCoord"],
  232. uniformNames: ["MVPMatrix", "FlipTexCoords", "Texture", "ColorMask"],
  233. vertex: "\
  234. #ifdef GL_ES\n\
  235. precision highp float;\n\
  236. #endif\n\
  237. uniform mat4 MVPMatrix;\n\
  238. uniform vec2 FlipTexCoords;\n\
  239. attribute vec2 Position;\n\
  240. attribute vec2 TexCoord;\n\
  241. varying vec2 v_TexCoord;\n\
  242. void main()\n\
  243. {\n\
  244. v_TexCoord = vec2(FlipTexCoords.x == 0.0 ? TexCoord.x : 1.0-TexCoord.x, FlipTexCoords.y == 0.0 ? TexCoord.y : 1.0-TexCoord.y);\n\
  245. gl_Position = MVPMatrix * vec4(Position, 0,1);\n\
  246. }\
  247. ",
  248. fragment: "\
  249. #ifdef GL_ES\n\
  250. precision mediump float;\n\
  251. #endif\n\
  252. uniform sampler2D Texture;\n\
  253. uniform vec4 ColorMask;\n\
  254. varying vec2 v_TexCoord;\n\
  255. void main()\n\
  256. {\n\
  257. vec4 texColor = texture2D(Texture, v_TexCoord);\n\
  258. texColor *= ColorMask;\n\
  259. gl_FragColor = texColor;\n\
  260. }\
  261. "
  262. };
  263. KNWebGLShader.flop = {
  264. attribNames: ["Position", "TexCoord", "Normal"],
  265. uniformNames: ["TextureMatrix", "FlipNormals", "MVPMatrix", "Texture"],
  266. vertex: "\
  267. \n\
  268. #ifdef GL_ES\n\
  269. precision highp float;\n\
  270. #endif\n\
  271. \n\
  272. uniform mat4 MVPMatrix;\n\
  273. uniform mat3 TextureMatrix;\n\
  274. uniform float FlipNormals;\n\
  275. \n\
  276. attribute vec3 Position;\n\
  277. attribute vec3 Normal;\n\
  278. attribute vec2 TexCoord;\n\
  279. \n\
  280. varying vec2 v_TexCoord;\n\
  281. varying vec3 v_DiffuseColor;\n\
  282. \n\
  283. const vec3 c_AmbientColor = vec3(0.1);\n\
  284. const vec3 c_DiffuseColor = vec3(1);\n\
  285. const float c_LightExponent = 32.0;\n\
  286. \n\
  287. const vec3 c_LightDirection = vec3(0.000, +0.000, 0.900);\n\
  288. \n\
  289. void main()\n\
  290. {\n\
  291. vec3 thisNormal = Normal * FlipNormals;\n\
  292. \n\
  293. // Lighting\n\
  294. vec3 lightDirection = vec3(c_LightDirection.x,c_LightDirection.y,c_LightDirection.z);\n\
  295. \n\
  296. float ndotl = max(0.0, dot(thisNormal, lightDirection));\n\
  297. \n\
  298. v_DiffuseColor = (c_AmbientColor + ndotl * c_DiffuseColor);\n\
  299. \n\
  300. gl_Position = MVPMatrix * vec4(Position, 1);\n\
  301. v_TexCoord = (TextureMatrix * vec3(TexCoord,1)).xy;\n\
  302. }\
  303. ",
  304. fragment: "\
  305. \n\
  306. #ifdef GL_ES\n\
  307. precision mediump float;\n\
  308. #endif\n\
  309. \n\
  310. uniform sampler2D Texture;\n\
  311. \n\
  312. varying vec2 v_TexCoord;\n\
  313. varying vec3 v_DiffuseColor;\n\
  314. \n\
  315. void main()\n\
  316. {\n\
  317. vec4 texColor = texture2D(Texture, v_TexCoord);\n\
  318. \n\
  319. // Lighting\n\
  320. texColor.xyz = texColor.xyz * v_DiffuseColor;\n\
  321. gl_FragColor = texColor;\n\
  322. }\
  323. "
  324. };
  325. KNWebGLShader.anvilsmoke = {
  326. attribNames: ["Rotation", "Speed", "Scale", "LifeSpan", "ParticleTexCoord", "Center", "Position"],
  327. uniformNames: ["Percent", "Opacity", "ParticleTexture", "MVPMatrix"],
  328. vertex: "\
  329. \n\
  330. uniform mat4 MVPMatrix;\n\
  331. uniform float Percent;\n\
  332. uniform float Opacity;\n\
  333. \n\
  334. attribute vec2 Position;\n\
  335. attribute vec2 Center;\n\
  336. attribute vec2 ParticleTexCoord;\n\
  337. attribute vec3 Rotation;\n\
  338. attribute vec3 Speed;\n\
  339. attribute float Scale;\n\
  340. attribute vec2 LifeSpan;\n\
  341. \n\
  342. varying vec4 v_Color;\n\
  343. varying vec2 v_TexCoord;\n\
  344. \n\
  345. const float Pi = 3.1415926;\n\
  346. const float Pi_2 = 1.5707963;\n\
  347. const float TwoPi = 6.2831852;\n\
  348. \n\
  349. const float sineConstB = 1.2732396; /* = 4./Pi; */\n\
  350. const float sineConstC = -0.40528476; /* = -4./(Pi*Pi); */\n\
  351. \n\
  352. vec3 fastSine(vec3 angle)\n\
  353. {\n\
  354. vec3 theAngle = mod(angle + Pi, TwoPi) - Pi;\n\
  355. return sineConstB * theAngle + sineConstC * theAngle * abs(theAngle);\n\
  356. }\n\
  357. \n\
  358. mat3 fastRotationMatrix(vec3 theRotation)\n\
  359. {\n\
  360. vec3 sinXYZ = fastSine(theRotation);\n\
  361. vec3 cosXYZ = fastSine(Pi_2 - theRotation);\n\
  362. mat3 rotMatrix = mat3( cosXYZ.y*cosXYZ.z, sinXYZ.x*sinXYZ.y*cosXYZ.z+cosXYZ.x*sinXYZ.z, -cosXYZ.x*sinXYZ.y*cosXYZ.z+sinXYZ.x*sinXYZ.z,\n\
  363. -cosXYZ.y*sinXYZ.z, -sinXYZ.x*sinXYZ.y*sinXYZ.z+cosXYZ.x*cosXYZ.z, cosXYZ.x*sinXYZ.y*sinXYZ.z+sinXYZ.x*cosXYZ.z,\n\
  364. sinXYZ.y, -sinXYZ.x*cosXYZ.y, cosXYZ.x*cosXYZ.y);\n\
  365. return rotMatrix;\n\
  366. }\n\
  367. \n\
  368. void main()\n\
  369. {\n\
  370. float realPercent = (Percent-LifeSpan.x)/LifeSpan.y;\n\
  371. realPercent = clamp(realPercent, 0.0, 1.0);\n\
  372. realPercent = sqrt(realPercent);\n\
  373. \n\
  374. /* SCALE */\n\
  375. vec4 originalPosition = vec4(Position,0,1);\n\
  376. vec4 center = vec4(Center, 0,1);\n\
  377. vec3 scaleDirectionVec = vec3(originalPosition.xy-center.xy,0) * Scale * mix(0.1, 1.0, realPercent);\n\
  378. \n\
  379. /* ROTATE */\n\
  380. mat3 rotMatrix = fastRotationMatrix(Rotation * realPercent);\n\
  381. vec3 rotatedVec = rotMatrix * scaleDirectionVec;\n\
  382. vec4 position = center + vec4(rotatedVec,0);\n\
  383. \n\
  384. float speedAdjust = realPercent;\n\
  385. vec3 thisSpeed = Speed;\n\
  386. thisSpeed.x *= sqrt(realPercent);\n\
  387. thisSpeed.y *= realPercent*realPercent;\n\
  388. position += vec4(thisSpeed, 0);\n\
  389. \n\
  390. float thisOpacity = Opacity;\n\
  391. thisOpacity *= (1.0 - realPercent); /* fade out gradually */\n\
  392. thisOpacity *= min(1.0, realPercent*20.0); /* fade in quickly */\n\
  393. \n\
  394. /* output */\n\
  395. gl_Position = MVPMatrix * position;\n\
  396. //v_Color = vec4(1.0, 1.0, 1.0, thisOpacity); //we applied a fix here, might not work everywhere\n\
  397. v_Color = vec4(thisOpacity);\n\
  398. v_TexCoord = ParticleTexCoord;\n\
  399. }\n\
  400. ",
  401. fragment: "\
  402. \n\
  403. #ifdef GL_ES\n\
  404. precision mediump float;\n\
  405. #endif\n\
  406. uniform sampler2D ParticleTexture;\n\
  407. \n\
  408. varying vec4 v_Color;\n\
  409. varying vec2 v_TexCoord;\n\
  410. \n\
  411. void main()\n\
  412. {\n\
  413. vec4 texColor = texture2D(ParticleTexture, v_TexCoord);\n\
  414. \n\
  415. texColor *= v_Color;\n\
  416. \n\
  417. gl_FragColor = texColor;\n\
  418. }\n\
  419. "
  420. };
  421. KNWebGLShader.anvilspeck = {
  422. attribNames: ["Speed", "Scale", "LifeSpan", "ParticleTexCoord", "Center", "Position"],
  423. uniformNames: ["Percent", "Opacity", "ParticleTexture", "MVPMatrix"],
  424. vertex: "\
  425. \n\
  426. uniform mat4 MVPMatrix;\n\
  427. uniform float Percent;\n\
  428. uniform float Opacity;\n\
  429. \n\
  430. attribute vec2 Position;\n\
  431. attribute vec2 Center;\n\
  432. attribute vec2 ParticleTexCoord;\n\
  433. attribute vec3 Speed;\n\
  434. attribute float Scale;\n\
  435. attribute vec2 LifeSpan;\n\
  436. \n\
  437. varying vec4 v_Color;\n\
  438. varying vec2 v_TexCoord;\n\
  439. \n\
  440. const float Pi = 3.1415926;\n\
  441. const float Pi_2 = 1.5707963;\n\
  442. const float TwoPi = 6.2831852;\n\
  443. \n\
  444. const float sineConstB = 1.2732396; /* = 4./Pi; */\n\
  445. const float sineConstC = -0.40528476; /* = -4./(Pi*Pi); */\n\
  446. \n\
  447. vec3 fastSine(vec3 angle)\n\
  448. {\n\
  449. vec3 theAngle = mod(angle + Pi, TwoPi) - Pi;\n\
  450. return sineConstB * theAngle + sineConstC * theAngle * abs(theAngle);\n\
  451. }\n\
  452. \n\
  453. void main()\n\
  454. {\n\
  455. float realPercent = (Percent-LifeSpan.x)/LifeSpan.y;\n\
  456. realPercent = clamp(realPercent, 0.0, 1.0);\n\
  457. \n\
  458. /* SCALE */\n\
  459. vec4 originalPosition = vec4(Position,0,1);\n\
  460. vec4 center = vec4(Center, 0,1);\n\
  461. vec3 thisScale = Scale * vec3(1, Speed.z, 1) * mix(0.1, 1.0, realPercent);\n\
  462. vec3 scaleDirectionVec = vec3(originalPosition.xy-center.xy,0) * thisScale;\n\
  463. \n\
  464. vec4 position = center + vec4(scaleDirectionVec,0);\n\
  465. \n\
  466. float speedAdjust = realPercent;\n\
  467. vec3 thisPos = vec3(Speed.x * realPercent,\n\
  468. Speed.y * fastSine(Pi*0.85*vec3(realPercent,0,0)).x, /* arc with gravity */\n\
  469. 0);\n\
  470. position += vec4(thisPos, 0);\n\
  471. \n\
  472. float thisOpacity = Opacity;\n\
  473. thisOpacity *= (1.0 - realPercent); /* fade out gradually */\n\
  474. thisOpacity *= min(1.0, realPercent*20.0); /* fade in quickly */\n\
  475. \n\
  476. /* output */\n\
  477. gl_Position = MVPMatrix * position;\n\
  478. v_Color = vec4(thisOpacity);\n\
  479. v_TexCoord = ParticleTexCoord;\n\
  480. }\
  481. ",
  482. fragment: "\
  483. \n\
  484. #ifdef GL_ES\n\
  485. precision mediump float;\n\
  486. #endif\n\
  487. uniform sampler2D ParticleTexture;\n\
  488. \n\
  489. varying vec4 v_Color;\n\
  490. varying vec2 v_TexCoord;\n\
  491. \n\
  492. void main()\n\
  493. {\n\
  494. vec4 texColor = texture2D(ParticleTexture, v_TexCoord);\n\
  495. \n\
  496. texColor *= v_Color;\n\
  497. \n\
  498. gl_FragColor = texColor;\n\
  499. }\
  500. "
  501. };
  502. KNWebGLShader.flame = {
  503. attribNames: ["Rotation", "Speed", "LifeSpan", "ParticleTexCoord", "Center", "Position"],
  504. uniformNames: ["Percent", "Duration", "Opacity", "RotationMax", "SpeedMax", "ParticleTexture", "MVPMatrix"],
  505. vertex: "\
  506. \n\
  507. precision highp float;\n\
  508. \n\
  509. uniform mat4 MVPMatrix;\n\
  510. uniform float Percent;\n\
  511. uniform float Duration;\n\
  512. \n\
  513. attribute vec3 Rotation;\n\
  514. attribute vec3 Speed;\n\
  515. uniform float Opacity;\n\
  516. attribute vec2 LifeSpan;\n\
  517. attribute vec2 ParticleTexCoord;\n\
  518. attribute vec2 Position;\n\
  519. attribute vec2 Center;\n\
  520. \n\
  521. uniform mediump float RotationMax;\n\
  522. uniform mediump float SpeedMax;\n\
  523. \n\
  524. varying vec4 v_Color;\n\
  525. varying vec2 v_TexCoord;\n\
  526. \n\
  527. const float Pi = 3.1415926;\n\
  528. const float Pi_2 = 1.5707963;\n\
  529. const float TwoPi = 6.2831852;\n\
  530. \n\
  531. const float sineConstB = 1.2732396; /* = 4./Pi; */\n\
  532. const float sineConstC = -0.40528476; /* = -4./(Pi*Pi); */\n\
  533. \n\
  534. float fastSine(float angle)\n\
  535. {\n\
  536. float theAngle = mod(angle + Pi, TwoPi) - Pi;\n\
  537. return sineConstB * theAngle + sineConstC * theAngle * abs(theAngle);\n\
  538. }\n\
  539. \n\
  540. const vec4 kStartColor = vec4( 1.0, 1.0, 1.0, 0.0 ); /* white */\n\
  541. const vec4 kMidColor = vec4( 0.97, 1.0, 0.32, 0.0 ); /* yellow */\n\
  542. const vec4 kEndColor = vec4( 0.9, 0.0, 0.0, 0.0 ); /* red */\n\
  543. const float kColorMidPoint = 0.1;\n\
  544. \n\
  545. vec4 flameColor(float aPercent)\n\
  546. {\n\
  547. float thePercent = aPercent;\n\
  548. /* CONSTANTS */\n\
  549. float beginCutoff = 0.4/Duration; /* start slow (not bright white) */\n\
  550. float smokeCutoff = 1.0 - (0.95/Duration); /* end with black, basically */\n\
  551. float alphaCutoff = 1.0 - 0.5/Duration; /* fade out towards the end */\n\
  552. \n\
  553. float alpha = (thePercent < alphaCutoff) ? 1.0 : (1.0-(thePercent-alphaCutoff)/(1.0-alphaCutoff));\n\
  554. vec4 theColor = vec4(0,0,0, alpha * 0.75);\n\
  555. \n\
  556. if (Percent < beginCutoff) {\n\
  557. float colorCutoff = beginCutoff*3.0;\n\
  558. thePercent += mix(colorCutoff, 0.0, Percent/beginCutoff);\n\
  559. }\n\
  560. \n\
  561. if (thePercent < kColorMidPoint) {\n\
  562. float newPercent = thePercent/kColorMidPoint;\n\
  563. theColor += mix(kStartColor, kMidColor, newPercent);\n\
  564. } else {\n\
  565. float newPercent = (thePercent-kColorMidPoint)/(1.0-kColorMidPoint);\n\
  566. theColor += mix(kMidColor, kEndColor, newPercent);\n\
  567. }\n\
  568. \n\
  569. if (Percent > smokeCutoff) {\n\
  570. /* smoke */\n\
  571. float smokeAmount = (Percent - smokeCutoff)/(1.0 - smokeCutoff);\n\
  572. smokeAmount = sqrt(smokeAmount);\n\
  573. smokeAmount *= (0.25+thePercent*thePercent);\n\
  574. theColor = vec4(theColor.rgb * max(0.0, 1.0-smokeAmount), theColor.a);\n\
  575. }\n\
  576. \n\
  577. return theColor;\n\
  578. }\n\
  579. \n\
  580. void main()\n\
  581. {\n\
  582. float realPercent = (Percent-LifeSpan.x)/LifeSpan.y;\n\
  583. bool shouldDiscard = realPercent < 0.0 || realPercent > 1.0;\n\
  584. realPercent = clamp(realPercent, 0.0, 1.0);\n\
  585. \n\
  586. vec4 scaleDirectionVec = vec4(Position-Center,0,0);\n\
  587. \n\
  588. /* ROTATE */\n\
  589. float halfPercent = realPercent/2.0;\n\
  590. vec3 thisRotation = Rotation * RotationMax;\n\
  591. float theRotation = thisRotation.x + thisRotation.z * (halfPercent * (halfPercent + 1.0));\n\
  592. float sinRot = fastSine(theRotation);\n\
  593. float cosRot = fastSine(Pi_2 - theRotation);\n\
  594. mat3 rotMatrix = mat3(cosRot,-sinRot,0, sinRot,cosRot,0, 0,0,1);\n\
  595. vec3 rotatedVec = rotMatrix * scaleDirectionVec.xyz;\n\
  596. \n\
  597. /* SCALE */\n\
  598. float scaleAdjust = (0.1 + 1.0-(1.0-realPercent)*(1.0-realPercent));\n\
  599. vec4 position = vec4(Center,0,1) + vec4(rotatedVec * scaleAdjust * (shouldDiscard ? 0.001 : 1.0), 0);\n\
  600. \n\
  601. /* POSITION */\n\
  602. vec3 thisSpeed = Speed * SpeedMax;\n\
  603. vec4 upVector = vec4(0.0, realPercent*realPercent * -thisSpeed.y, 0.0, 0.0);\n\
  604. position += upVector;\n\
  605. \n\
  606. v_Color = flameColor(realPercent)*Opacity;\n\
  607. gl_Position = MVPMatrix * position;\n\
  608. v_TexCoord = ParticleTexCoord;\n\
  609. }\
  610. ",
  611. fragment: "\
  612. \n\
  613. #ifdef GL_ES\n\
  614. precision mediump float;\n\
  615. #endif\n\
  616. uniform sampler2D ParticleTexture;\n\
  617. \n\
  618. varying vec4 v_Color;\n\
  619. varying vec2 v_TexCoord;\n\
  620. \n\
  621. void main()\n\
  622. {\n\
  623. vec4 texColor = texture2D(ParticleTexture, v_TexCoord);\n\
  624. \n\
  625. texColor *= v_Color;\n\
  626. \n\
  627. gl_FragColor = texColor;\n\
  628. }\n\
  629. "
  630. };
  631. KNWebGLShader.confetti = {
  632. attribNames: ["Rotation", "Speed", "TexCoord", "Center", "Position"],
  633. uniformNames: ["Percent", "Opacity", "ParticleTexture", "MVPMatrix"],
  634. vertex: "\
  635. \n\
  636. precision highp float;\n\
  637. uniform mat4 MVPMatrix;\n\
  638. \n\
  639. uniform float Percent;\n\
  640. uniform mediump float Opacity;\n\
  641. \n\
  642. attribute vec2 Position;\n\
  643. attribute vec2 Center;\n\
  644. attribute vec2 TexCoord;\n\
  645. attribute vec3 Rotation;\n\
  646. attribute vec3 Speed;\n\
  647. \n\
  648. varying vec4 v_Color;\n\
  649. varying vec2 v_TexCoord;\n\
  650. \n\
  651. const float Pi = 3.1415926;\n\
  652. const float Pi_2 = 1.5707963;\n\
  653. const float TwoPi = 6.2831852;\n\
  654. \n\
  655. const float sineConstB = 1.2732396;\n\
  656. const float sineConstC = -0.40528476;\n\
  657. \n\
  658. vec3 fastSine(vec3 angle)\n\
  659. {\n\
  660. vec3 theAngle = mod(angle + Pi, TwoPi) - Pi;\n\
  661. return sineConstB * theAngle + sineConstC * theAngle * abs(theAngle);\n\
  662. }\n\
  663. \n\
  664. mat3 fastRotationMatrix(vec3 theRotation)\n\
  665. {\n\
  666. vec3 sinXYZ = fastSine(theRotation);\n\
  667. vec3 cosXYZ = fastSine(Pi_2 - theRotation);\n\
  668. mat3 rotMatrix = mat3( cosXYZ.y*cosXYZ.z, sinXYZ.x*sinXYZ.y*cosXYZ.z+cosXYZ.x*sinXYZ.z, -cosXYZ.x*sinXYZ.y*cosXYZ.z+sinXYZ.x*sinXYZ.z,\n\
  669. -cosXYZ.y*sinXYZ.z, -sinXYZ.x*sinXYZ.y*sinXYZ.z+cosXYZ.x*cosXYZ.z, cosXYZ.x*sinXYZ.y*sinXYZ.z+sinXYZ.x*cosXYZ.z,\n\
  670. sinXYZ.y, -sinXYZ.x*cosXYZ.y, cosXYZ.x*cosXYZ.y);\n\
  671. return rotMatrix;\n\
  672. }\n\
  673. \n\
  674. void main()\n\
  675. {\n\
  676. /* SCALE */\n\
  677. vec4 originalPosition = vec4(Position, 0, 1);\n\
  678. vec3 scaleDirectionVec = vec3(Position-Center,0);\n\
  679. \n\
  680. /* ROTATE */\n\
  681. mat3 rotMatrix = fastRotationMatrix(Rotation * Percent);\n\
  682. vec3 rotatedVec = scaleDirectionVec * rotMatrix;\n\
  683. vec4 position = vec4(Center,0,1) + vec4(rotatedVec,0);\n\
  684. \n\
  685. float colorAdjust = abs((rotMatrix * vec3(0,0,1)).z);\n\
  686. \n\
  687. float speedAdjust = Percent;\n\
  688. position += vec4(Speed, 0) * speedAdjust;\n\
  689. \n\
  690. /* output */\n\
  691. gl_Position = MVPMatrix * position;\n\
  692. v_Color = vec4(vec3(colorAdjust), Opacity);\n\
  693. v_TexCoord = TexCoord;\n\
  694. }\
  695. ",
  696. fragment: "\
  697. \n\
  698. precision mediump float;\n\
  699. \n\
  700. uniform sampler2D ParticleTexture;\n\
  701. //uniform float Opacity;\n\
  702. \n\
  703. varying vec4 v_Color;\n\
  704. varying vec2 v_TexCoord;\n\
  705. \n\
  706. void main()\n\
  707. {\n\
  708. vec4 texColor = texture2D(ParticleTexture, v_TexCoord);\n\
  709. \n\
  710. texColor *= v_Color;\n\
  711. //texColor.a = Opacity;\n\
  712. \n\
  713. gl_FragColor = texColor;\n\
  714. }\
  715. "
  716. };
  717. KNWebGLShader.diffuse = {
  718. attribNames: ["Rotation", "Speed", "TexCoord", "Center", "Position", "LifeSpan"],
  719. uniformNames: ["Percent", "Opacity", "ParticleTexture", "MVPMatrix", "RotationMax", "SpeedMax"],
  720. vertex: "\
  721. \n\
  722. precision highp float;\n\
  723. \n\
  724. uniform mat4 MVPMatrix;\n\
  725. \n\
  726. uniform float Percent;\n\
  727. uniform mediump float Opacity;\n\
  728. \n\
  729. attribute vec2 Position;\n\
  730. attribute vec2 Center;\n\
  731. attribute vec2 TexCoord;\n\
  732. \n\
  733. attribute mediump vec3 Rotation;\n\
  734. uniform mediump float RotationMax;\n\
  735. attribute mediump vec3 Speed;\n\
  736. uniform mediump float SpeedMax;\n\
  737. attribute mediump vec2 LifeSpan;\n\
  738. \n\
  739. varying vec4 v_Color;\n\
  740. varying vec2 v_TexCoord;\n\
  741. \n\
  742. const float Pi = 3.1415926;\n\
  743. const float Pi_2 = 1.5707963;\n\
  744. const float TwoPi = 6.2831852;\n\
  745. \n\
  746. const float sineConstB = 1.2732396;\n\
  747. const float sineConstC = -0.40528476;\n\
  748. \n\
  749. vec3 fastSine(vec3 angle)\n\
  750. {\n\
  751. vec3 theAngle = mod(angle + Pi, TwoPi) - Pi;\n\
  752. return sineConstB * theAngle + sineConstC * theAngle * abs(theAngle);\n\
  753. }\n\
  754. \n\
  755. mat3 fastRotationMatrix(vec3 theRotation)\n\
  756. {\n\
  757. vec3 sinXYZ = fastSine(theRotation);\n\
  758. vec3 cosXYZ = fastSine(Pi_2 - theRotation);\n\
  759. mat3 rotMatrix = mat3( cosXYZ.y*cosXYZ.z, sinXYZ.x*sinXYZ.y*cosXYZ.z+cosXYZ.x*sinXYZ.z, -cosXYZ.x*sinXYZ.y*cosXYZ.z+sinXYZ.x*sinXYZ.z,\n\
  760. -cosXYZ.y*sinXYZ.z, -sinXYZ.x*sinXYZ.y*sinXYZ.z+cosXYZ.x*cosXYZ.z, cosXYZ.x*sinXYZ.y*sinXYZ.z+sinXYZ.x*cosXYZ.z,\n\
  761. sinXYZ.y, -sinXYZ.x*cosXYZ.y, cosXYZ.x*cosXYZ.y);\n\
  762. return rotMatrix;\n\
  763. }\n\
  764. \n\
  765. void main()\n\
  766. {\n\
  767. float realPercent = (Percent-LifeSpan.x)/LifeSpan.y;\n\
  768. float doDiscard = (realPercent > 1.0) ? 0.0 : 1.0;\n\
  769. realPercent = clamp(realPercent, 0.0,1.0);\n\
  770. float revPercent = 1.0-realPercent;\n\
  771. \n\
  772. //SCALE\n\
  773. vec4 originalPosition = vec4(Position, 0, 1);\n\
  774. vec3 scaleDirectionVec = vec3(Position-Center,0);\n\
  775. \n\
  776. //ROTATE\n\
  777. vec3 thisRotation = Rotation * RotationMax;\n\
  778. mat3 rotMatrix = fastRotationMatrix(thisRotation * realPercent);\n\
  779. vec3 rotatedVec = scaleDirectionVec * rotMatrix;\n\
  780. vec4 position = vec4(Center,0,1) + vec4(rotatedVec,0) * doDiscard;\n\
  781. \n\
  782. vec3 thisSpeed = Speed * SpeedMax;\n\
  783. float l2r = -thisSpeed.x/abs(thisSpeed.x);\n\
  784. float reverseVector = l2r*(thisSpeed.x+abs(thisSpeed.y)) * realPercent/8.0;\n\
  785. \n\
  786. float speedMultiplier = 1.-pow(revPercent, 2.0);\n\
  787. vec3 dist = thisSpeed * speedMultiplier;\n\
  788. dist.x += reverseVector;\n\
  789. position.xyz += dist;\n\
  790. \n\
  791. float colorAdjust = abs((rotMatrix * vec3(0,0,1)).z);\n\
  792. \n\
  793. //output\n\
  794. gl_Position = MVPMatrix * position;\n\
  795. v_Color = vec4(vec3(colorAdjust), 1) * (revPercent*Opacity);\n\
  796. v_TexCoord = TexCoord;\n\
  797. }\
  798. ",
  799. fragment: "\
  800. \n\
  801. precision mediump float;\n\
  802. \n\
  803. uniform sampler2D Texture;\n\
  804. uniform float Opacity;\n\
  805. \n\
  806. varying vec4 v_Color;\n\
  807. varying vec2 v_TexCoord;\n\
  808. \n\
  809. void main()\n\
  810. {\n\
  811. vec4 texColor = texture2D(Texture, v_TexCoord);\n\
  812. \n\
  813. texColor *= v_Color;\n\
  814. \n\
  815. gl_FragColor = texColor;\n\
  816. }\
  817. "
  818. };
  819. KNWebGLShader.fireworks = {
  820. attribNames: ["Color", "Speed", "LifeSpan", "Scale", "ParticleTexCoord", "Center", "Position"],
  821. uniformNames: ["Percent", "PreviousPercent", "Gravity", "StartScale", "ShouldSparkle", "SparklePeriod", "ParticleBurstTiming", "PreviousParticleBurstTiming", "SpeedMax", "ParticleTexture", "Opacity", "MVPMatrix"],
  822. vertex: "\
  823. \n\
  824. precision highp float;\n\
  825. \n\
  826. uniform mat4 MVPMatrix;\n\
  827. \n\
  828. uniform float Percent;\n\
  829. uniform float PreviousPercent;\n\
  830. uniform float Gravity;\n\
  831. uniform float StartScale;\n\
  832. uniform float ShouldSparkle;\n\
  833. uniform float SparklePeriod;\n\
  834. uniform float ParticleBurstTiming;\n\
  835. uniform float PreviousParticleBurstTiming;\n\
  836. uniform float SpeedMax;\n\
  837. \n\
  838. attribute vec2 Position;\n\
  839. attribute vec2 Center;\n\
  840. attribute vec2 ParticleTexCoord;\n\
  841. \n\
  842. attribute vec4 Color;\n\
  843. attribute vec3 Speed;\n\
  844. attribute vec2 LifeSpan;\n\
  845. attribute float Scale;\n\
  846. \n\
  847. varying vec4 v_Color;\n\
  848. varying vec2 v_TexCoord;\n\
  849. \n\
  850. void main()\n\
  851. {\n\
  852. float realPercent = (Percent-LifeSpan.x)/LifeSpan.y;\n\
  853. realPercent = clamp(realPercent, 0.0, 1.0);\n\
  854. \n\
  855. float prevRealPercent = (PreviousPercent-LifeSpan.x)/LifeSpan.y;\n\
  856. prevRealPercent = clamp(prevRealPercent, 0.0,1.0);\n\
  857. \n\
  858. vec4 center = vec4(Center,0,1);\n\
  859. vec4 scaleDirectionVec = vec4(Position-Center,0,0);\n\
  860. \n\
  861. // TRANSLATE\n\
  862. vec3 translation = Speed * (SpeedMax * ParticleBurstTiming); // (1.0-pow(1.0-realPercent, ExplosionPower));\n\
  863. translation.y -= Gravity * (Percent - LifeSpan.x); // Gravity is in terms of global percent, not particle system percent\n\
  864. \n\
  865. vec3 prevTranslation = Speed * (SpeedMax * PreviousParticleBurstTiming);\n\
  866. prevTranslation.y -= Gravity * (PreviousPercent - LifeSpan.x); // Gravity is in terms of global percent, not particle system percent\n\
  867. \n\
  868. vec3 blurOffset = translation - prevTranslation; // Blur in direction of velocity\n\
  869. \n\
  870. // project centerVec onto translationOffset to get direction\n\
  871. blurOffset *= (dot(blurOffset, scaleDirectionVec.xyz) >= 0.0 ? 1.0 : -1.0);\n\
  872. \n\
  873. center.xyz += translation;\n\
  874. \n\
  875. // SCALE\n\
  876. float scalePercent = (1.0-(1.0-realPercent)*(1.0-realPercent));\n\
  877. float scaleAdjust = mix(StartScale, Scale, scalePercent);\n\
  878. // scale down to zero, unless we're sparkling\n\
  879. scaleAdjust *= (ShouldSparkle>0.5 ? 0.25 : 1.0-scalePercent);\n\
  880. vec4 position = center + scaleDirectionVec * scaleAdjust;\n\
  881. position += vec4(blurOffset,0);\n\
  882. \n\
  883. // SPARKLE\n\
  884. float sparkleOpacity = fract(realPercent*realPercent * SparklePeriod);\n\
  885. sparkleOpacity = smoothstep(0.0, 1.0, sparkleOpacity);\n\
  886. \n\
  887. // COLOR\n\
  888. vec4 color = mix(vec4(1), Color, scalePercent * (ShouldSparkle<0.5 ? 1.0 : 0.5)); // white to color\n\
  889. color *= (ShouldSparkle<0.5 ? 1.0 : sparkleOpacity); // apply sparkle opacity\n\
  890. color *= (realPercent>=1.0 ? 0.0 : 1.0);\n\
  891. v_Color = color;\n\
  892. \n\
  893. gl_Position = MVPMatrix * position;\n\
  894. v_TexCoord = ParticleTexCoord;\n\
  895. }\
  896. ",
  897. fragment: "\
  898. \n\
  899. precision mediump float;\n\
  900. \n\
  901. uniform sampler2D ParticleTexture;\n\
  902. uniform float Opacity;\n\
  903. \n\
  904. varying vec4 v_Color;\n\
  905. varying vec2 v_TexCoord;\n\
  906. //varying float particleTexPercent;\n\
  907. \n\
  908. void main()\n\
  909. {\n\
  910. vec4 texColor = texture2D(ParticleTexture, v_TexCoord);\n\
  911. \n\
  912. texColor *= v_Color * Opacity;\n\
  913. //texColor.a *= Opacity;\n\
  914. \n\
  915. //texColor = vec4(v_TexCoord, 0, 1);\n\
  916. \n\
  917. gl_FragColor = texColor;\n\
  918. }\
  919. "
  920. };
  921. KNWebGLShader.fireworkstrails = {
  922. attribNames: [ "Position", "TexCoord"],
  923. uniformNames: ["Texture", "Opacity", "NoiseAmount", "NoiseSeed", "NoiseMax", "MVPMatrix"],
  924. vertex: "\
  925. \n\
  926. precision highp float;\n\
  927. \n\
  928. uniform mat4 MVPMatrix;\n\
  929. \n\
  930. attribute vec2 Position;\n\
  931. attribute vec2 TexCoord;\n\
  932. \n\
  933. varying vec2 v_TexCoord;\n\
  934. \n\
  935. void main()\n\
  936. {\n\
  937. gl_Position = MVPMatrix * vec4(Position, 0,1);\n\
  938. v_TexCoord = TexCoord;\n\
  939. }\
  940. ",
  941. fragment: "\
  942. precision mediump float;\n\
  943. \n\
  944. uniform sampler2D Texture;\n\
  945. uniform float Opacity;\n\
  946. uniform float NoiseAmount;\n\
  947. uniform vec2 NoiseSeed;\n\
  948. uniform float NoiseMax;\n\
  949. \n\
  950. //varying vec4 v_Color;\n\
  951. varying vec2 v_TexCoord;\n\
  952. //varying float particleTexPercent;\n\
  953. \n\
  954. float rand(vec2 co){\n\
  955. return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);\n\
  956. }\n\
  957. \n\
  958. float inverseSquare(float a) {\n\
  959. return 1.0-(1.0-a)*(1.0-a);\n\
  960. }\n\
  961. \n\
  962. void main()\n\
  963. {\n\
  964. vec4 texColor = texture2D(Texture, v_TexCoord);\n\
  965. \n\
  966. //texColor = bloom(texColor);\n\
  967. \n\
  968. // Dither transparency to add noise\n\
  969. float randomNoise = NoiseMax*rand(v_TexCoord*NoiseSeed);\n\
  970. float randomAmount = NoiseAmount * 1.5*max(0.0, texColor.a-0.3333);\n\
  971. \n\
  972. float thisOpacity = Opacity * mix(1.0, randomNoise, randomAmount);\n\
  973. texColor *= thisOpacity;\n\
  974. \n\
  975. //texColor = vec4(v_TexCoord, 0, 1);\n\
  976. \n\
  977. gl_FragColor = texColor;\n\
  978. }\
  979. "
  980. };
  981. KNWebGLShader.horizontalGaussianBlur = {
  982. attribNames: [ "Position"],
  983. uniformNames: ["Texture", "TextureSize", "MVPMatrix"],
  984. vertex: "\
  985. \n\
  986. precision highp float;\n\
  987. \n\
  988. uniform mat4 MVPMatrix;\n\
  989. \n\
  990. attribute vec2 Position;\n\
  991. \n\
  992. void main()\n\
  993. {\n\
  994. gl_Position = MVPMatrix * vec4(Position, 0, 1);\n\
  995. }\
  996. ",
  997. fragment: "\
  998. precision highp float;\n\
  999. \n\
  1000. uniform sampler2D Texture;\n\
  1001. uniform vec2 TextureSize;\n\
  1002. \n\
  1003. const vec2 offset1 = vec2(1.3846153846, 0);\n\
  1004. const vec2 offset2 = vec2(3.2307692308, 0);\n\
  1005. const float weight0 = 0.2270270270;\n\
  1006. const float weight1 = 0.3162162162;\n\
  1007. const float weight2 = 0.0702702703;\n\
  1008. \n\
  1009. void main()\n\
  1010. {\n\
  1011. vec4 color = texture2D(Texture, gl_FragCoord.xy*TextureSize) * weight0;\n\
  1012. \n\
  1013. color += texture2D(Texture, (gl_FragCoord.xy + offset1)*TextureSize) * weight1;\n\
  1014. color += texture2D(Texture, (gl_FragCoord.xy - offset1)*TextureSize) * weight1;\n\
  1015. \n\
  1016. color += texture2D(Texture, (gl_FragCoord.xy + offset2)*TextureSize) * weight2;\n\
  1017. color += texture2D(Texture, (gl_FragCoord.xy - offset2)*TextureSize) * weight2;\n\
  1018. \n\
  1019. gl_FragColor = color;\n\
  1020. }\
  1021. "
  1022. };
  1023. KNWebGLShader.verticalGaussianBlur = {
  1024. attribNames: [ "Position"],
  1025. uniformNames: ["Texture", "TextureSize", "MVPMatrix"],
  1026. vertex: "\
  1027. \n\
  1028. precision highp float;\n\
  1029. \n\
  1030. uniform mat4 MVPMatrix;\n\
  1031. \n\
  1032. attribute vec2 Position;\n\
  1033. \n\
  1034. void main()\n\
  1035. {\n\
  1036. gl_Position = MVPMatrix * vec4(Position, 0, 1);\n\
  1037. }\
  1038. ",
  1039. fragment: "\
  1040. precision highp float;\n\
  1041. \n\
  1042. uniform sampler2D Texture;\n\
  1043. uniform vec2 TextureSize;\n\
  1044. \n\
  1045. const vec2 offset1 = vec2(0, 1.3846153846);\n\
  1046. const vec2 offset2 = vec2(0, 3.2307692308);\n\
  1047. const float weight0 = 0.2270270270;\n\
  1048. const float weight1 = 0.3162162162;\n\
  1049. const float weight2 = 0.0702702703;\n\
  1050. \n\
  1051. void main()\n\
  1052. {\n\
  1053. vec4 color = texture2D(Texture, gl_FragCoord.xy*TextureSize) * weight0;\n\
  1054. \n\
  1055. color += texture2D(Texture, (gl_FragCoord.xy + offset1)*TextureSize) * weight1;\n\
  1056. color += texture2D(Texture, (gl_FragCoord.xy - offset1)*TextureSize) * weight1;\n\
  1057. \n\
  1058. color += texture2D(Texture, (gl_FragCoord.xy + offset2)*TextureSize) * weight2;\n\
  1059. color += texture2D(Texture, (gl_FragCoord.xy - offset2)*TextureSize) * weight2;\n\
  1060. \n\
  1061. gl_FragColor = color;\n\
  1062. }\
  1063. "
  1064. };
  1065. KNWebGLShader.bloom = {
  1066. attribNames: [ "Position", "TexCoord"],
  1067. uniformNames: ["Texture", "BlurTexture", "BloomAmount", "MVPMatrix"],
  1068. vertex: "\
  1069. \n\
  1070. precision highp float;\n\
  1071. \n\
  1072. uniform mat4 MVPMatrix;\n\
  1073. \n\
  1074. attribute vec2 Position;\n\
  1075. attribute vec2 TexCoord;\n\
  1076. \n\
  1077. varying vec2 v_TexCoord;\n\
  1078. \n\
  1079. void main()\n\
  1080. {\n\
  1081. v_TexCoord = TexCoord;\n\
  1082. gl_Position = MVPMatrix * vec4(Position, 0, 1);\n\
  1083. }\
  1084. ",
  1085. fragment: "\
  1086. precision mediump float;\n\
  1087. \n\
  1088. uniform sampler2D Texture;\n\
  1089. uniform sampler2D BlurTexture;\n\
  1090. uniform float BloomAmount;\n\
  1091. \n\
  1092. varying vec2 v_TexCoord;\n\
  1093. \n\
  1094. void main()\n\
  1095. {\n\
  1096. vec4 color = texture2D(Texture, v_TexCoord);\n\
  1097. vec4 blurColor = texture2D(BlurTexture, v_TexCoord);\n\
  1098. \n\
  1099. color += (blurColor + color) * BloomAmount;\n\
  1100. gl_FragColor = color;\n\
  1101. }\
  1102. "
  1103. };
  1104. KNWebGLShader.shimmerObject = {
  1105. attribNames: [ "Position", "Center", "TexCoord", "Color", "Speed"],
  1106. uniformNames: ["Percent", "Opacity", "RotationMatrix", "SpeedMax", "Texture", "MVPMatrix"],
  1107. vertex: "\
  1108. \n\
  1109. precision highp float;\n\
  1110. \n\
  1111. uniform mat4 MVPMatrix;\n\
  1112. uniform float Percent;\n\
  1113. uniform float Opacity;\n\
  1114. \n\
  1115. uniform mat3 RotationMatrix;\n\
  1116. \n\
  1117. attribute vec2 Position;\n\
  1118. attribute vec2 Center;\n\
  1119. attribute vec2 TexCoord;\n\
  1120. attribute vec4 Color;\n\
  1121. \n\
  1122. attribute vec3 Speed;\n\
  1123. uniform float SpeedMax;\n\
  1124. \n\
  1125. varying vec4 v_Color;\n\
  1126. varying vec2 v_TexCoord;\n\
  1127. \n\
  1128. void main()\n\
  1129. {\n\
  1130. float thisPercent = Percent;\n\
  1131. float invPercent = 1.0-thisPercent;\n\
  1132. float thisPercent2 = thisPercent*thisPercent;\n\
  1133. \n\
  1134. /* CENTER */\n\
  1135. vec3 scaleDirectionVec = vec3((Position.x-Center.x),(Position.y-Center.y),0);\n\
  1136. \n\
  1137. /* ROTATE */\n\
  1138. vec3 rotatedVec = RotationMatrix * scaleDirectionVec.xyz;\n\
  1139. \n\
  1140. /* SCALE */\n\
  1141. float scale = invPercent;\n\
  1142. vec4 position = vec4(Center.xy,0,1) + vec4(rotatedVec,0) * scale;\n\
  1143. \n\
  1144. vec3 thisSpeed = Speed * SpeedMax;\n\
  1145. position.xyz += thisSpeed * thisPercent*(3.0 + mix(thisPercent2*thisPercent, 1.0-invPercent*invPercent, thisPercent2));\n\
  1146. \n\
  1147. vec4 outColor = Color;\n\
  1148. outColor = vec4(Opacity);\n\
  1149. \n\
  1150. /* output */\n\
  1151. gl_Position = MVPMatrix * position;\n\
  1152. v_Color = outColor;\n\
  1153. v_TexCoord = TexCoord;\n\
  1154. }\n\
  1155. ",
  1156. fragment: "\
  1157. precision mediump float;\n\
  1158. \n\
  1159. uniform sampler2D Texture;\n\
  1160. \n\
  1161. varying vec4 v_Color;\n\
  1162. varying vec2 v_TexCoord;\n\
  1163. \n\
  1164. void main()\n\
  1165. {\n\
  1166. vec4 color = texture2D(Texture, v_TexCoord);\n\
  1167. \n\
  1168. color *= v_Color;\n\
  1169. \n\
  1170. gl_FragColor = color;\n\
  1171. }\
  1172. "
  1173. };
  1174. KNWebGLShader.shimmerParticle = {
  1175. attribNames: [ "Position", "Center", "ParticleTexCoord", "Color", "LifeSpan", "Speed", "Scale"],
  1176. uniformNames: ["Percent", "Opacity", "ParticleScalePercent", "RotationMatrix", "SpeedMax", "ParticleTexture", "MVPMatrix"],
  1177. vertex: "\
  1178. \n\
  1179. precision highp float;\n\
  1180. \n\
  1181. uniform mat4 MVPMatrix;\n\
  1182. uniform float Percent;\n\
  1183. uniform float Opacity;\n\
  1184. \n\
  1185. uniform float ParticleScalePercent;\n\
  1186. uniform mat3 RotationMatrix;\n\
  1187. \n\
  1188. attribute vec2 Position;\n\
  1189. attribute vec2 Center;\n\
  1190. attribute vec2 ParticleTexCoord;\n\
  1191. attribute vec4 Color;\n\
  1192. attribute vec2 LifeSpan;\n\
  1193. \n\
  1194. attribute vec3 Speed;\n\
  1195. uniform float SpeedMax;\n\
  1196. attribute float Scale;\n\
  1197. \n\
  1198. varying vec4 v_Color;\n\
  1199. varying vec2 v_TexCoord;\n\
  1200. \n\
  1201. float scaleUpDown(float x) {\n\
  1202. float result = 1.0 - abs(2.0*(x-0.5));\n\
  1203. result *= result;\n\
  1204. return result;\n\
  1205. }\n\
  1206. \n\
  1207. void main()\n\
  1208. {\n\
  1209. /* LIFESPAN */\n\
  1210. float realPercent = (Percent-LifeSpan.x)/LifeSpan.y;\n\
  1211. float doDiscard = (realPercent > 1.0 || realPercent < 0.0) ? 0.0 : 1.0;\n\
  1212. realPercent = clamp(realPercent, 0.0,1.0);\n\
  1213. float realPercent2 = realPercent*realPercent;\n\
  1214. float invPercent2 = 1.0-realPercent;\n\
  1215. invPercent2 *= invPercent2;\n\
  1216. \n\
  1217. vec3 scaleDirectionVec = vec3((Position.x-Center.x),(Position.y-Center.y),0);\n\
  1218. \n\
  1219. /* ROTATE */\n\
  1220. vec3 rotatedVec = RotationMatrix * scaleDirectionVec.xyz;\n\
  1221. \n\
  1222. /* SCALE */\n\
  1223. float scalePercent = (LifeSpan.x <= 0.001 ? ParticleScalePercent : scaleUpDown(realPercent));\n\
  1224. float scale = scalePercent * Scale * doDiscard;\n\
  1225. vec4 position = vec4(Center,0,1) + vec4(rotatedVec,0) * scale;\n\
  1226. \n\
  1227. vec3 thisSpeed = Speed * SpeedMax;\n\
  1228. position.xyz += thisSpeed * realPercent*(3.0 + mix(realPercent*realPercent2, 1.0-invPercent2, realPercent2));\n\
  1229. \n\
  1230. // Only adjust opacity on particles that last the duration of the animation\n\
  1231. float thisOpacity = (LifeSpan.x <= 0.001 ? Opacity : 1.0);\n\
  1232. vec4 color = vec4(Color.rgb, 1) * thisOpacity;\n\
  1233. \n\
  1234. v_Color = color;\n\
  1235. v_TexCoord = ParticleTexCoord;\n\
  1236. gl_Position = MVPMatrix * position;\n\
  1237. }\n\
  1238. ",
  1239. fragment: "\
  1240. precision mediump float;\n\
  1241. \n\
  1242. uniform sampler2D ParticleTexture;\n\
  1243. \n\
  1244. varying vec4 v_Color;\n\
  1245. varying vec2 v_TexCoord;\n\
  1246. \n\
  1247. void main()\n\
  1248. {\n\
  1249. vec4 color = texture2D(ParticleTexture, v_TexCoord);\n\
  1250. \n\
  1251. color *= v_Color;\n\
  1252. \n\
  1253. gl_FragColor = color;\n\
  1254. }\
  1255. "
  1256. };
  1257. KNWebGLShader.sparkle = {
  1258. attribNames: ["Scale", "LifeSpan", "Speed", "ParticleTexCoord", "Center", "Position"],
  1259. uniformNames: ["Percent", "Opacity", "Color", "SpeedMax", "ParticleTexture", "MVPMatrix"],
  1260. vertex: "\
  1261. \n\
  1262. precision highp float;\n\
  1263. \n\
  1264. uniform mat4 MVPMatrix;\n\
  1265. uniform float Percent;\n\
  1266. \n\
  1267. attribute vec2 Position;\n\
  1268. attribute vec2 Center;\n\
  1269. uniform float Opacity;\n\
  1270. attribute vec2 ParticleTexCoord;\n\
  1271. uniform vec4 Color;\n\
  1272. \n\
  1273. attribute mediump vec3 Speed;\n\
  1274. uniform mediump float SpeedMax;\n\
  1275. attribute mediump float Scale;\n\
  1276. attribute mediump vec2 LifeSpan;\n\
  1277. \n\
  1278. varying vec4 v_Color;\n\
  1279. varying vec2 v_TexCoord;\n\
  1280. \n\
  1281. float ReverseSquareOfFloat(float f) {\n\
  1282. return 1.0 - (1.0-f)*(1.0-f);\n\
  1283. }\n\
  1284. \n\
  1285. void main()\n\
  1286. {\n\
  1287. float doDiscard = 0.0;\n\
  1288. float realPercent = (Percent-LifeSpan.x)/LifeSpan.y;\n\
  1289. if (realPercent < 0.0 || realPercent > 1.0) {\n\
  1290. doDiscard = 1.0;\n\
  1291. realPercent = 1.0;\n\
  1292. }\n\
  1293. \n\
  1294. vec4 position;\n\
  1295. vec4 scaleDirectionVec = vec4((Position.x-Center.x),(Position.y-Center.y),0,0);\n\
  1296. \n\
  1297. // SCALE\n\
  1298. float scaleAdjust = realPercent;\n\
  1299. if (scaleAdjust < 0.1) {\n\
  1300. scaleAdjust /= 0.1;\n\
  1301. scaleAdjust = sqrt(scaleAdjust);\n\
  1302. } else {\n\
  1303. scaleAdjust = 1.0-(scaleAdjust-0.1)/0.9;\n\
  1304. scaleAdjust = scaleAdjust*scaleAdjust*scaleAdjust;\n\
  1305. }\n\
  1306. scaleAdjust *= (doDiscard==0.0 ? 1.0 : 0.0);\n\
  1307. position = vec4(Center,0,1) + scaleDirectionVec * scaleAdjust * Scale;\n\
  1308. \n\
  1309. // POSITION\n\
  1310. vec3 thisSpeed = Speed * SpeedMax;\n\
  1311. position += vec4(thisSpeed, 0) * realPercent;\n\
  1312. \n\
  1313. float invPercent = 1.0 - realPercent;\n\
  1314. vec3 rgbColor = mix(Color.rgb, vec3(1,1,1), invPercent*invPercent*invPercent);\n\
  1315. \n\
  1316. /* output */\n\
  1317. gl_Position = MVPMatrix * position;\n\
  1318. v_Color = vec4(rgbColor, (1.0-realPercent*realPercent)*Opacity);\n\
  1319. v_TexCoord = ParticleTexCoord;\n\
  1320. }\
  1321. ",
  1322. fragment: "\
  1323. \n\
  1324. precision mediump float;\n\
  1325. \n\
  1326. uniform sampler2D ParticleTexture;\n\
  1327. \n\
  1328. varying vec4 v_Color;\n\
  1329. varying vec2 v_TexCoord;\n\
  1330. \n\
  1331. void main()\n\
  1332. {\n\
  1333. vec4 texColor = texture2D(ParticleTexture, v_TexCoord);\n\
  1334. \n\
  1335. texColor *= v_Color;\n\
  1336. \n\
  1337. gl_FragColor = texColor;\n\
  1338. }\
  1339. "
  1340. };