-- @name koptilnya/mesh_loader/cl_builder -- @client -- @include /koptilnya/libs/table.txt -- @include obj_parser.txt -- @include /koptilnya/libs/utils.txt require("/koptilnya/libs/table.txt") require("obj_parser.txt") require("/koptilnya/libs/utils.txt") MeshBuilder = class("MeshBuilder") function MeshBuilder:initialize(link) self.link = link self.meshData = {} self._objects = {} self._parser = ObjParser:new(link) self._parser.onLoaded = function(parser, objData) self:onObjLoaded(objData) end self._parser.onParsed = function(parser, meshData, usedTriangles) net.start("obj_parsed") net.send() self:onObjParsed(meshData, usedTriangles) self.meshData = meshData self:_applyMeshes() end self._cachedMaterials = {} if player() == owner() then net.receive("initialized", function() net.readEntity(function(ply) notification.addProgress(tostring(ply:getUserID()), string.format("%s is loading mesh", ply:getName())) end) end) net.receive("obj_parsed", function() net.readEntity(function(ply) notification.kill(tostring(ply:getUserID())) end) end) end net.start("initialized") net.send() net.receive("objects", function() self._objects = {} local hasNext = net.readBit() while hasNext == 1 do local name = net.readString() local mat = net.readTable() net.readEntity(function(ent) local holo = ent:toHologram() if (isURL(mat.basetexture)) then mat = self:_createMaterial(mat.shader, mat.basetexture, mat.bumpmap, mat.options) else mat = material.load(mat.basetexture) end local object = {name = name, mat = mat, holo = holo} table.insert(self._objects, object) end) hasNext = net.readBit() end timer.simple(0.5, function() self:onHologramsReceived(self._objects) self:_applyMeshes() end) end) end function MeshBuilder:_applyMeshes() for _, v in pairs(self._objects) do if self.meshData[v.name] ~= nil then v.holo:setMesh(self.meshData[v.name]) v.holo:setMeshMaterial(v.mat) v.holo:setRenderBounds(Vector(-200), Vector(200)) v.meshApplied = true end end if #self._objects > 0 then self:onMeshesApplied() end end function MeshBuilder:_setTexture(mat, name, texture) if isURL(texture) then mat:setTextureURL(name, texture, function(_, _, _, _, layout) layout(0, 0, 1024, 1024) end) else mat:setTexture(name, texture) end end function MeshBuilder:_createMaterial(shader, basetexture, bumpmap, options) shader = shader or "VertexLitGeneric" basetexture = basetexture or "models/debug/debugwhite" bumpmap = bumpmap or "" options = options or {} local checksum = crc(shader .. basetexture .. bumpmap .. json.encode(options)) if self._cachedMaterials[checksum] ~= nil then return self._cachedMaterials[checksum] end local mat = material.create(shader) self:_setTexture(mat, "$basetexture", basetexture) if bumpmap ~= "" then self:_setTexture(mat, "$bumpmap", bumpmap) end for k, v in pairs(options) do if type(v) == "string" then mat:setString("$" .. k, v) elseif type(v) == "number" then if string.match(tostring(v), "%.") then mat:setFloat("$" .. k, v) else mat:setInt("$" .. k, v) end elseif type(v) == "nil" then mat:setUndefined("$" .. k, v) elseif type(v) == "Vector" then mat:setVector("$" .. k, tostring(v)) end end self._cachedMaterials[checksum] = mat return mat end -- STUB function MeshBuilder:onObjLoaded(objData) end function MeshBuilder:onObjParsed(meshData, usedTriangles) end function MeshBuilder:onHologramsReceived(objects) end function MeshBuilder:onMeshesApplied() end