Skip to content
Wizard Lizard edited this page Sep 7, 2017 · 6 revisions

This is place for posting examples made by users

Drawing mesh on hud/screen

--@name Simple textured mesh
--@author Sparky
--@client 

local texture, objdata, mymesh

local function renderHUD()
    render.pushMatrix(chip():getMatrix(),true)
    render.enableDepth(true)
    render.setRenderTargetTexture("material")
    mymesh:draw()
    render.popMatrix()
end

local function renderScreen()
    render.selectRenderTarget("mesh")
    render.pushViewMatrix({type="3D", x=0, y=0, w=1024, h=1024, origin=Vector(-50,50,0), angles = Angle(0,0,0), aspect=1})
    local rotation = Matrix()
    rotation:rotate(Angle(timer.curtime()*10,0,0))
    render.pushMatrix(rotation, true)
    render.clear(Color(0,0,0,0),true)
    render.enableDepth(true)
    render.setRenderTargetTexture("material")
    mymesh:draw()
    render.popMatrix()
    render.popViewMatrix()
    render.selectRenderTarget()
    
    render.enableDepth(false)
    render.setRenderTargetTexture("mesh")
    render.drawTexturedRect(0,0,512,512)
end

local function init()
    if not texture or not objdata then return end
    render.createRenderTarget("material")
    hook.add("render","setup",function()
        render.selectRenderTarget("material")
        render.setTexture(texture)
        render.clear()
        --The weird scaling will need to be fixed sometime
        render.drawTexturedRectUV(0,0,1024,1024,0.016,0.984,0.984,0.016)
        render.selectRenderTarget()
        
        hook.remove("render","setup")
        local start = mesh.trianglesLeft()
        mymesh = mesh.createFromObj(objdata)
        print("Used "..(start-mesh.trianglesLeft()).." triangles.")

        if render.getScreenEntity():getClass()=="starfall_hud" then
            hook.add("postdrawopaquerenderables","mesh",renderHUD)
        else
            render.createRenderTarget("mesh")
            hook.add("render","mesh",renderScreen)
        end
    end)
end

render.getTextureID("https://dl.dropboxusercontent.com/u/72472980/renamon.png", function(tex)
    texture = tex
    init()
end)

http.get("https://dl.dropboxusercontent.com/u/72472980/renamon.obj",function(body)
    objdata = body
    init()
end)

Drawing eyes on box(Retexturing Prop with starfall)

--@name eyebox
--@author thegrb93
--@shared 

if SERVER then
    hook.add("net","",function(name,len,ply)
        chip():isWeldedTo():setMaterial(net.readString(), ply)
    end)
else
    local ent
    render.createRenderTarget("rt")
    render.createRenderTarget("circle")
    local box = render.getTextureID("models/props_junk/woodcrates01a")
    net.start("")
    net.writeString(render.getRenderTargetMaterial("rt"))
    net.send()
    
    local function getEyes()
        local playerpos = ent:worldToLocal(player():getShootPos())
        local rightdiff = Vector(-7.373, -7.957, 19.722) - playerpos
        local leftdiff = Vector(-7.373, 6.908, 19.722) - playerpos
        local ratio = -15/leftdiff.z
        local x1, y1, x2, y2 = leftdiff.y*ratio, leftdiff.x*ratio, rightdiff.y*ratio, rightdiff.x*ratio
        local len1 = math.sqrt(x1^2 + y1^2)
        local len2 = math.sqrt(x2^2 + y2^2)
        if len1>20 then x1 = x1*20/len1 y1 = y1*20/len1 end
        if len2>20 then x2 = x2*20/len2 y2 = y2*20/len2 end
        return x1, y1, x2, y2
    end
    
    local function doRender()
        render.selectRenderTarget("rt")
        render.setTexture(box)
        render.drawTexturedRect(0,0,1024,1024)
    
        render.setRenderTargetTexture("circle")
        render.setColor(Color(0,0,0))
        render.drawTexturedRect(705,595,110,110)
        render.drawTexturedRect(855,595,110,110)
        
        render.setColor(Color(255,255,255))
        render.drawTexturedRect(710,600,100,100)
        render.drawTexturedRect(860,600,100,100)
        
        local x1, y1, x2, y2 = getEyes()
        render.setColor(Color(0,0,0))
        render.drawTexturedRect(730+x1,620+y1,60,60)
        render.drawTexturedRect(880+x2,620+y2,60,60)
        render.selectRenderTarget()
    end
    
    local function initRender()
        ent = find.inSphere(chip():getPos(), 50, function(e) return e and e:getClass()=="prop_physics" end)[1]
        if not ent then return end
        render.selectRenderTarget("circle")
        local poly = {}
        for i=1, 360 do
            local theta = i*math.pi/180
            poly[i] = {x=math.cos(theta)*512+512, y=math.sin(theta)*512+512}
        end
        render.clear(Color(0,0,0,0))
        render.drawPoly(poly)
        render.selectRenderTarget()
        
        hook.remove("render","init")
        hook.add("render","go",doRender)
    end
    hook.add("render","init",initRender)
end

Using multiple screens and global matrix + cam to achieve 3D skybox effect.

--@name 
--@author 
--@client

local scale = 80000
local tex = {
    render.getTextureID("https://dl.dropboxusercontent.com/u/72472980/cs_tibetft.png"),
    render.getTextureID("https://dl.dropboxusercontent.com/u/72472980/cs_tibetrt.png"),
    render.getTextureID("https://dl.dropboxusercontent.com/u/72472980/cs_tibetbk.png"),
    render.getTextureID("https://dl.dropboxusercontent.com/u/72472980/cs_tibetlf.png"),
    render.getTextureID("https://dl.dropboxusercontent.com/u/72472980/cs_tibetup.png"),
}
local m = {}
local angles = {
    Angle(0,0,-90),
    Angle(0,90,-90),
    Angle(0,180,-90),
    Angle(0,-90,-90),
    Angle(0,90,0),
    Angle(0,0,180)
}
local pos = chip():getPos()+Vector(0,0,scale/6)
for i=1, 6 do
    m[i] = Matrix()
    m[i]:translate(pos)
    m[i]:rotate(angles[i])
    m[i]:scale(Vector(scale,scale,scale))
    m[i]:translate(Vector(-0.5,-0.5,0.5))
end

hook.add("render","",function()
    for i=1, 5 do
        render.pushViewMatrix({type = "3D", zfar = 300000})
        render.pushMatrix(m[i], true)
        render.setTexture(tex[i])
        render.drawTexturedRect(0,0,1,1)
        render.popMatrix()
        render.popViewMatrix()
    end
end)

Basic chat command, prints whatever you type after !i

--@name basic commands
--@author Wizard Lizard
--@server

local Arg = {} 
    
hook.add("PlayerSay", "test", function(ply, text) 

    Arg = string.explode(" ", text)  

    if tostring(Arg[1]) == "!i" and ply == owner() then   
        print( tostring(Arg[2]) )            
        return ""  
      
    end
    
end)

TODO: Screenshots and code commenting.