-
Notifications
You must be signed in to change notification settings - Fork 4
/
PixelLoader.txt
76 lines (69 loc) · 2.74 KB
/
PixelLoader.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
--@name PixelLoader
--@author Sparky
--@shared
--Load an image url, reads it, and builds a pixel matrix out of props with them
local url = "https://dl.dropboxusercontent.com/s/aqj4jaudbmjivoi/sprite4.png"
--local url = "https://dl.dropboxusercontent.com/s/50kt65i17n4j4mv/sprite3.png?dl=0"
-- Adjust these to change the pixel props
local pixel = "models/hunter/blocks/cube05x05x025.mdl"
local pixelAngle = Angle(90,90,0)
local pixelSize = 23.725
--[[local pixel = "models/jaanus/wiretool/wiretool_pixel_sml.mdl"
local pixelAngle = Angle(90,90,0)
local pixelSize = 1.908]]
if SERVER then
hook.add("net","",function(n)
if n=="data" then
local w, h = net.readUInt(32), net.readUInt(32)
net.readStream(function(data)
local p = chip():getPos()
local i = 1
local props = {}
for x=1, w do
for y=1, h do
local col = Color(string.byte(string.sub(data,i,i+2),1,3))
i=i+3
if col.r > 0 or col.g > 0 or col.b > 0 then
props[#props+1] = {pos = p+Vector(x-1, 0, h-y+1)*pixelSize, col = col}
end
end
end
i = 1
hook.add("think","",function()
while quotaAverage()<0.1*quotaMax() and prop.canSpawn() do
if i>#props then timer.remove("SpawnProps") break end
local p = props[i]
local e = prop.create(p.pos, pixelAngle, pixel, true)
e:setColor(p.col)
e:setMaterial("debug/debugdrawflat")
e:setSolid(false)
e:setDrawShadow(false)
i=i+1
end
end)
end)
end
end)
elseif player()==owner() then
local w, h
render.createMaterial(url, function(t,_,w_,h_) w=w_ h=h_ end, function(t,_)
if not t then print("Failed to load url "..url) return end
hook.add("renderoffscreen","",function()
render.selectRenderTarget(t:getName().."$basetexture")
render.capturePixels()
local data = {}
for x=1, w do
for y=1, h do
local c = render.readPixel(x-0.5, y-0.5)
data[#data+1] = string.char(c.r)..string.char(c.g)..string.char(c.b)
end
end
net.start("data")
net.writeUInt(w, 32)
net.writeUInt(h, 32)
net.writeStream(table.concat(data))
net.send()
hook.remove("renderoffscreen","")
end)
end)
end