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)