SHARE
TWEET

Untitled

a guest Jun 19th, 2017 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*-------------------------------------------------------------------------------------------------------------------------
  2.     Rigid rope
  3. -------------------------------------------------------------------------------------------------------------------------*/
  4.  
  5. include( "shared.lua" )
  6.  
  7. local ang
  8. local function getStartPosition( p, d, angle, radius )
  9.     ang = d:Angle():Right():Angle()
  10.     ang:RotateAroundAxis( d, angle )
  11.     return p + ang:Forward() * radius
  12. end
  13.  
  14. local ang
  15. local function getEndPosition( p, d, angle, radius )
  16.     ang = d:Angle():Right():Angle()
  17.     ang:RotateAroundAxis( d, angle )
  18.     return p + ang:Forward() * radius
  19. end
  20.  
  21. local angle, ang
  22. local function drawCylinder( p1, d1, p2, d2, radius, segments )
  23.     segments = segments or 10
  24.     angle = 360 / segments
  25.    
  26.     mesh.Begin( MATERIAL_QUADS, segments * 2 )     
  27.         for i = 0, segments - 1 do
  28.             ang = i * angle
  29.            
  30.             -- Inside
  31.             mesh.Position( getStartPosition( p1, d1, ang, radius ) )
  32.             mesh.Normal( Vector( 0, 0, 1 ) )
  33.             mesh.AdvanceVertex()
  34.            
  35.             mesh.Position( getStartPosition( p1, d1, ang + angle, radius ) )
  36.             mesh.Normal( Vector( 0, 0, 1 ) )
  37.             mesh.AdvanceVertex()
  38.            
  39.             mesh.Position( getEndPosition( p2, d2, ang + angle, radius ) )
  40.             mesh.Normal( Vector( 0, 0, 1 ) )
  41.             mesh.AdvanceVertex()
  42.            
  43.             mesh.Position( getEndPosition( p2, d2, ang, radius ) )
  44.             mesh.Normal( Vector( 0, 0, 1 ) )
  45.             mesh.AdvanceVertex()
  46.            
  47.             -- Outside             
  48.             mesh.Position( getEndPosition( p2, d2, ang, radius ) )
  49.             mesh.Normal( Vector( 0, 0, 1 ) )
  50.             mesh.AdvanceVertex()
  51.            
  52.             mesh.Position( getEndPosition( p2, d2, ang + angle, radius ) )
  53.             mesh.Normal( Vector( 0, 0, 1 ) )
  54.             mesh.AdvanceVertex()
  55.            
  56.             mesh.Position( getStartPosition( p1, d1, ang + angle, radius ) )
  57.             mesh.Normal( Vector( 0, 0, 1 ) )
  58.             mesh.AdvanceVertex()
  59.            
  60.             mesh.Position( getStartPosition( p1, d1, ang, radius ) )
  61.             mesh.Normal( Vector( 0, 0, 1 ) )
  62.             mesh.AdvanceVertex()
  63.         end
  64.     mesh.End()
  65. end
  66.  
  67. local m0, m1, mu2, mu3
  68. local a0, a1, a2, a3
  69. local function hermiteInterpolate( y1, y2, y3, y4, tension, bias, mu ) 
  70.     mu2 = mu * mu
  71.     mu3 = mu2 * mu
  72.     m0 = ( y2 - y1 ) * ( 1 + bias ) * ( 1 - tension ) / 2 + ( y3 - y2 ) * ( 1 - bias ) * ( 1 - tension ) / 2
  73.     m1 = ( y3 - y2 ) * ( 1 + bias ) * ( 1 - tension ) / 2 + ( y4 - y3 ) * ( 1 - bias ) * ( 1 - tension ) / 2
  74.     a0 = 2 * mu3 - 3 * mu2 + 1
  75.     a1 = mu3 - 2 * mu2 + mu
  76.     a2 = mu3 - mu2
  77.     a3 = -2 * mu3 + 3 * mu2
  78.    
  79.     return a0 * y2 + a1 * m0 + a2 * m1 + a3 * y3
  80. end
  81.  
  82. local p1, p2, p1f, p2f, startpos, endpos, endpos2
  83. local resolution = 0.1
  84. function ENT:Draw()
  85.     if ( !self:GetDTEntity( 0 ):IsValid() or !self:GetDTEntity( 1 ):IsValid() ) then return end
  86.     if ( !self.Plug1 ) then
  87.         self.Plug1 = self:GetDTEntity( 0 )
  88.         self.Plug2 = self:GetDTEntity( 1 )
  89.     end
  90.     if ( !self.Material ) then self.Material = Material( "models/debug/debugwhite" ) end
  91.    
  92.     p1f = self.Plug1:GetAngles():Forward()
  93.     p2f = self.Plug2:GetAngles():Forward()
  94.    
  95.     p1 = self.Plug1:GetPos() + p1f * 11
  96.     p2 = self.Plug2:GetPos() + p2f * 11
  97.    
  98.     render.SetMaterial( self.Material )
  99.    
  100.     for mu = 0, 1 - resolution, resolution do
  101.         startpos =  Vector( ( p2.x - p1.x ) * mu + p1.x, hermiteInterpolate( p2.y - p1f.y * 100, p1.y, p2.y, p1.y - p2f.y * 100, 0, 0, mu ), hermiteInterpolate( p2.z - p1f.z * 100, p1.z, p2.z, p1.z - p2f.z * 100, 0, 0, mu ) )
  102.         endpos = Vector( ( p2.x - p1.x ) * ( mu + resolution ) + p1.x, hermiteInterpolate( p2.y - p1f.y * 100, p1.y, p2.y, p1.y - p2f.y * 100, 0, 0, mu + resolution ), hermiteInterpolate( p2.z - p1f.z * 100, p1.z, p2.z, p1.z - p2f.z * 100, 0, 0, mu + resolution ) )
  103.        
  104.         if ( mu + resolution >= 1 ) then
  105.             endpos2 = self.Plug2:GetPos() - p1f * 100
  106.         else
  107.             endpos2 = Vector( ( p2.x - p1.x ) * ( mu + resolution*2 ) + p1.x, hermiteInterpolate( p2.y - p1f.y * 100, p1.y, p2.y, p1.y - p2f.y * 100, 0, 0, mu + resolution*2 ), hermiteInterpolate( p2.z - p1f.z * 100, p1.z, p2.z, p1.z - p2f.z * 100, 0, 0, mu + resolution*2 ) )
  108.         end
  109.        
  110.         drawCylinder( startpos, endpos - startpos, endpos, endpos2 - endpos, 1.3 )
  111.     end
  112. end
RAW Paste Data
Top