add pixelate shader example

This commit is contained in:
Brandon 2018-06-25 14:16:16 -06:00
parent 1889c96475
commit 4e4b6b0a09
1 changed files with 25 additions and 21 deletions

View File

@ -95,17 +95,35 @@ func run() {
tilt := 0.01 // Default: 0.001 tilt := 0.01 // Default: 0.001
whichOn := false whichOn := false
onNumber := 0 onNumber := 0
jetpackName := "jetpack.png" jetpackOffName := "jetpack.png"
jetpackOn1Name := "jetpack-on.png"
jetpackOn2Name := "jetpack-on2.png"
camVector := win.Bounds().Center() camVector := win.Bounds().Center()
InstallShader(win)
bg, _ := loadSprite("sky.png") bg, _ := loadSprite("sky.png")
InstallShader(win) // Jetpack - Rendering
jetpackOff, err := loadSprite(jetpackOffName)
if err != nil {
panic(err)
}
jetpackOn1, err := loadSprite(jetpackOn1Name)
if err != nil {
panic(err)
}
jetpackOn2, err := loadSprite(jetpackOn2Name)
if err != nil {
panic(err)
}
// Tutorial Text // Tutorial Text
txt := loadTTF("intuitive.ttf", 50, pixel.V(win.Bounds().Center().X-450, win.Bounds().Center().Y-200)) txt := loadTTF("intuitive.ttf", 50, pixel.V(win.Bounds().Center().X-450, win.Bounds().Center().Y-200))
fmt.Fprintf(txt, "Explore the Skies with WASD or Arrow Keys!") fmt.Fprintf(txt, "Explore the Skies with WASD or Arrow Keys!")
currentSprite := jetpackOff
// Game Loop // Game Loop
for !win.Closed() { for !win.Closed() {
win.Update() win.Update()
@ -141,26 +159,20 @@ func run() {
if jetpackOn { if jetpackOn {
velY += jetAcc velY += jetAcc
whichOn = !whichOn whichOn = !whichOn
onNumber += 1 onNumber++
if onNumber == 5 { // every 5 frames, toggle anijetMation if onNumber == 5 { // every 5 frames, toggle anijetMation
onNumber = 0 onNumber = 0
if whichOn { if whichOn {
jetpackName = "jetpack-on.png" currentSprite = jetpackOn1
} else { } else {
jetpackName = "jetpack-on2.png" currentSprite = jetpackOn2
} }
} }
} else { } else {
jetpackName = "jetpack.png" currentSprite = jetpackOff
velY -= gravity velY -= gravity
} }
// Jetpack - Rendering
jetpack, err := loadSprite(jetpackName)
if err != nil {
panic(err)
}
positionVector := pixel.V(win.Bounds().Center().X+jetX, win.Bounds().Center().Y+jetY-372) positionVector := pixel.V(win.Bounds().Center().X+jetX, win.Bounds().Center().Y+jetY-372)
jetMat := pixel.IM jetMat := pixel.IM
jetMat = jetMat.Scaled(pixel.ZV, 4) jetMat = jetMat.Scaled(pixel.ZV, 4)
@ -194,7 +206,7 @@ func run() {
bg.Draw(win, pixel.IM.Moved(pixel.V(win.Bounds().Center().X, win.Bounds().Center().Y+766)).Scaled(pixel.ZV, 10)) bg.Draw(win, pixel.IM.Moved(pixel.V(win.Bounds().Center().X, win.Bounds().Center().Y+766)).Scaled(pixel.ZV, 10))
txt.Draw(win, pixel.IM) txt.Draw(win, pixel.IM)
win.SetSmooth(false) win.SetSmooth(false)
jetpack.Draw(win, jetMat) currentSprite.Draw(win, jetMat)
} }
@ -206,26 +218,20 @@ func main() {
var pixelateFragShader = ` var pixelateFragShader = `
#version 330 core #version 330 core
#ifdef GL_ES #ifdef GL_ES
precision mediump float; precision mediump float;
precision mediump int; precision mediump int;
#endif #endif
in vec4 Color; in vec4 Color;
in vec2 texcoords; in vec2 texcoords;
in float Intensity; in float Intensity;
out vec4 fragColor; out vec4 fragColor;
uniform vec4 u_colormask; uniform vec4 u_colormask;
uniform vec4 u_texbounds; uniform vec4 u_texbounds;
uniform sampler2D u_texture; uniform sampler2D u_texture;
// varying vec4 vertTexCoord; // varying vec4 vertTexCoord;
// uniform sampler2D texture; // uniform sampler2D texture;
// uniform vec2 pixels; // uniform vec2 pixels;
void main(void) void main(void)
{ {
fragColor = vec4(0, 0, 0, 0); fragColor = vec4(0, 0, 0, 0);
@ -233,9 +239,7 @@ void main(void)
vec2 t = (texcoords - u_texbounds.xy) / u_texbounds.zw; vec2 t = (texcoords - u_texbounds.xy) / u_texbounds.zw;
fragColor += Intensity * Color * texture(u_texture, t); fragColor += Intensity * Color * texture(u_texture, t);
fragColor *= u_colormask; fragColor *= u_colormask;
vec2 p = t.st; vec2 p = t.st;
p.x -= mod(texcoords.x, 1.0 / gl_FragCoord.x); p.x -= mod(texcoords.x, 1.0 / gl_FragCoord.x);
p.y -= mod(texcoords.y, 1.0 / gl_FragCoord.y); p.y -= mod(texcoords.y, 1.0 / gl_FragCoord.y);