From 6f8443d682c25aa44d9158e8728ced9dd54fa4ca Mon Sep 17 00:00:00 2001 From: HOSOKAWA Kenchi Date: Tue, 24 May 2011 08:11:28 +0900 Subject: [PATCH] shader --- dev4/PsychlopsSilverlight4.csproj | 2 + dev4/Shader/Grating.fx | 11 + dev4/Shader/Plaid.fx | 12 + dev4/psychlops/extention/standard/shader.cs | 431 ++++++++++++++++++++++++++++ test4/PsychlopsMain.cs | 149 +++++----- 5 files changed, 532 insertions(+), 73 deletions(-) diff --git a/dev4/PsychlopsSilverlight4.csproj b/dev4/PsychlopsSilverlight4.csproj index 37bd4af..571b9cb 100644 --- a/dev4/PsychlopsSilverlight4.csproj +++ b/dev4/PsychlopsSilverlight4.csproj @@ -93,6 +93,8 @@ + + diff --git a/dev4/Shader/Grating.fx b/dev4/Shader/Grating.fx index 88d73b8..f3ddd05 100644 --- a/dev4/Shader/Grating.fx +++ b/dev4/Shader/Grating.fx @@ -22,6 +22,17 @@ float phase: register(C3); /// 0 float orientation: register(C4); +/// Width of envelope +/// 1 +/// 1024 +/// 32 +float SizeH : register(C5); + +/// Height of envelope +/// 1 +/// 1024 +/// 32 +float SizeV : register(C6); float rp(float2 uv) { diff --git a/dev4/Shader/Plaid.fx b/dev4/Shader/Plaid.fx index e8635d1..1441994 100644 --- a/dev4/Shader/Plaid.fx +++ b/dev4/Shader/Plaid.fx @@ -46,6 +46,18 @@ float phase2: register(C7); /// 0.785398 float orientation2: register(C8); +/// Width of envelope +/// 1 +/// 1024 +/// 32 +float SizeH : register(C9); + +/// Height of envelope +/// 1 +/// 1024 +/// 32 +float SizeV : register(C10); + float rp(float2 uv) { diff --git a/dev4/psychlops/extention/standard/shader.cs b/dev4/psychlops/extention/standard/shader.cs index 8a511b6..7d7c935 100644 --- a/dev4/psychlops/extention/standard/shader.cs +++ b/dev4/psychlops/extention/standard/shader.cs @@ -22,6 +22,109 @@ namespace Psychlops public static partial class Figures { + public class ShaderGrating : ShaderField + { + protected Shader.GratingProgram ps = null; + public ShaderGrating() + { + initialize__ = initialize___; + setParameters = setParameters__; + } + internal void initialize() + { + if (!initialized) + { + Main.canvas.beginInvoke(initialize___); + initialized = true; + } + } + internal void initialize___() + { + if (!initialized) + { + if (ps == null) + { + ps = new Shader.GratingProgram(); + initializeShader(); + } + shader = ps; + initialized = true; + } + } + + public double contrast = 1.0, wavelength = 20.0, phase = 0.0, orientation = 0.0; + public ShaderGrating setSize(double s) { set(s, s); return this; } + public ShaderGrating setSize(double h, double v) { set(h, v); return this; } + + public void setParameters__() + { + ps.Contrast = contrast; + ps.WaveLength = wavelength; + ps.Phase = phase; + ps.Orientation = orientation; + ps.SizeH = width; + ps.SizeV = height; + ps.Update(); + } + + + } + + + public class ShaderPlaid : ShaderField + { + protected Shader.PlaidProgram ps = null; + public ShaderPlaid() + { + initialize__ = initialize___; + setParameters = setParameters__; + } + internal void initialize() + { + if (!initialized) + { + Main.canvas.beginInvoke(initialize___); + initialized = true; + } + } + internal void initialize___() + { + if (!initialized) + { + if (ps == null) + { + ps = new Shader.PlaidProgram(); + initializeShader(); + } + shader = ps; + initialized = true; + } + } + + public double contrast = 0.5, wavelength = 20.0, phase = 0.0, orientation = 0.0; + public double contrast2 = 0.5, wavelength2 = 20.0, phase2 = 0.0, orientation2 = Math.PI / 4; + public ShaderPlaid setSize(double s) { set(s, s); return this; } + public ShaderPlaid setSize(double h, double v) { set(h, v); return this; } + + public void setParameters__() + { + ps.Contrast = contrast; + ps.WaveLength = wavelength; + ps.Phase = phase; + ps.Orientation = orientation; + ps.Size = width; + ps.Contrast2 = contrast; + ps.WaveLength2 = wavelength; + ps.Phase2 = phase; + ps.Orientation2 = orientation; + ps.SizeH = width; + ps.SizeV = height; + ps.Update(); + } + + + } + public class ShaderGabor : ShaderField { @@ -150,6 +253,334 @@ namespace Psychlops { } + #region GratingProgram + public class GratingProgram : ShaderProgram + { + public static readonly Uri ps = new Uri(@"/PsychlopsSilverlight4;component/Shader/Grating.ps", UriKind.Relative); + + public static readonly DependencyProperty InputProperty = ShaderEffect.RegisterPixelShaderSamplerProperty("Input", typeof(GratingProgram), 0); + public static readonly DependencyProperty ContrastProperty = DependencyProperty.Register("Contrast", typeof(double), typeof(GratingProgram), new PropertyMetadata(((double)(1D)), PixelShaderConstantCallback(1))); + public static readonly DependencyProperty FrequencyProperty = DependencyProperty.Register("Frequency", typeof(double), typeof(GratingProgram), new PropertyMetadata(((double)(100D)), PixelShaderConstantCallback(2))); + public static readonly DependencyProperty PhaseProperty = DependencyProperty.Register("Phase", typeof(double), typeof(GratingProgram), new PropertyMetadata(((double)(0D)), PixelShaderConstantCallback(3))); + public static readonly DependencyProperty OrientationProperty = DependencyProperty.Register("Orientation", typeof(double), typeof(GratingProgram), new PropertyMetadata(((double)(0D)), PixelShaderConstantCallback(4))); + public static readonly DependencyProperty SizeHProperty = DependencyProperty.Register("SizeH", typeof(double), typeof(GratingProgram), new PropertyMetadata(((double)(32D)), PixelShaderConstantCallback(5))); + public static readonly DependencyProperty SizeVProperty = DependencyProperty.Register("SizeV", typeof(double), typeof(GratingProgram), new PropertyMetadata(((double)(32D)), PixelShaderConstantCallback(6))); + public GratingProgram() + { + PixelShader pixelShader = new PixelShader(); + pixelShader.UriSource = ps; + this.PixelShader = pixelShader; + + this.UpdateShaderValue(InputProperty); + this.UpdateShaderValue(ContrastProperty); + this.UpdateShaderValue(FrequencyProperty); + this.UpdateShaderValue(PhaseProperty); + this.UpdateShaderValue(OrientationProperty); + this.UpdateShaderValue(SizeHProperty); + this.UpdateShaderValue(SizeVProperty); + + Size = 200; + Contrast = 1.0; + WaveLength = 100.0; + Orientation = 0.0; + SizeH = 32.0; + SizeV = 32.0; + } + + public void Update() + { + this.UpdateShaderValue(InputProperty); + this.UpdateShaderValue(ContrastProperty); + this.UpdateShaderValue(FrequencyProperty); + this.UpdateShaderValue(PhaseProperty); + this.UpdateShaderValue(OrientationProperty); + this.UpdateShaderValue(SizeHProperty); + this.UpdateShaderValue(SizeVProperty); + } + + + private double size__, wavelength__; + public double Size { get { return size__; } set { size__ = value; setFrequency(); } } + private void setFrequency() + { + double freq = size__ * 2.0 * Math.PI / (wavelength__); + this.SetValue(FrequencyProperty, freq); + } + + /// Amplitude of Grating + public double Contrast + { + get + { + return ((double)(this.GetValue(ContrastProperty))) * 2.0; + } + set + { + this.SetValue(ContrastProperty, value / 2.0); + } + } + /// Phase of Grating + public double WaveLength + { + get + { + return wavelength__; + } + set + { + wavelength__ = value; + setFrequency(); + } + } + /// Phase of Grating + public double Phase + { + get + { + return ((double)(this.GetValue(PhaseProperty))); + } + set + { + this.SetValue(PhaseProperty, value); + } + } + /// Orientation of Grating + public double Orientation + { + get + { + return ((double)(this.GetValue(OrientationProperty))); + } + set + { + this.SetValue(OrientationProperty, value); + } + } + /// Width of envelope + public double SizeH + { + get + { + return ((double)(this.GetValue(SizeHProperty))); + } + set + { + this.SetValue(SizeHProperty, value); + } + } + /// Height of Figure + public double SizeV + { + get + { + return ((double)(this.GetValue(SizeVProperty))); + } + set + { + this.SetValue(SizeVProperty, value); + } + } + } + #endregion + + #region PlaidProgram + public class PlaidProgram : ShaderProgram + { + public static readonly Uri ps = new Uri(@"/PsychlopsSilverlight4;component/Shader/Plaid.ps", UriKind.Relative); + + public static readonly DependencyProperty InputProperty = ShaderEffect.RegisterPixelShaderSamplerProperty("Input", typeof(PlaidProgram), 0); + public static readonly DependencyProperty ContrastProperty = DependencyProperty.Register("Contrast", typeof(double), typeof(PlaidProgram), new PropertyMetadata(((double)(0.5D)), PixelShaderConstantCallback(1))); + public static readonly DependencyProperty FrequencyProperty = DependencyProperty.Register("Frequency", typeof(double), typeof(PlaidProgram), new PropertyMetadata(((double)(100D)), PixelShaderConstantCallback(2))); + public static readonly DependencyProperty PhaseProperty = DependencyProperty.Register("Phase", typeof(double), typeof(PlaidProgram), new PropertyMetadata(((double)(0D)), PixelShaderConstantCallback(3))); + public static readonly DependencyProperty OrientationProperty = DependencyProperty.Register("Orientation", typeof(double), typeof(PlaidProgram), new PropertyMetadata(((double)(0D)), PixelShaderConstantCallback(4))); + public static readonly DependencyProperty Contrast2Property = DependencyProperty.Register("Contrast2", typeof(double), typeof(PlaidProgram), new PropertyMetadata(((double)(0.5D)), PixelShaderConstantCallback(5))); + public static readonly DependencyProperty Frequency2Property = DependencyProperty.Register("Frequency2", typeof(double), typeof(PlaidProgram), new PropertyMetadata(((double)(100D)), PixelShaderConstantCallback(6))); + public static readonly DependencyProperty Phase2Property = DependencyProperty.Register("Phase2", typeof(double), typeof(PlaidProgram), new PropertyMetadata(((double)(0D)), PixelShaderConstantCallback(7))); + public static readonly DependencyProperty Orientation2Property = DependencyProperty.Register("Orientation2", typeof(double), typeof(PlaidProgram), new PropertyMetadata(((double)(0.785398D)), PixelShaderConstantCallback(8))); + public static readonly DependencyProperty SizeHProperty = DependencyProperty.Register("SizeH", typeof(double), typeof(PlaidProgram), new PropertyMetadata(((double)(32D)), PixelShaderConstantCallback(9))); + public static readonly DependencyProperty SizeVProperty = DependencyProperty.Register("SizeV", typeof(double), typeof(PlaidProgram), new PropertyMetadata(((double)(32D)), PixelShaderConstantCallback(10))); + public PlaidProgram() + { + PixelShader pixelShader = new PixelShader(); + pixelShader.UriSource = ps; + this.PixelShader = pixelShader; + + this.UpdateShaderValue(InputProperty); + this.UpdateShaderValue(ContrastProperty); + this.UpdateShaderValue(FrequencyProperty); + this.UpdateShaderValue(PhaseProperty); + this.UpdateShaderValue(OrientationProperty); + this.UpdateShaderValue(Contrast2Property); + this.UpdateShaderValue(Frequency2Property); + this.UpdateShaderValue(Phase2Property); + this.UpdateShaderValue(Orientation2Property); + this.UpdateShaderValue(SizeHProperty); + this.UpdateShaderValue(SizeVProperty); + + Size = 200; + Contrast = 0.5; + WaveLength = 100.0; + Phase = 0.0; + Orientation = 0.0; + Contrast2 = 0.5; + WaveLength2 = 100.0; + Phase2 = 0.0; + Orientation2 = Math.PI/4; + SizeH = 32.0; + SizeV = 32.0; + } + + public void Update() + { + this.UpdateShaderValue(InputProperty); + this.UpdateShaderValue(ContrastProperty); + this.UpdateShaderValue(FrequencyProperty); + this.UpdateShaderValue(PhaseProperty); + this.UpdateShaderValue(OrientationProperty); + this.UpdateShaderValue(Contrast2Property); + this.UpdateShaderValue(Frequency2Property); + this.UpdateShaderValue(Phase2Property); + this.UpdateShaderValue(Orientation2Property); + this.UpdateShaderValue(SizeHProperty); + this.UpdateShaderValue(SizeVProperty); + } + + + private double size__, wavelength__, wavelength2__; + public double Size { get { return size__; } set { size__ = value; setFrequency(); } } + private void setFrequency() + { + double freq = size__ * 2.0 * Math.PI / (wavelength__); + this.SetValue(FrequencyProperty, freq); + double freq2 = size__ * 2.0 * Math.PI / (wavelength2__); + this.SetValue(Frequency2Property, freq); + } + + /// Amplitude of Grating + public double Contrast + { + get + { + return ((double)(this.GetValue(ContrastProperty))) * 2.0; + } + set + { + this.SetValue(ContrastProperty, value / 2.0); + } + } + /// Phase of Grating + public double WaveLength + { + get + { + return wavelength__; + } + set + { + wavelength__ = value; + setFrequency(); + } + } + /// Phase of Grating + public double Phase + { + get + { + return ((double)(this.GetValue(PhaseProperty))); + } + set + { + this.SetValue(PhaseProperty, value); + } + } + /// Orientation of Grating + public double Orientation + { + get + { + return ((double)(this.GetValue(OrientationProperty))); + } + set + { + this.SetValue(OrientationProperty, value); + } + } + /// Amplitude of Grating + public double Contrast2 + { + get + { + return ((double)(this.GetValue(Contrast2Property))) * 2.0; + } + set + { + this.SetValue(Contrast2Property, value / 2.0); + } + } + /// Phase of Grating + public double WaveLength2 + { + get + { + return wavelength2__; + } + set + { + wavelength2__ = value; + setFrequency(); + } + } + /// Phase2 of Grating + public double Phase2 + { + get + { + return ((double)(this.GetValue(Phase2Property))); + } + set + { + this.SetValue(Phase2Property, value); + } + } + /// Orientation2 of Grating + public double Orientation2 + { + get + { + return ((double)(this.GetValue(Orientation2Property))); + } + set + { + this.SetValue(Orientation2Property, value); + } + } + /// Width of envelope + public double SizeH + { + get + { + return ((double)(this.GetValue(SizeHProperty))); + } + set + { + this.SetValue(SizeHProperty, value); + } + } + /// Height of Figure + public double SizeV + { + get + { + return ((double)(this.GetValue(SizeVProperty))); + } + set + { + this.SetValue(SizeVProperty, value); + } + } + } + #endregion + #region GaborProgram public class GaborProgram : ShaderProgram { diff --git a/test4/PsychlopsMain.cs b/test4/PsychlopsMain.cs index 7a79979..16acfd8 100644 --- a/test4/PsychlopsMain.cs +++ b/test4/PsychlopsMain.cs @@ -1,4 +1,79 @@ -/* + + + +using Psychlops; +//Position Bias Program +namespace PsychlopsSilverlightApp +{ + + public class PsychlopsMain + { + Canvas cnvs; + Image img, img2, img3; + int isize = 40; + int frames; + Psychlops.Widgets.Slider tfreq; + Psychlops.Widgets.Slider contrast; + Psychlops.Widgets.Slider lambda; + + + + public void psychlops_main() + { + cnvs = new Canvas(300, 600); + Interval rng = new Interval(); + tfreq = new Psychlops.Widgets.Slider("Temporal Frequency(Hz)", -5 <= rng <= 5, 3.0); + contrast = new Psychlops.Widgets.Slider("Contrast", 0.0 <= rng <= 1.0, 0.25); + lambda = new Psychlops.Widgets.Slider("Wave Length", 10.0 <= rng <= 120.0, 30); + + img = new Image(isize * 2, isize * 2); + img2 = new Image(isize * 2, isize * 2); + img3 = new Image(isize * 2, isize * 2); + + var gabor1 = StaticFunctions.NewArray(100); + foreach (var g in gabor1) + { + g.setSigma(isize/8).centering().shift(Math.random(300) - 150, Math.random(600) - 300); + //g.setSize(isize).centering().shift(Math.random(300) - 150, Math.random(600) - 300); + g.orientation = Math.random(2 * Math.PI); + //g.orientation2 = Math.random(2 * Math.PI); + } + + while (true) + { + cnvs.clear(new Color(0.5)); + + Figures.drawGabor(ref img, isize / 8, 1 / lambda, contrast, 0.5 * Math.PI, (double)frames * 2.0 * Math.PI * tfreq / 60); + Figures.drawGabor(ref img2, isize / 8, 1 / lambda, contrast, 0.5 * Math.PI, (double)frames * 2.0 * Math.PI * -tfreq / 60); + Figures.drawGabor(ref img3, isize / 8, 1 / lambda, contrast, 0.5 * Math.PI, (double)frames * 2.0 * Math.PI * tfreq / 60); + + + //img.centering().shift(0, -isize * 1.5).draw(); + //img2.centering().draw(); + //img3.centering().shift(0, isize * 1.5).draw(); + + + foreach (var g in gabor1) + { + g.wavelength = lambda; + g.phase = (double)frames * 2.0 * Math.PI * tfreq / 60; + g.contrast = contrast; + //g.wavelength2 = lambda * 2; + //g.phase2 = (double)frames * 2.0 * Math.PI * tfreq / 60; + //g.contrast2 = contrast / 2; + g.draw(); + } + + if (!Mouse.left.pressed()) frames++; + + cnvs.flip(); + } + } + } +} + + +/* ///+ Prefix linkto BasicCode1 //// Lines for set up Psychlops environment using Psychlops; @@ -540,75 +615,3 @@ using Psychlops; } } */ - - - -using Psychlops; -//Position Bias Program -namespace PsychlopsSilverlightApp -{ - - public class PsychlopsMain - { - Canvas cnvs; - Image img, img2, img3; - int isize = 40; - int frames; - Psychlops.Widgets.Slider tfreq; - Psychlops.Widgets.Slider contrast; - Psychlops.Widgets.Slider lambda; - - - - public void psychlops_main() - { - cnvs = new Canvas(300, 600); - Interval rng = new Interval(); - tfreq = new Psychlops.Widgets.Slider("Temporal Frequency(Hz)", -5 <= rng <= 5, 3.0); - contrast = new Psychlops.Widgets.Slider("Contrast", 0.0 <= rng <= 1.0, 0.25); - lambda = new Psychlops.Widgets.Slider("Wave Length", 10.0 <= rng <= 120.0, 30); - - img = new Image(isize * 2, isize * 2); - img2 = new Image(isize * 2, isize * 2); - img3 = new Image(isize * 2, isize * 2); - -<<<<<<< HEAD - var gabor1 = StaticFunctions.NewArray(100); -======= - var gabor1 = StaticFunctions.NewArray(100); ->>>>>>> remotes/psychlopssilverlight/master - foreach(var g in gabor1) - { - g.setSigma(isize / 8).centering().shift(Math.random(300) - 150, Math.random(600) - 300); - g.orientation = Math.random(2*Math.PI); - } - - while (true) - { - cnvs.clear(new Color(0.5)); - - Figures.drawGabor(ref img, isize / 8, 1/lambda, contrast, 0.5 * Math.PI, (double)frames * 2.0 * Math.PI * tfreq / 60); - Figures.drawGabor(ref img2, isize / 8, 1 / lambda, contrast, 0.5 * Math.PI, (double)frames * 2.0 * Math.PI * -tfreq / 60); - Figures.drawGabor(ref img3, isize / 8, 1 / lambda, contrast, 0.5 * Math.PI, (double)frames * 2.0 * Math.PI * tfreq / 60); - - - //img.centering().shift(0, -isize * 1.5).draw(); - //img2.centering().draw(); - //img3.centering().shift(0, isize * 1.5).draw(); - - - foreach (var g in gabor1) - { - g.wavelength = lambda; - g.phase = (double)frames * 2.0 * Math.PI * tfreq / 60; - g.contrast = contrast; - g.draw(); - } - - if (!Mouse.left.pressed()) frames++; - - cnvs.flip(); - } - } - } -} -- 2.11.0