This commit is contained in:
Nikita Kruglickiy
2021-09-19 22:34:23 +06:00
parent cb9c7b73bb
commit a47d2b43f7
17 changed files with 417 additions and 47 deletions

View File

@@ -2,8 +2,10 @@
-- @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")
@@ -26,6 +28,21 @@ function MeshBuilder:initialize(link)
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()
@@ -37,10 +54,18 @@ function MeshBuilder:initialize(link)
while hasNext == 1 do
local name = net.readString()
local mat = net.readTable()
net.readEntity(function(ent)
local holo = ent:toHologram()
local object = {name = name, holo = holo}
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)
@@ -59,6 +84,7 @@ 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
@@ -69,6 +95,57 @@ function MeshBuilder:_applyMeshes()
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)

View File

@@ -21,7 +21,6 @@ function ObjParser:initialize(link)
local objData
http.get(link, function(response)
objData = response
objData = string.gsub(response, "\nl%s%d+%s%d+", "")
end)
while not objData do

View File

@@ -17,17 +17,17 @@ function MeshBuilder:initialize(link, modelPlaceholder)
self._firstTimeSended = false
net.receive("obj_parsed", function(len, ply)
if DEBUG_MODE then
print(string.format("%s parsed the .obj", ply:getName()))
end
net.start("obj_parsed")
net.writeEntity(ply)
net.send(owner())
self:onPlayerParsedObj(ply)
end)
net.receive("initialized", function(len, ply)
if DEBUG_MODE then
print(string.format("%s initialized", ply:getName()))
end
net.start("initialized")
net.writeEntity(ply)
net.send(owner())
table.insert(self._playersWithAccess, ply)
@@ -74,10 +74,6 @@ function MeshBuilder:initialize(link, modelPlaceholder)
self:onObjectLoaded(objData)
for object in string.gmatch(objData, "^?\n?o%s([%w_%.%-]+)") do
table.insert(self._objectsNames, object)
end
self:onObjectParsed(self._objectsNames)
return 2
@@ -93,6 +89,7 @@ function MeshBuilder:_sendObjects(target)
for _, v in pairs(self._objectsToSend) do
net.writeBit(1)
net.writeString(v.name)
net.writeTable(type(v.mat) == "table" and v.mat or { basetexture = tostring(v.mat) })
net.writeEntity(v.holo)
end
net.writeBit(0)
@@ -116,14 +113,13 @@ function MeshBuilder:build(name, pos, ang, scale, color, mat, parent, relativeTo
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})
table.insert(self._objects, {name = name, mat = mat, holo = holo})
return holo
end