example.shader
shaders can be quite tricky but are very powerful once you learn to use them.
all of these will assume you have this as your base code
local cube = c3d.geometry.cube_simple():push():reposition(0,0,1)
function c3d.update()
cube:set_rotation(0,1,0,os.epoch("utc")/20)
end
vertex
wave effect
cube:set_vertex_shader(function(x,y,z,...)
local t = os.epoch("utc")
y = y + math.sin(x*10+t/200)/5
return c3d.shader.default.vertex(x,y,z,...)
end)
geometry
triangles colored by their index
cube:set_geometry_shader(function(triangle,index)
function triangle.fs()
return 2^(index%15)
end
return triangle
end)
fragment
random noise fragment shader
cube:set_frag_shader(function(pixel_data)
return 2^(math.random(0,1)*15)
end)
CC color gradient made using a fragment and vertex shader
local random_color_lookup = setmetatable({},{__index=function(this,index)
local c = math.random(0,15)
rawset(this,index,c)
return c
end})
cube:set_vertex_shader(function(...)
local x,y,z,w = c3d.shader.default.vertex(vertex,...)
return x,y,z,w,{epic_color=random_color_lookup[vertex.index]}
end)
cube:set_frag_shader(function(pixel_data)
return 2^math.ceil(math.min(math.max(pixel_data.frag.epic_color,0),15))
end)