mesh loader v2

This commit is contained in:
Nikita Kruglickiy 2021-03-23 00:42:00 +06:00
parent 39a1f63822
commit 4c12c9d9ed
7 changed files with 173 additions and 5 deletions

View File

@ -0,0 +1,5 @@
Anchor = class("Anchor")
function Anchor:initialize(pos, ang, parent, relativeTo)
end

View File

@ -22,7 +22,6 @@ function MeshBuilder:initialize(link, maxQuota)
net.receive("holograms", function()
local hasNext = net.readBit()
local test = {}
while hasNext == 1 do
local name = net.readString()
@ -30,14 +29,16 @@ function MeshBuilder:initialize(link, maxQuota)
local holo = ent:toHologram()
table.insert(self._objects, {name = name, holo = holo})
table.insert(test, {name = name, holo = holo})
end)
hasNext = net.readBit()
end
self:onHologramsReceived(self._objects)
self:_applyMeshes()
timer.simple(0, function()
self:onHologramsReceived(self._objects)
end)
end)
end

View File

@ -15,8 +15,11 @@ function MeshBuilder:initialize(link, modelPlaceholder)
table.insert(self._objectsNames, object)
end
self.isReady = true
self:onReady(self._objectsNames)
timer.simple(0, function()
self.isReady = true
self:onReady(self._objectsNames)
end)
for _, v in pairs(self._readyPlayers) do
self:_sendHolograms(v)

View File

@ -0,0 +1,7 @@
-- @include sv_builder.txt
-- @include cl_builder.txt
if SERVER then
require("sv_builder.txt")
else
require("cl_builder.txt")
end

View File

@ -0,0 +1,57 @@
-- @client
-- @include /koptilnya/libs/table.txt
-- @include obj_parser.txt
require("/koptilnya/libs/table.txt")
require("obj_parser.txt")
MeshBuilder = class("MeshBuilder")
function MeshBuilder:initialize(link, maxQuota)
self.link = link
self.meshData = {}
self._objects = {}
self._parser = ObjParser:new(link, maxQuota)
self._parser.onLoaded = function(parser, objData, meshData, usedTriangles)
self.meshData = meshData
self:_applyMeshes()
end
net.receive("holograms", function()
local newObjects = {}
local hasNext = net.readBit()
while hasNext == 1 do
local name = net.readString()
net.readEntity(function(ent)
local holo = ent:toHologram()
local object = {name = name, holo = holo, meshApplied = false}
table.insert(newObjects, object)
table.insert(self._objects, object)
end)
hasNext = net.readBit()
end
timer.simple(0, function()
self:_applyMeshes()
self:onHologramsReceived(newObjects, self._objects)
end)
end)
end
function MeshBuilder:_applyMeshes()
for _, v in pairs(self._objects) do
if self.meshData[v.name] ~= nil and not v.meshApplied then
v.holo:setMesh(self.meshData[v.name])
v.holo:setRenderBounds(Vector(-200), Vector(200))
v.meshApplied = true
end
end
end
function MeshBuilder:onHologramsReceived(objects)
end

View File

@ -0,0 +1,40 @@
-- @client
ObjParser = class("ObjParser")
local initialChipName = chip():getChipName()
local function setStatus(status)
setName(string.format("%s (%s)", initialChipName, status))
end
function ObjParser:initialize(link, maxQuota)
self.maxQuota = maxQuota or 0.6
local triangles = mesh.trianglesLeft()
setStatus("Getting file...")
http.get(link, function(objData)
objData = string.gsub(objData, "\nl%s%d+%s%d+", "")
local loadMesh = coroutine.wrap(function()
self.meshData = mesh.createFromObj(objData, true, true)
return true
end)
setStatus("File received, start parsing...")
hook.add("think", "loadingMesh", function()
while math.max(quotaAverage(), quotaUsed()) < quotaMax() * self.maxQuota do
if loadMesh() then
setName(initialChipName)
self:onLoaded(objData, self.meshData, triangles - mesh.trianglesLeft())
hook.remove("think", "loadingMesh")
return
end
end
end)
end)
end
function ObjParser:onLoaded(objData, meshData, usedTriangles)
end

View File

@ -0,0 +1,55 @@
-- @server
MeshBuilder = class("MeshBuilder")
function MeshBuilder:initialize(link, modelPlaceholder)
self.link = link
self.modelPlaceholder = modelPlaceholder or "models/holograms/cube.mdl"
self._objects = {}
end
function MeshBuilder:_sendHolograms()
if #self._objects == 0 then
return
end
net.start("holograms")
for _, v in pairs(self._objects) do
net.writeBit(1)
net.writeString(v.name)
net.writeEntity(v.holo)
print(v.name)
end
net.writeBit(0)
net.send()
end
function MeshBuilder:reset()
self._objects = {}
end
function MeshBuilder:build(name, pos, ang, scale, color, mat, parent, relativeTo)
if isValid(relativeTo) then
pos = relativeTo:localToWorld(pos)
ang = relativeTo:localToWorldAngles(ang)
end
local holo = holograms.create(pos, ang, self.modelPlaceholder, scale)
holo:setColor(color)
holo:setMaterial(mat)
if isValid(parent) then
holo:setParent(parent)
end
table.insert(self._objects, {name = name, holo = holo})
end
function MeshBuilder:getResult()
local result = self._objects
self:_sendHolograms()
self:reset()
return result
end