CapsAdmin

Untitled

Oct 25th, 2012
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.26 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.  
  21. do -- particles
  22.     flomp = flomp or {} local f = flomp
  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.         f.FFT = data
  111.  
  112.         f.FFT_Bass_1 = flomp.GetAverage(1, 6)
  113.        
  114.         time = time + f.FFT_Bass_1 * 50
  115.  
  116.         for i = 1, 4 do
  117.  
  118.             local fr = (i * 256)
  119.  
  120.             local volume = f.ScaleVolume(f.FFT[math.Clamp(math.Round(i*f.mul_fft), 1, f.FFT_Size)], peak)
  121.  
  122.             if volume < 0.01 then continue end
  123.  
  124.             local n_fr = -(fr-30) + f.FFT_Size -- negative fr, f.FFT_Size to 0
  125.  
  126.             local f_fr = (fr-30)/f.FFT_Size -- fraction fr, 0, 1
  127.             local nf_fr = n_fr/f.FFT_Size -- negative fraction, 1, 0
  128.  
  129.             local max = 32
  130.            
  131.             for i2 = 1, max do
  132.                 local pi = (i2/max) * math.pi * 2
  133.  
  134.                 local size = (f.FFT_Bass_1 * 20 ^ 1.5)
  135.                 local color = HSVToColor((time+(pi*volume))%360, 1, 1)
  136.                
  137.                 local velocity = Vector(math.sin(pi+i+time), -volume / 5, math.cos(pi+i+time)) * volume ^ 1.3
  138.                 velocity = velocity * 2
  139.  
  140.                 local particle = f.emitter:Add("particle/Particle_Glow_04_Additive", f:GetSourcePos() + (velocity*5*f.scale))
  141.  
  142.                 particle:SetVelocity(velocity*1000*f.scale)
  143.  
  144.                 particle:SetLifeTime(0)
  145.                 particle:SetDieTime(math.Clamp(volume*0.2, 0.1, 0.8))
  146.  
  147.                 particle:SetStartLength(size*3*f.scale * 2)
  148.                 particle:SetEndLength(size*1.5*f.scale)
  149.                 particle:SetStartSize(size*f.scale*volume*0.5)
  150.                 particle:SetEndSize(0)
  151.  
  152.                 particle:SetStartAlpha(50)
  153.                 particle:SetEndAlpha(0)
  154.  
  155.                 particle:SetAirResistance(math.Clamp((-size+800), 10, 1200)*f.scale)
  156.                 --particle:SetGravity((VectorRand()*50)*f.scale)
  157.  
  158.                 particle:SetColor(color.r, color.g, color.b)
  159.                 particle:SetCollide(true)
  160.                 particle:SetBounce(0.1)
  161.             end
  162.         end
  163.     end
  164.  
  165.     f.smooth_pp = 0
  166.     f.look_at_mult = 0
  167.  
  168.     function flomp.DrawPostProcess()
  169.         local w = ScrW()
  170.         local h = ScrH()
  171.  
  172.         local vec2 = f.GetSourcePos():ToScreen()
  173.  
  174.         local m = math.max(f.eye_angles:Forward():DotProduct((f.GetSourcePos()-f.eye_origin):GetNormalized()), 0) ^ 3
  175.         f.look_at_mult = m
  176.         m = m * (f.sound_vol * 2) ^ 2
  177.         m = m / 5
  178.        
  179.         --m = m * math.Clamp((-f.eye_origin:Distance(f.GetSourcePos()) / 15000) + 1, 0, 1)
  180.    
  181.         if m < 0.001 then return end
  182.  
  183.         m = math.Clamp(m, 0, 1)
  184.  
  185.         local vol = f.FFT_Bass_1
  186.         local darkness = (-m+1)
  187.  
  188.         local avr = math.Clamp(vol * 2  - 0.1,0,1) * m
  189.         local blur = math.Clamp((vol*-0.5)+1, 0.2, 1)
  190.         local invert = (vol*-10+1)
  191.  
  192.         local angle = Angle(math.Rand(-1,1), math.Rand(-1,1), math.Rand(-1,1)) * f.GetAverage(3, 7) ^ 1.5 * m
  193.         angle.x = math.Clamp(angle.p, -0.52, 0.52)
  194.         angle.y = math.Clamp(angle.y, -0.52, 0.52)
  195.         angle.z = 0
  196.         LocalPlayer():SetEyeAngles(LocalPlayer():EyeAngles() + angle)
  197.  
  198.         f.smooth_pp = f.smooth_pp + ((avr - f.smooth_pp)*FrameTime()*10)
  199.  
  200.         local mscale = m * f.vol_multplier
  201.  
  202.         local tbl= {}
  203.         tbl[ "$pp_colour_addr" ] = 0
  204.         tbl[ "$pp_colour_addg" ] = 0
  205.         tbl[ "$pp_colour_addb" ] = 0
  206.         tbl[ "$pp_colour_brightness" ] = -0.02
  207.         tbl[ "$pp_colour_contrast" ] = 1.1-m
  208.         tbl[ "$pp_colour_colour" ] = 1.25+(m^0.75)
  209.         tbl[ "$pp_colour_mulr" ] = 0
  210.         tbl[ "$pp_colour_mulg" ] = 0
  211.         tbl[ "$pp_colour_mulb" ] = 0
  212.  
  213.  
  214.         --DrawMotionBlur(blur, 1, 0)
  215.         --DrawBloom(darkness, invert*m, math.max(invert*20+2, 5), math.max(invert*20+10, 5), 4, 10, 1, 1, 1 )
  216.         DrawSunbeams(0.9, math.max(vol/4, 0.1), 0.2, vec2.x / ScrW(), vec2.y / ScrH())
  217.         DrawColorModify(tbl)
  218.         --DrawToyTown(Lerp(avr, 0, 10), ScrH()*0.55)
  219.         DrawSharpen(m*10,m)
  220.        
  221.         local avr = (flomp.GetAverage(3, 5) * 2.5) ^ 1.5
  222.         flomp.fov = Lerp(avr ^ 1.5, 150, 60)
  223.     end
  224.  
  225.     hook.Add("HUDPaint", "flomp_Helper", flomp.Spectrum2D)
  226.     hook.Add("RenderScreenspaceEffects", "flomp_RenderScreenspaceEffects", flomp.DrawPostProcess)
  227.  
  228.  
  229.     hook.Add("RenderScene", "flomp_CalcView", function(pos, ang)
  230.         f.eye_origin = pos
  231.         f.eye_angles = ang
  232.     end)
  233.  
  234.     local voldata = Vector(0,0,0) -- V, L, R
  235.  
  236.     local function calcsource(eye, rgt, src, dist, fwd, vel)
  237.         local vol = math.Clamp(-((eye - src):Length() / dist) + 1, 0, 1)
  238.         local dot = rgt:Dot((src - eye):GetNormalized())
  239.  
  240.         local left = math.Clamp(-dot, 0, 1) + 0.5
  241.         local right = math.Clamp(dot, 0, 1) + 0.5
  242.  
  243.         if vol ~= 0 then
  244.             return vol, -left + right
  245.         end
  246.     end
  247.  
  248.     function GetVolumeData(source, falloff)
  249.         local ply = LocalPlayer()
  250.         return calcsource(ply:EyePos(),f.eye_angles:Right(), source, falloff, f.eye_angles:Forward(), ply:GetVelocity())
  251.     end
  252.  
  253.     f.sound_vol = 0
  254.  
  255.     hook.Add("Think","flomp_volume",function()
  256.         local vol, panning = GetVolumeData(flomp.GetSourcePos(), 4000)
  257.  
  258.         if vol then
  259.             f.sound_vol = vol
  260.             if not f.wowo then vol = vol * f.look_at_mult end
  261.            
  262.             hook.Call("FlompVolume", GAMEMODE, (vol^1.5)*2, panning)
  263.         else
  264.             hook.Call("FlompVolume", GAMEMODE, 0, 0)
  265.         end
  266.     end)
  267.  
  268.  
  269.     hook.Add("HUDPaintBackground", "itsaparty")
  270.     hook.Add("CalcView", "itsaparty")
  271.    
  272.    
  273.     local last_peak = 1
  274.     hook.Add("Spectrum", "flomp_spectrum", function(fft)
  275.        
  276.         local peak = 0
  277.        
  278.         for k,v in pairs(fft) do
  279.             peak = math.max(peak, v)
  280.             fft[k] = (fft[k] ^ 1.75) * 0.75
  281.         end
  282.        
  283.         last_peak = 0
  284.        
  285.         flomp.SpectrumUpdate(fft, peak)
  286.     end)
  287. end
  288.  
  289. local size = 70
  290.  
  291. flomp.fov = 75-40
  292. local t = 0
  293. local offset = 6
  294.  
  295. function renderer:RenderOverride()
  296.     cam.Start3D(EyePos() + (EyeAngles():Right() * offset) + (EyeAngles():Up() * offset), EyeAngles(), (flomp.fov or 0) + 40)
  297.     --self:SetAngles(Angle(90,90, t*50%360))
  298.     render.SuppressEngineLighting(true)
  299.         render.MaterialOverride(mat_screenspace)
  300.         --render.DrawSphere(self:GetPos(), -50, 32, 32, white)
  301.        
  302.         self:SetRenderOrigin(self:GetPos() + self:GetUp() * -size)
  303.         scale(self, Vector(1,1,1) * size)
  304.         self:SetupBones()
  305.         self:DrawModel()
  306.        
  307.         render.CullMode(MATERIAL_CULLMODE_CW)
  308.         self:SetRenderOrigin(self:GetPos() + self:GetUp() * size)
  309.         scale(self, Vector(1,-1,1) * size)
  310.         self:SetupBones()
  311.         self:DrawModel()
  312.         render.MaterialOverride()
  313.         render.CullMode(MATERIAL_CULLMODE_CCW)
  314.        
  315.         self:SetRenderOrigin()
  316.     render.SuppressEngineLighting(false)
  317.    
  318.     flomp.SetSource(self:GetPos())
  319.     cam.End3D()
  320. end
  321.  
  322. SafeRemoveEntity(fractal_tree)
  323.  
  324. local tree = ClientsideModel("models/props_combine/combinethumper001a.mdl")
  325. tree:SetPos(LocalPlayer():EyePos() + LocalPlayer():GetAimVector() * 20)
  326. timer.Simple(0.1, function()
  327.     function tree:RenderOverride()
  328.         t = t + flomp.FFT_Bass_1 / 1000
  329.        
  330.         for i = 0, 16 do
  331.             i = (i / 16) * 360
  332.             scale(self, Vector(5,math.cos(t)*4,10))
  333.             self:SetAngles(Angle(t+i, 0, math.sin(t)*180))
  334.             self:SetupBones()
  335.             render.SuppressEngineLighting(true)
  336.             render.MaterialOverride(mat_screenspace)
  337.             self:DrawModel()
  338.             render.MaterialOverride()
  339.             render.SuppressEngineLighting(false)
  340.         end
  341.        
  342.         self:SetPos(fractal_renderer:GetPos() + fractal_renderer:GetUp() * 200)
  343.        
  344.         flomp.emitter:SetPos(self:GetPos())
  345.         flomp.emitter:Draw()
  346.     end
  347. end)
  348.  
  349. fractal_tree = tree
  350.  
  351. renderer:SetPos(Vector(-178, -3, -4777))
  352. renderer:SetAngles(Angle(90,90,0))
  353. renderer:Spawn()
  354. renderer:SetRenderBounds(Vector(1,1,1)*-size*100, Vector(1,1,1)*size*100)
  355.  
  356. flomp.SetScale(1)
  357. flomp.SetVolumeInputScale(3)
  358. flomp.powscale = 1
  359.  
  360. fractal_renderer = renderer
  361.  
  362. --if html then return end
  363.  
  364. do
  365.     if html and html:IsValid() then html:Remove() end
  366.  
  367.     local html = vgui.Create("DHTML") _G.html = html
  368.  
  369.     html:AddFunction("gmod", "print", print)
  370.     html:AddFunction("gmod", "data", function(data)
  371.         pcall(function()
  372.             local data = CompileString(data, "data")()
  373.             hook.Call("Spectrum", nil, data)
  374.         end)
  375.     end)
  376.  
  377.     local player = setmetatable(
  378.         {
  379.         },
  380.         {
  381.             __index = function(self, func_name)
  382.                 return function(...)
  383.                     local tbl = {...}
  384.                    
  385.                     for key, val in pairs(tbl) do
  386.                         tbl[key] = tostring(val)
  387.                        
  388.                         if tbl[key] == "nil" or tbl[key] == "NULL" then
  389.                             tbl[key] = "null"
  390.                         end
  391.                     end
  392.                    
  393.                     local str = ("%s(%q)"):format(func_name, table.concat(tbl, ", "))
  394.                     html:QueueJavascript(str)
  395.                     --print(str)
  396.                 end
  397.             end
  398.         }
  399.     )
  400.  
  401.     html:SetPos(ScrW(), ScrH())
  402.     html:OpenURL("http://dl.dropbox.com/u/244444/gmod_audio.html")
  403.     html:QueueJavascript[[
  404.         var AudioContext = window.AudioContext || window.webkitAudioContext;
  405.  
  406.         window.onerror = function(desc, file, line)
  407.         {
  408.             gmod.print(desc)
  409.             gmod.print(file)
  410.             gmod.print(line)
  411.         }
  412.  
  413.         var audio = new AudioContext
  414.         var analyser = audio.createAnalyser()
  415.         analyser.connect(audio.destination)
  416.        
  417.         setInterval(
  418.             function()
  419.             {
  420.                 var spectrum = new Uint8Array(analyser.frequencyBinCount);
  421.                 analyser.getByteFrequencyData(spectrum);
  422.                
  423.                 var lol = new Array(spectrum.length);
  424.                
  425.                 for(var i = 0; i < spectrum.length; ++i)
  426.                     lol[i] = spectrum[i] / 255;
  427.                
  428.                 gmod.data("return {" + lol.join(",") + "}");
  429.             },
  430.             20
  431.         );
  432.        
  433.         function download(url, callback)
  434.         {
  435.             var request = new XMLHttpRequest
  436.            
  437.             request.open("GET", url, true)
  438.             request.responseType = "arraybuffer"
  439.             request.send(null)
  440.            
  441.             request.onload = function()
  442.             {
  443.                 gmod.print("loaded \"" + url + "\"")
  444.                 gmod.print("status " + request.status)
  445.                 callback(request.response)
  446.             }
  447.            
  448.             request.onprogress = function(event)
  449.             {
  450.                 gmod.print(Math.round(event.loaded / event.total * 100) + "%")
  451.             }      
  452.         }
  453.        
  454.         var source = audio.createBufferSource()
  455.         var panctrl = audio.createPanner()
  456.        
  457.         function play(url)             
  458.         {              
  459.             download(url, function(data)
  460.             {
  461.                 gmod.print("decoding " + data.byteLength + " ...")
  462.                 audio.decodeAudioData(data, function(buffer)
  463.                 {
  464.                     source = audio.createBufferSource()
  465.                    
  466.                     //source.connect(panctrl)
  467.                     //panctrl.connect(audio.destination)
  468.                     source.connect(analyser)
  469.                     analyser.connect(audio.destination)
  470.                     //source.connect(audio.destination)
  471.                    
  472.                     source.buffer = buffer
  473.                     source.loop = true
  474.                     source.noteOn(0)
  475.                    
  476.                     gmod.print("LOADED AND DECODED")
  477.                 },
  478.                 function(err)
  479.                 {
  480.                     gmod.print("decoding error " + err)
  481.                 })
  482.             })
  483.         }  
  484.        
  485.         function SetListenerPos(x,y,z)
  486.         {
  487.             if (audio) audio.listener.setPosition(x,y,z)
  488.         }
  489.        
  490.         function SetListenerAng(x,y,z, a,b,c)
  491.         {
  492.             if (audio) audio.listener.setOrientation(x,y,z, a,b,c)
  493.         }
  494.        
  495.         function SetListenerVelocity(x,y,z)
  496.         {
  497.             if (audio) audio.listener.setVelocity(x,y,z)
  498.         }
  499.        
  500.         function SetPos(x,y,z)
  501.         {
  502.             panctrl.setPosition(x,y,z)
  503.         }
  504.        
  505.         function SetDir(x,y,z)
  506.         {
  507.             panctrl.setOrientation(x,y,z)
  508.         }
  509.        
  510.         function SetVel(x,y,z)
  511.         {
  512.             panctrl.setVelocity(x,y,z)
  513.         }
  514.     ]]
  515.  
  516.     player.play("http://dl.dropbox.com/u/244444/insect.ogg")
  517.    
  518.     do return end
  519.     local div = 10
  520.     timer.Simple(1, function()
  521.         local pos = renderer:GetPos() / div
  522.         player.SetPos(pos.x, pos.y, pos.z)
  523.        
  524.         timer.Create("fractals",0,0, function()
  525.  
  526.             local pos = flomp.eye_origin / div
  527.             local ang = flomp.eye_angles / 180
  528.            
  529.             player.SetListenerPos(pos.x, pos.y, pos.z)
  530.             local up = ang:Up()
  531.             player.SetListenerAng(ang.p,ang.y,ang.r, up.x, up.y, up.z)
  532.            
  533.             local vel = LocalPlayer():GetVelocity() / div
  534.             player.SetListenerVelocity(vel.x, vel.y, vel.z)
  535.         end)
  536.     end)
  537. end
Advertisement
Add Comment
Please, Sign In to add comment