Advertisement
CapsAdmin

Untitled

Feb 17th, 2013
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.76 KB | None | 0 0
  1. --if not LocalPlayer():CheckUserGroupLevel("developers") then return end
  2.  
  3. local mat_screenspace = Material("models/screenspace")
  4. local mdl_dome = Model("models/props_phx/construct/metal_dome360.mdl")
  5. local white = Color(255, 255, 255, 255)
  6.  
  7. SafeRemoveEntity(fractal_renderer)
  8.  
  9. local renderer = ClientsideModel(mdl_dome)
  10.  
  11. local function scale(ent, vec)
  12.     local mat = Matrix()
  13.     mat:Scale(vec)
  14.     ent:EnableMatrix("RenderMultiply", mat)
  15. end
  16.  
  17. RunConsoleCommand("pp_bloom", "1")
  18. RunConsoleCommand("pp_bloom_passes", "0")
  19. RunConsoleCommand("pp_bloom_multiply", "0")
  20. flomp = flomp or {} local f = flomp
  21. do -- particles
  22.    
  23.  
  24.     flomp.emitter = ParticleEmitter(EyePos(), false)
  25.     flomp.emitter:SetNoDraw(true)
  26.  
  27.     f.FFT = {}
  28.     f.FFT_Size = 1024
  29.  
  30.     f.FFT_Bass_1 = 0
  31.     f.FFT_Bass_2 = 0
  32.  
  33.     f.max_volume = 0
  34.     f.scale = 1
  35.     f.eye_angles = Angle(0)
  36.     f.eye_origin = vector_origin
  37.     f.vol_multplier = 1
  38.  
  39.     f.fft_detail = 10
  40.     f.mul_fft = f.FFT_Size/f.fft_detail
  41.  
  42.     for i = 1, f.FFT_Size do
  43.         f.FFT[i] = 0
  44.     end
  45.  
  46.     function flomp.SetSource(var)
  47.         var = type(var) == "number" and Entity(var) or var
  48.         if type(var) == "Vector" or (IsEntity(var) and var:IsValid()) then
  49.             f.source = var
  50.         end
  51.     end
  52.  
  53.     function flomp.SetScale(n)
  54.         f.scale = n
  55.     end
  56.  
  57.     function flomp.SetVolumeInputScale(n)
  58.         f.vol_multplier = n
  59.     end
  60.  
  61.     function flomp.GetSourcePos()
  62.         return type(f.source) == "Vector" and f.source or IsEntity(f.source) and f.source:IsValid() and f.source:EyePos() or vector_origin
  63.     end
  64.  
  65.     function flomp.GetAverage(istart, iend)
  66.         istart = math.Round(math.Clamp(istart, 1, f.FFT_Size))
  67.         iend = math.Round(math.Clamp(iend, 1, f.FFT_Size))
  68.         local n = 0
  69.         for i=istart, iend do
  70.         --  if f.FFT[i] then
  71.                 n = n + f.FFT[i]
  72.     --      end
  73.         end
  74.  
  75.         local div = (iend - istart)
  76.  
  77.         return div == 0 and 0 or (n / div)
  78.     end
  79.  
  80.     function flomp.IsAround(number, min, max)
  81.         return number > min and number < max and true or false
  82.     end
  83.  
  84.     function flomp.ScaleVolume(volume, peak)
  85.         return ((volume ^ flomp.powscale) * f.vol_multplier) * 2.5
  86.     end
  87.  
  88.     function flomp.Spectrum2D()
  89.         do return end
  90.         --if bawss and bawss.channel then bawss.channel:stop() bawss = nil end -- declan interuption protection
  91.  
  92.         local h = ScrH() + -400
  93.         local w = ScrW()
  94.         local volume = 0
  95.  
  96.         for fr = 1, f.FFT_Size do
  97.             volume = f.ScaleVolume(f.FFT[fr])
  98.  
  99.             surface.SetDrawColor(volume,volume,255*volume,255)
  100.             surface.DrawLine(
  101.                 (w+fr)-ScrW(), h,
  102.                 (w+fr)-ScrW(), h-(volume*50)
  103.             )
  104.         end
  105.     end
  106.  
  107.     local time = 0
  108.  
  109.     function flomp.SpectrumUpdate(data, peak)
  110.         if flomp.sound_vol == 0 then return end
  111.         f.FFT = data
  112.  
  113.         f.FFT_Bass_1 = flomp.GetAverage(1, 6)
  114.        
  115.         time = time + f.FFT_Bass_1 * 50
  116.  
  117.         for i = 1, 4 do
  118.  
  119.             local fr = (i * 256)
  120.  
  121.             local volume = f.ScaleVolume(f.FFT[math.Clamp(math.Round(i*f.mul_fft), 1, f.FFT_Size)], peak)
  122.  
  123.             if volume < 0.01 then continue end
  124.  
  125.             local n_fr = -(fr-30) + f.FFT_Size -- negative fr, f.FFT_Size to 0
  126.  
  127.             local f_fr = (fr-30)/f.FFT_Size -- fraction fr, 0, 1
  128.             local nf_fr = n_fr/f.FFT_Size -- negative fraction, 1, 0
  129.  
  130.             local max = 32
  131.            
  132.             for i2 = 1, max do
  133.                 local pi = (i2/max) * math.pi * 2
  134.  
  135.                 local size = (f.FFT_Bass_1 * 50 ^ 1.5)
  136.                 local color = HSVToColor((time+(pi*volume))%360, 1, 1)
  137.                
  138.                 local velocity = Vector(math.sin(pi+i+time), -volume / 5, math.cos(pi+i+time)) * volume ^ 1.3
  139.                 velocity = velocity * 5
  140.  
  141.                 local particle = f.emitter:Add("particle/Particle_Glow_04_Additive", f:GetSourcePos() + (velocity*5*f.scale))
  142.  
  143.                 particle:SetVelocity(velocity*1000*f.scale*5)
  144.  
  145.                 particle:SetLifeTime(0)
  146.                 particle:SetDieTime(math.Clamp(volume*0.2, 0.1, 0.8))
  147.  
  148.                 particle:SetStartLength(size*3*f.scale * 2*2)
  149.                 particle:SetEndLength(size*1.5*f.scale)
  150.                 particle:SetStartSize(size*f.scale*volume*0.5*2)
  151.                 particle:SetEndSize(0)
  152.  
  153.                 particle:SetStartAlpha(50)
  154.                 particle:SetEndAlpha(0)
  155.  
  156.                 particle:SetAirResistance(math.Clamp((-size+800), 10, 1200)*f.scale)
  157.                 --particle:SetGravity((VectorRand()*50)*f.scale)
  158.  
  159.                 particle:SetColor(color.r, color.g, color.b)
  160.                 particle:SetCollide(true)
  161.                 particle:SetBounce(0.1)
  162.             end
  163.         end
  164.     end
  165.  
  166.     f.smooth_pp = 0
  167.     f.look_at_mult = 0
  168.  
  169.     function flomp.DrawPostProcess()
  170.         if flomp.sound_vol == 0 then return end
  171.         local w = ScrW()
  172.         local h = ScrH()
  173.  
  174.         local vec2 = f.GetSourcePos():ToScreen()
  175.  
  176.         local m = math.max(f.eye_angles:Forward():DotProduct((f.GetSourcePos()-f.eye_origin):GetNormalized()), 0) ^ 3
  177.         f.look_at_mult = m
  178.         m = m * (f.sound_vol * 2) ^ 2
  179.         m = m / 5
  180.        
  181.         --m = m * math.Clamp((-f.eye_origin:Distance(f.GetSourcePos()) / 15000) + 1, 0, 1)
  182.    
  183.         if m < 0.001 then return end
  184.  
  185.         m = math.Clamp(m, 0, 1)
  186.  
  187.         local vol = f.FFT_Bass_1
  188.         local darkness = (-m+1)
  189.  
  190.         local avr = math.Clamp(vol * 2  - 0.1,0,1) * m
  191.         local blur = math.Clamp((vol*-0.5)+1, 0.2, 1)
  192.         local invert = (vol*-10+1)
  193.  
  194.         local angle = Angle(math.Rand(-1,1), math.Rand(-1,1), math.Rand(-1,1)) * f.GetAverage(3, 7) * m
  195.         angle.x = math.Clamp(angle.p, -0.52, 0.52)
  196.         angle.y = math.Clamp(angle.y, -0.52, 0.52)
  197.         angle.z = 0
  198.         LocalPlayer():SetEyeAngles(LocalPlayer():EyeAngles() + angle)
  199.  
  200.         f.smooth_pp = f.smooth_pp + ((avr - f.smooth_pp)*FrameTime()*10)
  201.  
  202.         local mscale = m * f.vol_multplier
  203.  
  204.         local tbl= {}
  205.         tbl[ "$pp_colour_addr" ] = 0
  206.         tbl[ "$pp_colour_addg" ] = 0
  207.         tbl[ "$pp_colour_addb" ] = 0
  208.         tbl[ "$pp_colour_brightness" ] = -0.02
  209.         tbl[ "$pp_colour_contrast" ] = 1.1-m
  210.         tbl[ "$pp_colour_colour" ] = 1.25+(m^0.75)
  211.         tbl[ "$pp_colour_mulr" ] = 0
  212.         tbl[ "$pp_colour_mulg" ] = 0
  213.         tbl[ "$pp_colour_mulb" ] = 0
  214.  
  215.  
  216.         --DrawMotionBlur(blur, 1, 0)
  217.         --DrawBloom(darkness, invert*m, math.max(invert*20+2, 5), math.max(invert*20+10, 5), 4, 10, 1, 1, 1 )
  218.         DrawSunbeams(0.9, math.max(vol/4, 0.1), 0.2, vec2.x / ScrW(), vec2.y / ScrH())
  219.         DrawColorModify(tbl)
  220.         --DrawToyTown(Lerp(avr, 0, 10), ScrH()*0.55)
  221.         DrawSharpen(m*10,m)
  222.        
  223.         local avr = (flomp.GetAverage(3, 5) * 2.5) ^ 1.5
  224.         flomp.fov = Lerp(avr ^ 1.5, 150, 60)
  225.     end
  226.  
  227.     hook.Add("HUDPaint", "flomp_Helper", flomp.Spectrum2D)
  228.     hook.Add("RenderScreenspaceEffects", "flomp_RenderScreenspaceEffects", flomp.DrawPostProcess)
  229.  
  230.  
  231.     hook.Add("RenderScene", "flomp_CalcView", function(pos, ang)
  232.         f.eye_origin = pos
  233.         f.eye_angles = ang
  234.     end)
  235.  
  236.     local voldata = Vector(0,0,0) -- V, L, R
  237.  
  238.     local function calcsource(eye, rgt, src, dist, fwd, vel)
  239.         local vol = math.Clamp(-((eye - src):Length() / dist) + 1, 0, 1)
  240.         local dot = rgt:Dot((src - eye):GetNormalized())
  241.  
  242.         local left = math.Clamp(-dot, 0, 1) + 0.5
  243.         local right = math.Clamp(dot, 0, 1) + 0.5
  244.  
  245.         if vol ~= 0 then
  246.             return vol, -left + right
  247.         end
  248.     end
  249.  
  250.     function GetVolumeData(source, falloff)
  251.         local ply = LocalPlayer()
  252.         return calcsource(ply:EyePos(),f.eye_angles:Right(), source, falloff, f.eye_angles:Forward(), ply:GetVelocity())
  253.     end
  254.  
  255.     f.sound_vol = 0
  256.  
  257.     hook.Add("Think","flomp_volume",function()
  258.         local vol, panning = GetVolumeData(flomp.GetSourcePos(), 4000)
  259.  
  260.         if vol then
  261.             f.sound_vol = vol
  262.             if not f.wowo then vol = vol * f.look_at_mult end
  263.            
  264.             hook.Call("FlompVolume", GAMEMODE, (vol^1.5)*2, panning)
  265.         else
  266.             hook.Call("FlompVolume", GAMEMODE, 0, 0)
  267.         end
  268.     end)
  269.  
  270.  
  271.     hook.Add("HUDPaintBackground", "itsaparty")
  272.     hook.Add("CalcView", "itsaparty")
  273.    
  274.    
  275.     local last_peak = 1
  276.     hook.Add("Spectrum", "flomp_spectrum", function(fft)
  277.        
  278.         local peak = 0
  279.        
  280.         for k,v in pairs(fft) do
  281.             peak = math.max(peak, v)
  282.             fft[k] = (fft[k] ^ 1.75) * 0.75
  283.         end
  284.        
  285.         last_peak = 0
  286.        
  287.         flomp.SpectrumUpdate(fft, peak)
  288.     end)
  289. end
  290.  
  291. local size = 30
  292.  
  293. flomp.fov = 75-40
  294. local t = 0
  295. local offset = 6
  296.  
  297. function renderer:RenderOverride()
  298.     cam.Start3D(EyePos() + (EyeAngles():Right() * offset) + (EyeAngles():Up() * offset), EyeAngles(), (flomp.fov or 0) + 40)
  299.     --self:SetAngles(Angle(90,90, t*50%360))
  300.     render.SuppressEngineLighting(true)
  301.         render.MaterialOverride(mat_screenspace)
  302.         --render.DrawSphere(self:GetPos(), -50, 32, 32, white)
  303.        
  304.         render.SetBlend(0.9)
  305.         self:SetRenderOrigin(self:GetPos() + self:GetUp() * -size)
  306.         scale(self, Vector(1,1,1) * size)
  307.         self:SetupBones()
  308.         self:DrawModel()
  309.        
  310.         render.CullMode(MATERIAL_CULLMODE_CW)
  311.         self:SetRenderOrigin(self:GetPos() + self:GetUp() * size)
  312.         scale(self, Vector(1,-1,1) * size)
  313.         self:SetupBones()
  314.         self:DrawModel()
  315.         render.MaterialOverride()
  316.         render.CullMode(MATERIAL_CULLMODE_CCW)
  317.        
  318.         render.SetBlend(1)
  319.        
  320.         self:SetRenderOrigin()
  321.     render.SuppressEngineLighting(false)
  322.    
  323.     flomp.SetSource(self:GetPos())
  324.     cam.End3D()
  325. end
  326.  
  327. SafeRemoveEntity(fractal_tree)
  328.  
  329. local tree = ClientsideModel("models/props_combine/combine_bridge.mdl")
  330. tree:SetPos(LocalPlayer():EyePos() + LocalPlayer():GetAimVector() * 20)
  331. timer.Simple(0.1, function()
  332.     function tree:RenderOverride()
  333.         t = t + flomp.FFT_Bass_1 / 1000
  334.        
  335.         --render.SetBlend(0.999)
  336.         for i = 0, 16 do
  337.             i = (i / 16) * 360
  338.             scale(self, Vector(5,math.cos(t)*4,5))
  339.             self:SetAngles(Angle(t+i, 0, math.sin(t)*180))
  340.             self:SetupBones()
  341.             render.SuppressEngineLighting(true)
  342.             render.MaterialOverride(mat_screenspace)
  343.             self:DrawModel()
  344.             render.MaterialOverride()
  345.             render.SuppressEngineLighting(false)
  346.         end
  347.         --render.SetBlend(1)
  348.        
  349.         self:SetPos(fractal_renderer:GetPos() + fractal_renderer:GetUp() * 200)
  350.        
  351.         cam.IgnoreZ(true)
  352.         flomp.emitter:SetPos(self:GetPos())
  353.         flomp.emitter:Draw()
  354.         cam.IgnoreZ(false)
  355.     end
  356. end)
  357.  
  358. fractal_tree = tree
  359.  
  360. renderer:Spawn()
  361.  
  362.  
  363. flomp.SetScale(1)
  364. flomp.SetVolumeInputScale(3)
  365. flomp.powscale = 1
  366.  
  367. fractal_renderer = renderer
  368.  
  369.  
  370. hook.Add("RenderScene", "flomp_CalcView", function(pos, ang)
  371.     f.eye_origin = pos
  372.     f.eye_angles = ang
  373. end)
  374.  
  375. local voldata = Vector(0,0,0) -- V, L, R
  376.  
  377. local function calcsource(eye, rgt, src, dist, fwd, vel)
  378.     local vol = math.Clamp(-((eye - src):Length() / dist) + 1, 0, 1)
  379.     local dot = rgt:Dot((src - eye):Normalize())
  380.  
  381.     local left = math.Clamp(-dot, 0, 1) + 0.5
  382.     local right = math.Clamp(dot, 0, 1) + 0.5
  383.  
  384.     if vol ~= 0 then
  385.         return vol, -left + right
  386.     end
  387. end
  388.  
  389. function GetVolumeData(source, falloff)
  390.     local ply = LocalPlayer()
  391.     return calcsource(ply:EyePos(),f.eye_angles:Right(), source, falloff, f.eye_angles:Forward(), ply:GetVelocity())
  392. end
  393.  
  394. f.sound_vol = 0
  395.  
  396. hook.Add("Think","flomp_volume",function()
  397.     local vol, panning = GetVolumeData(flomp.GetSourcePos(), 7000)
  398.  
  399.     if vol then
  400.         f.sound_vol = vol
  401.        
  402.         if not f.wowo then
  403.             vol = vol * f.look_at_mult
  404.         end
  405.        
  406.         hook.Call("FlompVolume", GAMEMODE, (vol^1.5)*2, panning)
  407.     else
  408.         hook.Call("FlompVolume", GAMEMODE, 0, 0)
  409.     end
  410.    
  411.     renderer:SetPos(Vector(-5381.6665039062, -3296.0832519531, 314.22338867188))
  412.     tree:SetPos(renderer:GetPos())
  413.     renderer:SetAngles(Angle(90,90,0))
  414.     renderer:SetRenderBounds(Vector(1,1,1)*-size*100, Vector(1,1,1)*size*100)
  415.     flomp.SetSource(renderer:GetPos())
  416. end)
  417.  
  418. function flomp.Panic()
  419.     if html and html:IsValid() then html:Remove() end
  420.     SafeRemoveEntity(renderer)
  421.     SafeRemoveEntity(fractal_tree)
  422.     hook.Remove("Think", "flomp_volume")
  423.     hook.Remove("HUDPaint", "flomp_Helper")
  424.     hook.Remove("RenderScreenspaceEffects", "flomp_RenderScreenspaceEffects")
  425.     hook.Remove("RenderScene", "flomp_CalcView")
  426.     hook.Remove("Spectrum", "flomp_spectrum")
  427. end
  428.  
  429. hook.Add("HUDPaintBackground", "itsaparty")
  430. hook.Add("CalcView", "itsaparty")
  431.  
  432.  
  433. local last_peak = 1
  434. hook.Add("Spectrum", "flomp_spectrum", function(fft)
  435.    
  436.     local peak = 0
  437.    
  438.     for k,v in pairs(fft) do
  439.         peak = math.max(peak, v)
  440.         fft[k] = (fft[k] ^ 2)
  441.     end
  442.    
  443.     last_peak = 0
  444.    
  445.     flomp.SpectrumUpdate(fft, peak)
  446. end)
  447.    
  448. flomp.SetScale(0.3)
  449. flomp.SetVolumeInputScale(1.25)
  450.  
  451. flomp.powscale = 1.75
  452.  
  453. hook.Add("HUDPaintBackground", "itsaparty")
  454. hook.Add("CalcView", "itsaparty")
  455.  
  456.  
  457. function startmusic(URL)
  458.     URL = URL or "http://dl.dropbox.com/u/244444/wtw.ogg"
  459.  
  460.     if html and html:IsValid() then html:Remove() end
  461.  
  462.     local html = vgui.Create("DHTML") _G.html = html
  463.  
  464.     html:AddFunction("gmod", "print", print)
  465.     html:AddFunction("gmod", "data", function(data)
  466.         if flomp.sound_vol == 0 then return end
  467.         pcall(function()
  468.             local data = CompileString(data, "data")()
  469.             hook.Call("Spectrum", nil, data)
  470.         end)
  471.     end)
  472.  
  473.     local player = setmetatable(
  474.         {
  475.         },
  476.         {
  477.             __index = function(self, func_name)
  478.                 return function(...)
  479.                     local tbl = {...}
  480.                    
  481.                     for key, val in pairs(tbl) do
  482.                         tbl[key] = tostring(val)
  483.                        
  484.                         if tbl[key] == "nil" or tbl[key] == "NULL" then
  485.                             tbl[key] = "null"
  486.                         end
  487.                     end
  488.                    
  489.                     local str = ("%s(%q)"):format(func_name, table.concat(tbl, ", "))
  490.                     html:QueueJavascript(str)
  491.                     --print(str)
  492.                 end
  493.             end
  494.         }
  495.     )
  496.  
  497.     html:SetPos(ScrW(), ScrH())
  498.     html:OpenURL("http://dl.dropbox.com/u/244444/gmod_audio.html")
  499.     html:QueueJavascript[[
  500.         var AudioContext = window.AudioContext || window.webkitAudioContext;
  501.  
  502.         window.onerror = function(desc, file, line)
  503.         {
  504.             gmod.print(desc)
  505.             gmod.print(file)
  506.             gmod.print(line)
  507.         }
  508.  
  509.         var audio = new AudioContext
  510.         var analyser = audio.createAnalyser()
  511.         analyser.connect(audio.destination)
  512.        
  513.         setInterval(
  514.             function()
  515.             {
  516.                 var spectrum = new Uint8Array(analyser.frequencyBinCount);
  517.                 analyser.getByteFrequencyData(spectrum);
  518.                
  519.                 var lol = new Array(spectrum.length);
  520.                
  521.                 for(var i = 0; i < spectrum.length; ++i)
  522.                     lol[i] = spectrum[i] / 255;
  523.                
  524.                 gmod.data("return {" + lol.join(",") + "}");
  525.             },
  526.             15
  527.         );
  528.        
  529.         function download(url, callback)
  530.         {
  531.             var request = new XMLHttpRequest
  532.            
  533.             request.open("GET", url, true)
  534.             request.responseType = "arraybuffer"
  535.             request.send(null)
  536.            
  537.             request.onload = function()
  538.             {
  539.                 gmod.print("loaded \"" + url + "\"")
  540.                 gmod.print("status " + request.status)
  541.                 callback(request.response)
  542.             }
  543.            
  544.             request.onprogress = function(event)
  545.             {
  546.                 gmod.print(Math.round(event.loaded / event.total * 100) + "%")
  547.             }      
  548.         }
  549.        
  550.         var source = audio.createBufferSource()
  551.         var volctrl = audio.createGainNode()
  552.        
  553.         function play(url)             
  554.         {              
  555.             download(url, function(data)
  556.             {
  557.                 gmod.print("decoding " + data.byteLength + " ...")
  558.                 audio.decodeAudioData(data, function(buffer)
  559.                 {
  560.                     source = audio.createBufferSource()
  561.                    
  562.                     source.connect(analyser)
  563.                     analyser.connect(volctrl)
  564.                     volctrl.connect(audio.destination)
  565.                    
  566.                     source.buffer = buffer
  567.                     source.loop = true
  568.                     source.noteOn(0)
  569.                    
  570.                     gmod.print("LOADED AND DECODED")
  571.                 },
  572.                 function(err)
  573.                 {
  574.                     gmod.print("decoding error " + err)
  575.                 })
  576.             })
  577.         }
  578.        
  579.         function SetVolume(vol)
  580.         {
  581.             if(volctrl) volctrl.gain.value = vol
  582.         }
  583.     ]]
  584.  
  585.     player.play(URL)
  586.     timer.Remove("fractals")
  587.     timer.Simple(1, function()
  588.         hook.Add("FlompVolume", "flomp_html_volume", function(vol)
  589.             if not html and html:IsValid() then return end
  590.             player.SetVolume(vol-1)
  591.         end)
  592.     end)
  593. end
  594.  
  595. if not html then
  596.     startmusic()
  597. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement