Advertisement
Guest User

asd

a guest
May 26th, 2015
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.11 KB | None | 0 0
  1. --Scatter.lua
  2. --Written by cxcharlie
  3. --Continue scrolling, I'll explain as you scroll down
  4. math.randomseed(tick())
  5. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  6.  
  7. local NevermoreEngine = require(ReplicatedStorage:WaitForChild("NevermoreEngine"))
  8. local LoadCustomLibrary = NevermoreEngine.LoadLibrary
  9.  
  10. local qSystems = LoadCustomLibrary("qSystems")
  11. local Easing = LoadCustomLibrary("Easing")
  12.  
  13. function QuaternionFromCFrame(cf)
  14. local mx, my, mz,
  15. m00, m01, m02,
  16. m10, m11, m12,
  17. m20, m21, m22 = cf:components()
  18. local trace = m00 + m11 + m22
  19. if trace > 0 then
  20. local s = math.sqrt(1 + trace)
  21. local recip = 0.5/s
  22. return (m21-m12)*recip, (m02-m20)*recip, (m10-m01)*recip, s*0.5
  23. else
  24. local i = 0
  25. if m11 > m00 then i = 1 end
  26. if m22 > (i == 0 and m00 or m11) then i = 2 end
  27. if i == 0 then
  28. local s = math.sqrt(m00-m11-m22+1)
  29. local recip = 0.5/s
  30. return 0.5*s, (m10+m01)*recip, (m20+m02)*recip, (m21-m12)*recip
  31. elseif i == 1 then
  32. local s = math.sqrt(m11-m22-m00+1)
  33. local recip = 0.5/s
  34. return (m01+m10)*recip, 0.5*s, (m21+m12)*recip, (m02-m20)*recip
  35. elseif i == 2 then
  36. local s = math.sqrt(m22-m00-m11+1)
  37. local recip = 0.5/s
  38. return (m02+m20)*recip, (m12+m21)*recip, 0.5*s, (m10-m01)*recip
  39. end
  40. end
  41. end
  42.  
  43. function QuaternionToCFrame(px, py, pz, x, y, z, w)
  44. local xs, ys, zs = x + x, y + y, z + z
  45. local wx, wy, wz = w*xs, w*ys, w*zs
  46. local xx = x*xs
  47. local xy = x*ys
  48. local xz = x*zs
  49. local yy = y*ys
  50. local yz = y*zs
  51. local zz = z*zs
  52. return CFrame.new(px, py, pz, 1-(yy+zz), xy - wz, xz + wy, xy + wz, 1-(xx+zz), yz - wx, xz - wy, yz + wx, 1-(xx+yy))
  53. end
  54.  
  55. function QuaternionSlerp(a, b, t)
  56. local cosTheta = a[1]*b[1] + a[2]*b[2] + a[3]*b[3] + a[4]*b[4]
  57. local startInterp, finishInterp;
  58. if cosTheta >= 0.0001 then
  59. if (1 - cosTheta) > 0.0001 then
  60. local theta = math.acos(cosTheta)
  61. local invSinTheta = 1/math.sin(theta)
  62. startInterp = math.sin((1-t)*theta)*invSinTheta
  63. finishInterp = math.sin(t*theta)*invSinTheta
  64. else
  65. startInterp = 1-t
  66. finishInterp = t
  67. end
  68. else
  69. if (1+cosTheta) > 0.0001 then
  70. local theta = math.acos(-cosTheta)
  71. local invSinTheta = 1/math.sin(theta)
  72. startInterp = math.sin((t-1)*theta)*invSinTheta
  73. finishInterp = math.sin(t*theta)*invSinTheta
  74. else
  75. startInterp = t-1
  76. finishInterp = t
  77. end
  78. end
  79. return a[1] * startInterp + b[1] * finishInterp,
  80. a[2] * startInterp + b[2] * finishInterp,
  81. a[3] * startInterp + b[3] * finishInterp,
  82. a[4] * startInterp + b[4] * finishInterp
  83. end
  84.  
  85. function SlerpCFrame(a, b, scale)
  86. local qa = {QuaternionFromCFrame(a)}
  87. local qb = {QuaternionFromCFrame(b)}
  88. local ax, ay, az = a.x, a.y, a.z
  89. local bx, by, bz = b.x, b.y, b.z
  90. local _scale = 1 - scale;
  91. return QuaternionToCFrame(_scale * ax + scale*bx, _scale*ay + scale*by, _scale*az + scale*bz, QuaternionSlerp(qa, qb, scale))
  92. end
  93.  
  94.  
  95. local function GetParts(a)
  96. local f = {}
  97. for _,v in pairs(a:GetChildren()) do
  98. if ypcall(function()local d=v:IsA('BasePart')end) then
  99. if v:IsA('BasePart') then
  100. f[#f+1] = v
  101. end
  102. if ypcall(function()local d=v:GetChildren()end) then
  103. if #v:GetChildren() >= 1 then
  104. table.foreach(GetParts(v),function(ind,val) f[#f+1] = val;end)
  105. end
  106. end
  107. end
  108. end
  109. return f
  110. end
  111.  
  112.  
  113. qSystems:Import(getfenv(0));
  114.  
  115. local Main = {}
  116. local MR = math.random
  117.  
  118. --Gets the normal CFrame relative to the model's orignial center CFrame
  119. --model:The model
  120. --Returns: (A table of {part,part CFrame offset} or "CF_DATA" Accoutrement (not of much use)), The center of the model
  121. function GetCFrameHierarchy(model)
  122. if not type(model) == 'userdata' then return end
  123. if not model.ClassName == 'Model' then return end
  124. if model:FindFirstChild('CF_DATA') then model.CF_DATA:remove() end
  125. local OriginOffsets = {}
  126. local modelCenter = model:GetModelCFrame()
  127. local Parts = GetParts(model)
  128. if #Parts < 500 then --Just in case because table may "lose" its values
  129. for _,v in pairs(Parts) do table.insert(OriginOffsets,{v,v.CFrame:toObjectSpace(modelCenter)}) end
  130. return OriginOffsets,modelCenter
  131. else
  132. local cf = Instance.new('Accoutrement',model)
  133. cf.Name = 'CF_DATA'
  134. for _,v in pairs(Parts) do
  135. if v.ClassName == 'Part' then
  136. if v:FindFirstChild('DATA') then v.DATA:remove() end
  137. local off = v.CFrame:toObjectSpace(modelCenter)
  138. local nv = Instance.new('CFrameValue',v)
  139. nv.Name = 'DATA'
  140. nv.Value = off
  141. end end
  142. return cf,modelCenter
  143. end
  144. end
  145. Main.GetCFrameHierarchy = GetCFrameHierarchy
  146. --Spreads the models parts randomly
  147. --model: The model -_-
  148. --radius: The max distance each part can be offset
  149. --intensity: The max degree/radian the parts can be rotated about
  150. --callback: The function that is applied to each found part
  151. function Scatter(model,radius,intensity,callback)
  152. math.randomseed(tick())
  153. if not type(model) == 'userdata' then return end
  154. if not model.ClassName == 'Model' then return end
  155. --if model:FindFirstChild('CF_DATA') then model.CF_DATA:remove() end
  156. local Parts = GetParts(model)
  157. for _,v in pairs(Parts) do
  158. v.CFrame = (v.CFrame * CFrame.Angles(MR(-intensity,intensity),MR(-intensity,intensity),MR(-intensity,intensity)) * CFrame.new(MR(-radius,radius),MR(-radius,radius),MR(-radius,radius)))
  159. end
  160. callback(Parts)
  161. end
  162. Main.Scatterz = Scatter
  163.  
  164. --When a model is "scattered", this will rearange it back to normal
  165. --model: The model -___-
  166. --time: The approximate time it should take (not exact)
  167. --c: The callback which takes the arguments of the part being tweened and the progress (alpha)
  168. --See example below
  169. function TweenBack(model,time,c,hierarchy,center)
  170. local found = {}
  171. if model:FindFirstChild('CF_DATA') ~= nil and type(hierarchy) == 'userdata' then
  172. for _,v in pairs(model:GetChildren()) do
  173. if v.ClassName == 'Part' then
  174. if v:FindFirstChild('DATA') ~= nil then
  175. table.insert(found,{v,v.DATA.Value})
  176. end end end
  177. for _,v in pairs(found) do
  178. local origin = v[1].CFrame
  179. local goal = (center:toWorldSpace(v[2]:inverse()))
  180. coroutine.wrap(function()
  181. for i = 1,(time*29.99) do
  182. Game:GetService('RunService').RenderStepped:wait()
  183. v[1].CFrame = SlerpCFrame(origin, goal, i/(time*29.99))
  184. if c then c(v[1],i/(time*29.99)) end
  185. end
  186. ypcall(function()model.CF_DATA:remove()end)
  187. ypcall(function()for _,v in pairs(GetParts(model)) do if v:FindFirstChild('DATA') then v.DATA:remove() end end end)
  188. end)()
  189. end
  190. elseif model:FindFirstChild('CF_DATA') == nil and type(hierarchy) == 'table' then
  191. for _,v in pairs(hierarchy) do
  192. coroutine.wrap(function()
  193. local start = tick()
  194. local origin = v[1].CFrame
  195. local goal = (center:toWorldSpace(v[2]:inverse()))
  196. for i = 1,(time*29.99) do
  197. Game:GetService('RunService').RenderStepped:wait()
  198. v[1].CFrame = SlerpCFrame(origin, goal, i/(time*29.99))
  199. if c then c(v[1],i/(time*29.99)) end
  200. end
  201. end)()
  202. end
  203. end
  204. end
  205.  
  206. Main.TweenBack = TweenBack
  207.  
  208. function ManualTweenBack(model,hierarchy,center,callback)
  209. local found = {}
  210. if model:FindFirstChild('CF_DATA') ~= nil and type(hierarchy) == 'userdata' then
  211. for _,v in pairs(model:GetChildren()) do
  212. if v.ClassName == 'Part' then
  213. if v:FindFirstChild('DATA') ~= nil then
  214. table.insert(found,{v,v.DATA.Value})
  215. end end end
  216. return function(alpha)
  217. for _,v in pairs(found) do
  218. local origin = v[1].CFrame
  219. local goal = (center:toWorldSpace(v[2]:inverse()))
  220. v[1].CFrame = SlerpCFrame(origin, goal, alpha)
  221. if callback then ypcall(function()callback(v[1],alpha)end) end
  222. end
  223. if alpha == 1 then
  224. ypcall(function()model.CF_DATA:remove()end)
  225. ypcall(function()for _,v in pairs(GetParts(model)) do if v:FindFirstChild('DATA') then v.DATA:remove() end end end)
  226. end
  227. end
  228. elseif model:FindFirstChild('CF_DATA') == nil and type(hierarchy) == 'table' then
  229. return function(alpha)
  230. for _,v in pairs(hierarchy) do
  231. local origin = v[1].CFrame
  232. local goal = (center:toWorldSpace(v[2]:inverse()))
  233. v[1].CFrame = SlerpCFrame(origin, goal, alpha)
  234. if callback then ypcall(function()callback(v[1],alpha)end) end
  235. if alpha == 1 then
  236. ypcall(function()for _,v in pairs(GetParts(model)) do if v:FindFirstChild('DATA') then v.DATA:remove() end end end)
  237. end
  238. end
  239. end
  240. end
  241. end
  242.  
  243. Main.ManualTweenBack = ManualTweenBack
  244.  
  245. function TweenBackEase(model,time,hierarchy,center,easing,callback,finished)
  246. local start = tick()
  247. coroutine.wrap(function()
  248. local step = ManualTweenBack(model,hierarchy,center,callback)
  249. local now=0
  250. local alpha=0
  251. while game:GetService('RunService').RenderStepped:wait() and alpha<1 do
  252. now = tick()-start
  253. alpha = easing(now,0,1,time)
  254. step(alpha)
  255. end
  256. if finished then ypcall(function()finished()end) end
  257. end)()
  258. end
  259.  
  260. Main.TweenBackEase = TweenBackEase
  261.  
  262. local RemoteEvent --= NevermoreEngine.GetEventStreamObject("Tween")
  263.  
  264. function FakeModelTweenEase(model,radius,intensity,time,easing,callback1,callback2,f,b)
  265. if not b then
  266. pcall(function()RemoteEvent:FireAllClients(model,radius,intensity,time,easing,callback1,callback2,f,true)end)
  267. end
  268. if not callback1 then callback1 = function()end end
  269. if not callback2 then callback2 = function()end end
  270. if not easing then easing = Easing.inQuad end
  271. local Fake = model:Clone()
  272. local h,c = GetCFrameHierarchy(Fake)
  273. Scatter(Fake,radius,intensity,callback1)
  274. Fake.Parent = game:GetService('Workspace').CurrentCamera
  275. if not b then
  276. TweenBackEase(Fake,time,h,c,easing,callback2,function()
  277. model:MakeJoints()
  278. model.Parent=game:GetService('Workspace')Fake:remove()
  279. coroutine.wrap(f)()
  280. ypcall(function()model:MakeJoints()end)
  281. end)
  282. else
  283. TweenBackEase(Fake,time,h,c,easing,callback2,function()Fake:remove()end)
  284. end
  285. end
  286.  
  287. --RemoteEvent.OnClientEvent:connect(FakeModelTweenEase)
  288.  
  289.  
  290.  
  291. Main.FakeModelTweenEase = FakeModelTweenEase
  292.  
  293.  
  294. return Main
  295. --[[
  296. --Press play to see what happens
  297. local norm,OriginalCenter = GetCFrameHierarchy(script.Parent.Noobie)
  298. wait(5)
  299.  
  300. --You can do what ever and break up your model and stuff
  301. Scatter(script.Parent.Noobie,4,math.rad(30),function(tab)
  302. for _,v in pairs(tab) do v.Transparency = 1 end --makes part invisible
  303. end)
  304. wait(3)
  305.  
  306. --Now the model is back
  307. TweenBack(script.Parent.Noobie,5,function(part,progress) --this funciton iterates through each part
  308. part.Transparency = 1 - progress --makes fade in effect
  309. end,norm,OriginalCenter)
  310.  
  311. ]]--
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement