Hi
I have made a new example with your shader (Matty) and it works great, it very fast !
Here the code (agk) :
// Settings
SetWindowTitle( "eraser shader" )
SetScreenResolution( 1280, 720)
SetSyncRate( 0, 0 )
SetVirtualResolution( 1280, 720 )
SetScissor( 0, 0, 0, 0 )
SetPrintSize(20)
Global w,h as integer
w = GetVirtualWidth()
h = GetVirtualHeight()
//load the shader.
global EraserShaderID as integer
EraserShaderID = LoadShader( "Quad.vs", "eraser.ps" )
// image & Layers
global LayerTotal as integer
LayerTotal = 1
CreateRenderImage( LayerTotal, w, h, 0, 0 )
global LayerActiv as integer
LayerActiv = 2
CreateRenderImage( LayerActiv, w, h, 0, 0 )
global TempLayer as integer // the temp image
TempLayer = 3
CreateRenderImage( TempLayer, w, h, 0, 0 )
//create Quad object to apply full screen shaders too
global ScreenObj as integer
ScreenObj = CreateObjectQuad()
//************** MEDIA LOADING
iBrush = 0
// iBrush = LoadImage( "brush.png" ) // here, load your own brush if needed
Global Brush as integer
Brush = CreateSprite(iBrush)
SetSpriteSize(brush,64,64)
SetSpriteOffset(brush, GetSpriteWidth(brush)/2,GetSpriteHeight(brush)/2)
SetSpriteColorAlpha(brush, 60)
SetSpritePosition(brush,-200,0)
Action = 0 // 0 = paint, 1 = eraser
SetClearColor(100,100,100)
//MAIN LOOP
repeat
Print("FPS:"+str(ScreenFPS(),0) +" / Action : "+str(action))
if GetPointerPressed()=1
oldx = ScreenToWorldX( GetPointerX() )
oldy = ScreenToWorldY( GetPointerY() )
ok = 1
elseif GetPointerReleased()=1
ok = 0
SetRenderToImage(TempLayer,0)
ClearScreen()
SetRenderToScreen()
if action = 1
SetSpriteTransparency(1,0)
SetRenderToImage(LayerTotal,0)
ClearScreen()
endif
SetSpritePosition(1,0,0)
SetRenderToImage(LayerTotal,0)
DrawSprite(1)
SetRenderToScreen()
SetSpriteTransparency(1,1)
endif
if GetPointerState()=1 and ok = 1
x = ScreenToWorldX( GetPointerX() )
y = ScreenToWorldY( GetPointerY() )
if x <> oldx or y <> oldy
if action = 0 // apint
Paint(x,y)
else
erase(x,y)
endif
oldx = x
oldy = y
endif
endif
if GetRawKeyReleased(65 ) = 1 // A
Action = 1-Action
Endif
sync()
until GetRawKeyReleased( 27 )
// FUNCTION
function Paint(x,y)
SetSpritePosition(1,-10000,0) // hide the layer activ to not draw it-self
SetRenderToImage( LayerActiv, 0 )
SetSpritePositionByOffset(Brush,x,y)
SetSpriteAngle(Brush,random(0,360))
DrawSprite(Brush)
CreateSprite(1,LayerActiv)
SetSpriteTransparency(1,1)
SetRenderToScreen()
SetSpritePosition(1,0,0)
endfunction
Function Erase(x,y)
SetSpritePosition(1,-10000,0)
SetRenderToImage( TempLayer, 0 )
SetSpritePositionByOffset(Brush,x,y)
SetSpriteAngle(Brush,random(0,360))
DrawSprite(Brush)
SetRenderToImage( LayerActiv,0)
SetObjectImage( ScreenObj, LayerActiv, 0 )
SetObjectImage( ScreenObj, TempLayer, 1 )
SetObjectShader( ScreenObj, EraserShaderID )
DrawObject( ScreenObj )
DeleteSprite(1)
CreateSprite(1,LayerActiv)
SetSpriteTransparency(1,1)
SetSpritePosition(1,0,0)
SetRenderToImage(TempLayer,0)
ClearScreen()
SetRenderToScreen()
EndFunction
The pixelshader "eraser.ps" (from Matty_H) :
// Eraser shader from MATTY_H -AGK forum
// texture
uniform sampler2D texture0;
uniform sampler2D texture1;
varying vec2 uvVarying;
void main()
{
vec4 basecolor = texture2D(texture0, uvVarying);
vec4 overcolor = texture2D(texture1, uvVarying);
//-- alpha blend
float blendA = 1.0 - overcolor.a;
float blendB = overcolor.a;
//-- minus to erase
gl_FragColor = (blendA * basecolor)-(blendB * overcolor);
// tests
// gl_FragColor = (blendA * overcolor)-(blendB * overcolor); // space
// gl_FragColor = (blendA * basecolor) - (blendB * basecolor); // other eraser
}
and the Quad.vs (from agk source) :
attribute vec3 position;
attribute vec4 color;
attribute vec2 uv;
varying vec2 uvVarying;
varying vec2 posVarying;
varying vec4 colorVarying;
uniform vec4 uvBounds0; // to adjust for atlas sub images
uniform float agk_invert; // FBOs render upside down so flip the quad
void main()
{
gl_Position = vec4(position.xy*vec2(1,agk_invert),0.5,1.0);
uvVarying = (position.xy*vec2(0.5,-0.5) + 0.5) * uvBounds0.xy + uvBounds0.zw;
colorVarying = color;
}
I'm pretty sur it's possible to create a shader to paint and avoid the "black border" of my first image, I will tried to understand how to create that shader (a "simple" blend color, I think).
Again, thank you very much MATTY_H, your shader is excellent
!
http://www.dracaena-studio.com