mesh loader v2
This commit is contained in:
parent
39a1f63822
commit
4c12c9d9ed
5
koptilnya/car_parts/anchor.txt
Normal file
5
koptilnya/car_parts/anchor.txt
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
Anchor = class("Anchor")
|
||||||
|
|
||||||
|
function Anchor:initialize(pos, ang, parent, relativeTo)
|
||||||
|
|
||||||
|
end
|
||||||
@ -22,7 +22,6 @@ function MeshBuilder:initialize(link, maxQuota)
|
|||||||
net.receive("holograms", function()
|
net.receive("holograms", function()
|
||||||
local hasNext = net.readBit()
|
local hasNext = net.readBit()
|
||||||
|
|
||||||
local test = {}
|
|
||||||
while hasNext == 1 do
|
while hasNext == 1 do
|
||||||
local name = net.readString()
|
local name = net.readString()
|
||||||
|
|
||||||
@ -30,14 +29,16 @@ function MeshBuilder:initialize(link, maxQuota)
|
|||||||
local holo = ent:toHologram()
|
local holo = ent:toHologram()
|
||||||
|
|
||||||
table.insert(self._objects, {name = name, holo = holo})
|
table.insert(self._objects, {name = name, holo = holo})
|
||||||
table.insert(test, {name = name, holo = holo})
|
|
||||||
end)
|
end)
|
||||||
|
|
||||||
hasNext = net.readBit()
|
hasNext = net.readBit()
|
||||||
end
|
end
|
||||||
|
|
||||||
self:onHologramsReceived(self._objects)
|
|
||||||
self:_applyMeshes()
|
self:_applyMeshes()
|
||||||
|
|
||||||
|
timer.simple(0, function()
|
||||||
|
self:onHologramsReceived(self._objects)
|
||||||
|
end)
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -15,8 +15,11 @@ function MeshBuilder:initialize(link, modelPlaceholder)
|
|||||||
table.insert(self._objectsNames, object)
|
table.insert(self._objectsNames, object)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
timer.simple(0, function()
|
||||||
self.isReady = true
|
self.isReady = true
|
||||||
self:onReady(self._objectsNames)
|
self:onReady(self._objectsNames)
|
||||||
|
end)
|
||||||
|
|
||||||
for _, v in pairs(self._readyPlayers) do
|
for _, v in pairs(self._readyPlayers) do
|
||||||
self:_sendHolograms(v)
|
self:_sendHolograms(v)
|
||||||
|
|||||||
7
koptilnya/mesh_loader2/builder.txt
Normal file
7
koptilnya/mesh_loader2/builder.txt
Normal 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
|
||||||
57
koptilnya/mesh_loader2/cl_builder.txt
Normal file
57
koptilnya/mesh_loader2/cl_builder.txt
Normal 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
|
||||||
40
koptilnya/mesh_loader2/obj_parser.txt
Normal file
40
koptilnya/mesh_loader2/obj_parser.txt
Normal 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
|
||||||
55
koptilnya/mesh_loader2/sv_builder.txt
Normal file
55
koptilnya/mesh_loader2/sv_builder.txt
Normal 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
|
||||||
Loading…
x
Reference in New Issue
Block a user