Advertisement
CapsAdmin

Untitled

Mar 6th, 2013
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.68 KB | None | 0 0
  1. setfenv(1, _G)
  2.  
  3. water = {}
  4.  
  5. local water = water
  6.  
  7. function water.SetConfig(cfg)
  8.  
  9.     water.Resolution = cfg.resolution or 16
  10.     water.Size = cfg.size or 32768 * 8
  11.     water.Origin = cfg.origin or Vector(-32768, -32768, 0)*2
  12.     water.Height = cfg.height or 256
  13.     water.Depth = cfg.depth or 1024
  14.     water.Color = cfg.color or Color(32, 128, 200, 200)
  15.  
  16.     water.surfaces = {}
  17.     water.grid_size = water.Size / water.Resolution
  18.  
  19.     for y = 0, water.Resolution do
  20.         local vertices = {}
  21.  
  22.         for x = 0, water.Resolution do
  23.             table.insert(vertices, Vector(x * water.grid_size, y * water.grid_size, 0))
  24.             table.insert(vertices, Vector(x * water.grid_size, y * water.grid_size + water.grid_size, 0))
  25.         end
  26.  
  27.         table.insert(water.surfaces, vertices)
  28.     end
  29. end
  30.  
  31. water.SetConfig({})
  32.  
  33. function water.IsPositionUnderWater(v)
  34.     return v.x >= water.Origin.x and v.x <= water.Origin.x + water.Size and
  35.         v.y >= water.Origin.y and v.y <= water.Origin.y + water.Size and
  36.         v.z >= water.Origin.z and v.z <= water.Origin.z + water.Depth
  37. end
  38.  
  39. function water.GetDepthFromPosition(v)
  40.     return (water.Origin.z + water.Depth) - v.z
  41. end
  42.  
  43. do -- meta override
  44.     local META = FindMetaTable("Entity")
  45.    
  46.     water_oldWaterLevel = water_oldWaterLevel or META.WaterLevel
  47.     local water_oldWaterLevel = _G.water_oldWaterLevel
  48.  
  49.     function META:WaterLevel(...)
  50.         if water.IsPositionUnderWater(self:EyePos()) then
  51.             return 3
  52.         end
  53.        
  54.         return water_oldWaterLevel(self, ...)
  55.     end
  56. end
  57.  
  58.  
  59. hook.Add("Move", "water", function(ply, move)
  60.     if water.IsPositionUnderWater(ply:EyePos()) then
  61.         ply:SetGroundEntity(NULL)
  62.  
  63.         local vel = move:GetVelocity()
  64.         vel = vel + Vector(0, 0, 5)
  65.  
  66.         if ply:KeyDown(IN_FORWARD) then
  67.             vel = vel + ply:GetAimVector() * 5
  68.         end
  69.  
  70.         if ply:KeyDown(IN_JUMP) then
  71.             vel = vel + ply:GetUp() * 5
  72.         end
  73.  
  74.         vel = vel + move:GetVelocity() * -0.01
  75.  
  76.         move:SetVelocity(vel)
  77.     end
  78. end)
  79.  
  80. if SERVER then
  81.     hook.Add("Think", "float", function()
  82.         do return end
  83.         for _, ent in ipairs(ents.GetAll()) do
  84.             if water.IsPositionUnderWater(ent:EyePos()) then
  85.                 if ent:IsPlayer() then
  86.                     --ent:SetVelocity(Vector(0, 0, 25) + ent:GetVelocity() * -0.01)
  87.                     --ent:EmitSound("player/geiger3.wav")
  88.                 else
  89.                     local phys = ent:GetPhysicsObject()
  90.  
  91.                     if phys:IsValid() then
  92.                         local force = (phys:GetVolume()/phys:GetMass()) * 0.1
  93.  
  94.                         if phys:GetMaterial():sub(0,5) ~= "metal" then
  95.                             phys:AddVelocity(Vector(0, 0, force))
  96.                         else
  97.                             phys:AddVelocity(Vector(0, 0, force * 0.5))
  98.                         end
  99.                         phys:AddVelocity(phys:GetVelocity() * -0.1)
  100.                         phys:AddAngleVelocity(phys:GetAngleVelocity() * -0.1)
  101.                     end
  102.                 end
  103.             end
  104.         end
  105.     end)
  106. end
  107.  
  108. if CLIENT then
  109.  
  110.     local ply
  111.     local snd
  112.  
  113.     hook.Add("PreDrawHUD", "water", function()
  114.         ply = ply or LocalPlayer()
  115.         snd = snd or CreateSound(ply, "ambient/water/underwater.wav")
  116.        
  117.         if water.IsPositionUnderWater(ply:EyePos()) then
  118.             snd:PlayEx(100, 50)
  119.             ply:SetDSP(14)
  120.  
  121.             surface.SetDrawColor(water.Color.r*0.3, water.Color.g*0.3, water.Color.b*0.3, math.Clamp(water.Color.a*1.3, 0, 250))
  122.             surface.DrawRect(-2,-2, 4,4)
  123.  
  124.             ply.water_dsp_reset = false
  125.         elseif not ply.water_dsp_reset then
  126.             snd:Stop()
  127.             ply:SetDSP(0)
  128.  
  129.             ply.water_dsp_reset = true
  130.         end
  131.     end)
  132.  
  133.     water.Translucent = true
  134.     water.Material = CreateMaterial("water_overlay_" .. CurTime(),
  135.         "Refract",
  136.         {
  137.             ["$model"] = 1,
  138.             ["$nocull"] = 1,
  139.             ["$vertexcolor"] = 1,
  140.             ["$vertexalpha"] = 1,
  141.            
  142.             ["$refractamount"] = 0.1,
  143.             ["$normalmap"] = "dev/water_normal",
  144.  
  145.             Proxies =
  146.             {
  147.                 AnimatedTexture =
  148.                 {
  149.                     animatedtexturevar = "$normalmap",
  150.                     animatedtextureframenumvar = "$bumpframe",
  151.                     animatedtextureframerate = 30,
  152.                 },
  153.  
  154.             },
  155.         }
  156.     )
  157.  
  158.     local vector = Vector(0, 0, 0)
  159.    
  160.     -- hacky optimization
  161.     -- allows only the last draw call
  162.     local function setup_suppress()
  163.         local last_framenumber = 0
  164.         local current_frame = 0
  165.         local current_frame_count = 0
  166.        
  167.         return function()
  168.             local frame_number = FrameNumber()
  169.            
  170.             if frame_number == last_framenumber then
  171.                 current_frame = current_frame + 1
  172.             else
  173.                 last_framenumber = frame_number
  174.                            
  175.                 if current_frame_count ~= current_frame then
  176.                     current_frame_count = current_frame
  177.                 end
  178.                
  179.                 current_frame = 1
  180.             end
  181.                    
  182.             return current_frame < current_frame_count
  183.         end
  184.     end
  185.  
  186.     local should_suppres = setup_suppress()
  187.    
  188.     function water.Draw()
  189.         if should_suppres() then return end
  190.        
  191.         render.SetMaterial(water.Material)
  192.  
  193.         render.FogMode(MATERIAL_FOG_LINEAR)
  194.         render.FogStart(0)
  195.         render.FogEnd(512)
  196.         render.FogColor(water.Color.r, water.Color.g, water.Color.b)
  197.         render.FogMaxDensity(water.Color.a / 255)  
  198.  
  199.         local matrix = Matrix()
  200.         matrix:Translate(water.Origin + Vector(0, 0, water.Depth))
  201.        
  202.         local t = CurTime()
  203.        
  204.         render.SetBlend(0.1)
  205.        
  206.         cam.PushModelMatrix(matrix)
  207.             for y, vertices in ipairs(water.surfaces) do
  208.                 mesh.Begin(MATERIAL_TRIANGLE_STRIP, #vertices - 2)
  209.                     for x, vertex in ipairs(vertices) do
  210.                         vector.x = vertex.y
  211.                         vector.y = vertex.x
  212.                         vector.z = vertex.z + math.sin(vertex.x * 3 + vertex.y * 2.5 + t) * water.Height
  213.                        
  214.                         --mesh.Color(255, 0, 255, 100)
  215.  
  216.                         --mesh.Normal(vector)
  217.                         mesh.Position(vector)
  218.  
  219.                         mesh.TexCoord(0, vertex.x / 1024 + (t*0.2), vertex.y / 1024)
  220.  
  221.                         mesh.AdvanceVertex()
  222.                     end
  223.                 mesh.End()
  224.             end
  225.         cam.PopModelMatrix()
  226.  
  227.         render.FogMode(MATERIAL_FOG_NONE)
  228.         render.FogStart(0)
  229.         render.FogEnd(0)
  230.     end
  231.    
  232.     hook.Add("PostDrawTranslucentRenderables", "water", function() if water.Translucent then water.Draw() end end)
  233.     hook.Add("PostDrawOpaqueRenderables", "water", function() if not water.Translucent then water.Draw() end end)
  234. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement