This commit is contained in:
Nikita Kruglickiy
2021-03-17 18:21:42 +06:00
parent 19ab152f5f
commit f1eaecd79e
11 changed files with 46 additions and 98 deletions

View File

@@ -1,20 +1,16 @@
local MODEL_PLACEHOLDER = "models/holograms/cube.mdl"
local BUNDLE_SIZE = 2
local SEND_DELAY = 0.5
MeshBuilder = class("MeshBuilder")
function MeshBuilder:initialize()
function MeshBuilder:initialize(modelPlaceholder, bundleSize, sendDelay)
self.modelPlaceholder = modelPlaceholder or "models/holograms/cube.mdl"
self.bundleSize = bundleSize or 10
self.sendDelay = sendDelay or 0.5
self._objects = {}
self._setups = {}
if CLIENT then
self._setups = {}
self._meshData = {}
--[[
self._shouldBuild = false
self._shouldRevision = true
]]
net.receive("holograms", function(len)
local hasNext = net.readBit()
@@ -38,46 +34,7 @@ function MeshBuilder:initialize()
hasNext = net.readBit()
end
end)
--[[
net.receive("revision", function()
local hasNext = net.readBit()
while hasNext == 1 do
local key = net.readString()
net.readEntity(function(ent)
local holo = ent:toHologram()
self._objects[key] = holo
end)
hasNext = net.readBit()
end
self._shouldRevision = false
if self._shouldBuild then
self:build()
self._shouldBuild = false
end
end)
]]
else
--[[
net.receive("revision", function(len, ply)
net.start("revision")
for k, v in pairs(self._objects) do
net.writeBit(1)
net.writeString(k)
net.writeEntity(v)
end
net.writeBit(0)
net.send()
end)
]]
net.receive("setups", function(len, ply)
local hasNext = net.readBit()
@@ -97,7 +54,7 @@ function MeshBuilder:initialize()
ang = relativeTo:localToWorldAngles(ang)
end
local holo = holograms.create(pos, ang, MODEL_PLACEHOLDER, scale)
local holo = holograms.create(pos, ang, self.modelPlaceholder, scale)
holo:setColor(color)
holo:setMaterial(mat)
holo:setParent(parent)
@@ -152,17 +109,6 @@ if CLIENT then
end
function MeshBuilder:build()
--[[
if self._shouldRevision then
net.start("revision")
net.send()
self._shouldBuild = true
return
end
]]
local function sendSetups(setups)
net.start("setups")
for k, v in pairs(setups) do
@@ -181,20 +127,20 @@ if CLIENT then
end
local bundleKeys = table.getKeys(self._setups)
if #bundleKeys > BUNDLE_SIZE then
if #bundleKeys > self.bundleSize then
local i = 1
timer.create("sendSetups", SEND_DELAY, math.ceil(#bundleKeys / BUNDLE_SIZE), function()
local from = (i - 1) * BUNDLE_SIZE + 1
timer.create("sendSetups", self.sendDelay, math.ceil(#bundleKeys / self.bundleSize), function()
local from = (i - 1) * self.bundleSize + 1
local setups = {}
for _, v in pairs({ unpack(bundleKeys, from, from + BUNDLE_SIZE - 1) }) do
for _, v in pairs({ unpack(bundleKeys, from, from + self.bundleSize - 1) }) do
setups[v] = self._setups[v]
end
sendSetups(setups)
if i < math.ceil(#bundleKeys / BUNDLE_SIZE) then
if i < math.ceil(#bundleKeys / self.bundleSize) then
i = i + 1
else
table.empty(self._setups)

View File

@@ -1,109 +0,0 @@
local MODEL_PLACEHOLDER = "models/holograms/cube.mdl"
MeshBuilder = class("MeshBuilder")
function MeshBuilder:initialize()
self._holograms = {}
if SERVER then
self._meshObjects = {}
self._setupAllArgs = nil
net.receive("meshDataSet", function(_, ply)
self._meshObjects = net.readTable()
if self._setupAllArgs ~= nil then
self:setupAll(unpack(self._setupAllArgs))
self._setupAllArgs = nil
end
net.start("holograms")
for k, v in pairs(self._holograms) do
net.writeBit(1)
net.writeString(k)
net.writeEntity(v)
end
net.writeBit(0)
net.send(ply)
end)
else
self._meshData = {}
self._hologramsReceived = false
net.receive("holograms", function()
local hasNext = net.readBit()
while hasNext == 1 do
local key = net.readString()
net.readEntity(function(ent)
if not isValid(ent) then return end
if self._meshData[key] and not self._holograms[key] then
self._holograms[key] = ent:toHologram()
end
end)
hasNext = net.readBit()
end
self._hologramsReceived = true
self:applyMeshes()
end)
end
end
if SERVER then
function MeshBuilder:setup(key, pos, ang, scale, color, mat, parent, relativeTo)
if isValid(self._holograms[key]) then return end
if isValid(relativeTo) then
pos = relativeTo:localToWorld(pos)
ang = relativeTo:localToWorldAngles(ang)
end
local holo = holograms.create(pos, ang, MODEL_PLACEHOLDER, scale)
holo:setColor(color)
holo:setMaterial(mat)
holo:setParent(parent)
self._meshes[key] = Mesh:new()
self._holograms[key] = holo
return holo
end
function MeshBuilder:setupAll(pos, ang, scale, color, mat, parent, relativeTo)
if #self._meshObjects > 0 then
for _, v in pairs(self._meshObjects) do
self:setup(v, pos, ang, scale, color, mat, parent, relativeTo)
end
else
self._setupAllArgs = {pos, ang, scale, color, mat, parent, relativeTo}
end
end
else
function MeshBuilder:setMeshData(meshData)
self._meshData = meshData
if self._hologramsReceived then
self:applyMeshes()
else
net.start("meshDataSet")
net.writeTable(table.getKeys(meshData))
net.send()
end
end
function MeshBuilder:applyMeshes()
for k, v in pairs(self._holograms) do
if isValid(v) then
v:setMesh(self._meshData[k])
v:setRenderBounds(Vector(-200), Vector(200))
end
end
end
end

View File

@@ -1,3 +0,0 @@
--@include /koptilnya/libs/utils.txt
require("/koptilnya/libs/utils.txt")

View File

@@ -1,17 +0,0 @@
--@include /koptilnya/libs/utils.txt
--@include /koptilnya/mesh_loader/sv_mesh.txt
require("/koptilnya/libs/utils.txt")
Mesh = class("Mesh")
accessorFunc(Mesh, "_hologram", "Hologram", nil)
if SERVER then
require("/koptilnya/mesh_loader/sv_mesh.txt")
else
function Mesh:setData(data)
self:getEntity():setMesh(data)
self:getEntity():setRenderBounds(Vector(-200), Vector(200))
end
end

View File

@@ -1,5 +1,3 @@
local MAX_QUOTA = 0.75
Parser = class("Parser")
local initialChipName = chip():getChipName()
@@ -7,7 +5,7 @@ local function setStatus(status)
setName(string.format("%s (%s)", initialChipName, status))
end
function Parser:initialize(link)
function Parser:initialize(link, maxQuota)
if CLIENT then
local triangles = mesh.trianglesLeft()
local createFromObjCoroutine = coroutine.create(function(objData)
@@ -24,7 +22,7 @@ function Parser:initialize(link)
setStatus("File received, start parsing...")
hook.add("think", "loadingMesh", function()
while quotaAverage() < quotaMax() * MAX_QUOTA do
while quotaAverage() < quotaMax() * (maxQuota or 0.75) do
if loadMesh() then
setName(initialChipName)
self:onLoaded(objData, self.meshData, triangles - mesh.trianglesLeft())

View File

@@ -1,97 +0,0 @@
--@include /koptilnya/libs/utils.txt
require("/koptilnya/libs/utils.txt")
accessorFunc(Mesh, "_relativeTo", "RelativeTo", nil)
function Mesh:initialize(pos, ang, scale, color, mat, parent, relativeTo)
--create holo
self:setPos(pos)
self:setAng(ang)
self:setScale(scale)
self:setColor(color)
self:setMaterial(mat)
self:setParent(parent)
self:setRelativeTo(relativeTo)
end
function Mesh:getPos()
return self:getEntity():getPos()
end
function Mesh:setPos(pos)
self:getEntity():setPos(pos)
end
function Mesh:getRelativePos()
if isValid(self:getRelativeTo()) then
return self:getRelativeTo():worldToLocal(self:getPos())
else
return self:getPos()
end
end
function Mesh:setRelativePos(pos)
if isValid(self:getRelativeTo()) then
self:setPos(self:getRelativeTo():localToWorld(pos))
else
self:setPos(pos)
end
end
function Mesh:getAng()
return self:getEntity():getAng()
end
function Mesh:setAng(pos)
self:getEntity():setAng(pos)
end
function Mesh:getRelativeAng()
if isValid(self:getRelativeTo()) then
return self:getRelativeTo():worldToLocalAngles(self:getAng())
else
return self:getAng()
end
end
function Mesh:setRelativeAng(ang)
if isValid(self:getRelativeTo()) then
self:setAng(self:getRelativeTo():localToWorldAngles(ang))
else
self:setAng(ang)
end
end
function Mesh:getScale()
return self:getEntity():getScale()
end
function Mesh:setScale(scale)
self:getEntity():setScale(scale)
end
function Mesh:getColor()
return self:getEntity():getColor()
end
function Mesh:setColor(color)
self:getEntity():setColor(color)
end
function Mesh:getMaterial()
return self:getEntity():getMaterial()
end
function Mesh:setMaterial(material)
self:getEntity():setMaterial(material)
end
function Mesh:getParent()
return self:getEntity():getParent()
end
function Mesh:setParent(parent)
self:getEntity():setParent(parent)
end