Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
RenderTarget and shader demo — Gideros Forum

RenderTarget and shader demo

SinisterSoftSinisterSoft Maintainer
edited August 2016 in Code snippets
Here is an example of using RenderTarget and a shader to make a nice decay effect.
local width=application:getContentWidth()
local height=application:getContentHeight()
 
local fakeScreen=RenderTarget.new(width,height,true)
fakeScreen:clear(0x000000,1)
 
local playfield=Bitmap.new(fakeScreen)
stage:addChild(playfield)
 
local decayV=[[
attribute highp vec3 vVertex;
attribute mediump vec2 vTexCoord;
uniform highp mat4 vMatrix;
varying mediump vec2 fTexCoord;
 
void main() {
  vec4 vertex = vec4(vVertex,1.0);
  gl_Position = vMatrix*vertex;
  fTexCoord=vTexCoord;
}
]]
 
local decayF=[[
uniform lowp vec4 fColor;
uniform lowp sampler2D fTexture;
varying mediump vec2 fTexCoord;
 
void main() {
 lowp vec4 pixel=fColor*texture2D(fTexture, fTexCoord);
 if (pixel.a==0.0) discard;
 pixel=floor(pixel*255.0*0.99)*0.0039;
 pixel.a=1.0;
 gl_FragColor = pixel;
}
]]
 
local shader=Shader.new(decayV,decayF,Shader.FLAG_FROM_CODE,
{
{name="vMatrix",type=Shader.CMATRIX,sys=Shader.SYS_WVP,vertex=true},
{name="fColor",type=Shader.CFLOAT4,sys=Shader.SYS_COLOR,vertex=false},
{name="fTexture",type=Shader.CTEXTURE,vertex=false},
},
{
{name="vVertex",type=Shader.DFLOAT,mult=3,slot=0,offset=0},
{name="vColor",type=Shader.DUBYTE,mult=4,slot=1,offset=0},
{name="vTexCoord",type=Shader.DFLOAT,mult=2,slot=2,offset=0},
});
 
local oldScreen=Bitmap.new(fakeScreen)
oldScreen:setShader(shader)
 
local fakeStage=Sprite.new()
fakeStage:addChild(oldScreen)
 
local lines={}
for loop=1,10 do
	local line={}
	local r=math.random(50,255)
	local g=math.random(50,255)
	local b=math.random(50,255)
	line.sprite=Pixel.new((r*0x10000)+(g*0x100)+b,1,200,6)
	line.sprite:setAnchorPosition(100,3)
	fakeStage:addChild(line.sprite)
 
	line.x=math.random(width)
	line.y=math.random(height)
	line.dir=math.random(360)
	line.speed=(math.random(10)/10)+4
	line.rotation=0
	lines[loop]=line
end
 
function gameLoop(e)
	for loop=1,#lines do
		local line=lines[loop]
		line.rotation=line.rotation+line.dir/200
		local nx=line.x+(math.sin(math.rad(line.dir))*line.speed)
		if nx<0 or nx>width then line.dir=360-line.dir else line.x=nx end
		local ny=line.y+(math.cos(math.rad(line.dir))*line.speed)
		if ny<0 or ny>height then line.dir=180-line.dir else line.y=ny end
 
		line.sprite:setRotation(line.rotation)
		line.sprite:setPosition(line.x,line.y)
	end
 
	fakeScreen:draw(fakeStage)
end
 
stage:addEventListener(Event.ENTER_FRAME,gameLoop)
Thanks to help from @hgy29 for the shader code. :)

Likes: pie, n1cke

Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
https://deluxepixel.com
+1 -1 (+2 / -0 )Share on Facebook

Comments

Sign In or Register to comment.