Guest User

Untitled

a guest
Mar 29th, 2015
241
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --you have a rectangle and a triangle to limit the drawing to
  2. --the stencil will draw one single rectangle that only shows up within the two desired shape
  3. --because i'm using a polygon for the triangle, i may as well do it for the rectangle too
  4.  
  5. local rectangle = {}
  6. --rectangle looks like:
  7. -- D C
  8. -- A B
  9. rectangle.x, rectangle.y = 0, ScrH()-100 --bottom left corner of rectangle (A)
  10. rectangle.w, rectangle.h = 400, 100--dimensions of rectangle
  11. rectangle.poly = {
  12.     {x=rectangle.x, y=rectangle.y}, --A
  13.     {x=rectangle.x+rectangle.w, y=rectangle.y}, --B
  14.     {x=rectangle.x+rectangle.w, y=rectangle.y-rectangle.h}, --C
  15.     {x=rectangle.x, y=rectangle.y-rectangle.h} --D
  16. }
  17. local triangle = {}
  18. --triangle looks like:
  19. -- C
  20. --     B
  21. -- A
  22. triangle.x, triangle.y = rectangle.x+rectangle.w, rectangle.y+50 --bottom left corner of triangle (A)
  23. triangle.w, triangle.h = 100, rectangle.h+100 --will now draw 50 units below and above the rectangle
  24. triangle.poly = {
  25.     {x = triangle.x, y = triangle.y}, --A
  26.     {x = triangle.x+triangle.w, y = triangle.y-triangle.h/2}, --B
  27.     {x = triangle.x, y = triangle.y-triangle.h} --C
  28. }
  29.  
  30. hook.Add("HUDPaint","stencilpoly",function()
  31.     render.ClearStencil()
  32.     render.SetStencilEnable(true)
  33.         render.SetStencilWriteMask(255)
  34.         render.SetStencilTestMask(255)
  35.         render.SetStencilFailOperation(STENCILOPERATION_KEEP)
  36.         render.SetStencilZFailOperation(STENCILOPERATION_KEEP)
  37.         render.SetStencilPassOperation(STENCILOPERATION_REPLACE)
  38.         render.SetStencilCompareFunction(STENCILCOMPARISONFUNCTION_ALWAYS)
  39.         render.SetStencilReferenceValue(10)
  40.             surface.SetDrawColor(255,0,0,50)
  41.             surface.DrawPoly(rectangle.poly)
  42.             surface.DrawPoly(triangle.poly) --the arrow is now written part-visibly to the stencil buffer as value 10
  43.         render.SetStencilCompareFunction(STENCILCOMPARISONFUNCTION_EQUAL)
  44.         render.SetStencilPassOperation(STENCILOPERATION_KEEP)
  45.             surface.SetDrawColor(255,0,0,255)
  46.             surface.DrawRect(rectangle.x,triangle.y-triangle.h,LocalPlayer():Health()/100*(rectangle.w+triangle.w),triangle.h)
  47.     render.SetStencilEnable(false)
  48. end)
RAW Paste Data