Guest User

Untitled

a guest
Sep 30th, 2018
196
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Rotate Animations
  2. --
  3. -- Examples:
  4. -- setRotateAnimation(Objects["2040_Bush_light_17"], 1.0, 0.3, 0.2, 3, graphics.noise)
  5. -- setRotateAnimation(Objects["2040_Tree_b_light_01"], 1.0, 0.0, 0.2, 3, math.sin)
  6. -- setRotateAnimation(Objects["2040_VG2"], 1.0, 0.0, 0.2, 3, function(a)return math.sin(graphics.noise(a)) end)
  7. --
  8. -- setRotateAnimation(object, speed, offset, strength, direction, func)
  9. -- func can be: function(a)return math.sin(graphics.noise( a )) end
  10. -- setRotateAnimationToStrength(objectnamestring, speed, offset, strengthFrom, strengthTo, direction, function, time) -> smooth Rotation From - To Strength
  11. -- direction/pinned side:
  12. -- 1 - down, 2 - up, 3 - left, 4 - right
  13. --
  14.  
  15. local matrix = {_TYPE='module', _NAME='matrix', _VERSION='0.2.11.20120416'}
  16. local matrix_meta = { }
  17.  
  18. function matrix:new( rows, columns, value )
  19. if type( rows ) == "table" then
  20. if type(rows[1]) ~= "table" then
  21. return setmetatable( {{rows[1]},{rows[2]},{rows[3]}},matrix_meta )
  22. end
  23. return setmetatable( rows,matrix_meta )
  24. end
  25. local mtx = {}
  26. local value = value or 0
  27. if columns == "I" then
  28. for i = 1,rows do
  29. mtx[i] = {}
  30. for j = 1,rows do
  31. if i == j then
  32. mtx[i][j] = 1
  33. else
  34. mtx[i][j] = 0
  35. end
  36. end
  37. end
  38. else
  39. for i = 1,rows do
  40. mtx[i] = {}
  41. for j = 1,columns do
  42. mtx[i][j] = value
  43. end
  44. end
  45. end
  46. return setmetatable( mtx,matrix_meta )
  47. end
  48.  
  49. function matrix.mul( m1, m2 )
  50. local mtx = {}
  51. for i = 1,#m1 do
  52. mtx[i] = {}
  53. for j = 1,#m2[1] do
  54. local num = m1[i][1] * m2[1][j]
  55. for n = 2,#m1[1] do
  56. num = num + m1[i][n] * m2[n][j]
  57. end
  58. mtx[i][j] = num
  59. end
  60. end
  61. return setmetatable( mtx, matrix_meta )
  62. end
  63.  
  64. function matrix.type( mtx )
  65. local e = mtx[1][1]
  66. if type(e) == "table" then
  67. if e.type then
  68. return e:type()
  69. end
  70. return "tensor"
  71. end
  72. return "number"
  73. end
  74.  
  75. local num_copy = function( num )
  76. return num
  77. end
  78. local t_copy = function( t )
  79. local newt = setmetatable( {}, getmetatable( t ) )
  80. for i,v in ipairs( t ) do
  81. newt[i] = v
  82. end
  83. return newt
  84. end
  85.  
  86. function matrix.transpose( m1 )
  87. local docopy = matrix.type( m1 ) == "number" and num_copy or t_copy
  88. local mtx = {}
  89. for i = 1,#m1[1] do
  90. mtx[i] = {}
  91. for j = 1,#m1 do
  92. mtx[i][j] = docopy( m1[j][i] )
  93. end
  94. end
  95. return setmetatable( mtx, matrix_meta )
  96. end
  97.  
  98. function matrix.tofloat( m1 )
  99. local mtx = {}
  100. local pos = 1
  101. for i = 1,#m1 do
  102. for j = 1,#m1[1] do
  103. mtx[pos] = m1[i][j]
  104. pos=pos+1
  105. end
  106. end
  107. return mtx
  108. end
  109.  
  110. matrix_meta.__mul = matrix.mul
  111. matrix_meta.__index = {tofloat = matrix.tofloat, transpose = matrix.transpose}
  112.  
  113. setmetatable( matrix, { __call = function( ... ) return matrix.new( ... ) end } )
  114.  
  115. -- End of matrix functions
  116.  
  117. local count = 15
  118.  
  119. function readAll(file)
  120. local f = io.open(file, "rb")
  121. local content = f:read("*all")
  122. f:close()
  123. return content
  124. end
  125.  
  126. local animBuffer = {}
  127. local animIndexBuffer = {}
  128. for k = 1,4 do
  129. local verticesList = {}
  130. local indexList = {}
  131.  
  132. local factor = 1.0 / count
  133. for i = 0, count - 1 do
  134. local verts = nil
  135. if k == 1 or k == 2 then
  136. local up = math.pow(factor * (count - (i+1)),2)
  137. local down = math.pow(factor * (count - i),2)
  138. if k == 2 then
  139. up = math.pow(factor * (i+1),2)
  140. down = math.pow(factor * i,2)
  141. end
  142. verts = {
  143. 0, factor * i, 0, factor * i, down,
  144. 1, factor * i, 1, factor * i, down,
  145. 1, factor * (i+1), 1, factor * (i+1), up,
  146. 0, factor * (i+1), 0, factor * (i+1), up
  147. }
  148. elseif k==3 or k==4 then
  149. local up = math.pow(factor * (count - (i+1)),2)
  150. local down = math.pow(factor * (count - i),2)
  151. if k == 3 then
  152. up = math.pow(factor * (i+1),2)
  153. down = math.pow(factor * i,2)
  154. end
  155. verts = {
  156. factor * i, 0, factor * i, 0, down,
  157. factor * i, 1, factor * i, 1, down,
  158. factor * (i+1), 1, factor * (i+1), 1, up,
  159. factor * (i+1), 0, factor * (i+1), 0, up
  160. }
  161. end
  162. local index = {
  163. i*4, i*4 + 1, i*4 + 2, i*4, i*4 + 2, i*4 + 3
  164. }
  165. for j = 1, #verts do
  166. table.insert(verticesList, verts[j])
  167. end
  168. for j = 1, #index do
  169. table.insert(indexList, index[j])
  170. end
  171. end
  172.  
  173. animBuffer[k] = graphics.createBuffer(false, 4 * count * 4) -- 4 bytes * 4 points * rects
  174. animBuffer[k]:update(verticesList)
  175. animIndexBuffer[k] = graphics.createIndexBuffer(false, 4 * count * 4) -- 4 bytes * 4 points * rects
  176. animIndexBuffer[k]:update(indexList)
  177. end
  178.  
  179. local animShader = shaderCompile([[VSCBINmxcAAHicvVhfbFtXGf/u9Y3j3Dj2bZe1JQ3pXdNtTpemtuOkLZe5af61QVkT4qwUNtVNEyfxSGzLdkzDNuN10hilgyBNqA99yANPC0I88IhS6CQU/okx8QB7KlI1TfAyVRUqAsV83/lzfePZXQtoJ7095/ud75zvzznfOd/xudjg2Fl9YSm3ZBVyVl+/lUnnrLAVsSYnYmPTYxNnrZUCo6dHzg9NTEwNW7l8NpFayC9aIQcasvpPHLOOHl1YioeCQb0zOT+XmDdPj8dHYnomm5hN5pLplLmcmEuuLGfM+aX0TN5ydCylv5ExkynEOhOpueS8fvToWCqzkjfHZ1bTK/kvIF1IzIZNVE42VwrYYhOZUiN9JZWcT2eXzeWZfMRcLmT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Ylcs/j8uttGLIFMTFeYExsSBTNEqry0mG3ENw1dhf7xkHu1BtSFfQihZvpDWwONfH2SqsJcH0vyVg3hJvjocr4RPd8yxvl6Wt5eftF492Hlg7739U1d/6S9cJKU/Vit5O57jcAO/9/DLqzxv94i8/bpwDs/byyxvv43NV6ByfzqLIr5vC0eTDM0Rq9TXIuKvVtnh6DhtOuFzx0PgLcH3UwD7vqGyJWpDyH6vzmLK8rCrNfa/5Lqu+rku5dDdgo/mNavGy3lx/PSpqdMj08DzY/qJYQC/p2FnfkzzfI2GXuReOb8Pjc3w9vPPA1wyKvky5cQyR6acmLARZ74n+EaLwsNiTrTdc/cB+Z4zx5I+AJBBVykPypkeNf9yFp5jfXps9IV5znOy9wxbTrpb7teYT+Y86mN8SW6IpaHDg/b0a9Btr5sm7HTaSh7yV2hMATzY77VzH6oN1783oeWNW0Xtb5tFfXsTXbwFindLgaYtRWnZwvMAw3UA76drNzFrMFpU1fBpmuF3u3FF4QImEBde0rY3/e34xBU5xgEv5Rb2XWnLpVKdU5x6kecU0heF0Ncb5atUxrMiDJCHJpunoXICZBz2Uq4h7b/AhbtkH8DOe5t+F/hSlc8pjuS9PITfBM1xvCL/OYBPHAo8xit3s+P8kPetvE/tEKQ7iubU/wMsaHl1]])
  180. --readAll("animTest.b64"))
  181. graphics.shaderCallback(animShader, "rotateAnimShaderCallback")
  182. graphics.shaderDrawCallback(animShader, "rotateAnimShaderDrawCallback")
  183.  
  184. local _time = 0.0
  185.  
  186. local curTable = nil
  187.  
  188. function rotateAnimShaderDrawCallback(data)
  189. local pos
  190.  
  191. if curTable.direction == 1 then
  192. pos = {x = data[1] + (data[9] - data[1]) / 2, y = data[10]}
  193. elseif curTable.direction == 2 then
  194. pos = {x = data[1] + (data[9] - data[1]) / 2, y = data[2]}
  195. elseif curTable.direction == 3 then
  196. pos = {x = data[1], y = data[2] + (data[10] - data[2]) / 2}
  197. elseif curTable.direction == 4 then
  198. pos = {x = data[9], y = data[2] + (data[10] - data[2]) / 2}
  199. end
  200. local rotate = curTable.func(_time * curTable.speed + curTable.offset) * curTable.strength
  201. -- local rot = matrix{{math.cos(rotate),math.sin(rotate),0,0},{-math.sin(rotate),math.cos(rotate),0,0},{0,0,1,0},{0,0,0,1}}
  202. -- local translate = matrix{{1,0,0,0},{0,1,0,0},{0,0,1,0},{-pos.x,-pos.y,0,1}}
  203. -- local translate2 = matrix{{1,0,0,0},{0,1,0,0},{0,0,1,0},{pos.x,pos.y,0,1}}
  204. -- local cam_mat = ((translate*rot)*translate2)
  205. local c = math.cos(rotate)
  206. local s = math.sin(rotate)
  207. local x = pos.x
  208. local y = pos.y
  209. graphics.shaderUniform(animShader, "c_mat", {c, s, 0, 0, -s, c, 0, 0, 0, 0, 1, 0, -(c*x + (-s)*y - x), -(s*x + c*y - y), 0, 1})
  210. -- graphics.shaderUniform(animShader, "c_mat", cam_mat:tofloat())
  211. graphics.shaderUniform(animShader, "t_offset", {data[3], data[4]})
  212. graphics.shaderUniform(animShader, "t_size", {data[11] - data[3], data[12] - data[4]})
  213. graphics.shaderUniform(animShader, "offset", {data[1], data[2]})
  214. graphics.shaderUniform(animShader, "size", {data[9] - data[1], data[10] - data[2]})
  215.  
  216. animIndexBuffer[curTable.direction]:bind()
  217. animBuffer[curTable.direction]:bind()
  218. graphics.setupOffsets(graphics.shaderAttrib(animShader, "pos"), 2, 20, 0)
  219. graphics.setupOffsets(graphics.shaderAttrib(animShader, "uv"), 2, 20, 8)
  220. graphics.setupOffsets(graphics.shaderAttrib(animShader, "strength"), 1, 20, 16)
  221. graphics.drawIndexed(0, 6*count)
  222.  
  223. end
  224.  
  225. saveTable = {}
  226. saveTable["_temporary_"] = ""
  227.  
  228. function rotateAnimShaderCallback(object)
  229. curTable = saveTable[object:getName()]
  230. end
  231.  
  232. local frameNum = 0
  233. local _timeInner = 0
  234. local lastTime = getTime()
  235. function rotateAnimMainLoop()
  236. local newTime = getTime()
  237. local delta = (newTime - lastTime) / 1000
  238. if delta > 0.5 then
  239. delta = 0.5
  240. end
  241. lastTime = getTime()
  242. _time = _time + delta
  243. end
  244. registerEventHandler("mainLoop", "rotateAnimMainLoop")
  245.  
  246. function setRotateAnimationToStrength(objectname, speed, offset, strengthFrom, strengthTo, direction, func, time)
  247. objectname = game.CurrentScene.Objects[objectname]
  248. setRotateAnimation(objectname, speed, offset, strengthFrom, direction, func)
  249. startTween("saveTable[\""..objectname:getName().."\"].strength", strengthFrom, strengthTo, time, easeQuadOut)
  250. end
  251.  
  252. function setRotateAnimation(object, speed, offset, strength, direction, t)
  253. local func = graphics.noise
  254. if t~=nil then
  255. func = t
  256. end
  257. local currentScene = game.CurrentScene
  258. if type(object) == "string" then
  259. object = currentScene.Objects[object]
  260. end
  261. object.ShaderSet = animShader
  262. saveTable[object:getName()] = {speed = speed, offset = offset, strength = strength, direction = direction, func = func}
  263. end
  264.  
  265. -- lua
  266.  
  267. function setRotateAnimationForLua(object, speed, offset, strength, direction, t)
  268. local func = graphics.noise
  269. if t~=nil then
  270. func = t
  271. end
  272. object.shaderSet = animShader
  273. saveTable[object] = {speed = speed, offset = offset, strength = strength, direction = direction, func = func}
  274. end
  275.  
  276. function rotateAnimDrawSprite(sprite)
  277. curTable = saveTable[sprite]
  278. graphics.drawSprite(sprite)
  279. end
RAW Paste Data