Guest User

whiterabbit meshbuilder

a guest
Aug 8th, 2016
24
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function ENT:RebuildMesh(returnVerts)
  2.    
  3.     local Normals = {
  4.         Up = Vector(0,0,1),
  5.         Forward = Vector(0,1,0),
  6.         Right = Vector(1,0,0),
  7.         Down = Vector(0,0,-1),
  8.         Back = Vector(0,-1,0),
  9.         Left = Vector(-1,0,0),
  10.     }
  11.    
  12.     --[[local Colors = {
  13.         Red = Color(255,0,0,125),
  14.         Green = Color(0,255,0,128),
  15.         Blue = Color(0,0,255,128),
  16.         White = Color(255,255,255,128),
  17.     }]]
  18.    
  19.     local Colors = {
  20.         Red = Color(255,255,255,255),
  21.         Green = Color(255,255,255,255),
  22.         Blue = Color(255,255,255,255),
  23.         White = Color(255,255,255,255),
  24.     }
  25.    
  26.     local Verts = {}
  27.     local function AddVert(pos,u,v,normal,color)
  28.         Verts[#Verts+1] = { pos=pos, u=u, v=v, normal=normal, color=color or Colors.White }
  29.     end
  30.    
  31.     local cpos = self:GetPos()
  32.     local mins,maxs = self:GetBlockBuilderMins(),self:GetBlockBuilderMaxs()
  33.    
  34.     local RightX = maxs.x
  35.     local LeftX = mins.x
  36.     local FrontY = maxs.y
  37.     local BackY = mins.y
  38.     local TopZ = maxs.z
  39.     local BottomZ = mins.z
  40.    
  41.     local TopFrontLeft = Vector( LeftX, FrontY, TopZ )
  42.     local TopFrontRight = Vector( RightX, FrontY, TopZ )
  43.     local TopBackLeft = Vector( LeftX, BackY, TopZ )
  44.     local TopBackRight = Vector( RightX, BackY, TopZ )
  45.     local BottomFrontLeft = Vector( LeftX, FrontY, BottomZ )
  46.     local BottomFrontRight = Vector( RightX, FrontY, BottomZ )
  47.     local BottomBackLeft = Vector( LeftX, BackY, BottomZ )
  48.     local BottomBackRight = Vector( RightX, BackY, BottomZ )
  49.    
  50.     --Top
  51.     AddVert( TopFrontLeft, 0, 0, Normals.Up, Colors.Blue )
  52.     AddVert( TopFrontRight, 1, 0, Normals.Up, Colors.Blue )
  53.     AddVert( TopBackLeft, 0, 1, Normals.Up, Colors.Blue )
  54.     AddVert( TopFrontRight, 1, 0, Normals.Up, Colors.Blue )
  55.     AddVert( TopBackRight, 1, 1, Normals.Up, Colors.Blue )
  56.     AddVert( TopBackLeft, 0, 1, Normals.Up, Colors.Blue )
  57.    
  58.     --Bottom
  59.     AddVert( BottomFrontLeft, 0, 1, Normals.Down )
  60.     AddVert( BottomBackLeft, 0, 0, Normals.Down )
  61.     AddVert( BottomFrontRight, 1, 1, Normals.Down )
  62.     AddVert( BottomFrontRight, 1, 1, Normals.Down )
  63.     AddVert( BottomBackLeft, 0, 0, Normals.Down )
  64.     AddVert( BottomBackRight, 1, 0, Normals.Down )
  65.    
  66.     --Front
  67.     AddVert( TopFrontLeft, 1, 0, Normals.Forward, Colors.Green )
  68.     AddVert( BottomFrontLeft, 1, 1, Normals.Forward, Colors.Green )
  69.     AddVert( TopFrontRight, 0, 0, Normals.Forward, Colors.Green )
  70.     AddVert( BottomFrontLeft, 1, 1, Normals.Forward, Colors.Green )
  71.     AddVert( BottomFrontRight, 0, 1, Normals.Forward, Colors.Green )
  72.     AddVert( TopFrontRight, 0, 0, Normals.Forward, Colors.Green )
  73.    
  74.     --Back
  75.     AddVert( TopBackLeft, 0, 0, Normals.Back )
  76.     AddVert( TopBackRight, 1, 0, Normals.Back )
  77.     AddVert( BottomBackLeft, 0, 1, Normals.Back )
  78.     AddVert( BottomBackLeft, 0, 1, Normals.Back )
  79.     AddVert( TopBackRight, 1, 0, Normals.Back )
  80.     AddVert( BottomBackRight, 1, 1, Normals.Back )
  81.    
  82.     --Left
  83.     AddVert( TopBackLeft, 1, 0, Normals.Left )
  84.     AddVert( BottomBackLeft, 1, 1, Normals.Left )
  85.     AddVert( TopFrontLeft, 0, 0, Normals.Left )
  86.     AddVert( BottomBackLeft, 1, 1, Normals.Left )
  87.     AddVert( BottomFrontLeft, 0, 1, Normals.Left )
  88.     AddVert( TopFrontLeft, 0, 0, Normals.Left )
  89.    
  90.     --Right
  91.     AddVert( TopBackRight, 0, 0, Normals.Right, Colors.Red )
  92.     AddVert( TopFrontRight, 1, 0, Normals.Right, Colors.Red )
  93.     AddVert( BottomBackRight, 0, 1, Normals.Right, Colors.Red )
  94.     AddVert( BottomBackRight, 0, 1, Normals.Right, Colors.Red )
  95.     AddVert( TopFrontRight, 1, 0, Normals.Right, Colors.Red )
  96.     AddVert( BottomFrontRight, 1, 1, Normals.Right, Colors.Red )
  97.    
  98.     if returnVerts then
  99.         return Verts
  100.     end
  101.    
  102.     local mymesh = Mesh()
  103.     mymesh:BuildFromTriangles(Verts)
  104.     self.BlockMesh = mymesh
  105.    
  106. end
RAW Paste Data