Vzurxy

IC3'S Drawing API

Jun 28th, 2020 (edited)
265,590
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.45 KB | None | 0 0
  1. if not shared.__BIGCACHE then
  2.     shared.__BIGCACHE = {};
  3. end
  4.  
  5. shared.ckill = false;
  6.  
  7. local Players = game:GetService'Players';
  8. local LocalPlayer = Players.LocalPlayer;
  9. local Mouse = LocalPlayer:GetMouse();
  10. local Camera = workspace.CurrentCamera;
  11. local RunService = game:GetService'RunService';
  12.  
  13.     local _G, game, script, getfenv, setfenv, workspace,
  14.         getmetatable, setmetatable, loadstring, coroutine,
  15.         rawequal, typeof, print, math, warn, error,  pcall,
  16.         xpcall, select, rawset, rawget, ipairs, pairs,
  17.         next, Rect, Axes, os, tick, Faces, unpack, string, Color3,
  18.         newproxy, tostring, tonumber, Instance, TweenInfo, BrickColor,
  19.         NumberRange, ColorSequence, NumberSequence, ColorSequenceKeypoint,
  20.         NumberSequenceKeypoint, PhysicalProperties, Region3int16,
  21.         Vector3int16, elapsedTime, require, table, type, wait,
  22.         Enum, UDim, UDim2, Vector2, Vector3, Region3, CFrame, Ray, delay =
  23.         _G, game, script, getfenv, setfenv, workspace,
  24.         getmetatable, setmetatable, loadstring, coroutine,
  25.         rawequal, typeof, print, math, warn, error,  pcall,
  26.         xpcall, select, rawset, rawget, ipairs, pairs,
  27.         next, Rect, Axes, os, tick, Faces, unpack, string, Color3,
  28.         newproxy, tostring, tonumber, Instance, TweenInfo, BrickColor,
  29.         NumberRange, ColorSequence, NumberSequence, ColorSequenceKeypoint,
  30.         NumberSequenceKeypoint, PhysicalProperties, Region3int16,
  31.         Vector3int16, elapsedTime, require, table, type, wait,
  32.         Enum, UDim, UDim2, Vector2, Vector3, Region3, CFrame, Ray, delay
  33.  
  34. local CoroWrap = coroutine.wrap
  35. local Clamp = math.clamp
  36.  
  37. RunService:UnbindFromRenderStep'RLESP';
  38.  
  39. local RefreshRate = 60; RefreshRate = 1 / RefreshRate;
  40.  
  41. local Colors = {
  42.     White           = Color3.new(1, 1, 1);
  43.     Black           = Color3.new(0.12, 0.12, 0.12);
  44.     ReallyBlack     = Color3.new(0, 0, 0);
  45.     Red             = Color3.new(1, 0, 0);
  46.     Green           = Color3.new(0, 1, 0);
  47.     Blue            = Color3.new(0, 0, 1);
  48.     Scroll          = Color3.fromRGB(255, 151, 41);
  49.     ScrollOutline   = Color3.fromRGB(25, 20, 30);
  50.     Trinket         = Color3.fromRGB(81, 117, 255);
  51.     TrinketOutline  = Color3.fromRGB(25, 25, 25);
  52.     Gem             = Color3.fromRGB(205, 46, 255);
  53.     HotPink         = Color3.fromRGB(255, 0, 255);
  54.     Ice             = Color3.fromRGB(70, 255, 255);
  55.     Shroom          = Color3.fromRGB(100, 220, 25);
  56.     Crimson         = Color3.fromRGB(220, 50, 50);
  57.     PD              = Color3.fromRGB(255, 120, 0);
  58.     Diamond         = Color3.fromRGB(255, 255, 255);
  59.     Ruby            = Color3.fromRGB(255, 25, 75);
  60.     Emerald         = Color3.fromRGB(50, 220, 50);
  61.     Sapphire        = Color3.fromRGB(25, 75, 255);
  62.     RoyalPurple     = BrickColor.new'Royal purple'.Color;
  63. }
  64.  
  65. shared.__Colors = Colors;
  66.  
  67. local Updates = setmetatable({}, {
  68.     __index = function(t, i)
  69.         return rawget(t, i) and rawget(t, i) or 0;
  70.     end;
  71. });
  72.  
  73. shared.RLDrawings = shared.RLDrawings or {
  74.     ObjectsToDraw = setmetatable({
  75.         Tables = {};
  76.         Cap = 150;
  77.     }, {
  78.         __index = function(self, i)
  79.             return rawget(self.Tables, i) or rawget(self, i);
  80.         end;
  81.  
  82.         __newindex = function(self, i, v)
  83.             local current = self.Tables[#self.Tables];
  84.            
  85.             if typeof(current) ~= 'table' or #current > self.Cap then
  86.                 current = {};
  87.                 table.insert(self.Tables, current);
  88.             end
  89.  
  90.             current[i] = v;
  91.         end
  92.     });
  93.     Cache = {};
  94.     ScreenPositionCache = {};
  95.     Enabled = true;
  96. };
  97.  
  98. local function IsStringEmpty(String)
  99.     if type(String) == 'string' then
  100.         return String:match'^%s+$' ~= nil or #String == 0 or String == '' or false;
  101.     end
  102.     return false
  103. end
  104.  
  105. local function Set(t, i, v)
  106.     t[i] = v;
  107. end
  108.  
  109. function GetIndex(T, V)
  110.     for i, v in pairs(T) do
  111.         if v == V then
  112.             return i;
  113.         end
  114.     end
  115.  
  116.     return -1;
  117. end
  118.  
  119. local DefaultPropties = {
  120.     Line = {
  121.         Visible         = false;
  122.         Color           = Color3.new(1, 1, 1);
  123.         Transparency    = 1;
  124.         Thickness       = 1;
  125.     };
  126.     Square = {
  127.         Size            = Vector2.new(100, 100);
  128.         Position        = Vector2.new(250, 100);
  129.         Filled          = false;
  130.         Color           = Color3.new(1, 1, 1);
  131.         Thickness       = 2;
  132.         Visible         = true;
  133.     };
  134.     Text = {
  135.         Size            = 16;
  136.         Position        = Vector2.new(25, 15);
  137.         Text            = 'undefined';
  138.         Color           = Color3.new(1, 1, 1);
  139.         Visible         = true;
  140.         Center          = true;
  141.         Outline         = true;
  142.         OutlineColor    = Colors.Black;
  143.     };
  144.     Circle = {
  145.         Radius          = 10;
  146.         Position        = Vector2.new(25, 25);
  147.         Color           = Color3.new(1, 1, 1);
  148.         Filled          = true;
  149.         Visible         = true;
  150.     };
  151. };
  152.  
  153. function SetDefaultProperties(Drawing)
  154.     local Type = getmetatable(Drawing).__type;
  155.  
  156.     if DefaultPropties[Type] then
  157.         for i, v in pairs(DefaultPropties[Type]) do
  158.             pcall(Set, Drawing, i, v);
  159.         end
  160.     end
  161. end
  162.  
  163. local function False() return false; end
  164.  
  165. function shared.RLDrawings:GetInstanceName(Instance, Name)
  166.     if Instance == nil or typeof(Instance) ~= 'Instance' then
  167.         return false;
  168.     end
  169.  
  170.     return string.format('%s-%s', Instance:GetDebugId(), Name);
  171. end
  172.  
  173. function shared.RLDrawings:DrawingExists(Instance, Name)
  174.     local CachedName = self:GetInstanceName(Instance, Name);
  175.     return self.Cache[CachedName] ~= nil;
  176. end
  177.  
  178. function shared.RLDrawings:CreateDrawing(Type, Instance, Name)
  179.     if not self:DrawingExists(Instance, Name) then
  180.         local CachedName = self:GetInstanceName(Instance, Name);
  181.  
  182.         local Drawing = Drawing.new(Type);
  183.         table.insert(shared.__BIGCACHE, Drawing);
  184.        
  185.         SetDefaultProperties(Drawing);
  186.  
  187.         self.Cache[CachedName] = {
  188.             LastUpdate = 0;
  189.             Instance = Instance;
  190.             Drawing = Drawing;
  191.         };
  192.  
  193.         return Drawing;
  194.     else
  195.         return false, 'drawing already exists';
  196.     end
  197. end
  198.  
  199. function shared.RLDrawings:GetDrawing(Instance, Name)
  200.     if self:DrawingExists(Instance, Name) then
  201.         local CachedName = self:GetInstanceName(Instance, Name);
  202.         local Drawing = nil;
  203.         local Cache = self.Cache[CachedName];
  204.  
  205.         self.Cache[CachedName].LastUpdate = os.clock();
  206.  
  207.         if Cache.Drawing then
  208.             Drawing = Cache.Drawing
  209.         else
  210.             return False;
  211.         end
  212.  
  213.         return function(func)
  214.             if typeof(func) == 'function' then
  215.                 func(Drawing);
  216.             else
  217.                 return Drawing;
  218.             end
  219.         end;
  220.     end
  221.  
  222.     return False
  223. end
  224.  
  225. function shared.RLDrawings:RemoveDrawing(Instance, Name)
  226.     if self:DrawingExists(Instance, Name) then
  227.         self:GetDrawing(Instance, Name)(function(Drawing)
  228.             local CachedName = self:GetInstanceName(Instance, Name);
  229.            
  230.             Drawing.Visible = false;
  231.             Drawing:Remove();
  232.             self.Cache[CachedName] = nil;
  233.         end);
  234.     end    
  235. end
  236.  
  237. function shared.RLDrawings:CheckForInvalidDrawings()
  238.     for i, v in pairs(self.Cache) do
  239.         if i == nil or typeof(v) ~= 'table' or v.Instance == nil or not v.Instance:IsDescendantOf(workspace) or (os.clock() - v.LastUpdate) > 10 then
  240.             -- i == nil or typeof(v) ~= 'table' or v.Instance == nil or (v.Instance.Parent ~= workspace and not v.Instance:IsDescendantOf(workspace)) or (os.clock() - v.LastUpdate) > 1 or not v.Instance:IsDescendantOf(workspace)
  241.             -- print('1',i==nil)
  242.             -- print('2',i.Parent==nil)
  243.             -- print('3',v.Instance==nil)
  244.             -- print('4',v.Instance.Parent~=workspace)
  245.             -- print('5',os.clock() - v.LastUpdate > 1)
  246.             -- print('6',v.Instance:IsDescendantOf(workspace));
  247.             -- print('removing', v.Instance:GetDebugId());
  248.             -- print()
  249.             v.Drawing.Visible = false;
  250.             v.Drawing:Remove();
  251.             self.Cache[i] = nil;
  252.             self:RemoveObject(v.Instance);
  253.         end
  254.     end
  255. end
  256.  
  257. function shared.RLDrawings:Cleanup()
  258.     for i, v in pairs(self.Cache) do
  259.         if i.Parent == nil or not i:IsDescendantOf(workspace) or (os.clock() - v.LastUpdate) > 1 / 3 then
  260.             v.Drawing.Visible = false;
  261.             v.Drawing:Remove();
  262.             self.Cache[i] = nil;
  263.         end
  264.     end
  265. end
  266.  
  267. -- local t = nil;
  268.  
  269. function shared.RLDrawings:GetScreenPosition(Instance)
  270.     -- if Instance.ClassName ~= 'Part' and Instance.ClassName ~= 'UnionOperation' and not Instance:IsA'BasePart' then
  271.     --     -- print(Instance.ClassName ~= 'Part', Instance.ClassName ~= 'UnionOperation', Instance:IsA'BasePart');
  272.     --     return false;
  273.     -- end
  274.  
  275.     local Unit = (Instance.Position - Camera.CFrame.p).unit;
  276.     local Dot = Camera.CFrame.lookVector:Dot(Unit);
  277.  
  278.     if Dot > 1.000001 then
  279.         return false;
  280.     end
  281.  
  282.     if self.ScreenPositionCache[Instance] == nil then
  283.         self.ScreenPositionCache[Instance] = {
  284.             LastUpdate = os.clock() + 1;
  285.             ScreenPosition = Vector2.new();
  286.         };
  287.     end
  288.  
  289.     local Cache = self.ScreenPositionCache[Instance];
  290.  
  291.     if (os.clock() - Cache.LastUpdate) > RefreshRate then
  292.         -- local t = t or Instance;
  293.  
  294.         -- if Instance == t then
  295.         --     print(Instance:GetFullName(), os.clock(), os.clock() - Cache.LastUpdate, tableToString(debug.getlocals(2)));
  296.         -- end
  297.  
  298.         local Position, _V = Camera:WorldToViewportPoint(Instance.Position);
  299.  
  300.         Cache.LastUpdate = os.clock();
  301.         Cache.ScreenPosition = Vector2.new(Position.X, Position.Y);
  302.         Cache.Depth = Position.Z;
  303.         Cache.Distance = (Camera.CFrame.p - Instance.Position).magnitude;
  304.         -- tonumber(string.format('%.1f', (Camera.CFrame.p - Instance.Position).magnitude));
  305.         Cache.Visible = _V;
  306.         Cache.OVisible = Position.Z > 0;
  307.     end
  308.  
  309.     self.ScreenPositionCache[Instance] = Cache;
  310.  
  311.     return Cache;
  312. end
  313.  
  314. function shared.RLDrawings:AddObject(Instance, Text, Color, DrawDistance, DrawTracer, DrawCircle)
  315.     if typeof(Instance) ~= 'Instance' and not Instance:IsA'BasePart' then return false; end
  316.  
  317.     local Table = {
  318.         Instance = Instance;
  319.         Text = IsStringEmpty(Text) and nil or Text;
  320.         Color = Color or Colors.White;
  321.         DrawTracer = DrawTracer;
  322.         DrawCircle = DrawCircle;
  323.         DrawDistance = DrawDistance;
  324.     };
  325.  
  326.     self.ObjectsToDraw[Instance] = Table;
  327.  
  328.     return Table;
  329. end
  330.  
  331. function shared.RLDrawings:RemoveObject(Instance)
  332.     self.ObjectsToDraw[Instance] = nil;
  333. end
  334.  
  335. function DrawObjectText(Instance, DisplayText, DisplayColor, ShowDistance)
  336.     local TextObject = shared.RLDrawings:GetDrawing(Instance, 'NameTag')() or shared.RLDrawings:CreateDrawing('Text', Instance, 'NameTag');
  337.  
  338.     local Position, Visible = shared.RLDrawings:GetScreenPosition(Instance);
  339.  
  340.     -- print(Position);
  341.    
  342.     if Position and Position.Visible and Position.Distance < 25000 then
  343.         if ShowDistance then
  344.             DisplayText = DisplayText .. '\n' .. tostring(Position.Distance):match'%d+.[%d]?';
  345.             -- string.format('%s\n[%s]', DisplayText, tostring(Position.Distance):match'%d+.[%d]?');
  346.         end
  347.  
  348.         TextObject.Visible = true;
  349.         TextObject.Size = 17;
  350.         TextObject.Text = DisplayText or 'undefined';
  351.         TextObject.Color = DisplayColor or Colors.White;
  352.         TextObject.Position = Position.ScreenPosition;
  353.         TextObject.Outline = true;
  354.        
  355.         if syn then
  356.             TextObject.OutlineColor = Color3.fromRGB(45, 45, 45);
  357.             TextObject.Font = Drawing.Fonts.Monospace;
  358.         end
  359.     else
  360.         TextObject.Visible = false;
  361.     end
  362. end
  363.  
  364. function DrawObjectTracer(Instance, DisplayColor)
  365.     local LineObject = shared.RLDrawings:GetDrawing(Instance, 'Tracer')() or shared.RLDrawings:CreateDrawing('Line', Instance, 'Tracer');
  366.  
  367.     local Position = shared.RLDrawings:GetScreenPosition(Instance);
  368.  
  369.     if Position and Position.OVisible and Position.Distance < 1000 then
  370.         LineObject.Visible = true;
  371.         LineObject.Color = DisplayColor or Colors.White;
  372.         LineObject.From = Vector2.new(Camera.ViewportSize.X / 2, Camera.ViewportSize.Y);
  373.         LineObject.To = Position.ScreenPosition;
  374.     else
  375.         LineObject.Visible = false;
  376.     end
  377. end
  378.  
  379. function DrawObjectCircle(Instance, DisplayColor)
  380.     local CircleObject = shared.RLDrawings:GetDrawing(Instance, 'Dot')() or shared.RLDrawings:CreateDrawing('Circle', Instance, 'Dot');
  381.  
  382.     local Position, Visible = shared.RLDrawings:GetScreenPosition(Instance);
  383.  
  384.     if Position and Position.Visible and Position.Distance < 1000 then
  385.         local Distance = Position.Depth;
  386.  
  387.         if LocalPlayer.Character and LocalPlayer.Character.PrimaryPart then
  388.             Distance = (LocalPlayer.Character.PrimaryPart.Position - Instance.Position).magnitude;
  389.         end
  390.  
  391.         CircleObject.Visible = true;
  392.         CircleObject.Color = DisplayColor or Colors.White;
  393.         CircleObject.Radius = Clamp(650 / Distance, 3, 20);
  394.         CircleObject.Filled = true;
  395.         CircleObject.Transparency = CircleObject.Radius <= 7.5 and 1 or 0.35;
  396.         CircleObject.Position = Position.ScreenPosition;
  397.     else
  398.         CircleObject.Visible = false;
  399.     end
  400. end
  401.  
  402. function DrawObjects(Table)
  403.     if typeof(Table) ~= 'table' then return; end
  404.  
  405.     for i, v in pairs(Table) do
  406.         -- print(i, v, typeof(v));
  407.         if shared.ckill then break; end
  408.  
  409.         if typeof(v) == 'table' and v.Instance ~= nil then
  410.             if v.Text then
  411.                 DrawObjectText(v.Instance, v.Text, v.Color, v.DrawDistance);
  412.             end
  413.             if v.DrawTracer then
  414.                 DrawObjectTracer(v.Instance, v.Color);
  415.             end
  416.             if v.DrawCircle then
  417.                 DrawObjectCircle(v.Instance, v.Color);
  418.             end
  419.         end
  420.     end
  421. end
  422.  
  423. RunService:BindToRenderStep('RLESP', 1, function()
  424.     if (os.clock() - Updates.InvalidCheck) > 1 then
  425.         Updates.InvalidCheck = os.clock();
  426.         shared.RLDrawings:CheckForInvalidDrawings();
  427.     end
  428.  
  429.     if (os.clock() - Updates.ESP) > RefreshRate then
  430.         Updates.ESP = os.clock();
  431.         for _, v in ipairs(shared.RLDrawings.ObjectsToDraw.Tables) do
  432.             if shared.ckill then break; end
  433.  
  434.             CoroWrap(DrawObjects)(v);
  435.         end
  436.     end
  437. end)
  438.  
  439. return shared.RLDrawings
Add Comment
Please, Sign In to add comment