CapsAdmin

Untitled

Aug 8th, 2013
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.91 KB | None | 0 0
  1. -- config
  2.  
  3. if me then
  4.     local allowed = {caps, katie, chess, elmo}
  5.  
  6.     if not table.HasValue(allowed, LocalPlayer()) then return end
  7. end
  8.  
  9. local STEPS = 512
  10. local DISTANCE = 4096
  11. local SECTOR_LIMIT = 4096
  12. local GRID_SIZE = 256
  13.  
  14. --
  15.  
  16. local eyepos =  Vector(0,0,0)
  17. hook.Add("RenderScene", "grass", function(pos) eyepos = pos end)
  18.  
  19. local DATA = _G.GRASS_DATA or {}
  20. local DATA_LIST = _G.GRASS_DATA_LIST or {}
  21.  
  22. _G.GRASS_DATA = DATA
  23. _G.GRASS_DATA_LIST = DATA_LIST
  24.  
  25. local function round(v)
  26.     return
  27.         math.ceil(v.x/GRID_SIZE) * GRID_SIZE,
  28.         math.ceil(v.y/GRID_SIZE) * GRID_SIZE,
  29.         math.ceil(v.z/GRID_SIZE) * GRID_SIZE
  30. end
  31.  
  32. local function get_sector(pos)
  33.     local x,y,z = round(pos)
  34.    
  35.     DATA[x] = DATA[x] or {}
  36.     DATA[x][y] = DATA[x][y] or {}
  37.    
  38.     if not DATA[x][y][z] then
  39.         local data = {points = {}, origin = Vector(x,y,z)}
  40.         table.insert(DATA_LIST, data)
  41.         DATA[x][y][z] = data
  42.     end
  43.    
  44.     return DATA[x][y][z]
  45. end
  46.  
  47. local temp_vec1 = Vector(0,0,0)
  48.  
  49. local params = {}
  50. params.mask = MASK_SOLID_BRUSHONLY
  51. local function trace(pos, dir)
  52.     params.start = pos
  53.    
  54.     temp_vec1.x = pos.x + dir.x
  55.     temp_vec1.y = pos.y + dir.y
  56.     temp_vec1.z = pos.z + dir.z
  57.    
  58.     params.endpos = temp_vec1
  59.    
  60.     local res = util.TraceLine(params)
  61.    
  62.     if res.Hit then
  63.         return {HitPos = res.HitPos, HitNormal = res.HitNormal}
  64.     end
  65. end
  66.  
  67. local random_vectors = {}
  68.  
  69. local function update()
  70.     if  LocalPlayer():GetVelocity() == vector_origin then return end
  71.        
  72.     for i = 0, STEPS do
  73.         local dir = VectorRand() * DISTANCE--random_vectors[i] or VectorRand():GetNormalized() * DISTANCE
  74.         --random_vectors[i] = dir
  75.        
  76.         local res = trace(eyepos, dir)
  77.        
  78.         if res then
  79.             local data = get_sector(res.HitPos)
  80.            
  81.             if #data.points < SECTOR_LIMIT then
  82.                 table.insert(data.points, res)
  83.             else
  84.                 data.done = true   
  85.             end
  86.         end
  87.     end
  88. end
  89.  
  90. local meshes = _G.GRASS_MESHES or {}
  91.  
  92. _G.GRASS_MESHES = meshes
  93.  
  94. local temp_ang = Angle(0,0,0)
  95. local function rot(vec, yaw)
  96.     temp_ang.y = yaw
  97.     temp_ang.p = math.Rand(-10, 10) * 2
  98.     vec:Rotate(temp_ang)
  99.    
  100.     return vec
  101. end
  102.  
  103. local up = Vector(0,0,1)
  104. local right = Vector(0,0,1)
  105. local gravity = physenv.GetGravity():GetNormalized()
  106.  
  107. local function generate_meshes()
  108.     for key, sector in pairs(DATA_LIST) do 
  109.        
  110.         if meshes[key] or not sector.done then continue end
  111.            
  112.         local mesh = Mesh()
  113.         local triangles = {}       
  114.             for _, trace_res in pairs(sector.points) do
  115.                 local pos = trace_res.HitPos - sector.origin
  116.                
  117.                 local width = math.Rand(-10, 10)
  118.                 local nrm = trace_res.HitNormal
  119.  
  120.                 local x, y = math.sin(pos.x/100), math.cos(pos.y/100)
  121.                 local height = math.Rand(10, 30) + math.abs((x^3-3*x+y^3-3*y) * 50)
  122.                
  123.                 local v1 = Vector(0, -width, 0)
  124.                 local v2 = Vector(0, 0, 0)
  125.                 local v3 = Vector(height, width, 0)
  126.                
  127.                 local ang = nrm:Angle()
  128.                 ang:RotateAroundAxis(nrm, math.random(360))
  129.                    
  130.                 v1:Rotate(ang)
  131.                 v2:Rotate(ang)
  132.                 v3:Rotate(ang)
  133.                    
  134.                 local c = render.GetAmbientLightColor(trace_res.HitPos)
  135.                 c = c + render.GetLightColor(trace_res.HitPos)
  136.                 --c = c + render.GetSurfaceColor(trace_res.HitPos, trace_res.HitPos + trace_res.Normal * 5)
  137.                
  138.                 c.r = c.r ^ 0.8
  139.                 c.g = c.g ^ 0.8
  140.                 c.b = c.b ^ 0.8
  141.                
  142.                 c = c * 255 * 2
  143.                
  144.                 c = Color(c.r, c.g, c.b)
  145.                
  146.                 local c2 = Color(c.r / 3, c.g / 3, c.b / 3)
  147.                
  148.                 table.insert(triangles, {u = 1, v = -0, pos = pos + v1, color = c})
  149.                 table.insert(triangles, {u = 0, v = -0, pos = pos + v2, color = c2})
  150.                 table.insert(triangles, {u = 0.5, v = -1, pos = pos + v3, color = c})
  151.             end    
  152.         mesh:BuildFromTriangles(triangles)
  153.        
  154.         local data = {}
  155.             data.origin = sector.origin
  156.             data.mesh = mesh
  157.             data.pixvis = util.GetPixelVisibleHandle()
  158.             data.rand = math.random()
  159.         meshes[key] = data
  160.     end
  161. end
  162.  
  163. local matrix = Matrix()
  164.  
  165. local material = CreateMaterial("grass_lol_" .. os.clock(), "UnlitGeneric",{
  166.     ["$basetexture"] = "models/props_swamp/tallgrass_01",
  167.     ["$vertexcolor"] = 1,
  168. })
  169.  
  170. local lol = ClientsideModel("error.mdl")
  171. lol:SetModelScale(0,0)
  172. local temp_vec = Vector(1,1,1)
  173.  
  174. local function draw(data)
  175.     render.CullMode(1)
  176.     data.mesh:Draw()
  177.     render.CullMode(0)
  178.     data.mesh:Draw()
  179. end
  180.  
  181. hook.Add("PostDrawOpaqueRenderables", "grass", function()      
  182.     local eyepos = LocalPlayer():EyePos()
  183.     local t = RealTime()
  184.     lol:DrawModel()
  185.     render.SetMaterial(material)
  186.    
  187.     for _, data in pairs(meshes) do
  188.         local alpha = util.PixelVisible(data.origin, GRID_SIZE*2, data.pixvis)
  189.         if alpha > 0 or data.origin:Distance(eyepos) < GRID_SIZE*2 then
  190.             matrix:SetTranslation(data.origin)         
  191.            
  192.             cam.PushModelMatrix(matrix)
  193.                 draw(data)
  194.             cam.PopModelMatrix()
  195.         end
  196.     end
  197.    
  198. --  if #meshes < 10 then
  199.         update()
  200.        
  201.         --if math.random() > 0.99 then
  202.             generate_meshes()  
  203.         --end
  204. --  end
  205.  
  206. end)
  207.  
  208. KillGrass = function()
  209.     hook.Remove("PostDrawOpaqueRenderables", "grass")
  210.     GRASS_DATA = nil
  211.     GRASS_DATA_LIST = nil
  212.     GRASS_MESHES = nil
  213.     collectgarbage("collect")
  214. end
Add Comment
Please, Sign In to add comment