Skip to content

Commit

Permalink
feat: Added static, prerendered hat icons to drastically shorten the …
Browse files Browse the repository at this point in the history
…open time of the shop
  • Loading branch information
ValentinFunk committed Apr 27, 2017
1 parent 4fdaf39 commit 3d91654
Show file tree
Hide file tree
Showing 10 changed files with 378 additions and 27 deletions.
2 changes: 1 addition & 1 deletion lua/autorun/pointshop2_init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ LibK.InitializeAddon{
author = "Kamshak", --Name of the author
luaroot = "ps2", --Folder that contains the client/shared/server structure relative to the lua folder,
loadAfterGamemode = false,
version = "2.6.0",
version = "2.8.0",
requires = { "KInventory" }
}

Expand Down
1 change: 1 addition & 0 deletions lua/kinv/items/pointshop/sh_0_base_pointshop_item.lua
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ function ITEM.static.generateFromPersistence( itemTable, persistenceItem )
points = persistenceItem.price,
premiumPoints = persistenceItem.pricePremium,
}
itemTable.static.UUID = persistenceItem.uuid
itemTable.PrintName = persistenceItem.name
itemTable.Description = persistenceItem.description
itemTable.Servers = persistenceItem.servers or {}
Expand Down
27 changes: 19 additions & 8 deletions lua/ps2/client/cl_dpointshopitemicon.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ function PANEL:Init( )
self.Label:SetContentAlignment( 5 )
self.Label:DockMargin( 0, 4, 0, 4 )
self.Label:SetTextColor( Color( 255, 255, 255, 255 ) )
self.Label:SetExpensiveShadow( 1, Color( 0, 0, 0, 200 ) )
--self.Label:SetExpensiveShadow( 1, Color( 0, 0, 0, 200 ) )

self.iconContainer = vgui.Create( "DIconLayout", self )
self.iconContainer = self.iconContainer or vgui.Create( "DIconLayout", self )
self.iconContainer:SetSpaceX( 5 )
self.iconContainer:DockMargin( 5, 5, 5, 0 )
self.iconContainer:SetPos(8, 8)
--self.iconContainer:DockMargin( 5, 5, 5, 0 )
self.iconContainer:SetTall( 12 )
self.iconContainer:Dock( TOP )
--self.iconContainer:Dock( TOP )
function self.iconContainer:Think( )
if #self:GetChildren( ) == 0 then
self:SetVisible( false )
Expand All @@ -39,6 +40,20 @@ function PANEL:Init( )
end )
end

function PANEL:PerformLayout()
self.iconContainer = self.iconContainer or vgui.Create( "DIconLayout", self )
if IsValid(self.iconContainer) then
self.iconContainer:SetWidth(self:GetWide())
end
end

function PANEL:PaintOver(w, h)
self.iconContainer:SetPaintedManually(true)
self.iconContainer:PaintManual()
self.iconContainer:SetPaintedManually(false)
self:DrawSelections()
end

function PANEL:GetItemClass( )
return self.itemClass
end
Expand Down Expand Up @@ -89,10 +104,6 @@ function PANEL:OpenMenu( )
--For override
end

function PANEL:PaintOver( w, h )
self:DrawSelections()
end

function PANEL:OnSelected( )
end

Expand Down
58 changes: 41 additions & 17 deletions lua/ps2/modules/pointshop2/hat/cl_dpointshophaticon.lua
Original file line number Diff line number Diff line change
@@ -1,40 +1,45 @@
local PANEL = {}

function PANEL:Init( )
self.image = vgui.Create( "DPreRenderedModelPanel", self )
self.image:Dock( FILL )
--self.image:SetSize( 128, 128 )
self.image:DockMargin( 5, 0, 5, 5 )
self.image = vgui.Create( "DImage", self )
self.image:SetSize( 128, 128 )
self.image:SetMouseInputEnabled( false )
self.image:SetPos( 0, 0 )

self.Label:Dock( NODOCK )
function self.Label:Paint(w, h)
surface.SetDrawColor( 47, 47, 47, 200 )
surface.DrawRect(0, 0, w, h)
end
end

function PANEL:PerformLayout()
self.BaseClass.PerformLayout(self)

self.Label:SetWide(self:GetWide())
self.Label:SetPos(0, self:GetTall() - 25)
self.Label:SetTall(25)
end

function PANEL:SetItemClass( itemClass )
self.BaseClass.SetItemClass( self, itemClass )

if itemClass.iconInfo.inv.useMaterialIcon then
self.image:Remove()
self.image = vgui.Create( "DImage", self )
self.image:Dock( FILL )
self.image:SetSize( 64, 64 )
self.image:SetMouseInputEnabled( false )
self.image:SetImage( itemClass.iconInfo.inv.iconMaterial )
else
local model = Pointshop2:GetPreviewModel()
self.image:ApplyModelInfo( model )
self.image:SetPacOutfit( itemClass.getOutfitForModel( model.model ) )
self.image:SetViewInfo( itemClass.iconInfo.shop.iconViewInfo )
Pointshop2.RequestIcon(itemClass, self):Then(function(icon)
if IsValid(self) then
self.image:SetMaterial(icon)
end
end)
end
end

function PANEL:SetItem( item )
self:SetItemClass( item.class )
end

function PANEL:PerformLayout( )
if not IsValid( self.image ) then return end
--self:SetTall( self.image:GetTall( ) + self.Label:GetTall( ) + 10 )
end

function PANEL:OnSelected( )
self.image.forceRender = true
hook.Run( "PACItemSelected", self.itemClass )
Expand All @@ -45,6 +50,25 @@ function PANEL:OnDeselected( )
hook.Run( "PACItemDeSelected", self.itemClass )
end

local function drawOutlinedBox( x, y, w, h, thickness, clr )
surface.SetDrawColor( clr )
for i=0, thickness - 1 do
surface.DrawOutlinedRect( x + i, y + i, w - i * 2, h - i * 2 )
end
end

function PANEL:PaintOver(w, h)
self.BaseClass.PaintOver(self, w, h)

self.Label:SetPaintedManually(true)
self.Label:PaintManual()
self.Label:SetPaintedManually(false)

if self.Selected or self.Hovered or self:IsChildHovered( 2 ) then
drawOutlinedBox( 0, 0, w, h, 3, self:GetSkin().Highlight )
end
end

derma.DefineControl( "DPointshopHatIcon", "", PANEL, "DPointshopItemIcon" )

local PANEL = {}
Expand Down
105 changes: 105 additions & 0 deletions lua/ps2/modules/pointshop2/hat/cl_itemqueue.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
-- Adapted from MDave's MIT Licensed code. Now ISC
Pointshop2.IconQueue = Pointshop2.IconQueue or LibK.CircularQueue()
Pointshop2.IconPromises = {}

local queue = Pointshop2.IconQueue
local promises = Pointshop2.IconPromises

local function getIconUID( itemClass )
return "ps2/" .. itemClass.UUID:lower()
end

local function getIconPath( itemClass )
return string.format( "spawnicons/%s_256.png", getIconUID(itemClass) )
end

function Pointshop2.RequestIcon( itemClass, panel )
local iconID = getIconUID( itemClass )
local pending = promises[iconID]
if pending then
return pending
end

-- Check if the icon exists
local path = getIconPath( itemClass )

print(path)
if file.Exists( "materials/" .. path, "GAME" ) then
return Promise.Resolve(Material( path ))
end

promises[iconID] = Deferred()
queue:Add( itemClass )
return promises[iconID]
end

do
Pointshop2.rIcon = Pointshop2.rIcon or vgui.Create( "ModelImage" )
Pointshop2.rModel = Pointshop2.rModel or ClientsideModel( "error" )

local rIcon = Pointshop2.rIcon
local rModel = Pointshop2.rModel

local rTable = {
ent = rModel,

cam_pos = Vector(),
cam_ang = Angle(),
cam_fov = 90
}

local rTexture


rIcon:SetVisible( false )
rIcon:SetSize( 256, 256 )

rModel:SetNoDraw( true )

function rModel:RenderOverride()
if not rTexture then return end
cam.Start2D()
surface.SetDrawColor( 255, 255, 255 )

render.PushFilterMin( TEXFILTER.ANISOTROPIC );
render.PushFilterMag( TEXFILTER.ANISOTROPIC );
surface.SetMaterial( rTexture )
surface.DrawTexturedRectUV( 0, 0, 512, 512, 0, 0, 1, 1 )
render.PopFilterMag( )
render.PopFilterMin( )
cam.End2D()
end

hook.Add( "HUDPaint", "TShop: ProcessIconQueue", function()
if queue:IsEmpty() then
return
end

-- Render icon
local itemClass = queue:Pop()

rTexture = Pointshop2.RenderModelIcon(itemClass)

rIcon:SetModel( getIconUID( itemClass ) )
rIcon:RebuildSpawnIconEx( rTable )
end )

hook.Add( "SpawniconGenerated", "TShop: IconPopulate", function( model, image )
local pending = promises[model]

if pending then
local path = image:gsub( "materials\\", "" )
local icon = Material( path )

-- Force the icon to refresh
icon:GetTexture( "$basetexture" ):Download()

pending:Resolve(icon)
end

if !pending then
print( "no prom for ", model )
end
end )

end
125 changes: 125 additions & 0 deletions lua/ps2/modules/pointshop2/hat/cl_prerender.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
local plyModel = hook.Run( "PS2_GsetPreviewModel" ) or "models/player/alyx.mdl"
local entity = ClientsideModel( plyModel, RENDER_GROUP_OPAQUE_ENTITY )
pac.SetupENT( entity )
entity:SetNoDraw(true)
entity:SetIK( false )
local colAmbientLight = Color( 50, 50, 50 )
local colColor = Color( 255, 255, 255, 255 )
local directionalLight = {
[BOX_TOP] = Color(255, 255, 255),
[BOX_FRONT] = Color(255, 255, 255)
}
local function paintActual(itemClass)
local outfit = itemClass.getOutfitForModel(plyModel)
pac.SetupENT( entity )
entity:AttachPACPart(outfit)
entity:FrameAdvance( 100 )


local viewInfo = itemClass.iconInfo.shop.iconViewInfo
for i = 1, 100 do
pac.Think()
end

cam.Start3D( viewInfo.origin, viewInfo.angles, viewInfo.fov - 20, 0, 0, 512, 512, 5, 4096 )
cam.IgnoreZ( true )
render.SuppressEngineLighting( true )
render.SetLightingOrigin( entity:GetPos() )
render.ResetModelLighting( colAmbientLight.r/255, colAmbientLight.g/255, colAmbientLight.b/255 )
render.SetColorModulation( colColor.r/255, colColor.g/255, colColor.b/255 )
render.SetBlend( 1 )

for i=0, 6 do
local col = directionalLight[ i ]
if ( col ) then
render.SetModelLighting( i, col.r/255, col.g/255, col.b/255 )
end
end

pac.FlashlightDisable( true )
pac.ForceRendering( true )
pac.RenderOverride( entity, "opaque" )
pac.RenderOverride( entity, "translucent", true )
entity:DrawModel( )
pac.RenderOverride( entity, "translucent", true )
pac.ForceRendering( false )
pac.FlashlightDisable( false )

cam.IgnoreZ( false )
render.SuppressEngineLighting( false )
cam.End3D( )

entity:RemovePACPart(outfit)
end


local mat_BlurX = Material( "pp/blurx" )
local mat_BlurY = Material( "pp/blury" )
local mat_Copy = Material( "pp/copy" )
local blurredRt, otherRt
local function blur( rt, x, y, w, h, sizex, sizey, passes )
blurredRt = blurredRt or GetRenderTarget("BlurredRT", 512, 512)
otherRt = otherRt or GetRenderTarget("TempRT", 512, 512)

render.PushRenderTarget( blurredRt, 0, 0, 512, 512 )
render.OverrideAlphaWriteEnable( true, true )
render.Clear( 0, 0, 0, 255, true, true )

-- Copy rt to blur texture
mat_Copy:SetTexture( "$basetexture", rt )
surface.SetMaterial(mat_Copy)
surface.SetDrawColor(color_white)
surface.DrawTexturedRectUV(0, 0, 512, 512, 0, 0, 1, 1)

mat_BlurX:SetTexture( "$basetexture", blurredRt )
mat_BlurY:SetTexture( "$basetexture", otherRt )
mat_BlurX:SetFloat( "$size", sizex )
mat_BlurY:SetFloat( "$size", sizey )

for i=1, passes+1 do
render.SetRenderTarget( otherRt )
surface.SetMaterial( mat_BlurX )
surface.DrawTexturedRect(0, 0, 512, 512)

render.SetRenderTarget( blurredRt )
surface.SetMaterial( mat_BlurY )
surface.DrawTexturedRect(0, 0, 512, 512)
end
render.OverrideAlphaWriteEnable( false )
render.PopRenderTarget()
end

local UID = "ItemTextureRT"
local csgoBg = Material('materials/itembg-ps2.png')
function Pointshop2.RenderModelIcon(itemClass)
local rt = GetRenderTarget( UID, 512, 512 )
local mat = CreateMaterial( UID .. "_Mat", "UnlitGeneric", {
["$basetexture"] = rt,
} )

local oldW, oldH = ScrW(), ScrH()
local oldRt = render.GetRenderTarget( )
render.SetRenderTarget( rt )
render.Clear( 0, 0, 0, 255, true, true )
render.SetViewPort( 0, 0, 512, 512 )
cam.Start2D()
surface.SetDrawColor( 255, 255, 255, 255 )
surface.SetMaterial(csgoBg)
surface.DrawTexturedRect(0, 0, 512, 512)
paintActual(itemClass)
blur(rt, 100, 100, 512, 512, 2, 2, 25)

cam.IgnoreZ(true)
mat:SetTexture( "$basetexture", blurredRt )
surface.SetMaterial(mat)
render.SetScissorRect( 0, 512 - 120, 512, 512, true ) -- Enable the rect
surface.DrawTexturedRect(0, 0, 512, 512)
render.SetScissorRect( 0, 0, 0, 0, false ) -- disable the rect
cam.IgnoreZ(false)
cam.End2D()
render.SetViewPort( 0, 0, oldW, oldH )
render.SetRenderTarget( oldRt )

mat:SetTexture( "$basetexture", rt )
return mat
end
1 change: 1 addition & 0 deletions lua/ps2/modules/pointshop2/hat/sh_model_hatpersistence.lua
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ function HatPersistence.static.importDataFromTable( exportTable )
--Create basic persistence
local itemPersistence = Pointshop2.ItemPersistence:new( )
copyModelFields( itemPersistence, instanceExport.ItemPersistence, Pointshop2.ItemPersistence.model )
itemPersistence.uuid = LibK.GetUUID()

local promise = itemPersistence:save( )
:Then( function( itemPersistence )
Expand Down
Loading

0 comments on commit 3d91654

Please sign in to comment.