mattjeanes

Generate mesh with collision

Jan 18th, 2016
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.83 KB | None | 0 0
  1. -- Test
  2.  
  3. function ENT:MakeCube(pos,ang,length,width,height)
  4.     pos=pos or Vector()
  5.     length=length or 1
  6.     width=width or 1
  7.     height=height or 1
  8.    
  9.     -- http://wiki.unity3d.com/index.php/ProceduralPrimitives
  10.     local p0 = Vector(-length*0.5,-width*0.5,height*0.5)+pos
  11.     local p1 = Vector(length*0.5,-width*0.5,height*0.5)+pos
  12.     local p2 = Vector(length*0.5,-width*0.5,-height*0.5)+pos
  13.     local p3 = Vector(-length*0.5,-width*0.5,-height*0.5)+pos
  14.     p0:Rotate(ang)
  15.     p1:Rotate(ang)
  16.     p2:Rotate(ang)
  17.     p3:Rotate(ang)
  18.      
  19.     local p4 = Vector(-length*0.5,width*0.5,height*0.5)+pos
  20.     local p5 = Vector(length*0.5,width*0.5,height*0.5)+pos
  21.     local p6 = Vector(length*0.5,width*0.5,-height*0.5)+pos
  22.     local p7 = Vector(-length*0.5,width*0.5,-height*0.5)+pos
  23.     p4:Rotate(ang)
  24.     p5:Rotate(ang)
  25.     p6:Rotate(ang)
  26.     p7:Rotate(ang)
  27.      
  28.     local vertices = {
  29.         -- Left
  30.         p0, p1, p2,
  31.         p2, p3, p0,
  32.      
  33.         -- Back
  34.         p7, p4, p0,
  35.         p7, p0, p3,
  36.      
  37.         -- Top
  38.         p4, p5, p1,
  39.         p4, p1, p0,
  40.      
  41.         -- Bottom
  42.         p3, p6, p7,
  43.         p3, p2, p6,
  44.      
  45.         -- Front
  46.         p5, p6, p2,
  47.         p5, p2, p1,
  48.      
  49.         -- Right
  50.         p6, p5, p7,
  51.         p5, p4, p7,
  52.     };
  53.  
  54.     local up = Vector(0,0,1)
  55.     local down = Vector(0,0,-1)
  56.     local front = Vector(1,0,0)
  57.     local back = Vector(-1,0,0)
  58.     local left = Vector(0,-1,0)
  59.     local right = Vector(0,1,0)
  60.      
  61.     local normales = {
  62.         -- Left
  63.         left, left, left, left, left, left,
  64.      
  65.         -- Back
  66.         back, back, back, back, back, back,
  67.      
  68.         -- Top
  69.         up, up, up, up, up, up,
  70.      
  71.         -- Bottom
  72.         down, down, down, down, down, down,
  73.      
  74.         -- Front
  75.         front, front, front, front, front, front,
  76.      
  77.         -- Right
  78.         right, right, right, right, right, right,
  79.     };
  80.  
  81.     local _00 = {0,0}
  82.     local _10 = {1,0}
  83.     local _01 = {0,1}
  84.     local _11 = {1,1}
  85.      
  86.     local uvs = {
  87.         -- Left
  88.         _10, _00, _01, _01, _11, _10,
  89.      
  90.         -- Back
  91.         _11, _10, _00, _11, _00, _01,
  92.      
  93.         -- Top
  94.         _00, _01, _11, _00, _11, _10,
  95.      
  96.         -- Bottom
  97.         _00, _11, _10, _00, _01, _11,
  98.      
  99.         -- Front
  100.         _00, _01, _11, _00, _11, _10,
  101.      
  102.         -- Right
  103.         _01, _00, _11, _00, _10, _11,
  104.     }
  105.  
  106.     local verts = {}
  107.     for i=1,6*6 do
  108.         table.insert(verts,{pos = vertices[i], normal = normales[i], u = uvs[i][1], v = uvs[i][2] })
  109.     end
  110.    
  111.     return verts,vertices
  112. end
  113.  
  114. ENT:AddHook("Initialize","test",function(self)
  115.     local width=500
  116.     local height=1000
  117.     local size=100
  118.     self:SetRenderMode(RENDERMODE_TRANSALPHA)
  119.     self:SetColor(Color(255,255,255,1))
  120.    
  121.     local sections={
  122.         {self:MakeCube(Vector(0,-width/2,height/2),Angle(0,0,0),size,size,height)},
  123.         {self:MakeCube(Vector(0,0,height+(size/2)),Angle(0,0,0),size,width+size,size)},
  124.         {self:MakeCube(Vector(0,width/2,height/2),Angle(0,0,0),size,size,height)},
  125.     }
  126.     if CLIENT then
  127.         self:SetRenderBounds(Vector(0,-(width/2)-(size/2),0),Vector(0,(width/2)+(size/2),height))
  128.         self.meshes = {}
  129.         for k,v in pairs(sections) do
  130.             local newmesh=Mesh()
  131.             newmesh:BuildFromTriangles(v[1])
  132.             table.insert(self.meshes,newmesh)
  133.         end
  134.     end
  135.     local meshes={}
  136.     for k,v in pairs(sections) do
  137.         table.insert(meshes,v[2])
  138.     end
  139.     self:PhysicsInitMultiConvex(meshes)
  140.     self:EnableCustomCollisions(true)
  141.    
  142.     self.phys = self:GetPhysicsObject()
  143.     if not IsValid(self.phys) then
  144.         self:Remove()
  145.         return
  146.     end
  147.    
  148.     self.phys:SetMass(5000)
  149.     self.phys:Wake()
  150. end)
  151.  
  152. local rendermat=Material("models/props_wasteland/wood_fence01a")
  153.  
  154. ENT:AddHook("Draw","test",function(self)   
  155.     self:DrawModel()
  156.  
  157.     mat = Matrix()
  158.     mat:Translate( self:GetPos() )
  159.     mat:Rotate(self:GetAngles())
  160.     mat:Scale( Vector( 1, 1, 1 ) )
  161.  
  162.  
  163.     render.SetMaterial(rendermat)
  164.     cam.PushModelMatrix(mat)
  165.         for k,v in pairs(self.meshes) do
  166.             v:Draw()
  167.         end
  168.     cam.PopModelMatrix()
  169. end)
  170.  
  171. -- https://facepunch.com/showthread.php?t=1459677
  172. ENT:AddHook("Think","test",function(self)
  173.     if CLIENT and IsValid(self.phys) then
  174.         self.phys:EnableMotion(false)
  175.         self.phys:SetPos(self:GetPos())
  176.         self.phys:SetAngles(self:GetAngles())
  177.     end
  178. end)
Advertisement
Add Comment
Please, Sign In to add comment