Lua Shader Examples

From GiderosMobile
Revision as of 01:58, 7 November 2023 by MoKaLux (talk | contribs) (Created page with "'''Parent:''' Writing Lua Shaders<br/> __TOC__ '''Requirements''': '''In order to use Lua Shaders you need to include ''luashader'' standard library in your projects'...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Parent: Writing Lua Shaders


Requirements:
In order to use Lua Shaders you need to include luashader standard library in your projects
luashader standard library is available in your Gideros installation folder under Library

Rain drops demo - by Hgy29

Rain drops shader effect

-- Custom shader for a plain colored pixel (BASIC PROGRAM)
local shader=StandardShaders:getShaderSpecification(Shader.SHADER_PROGRAM_BASIC)

-- We will need vertex position in fragment shader
function shader:vertexShader(vVertex,vColor,vTexCoord) : Shader
	local vertex = hF4(vVertex,0.0,1.0)
	fVertex=vVertex.xy
	return vMatrix*vertex
end

-- Fragment shader will compute caustics and ripples
function shader:fragmentShader() : Shader
	local tp = hF2(1.0/32.0,1.0/16.0)
	local mt = fVertex/1024.0
	local p = mod(mt*6.28, 6.28)-360.0
	local i = p
	local c = 1.0
	local inten = .005

	for n=1,3 do 
		local t = fTime*0.5 * (1.0 - (3.0 / hF1(n)))
		i = p + hF2(cos(t - i.x) + sin(t + i.y), sin(t - i.y) + cos(t + i.x))
		c += 1.0/length(hF2(p.x / (sin(i.x+t)/inten),p.y / (cos(i.y+t)/inten)))
	end
	for n=1,32 do
		local fr=fRippleT[n]
		if (fr>0.0) then
			local pd=length(fVertex-fRippleC[n])
			local rd=1.0-smoothstep(0.0,128.0,pd)
			if (rd>0.0) then
				local td=(fTime-fr)*10.0
				local dr=min(1.0,max(0.0,1.0-abs(td-(1.0-rd)*20.0)/10.0))
				c+=sin(rd*20.0+td)*rd*dr
			end
		end
	end
	c /= 3
	c = (1.5-c^.5)^4
	return lF4((hF4(c,c,c,1.0) + hF4(0.0, 0.3, 0.5, 1.0))*fColor)
end

-- Add our custom constants and inter-stage varibale
local NDROPS=32
table.insert(shader.uniforms,{name="fRippleC",type=Shader.CFLOAT2,vertex=false,mult=NDROPS})
table.insert(shader.uniforms,{name="fTime",type=Shader.CFLOAT,sys=Shader.SYS_TIMER,vertex=false})
table.insert(shader.uniforms,{name="fRippleT",type=Shader.CFLOAT,vertex=false,mult=NDROPS})
table.insert(shader.varying,{name="fVertex",type=Shader.CFLOAT2})

-- A plain blue screen wide Pixel with our shader
local ax,ay,aw,ah=application:getLogicalBounds()
local p=Pixel.new(0x5678CD,1,aw-ax,ah-ay) p:setPosition(ax,ay)
p:setShader(shader:build())
stage:addChild(p)

-- Main loop: create random rain drops
local drop=0
local dropsP,dropsT={},{}
for i=1,NDROPS do dropsP[i*2-1]=0 dropsP[i*2]=0 dropsT[i]=0 end
Core.asyncCall(function()
	while true do
		Core.yield(.05)
		drop+=1
		if drop==(NDROPS+1) then drop=1 end
		dropsP[drop*2-1]=math.random(aw-ax)
		dropsP[drop*2]=math.random(ah-ay)
		dropsT[drop]=os:timer()
		p:setShaderConstant("fRippleC",Shader.CFLOAT2,NDROPS,dropsP)
		p:setShaderConstant("fRippleT",Shader.CFLOAT,NDROPS,dropsT)
	end
end)