Hello Guest

Author Topic: Neat Shader Example  (Read 3125 times)

0 Members and 1 Guest are viewing this topic.

Kyle

  • Administrator
  • Member
  • Posts: 258
    • View Profile
Neat Shader Example
« on: June 02, 2014, 11:28:14 AM »


Just a quick shader example that I was playing around with yesterday.  Download the source and the files here: http://otter2d.com/download/shaderz.zip

You can paint onto a hidden surface that manipulates the image with a shader.

Code: [Select]
using Otter;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ShaderTesting {
    class Program {
        static void Main(string[] args) {
            var image = new Image("pic.jpg");
            var game = new Game("ShaderZ", image.Width, image.Height);

            game.FirstScene = new TestScene();

            game.Start();
        }
    }

    class TestScene : Scene {

        Image image = new Image("pic.jpg");
        Image circle = new Image("circle.png");
        Axis axis = new Axis(Key.W, Key.D, Key.S, Key.A);
        Surface distortion;

        public TestScene() {
            AddGraphic(image);
            distortion = new Surface(image.Width, image.Height);
            circle.CenterOrigin();
            circle.Blend = BlendMode.Add;
            AddGraphic(distortion);
            circle.Alpha = 0.1f;
            distortion.Visible = false;
            distortion.AutoClear = false;
        }

        public override void Update() {
            base.Update();
            axis.UpdateFirst();
            //image.X -= axis.X * 3;
            //image.Y -= axis.Y * 3;

            if (Timer % 30 == 0) {
                try {
                    image.Shader = new Shader("../../shader.frag");
                }
                catch(Exception e) {
                    Console.WriteLine("Shader error:");
                    Console.WriteLine(e.Message);
                }
                image.Shader.SetParameter("gradient", new Texture("gradient.png"));
            }
            if (Input.MouseButtonPressed(MouseButton.Right)) {
                distortion.Clear();
            }
            image.Shader.SetParameter("time", Timer);
            image.Shader.SetParameter("distortion", distortion.Texture);
            image.Shader.SetParameter("offset", Util.SinScale(Timer, 0, 1));
            image.Shader.SetParameter("mixfactor", Util.SinScale(Timer * 0.1f, 0, 1));

            if (Input.MouseButtonDown(MouseButton.Left)) {
                Draw.SetTarget(distortion);
                Draw.Graphic(circle, MouseX, image.Height - MouseY);
                Draw.ResetTarget();
            }
        }

        public override void Render() {
            base.Render();

            Draw.Circle(MouseX - 15, MouseY - 15, 30, Color.None, Color.Red, 2);
        }
    }
}

Code: [Select]
sampler2D texture;
sampler2D gradient;
sampler2D distortion;
uniform float offset;
uniform float time;
uniform float mixfactor;

void main() {
vec2 pos = gl_TexCoord[0];
vec2 distortColor = texture2D(distortion, pos);
pos.x += distortColor.r * (sin(time * 0.1) * 0.05);
pos.y -= distortColor.g * (cos(time * 0.1) * 0.05);
vec4 color = texture2D(texture, pos);

float gray = (color.r + color.g + color.b) / 3;

vec4 gradientColor = texture2D(gradient, vec2(gray, offset));

gl_FragColor = mix(color, gradientColor, distortColor.r + distortColor.g);
}
« Last Edit: June 02, 2014, 11:31:09 AM by Kyle »