扇形を描く

深い意味は無いのだけどシェーダを使って描画してみた。360度系なので0度(360度)をはさんだ場合がちょっと厄介。あとシェーダに渡す長さがピクセル単位にならないのも困りもの。このへんはメソッド作って内部で処理してやると簡単になるだろう。

require 'dxruby'

hlsl = <<EOS
  texture tex0;
  float4 color;
  float start_angle, end_angle, flag, length;

  sampler Samp0 = sampler_state
  {
   Texture =<tex0>;
  };

  float4 PS(float2 input : TEXCOORD0) : COLOR0
  {
    float temp;
    temp = degrees(atan2(0.5 - input.y ,0.5 - input.x)) + 180.0;
    clip(float2((temp - start_angle) * (end_angle - temp) * flag, length - distance(float2(0.5, 0.5), input)));

    return color;
  }

  technique
  {
   pass
   {
    PixelShader = compile ps_3_0 PS();
   }
  }
EOS

core = Shader::Core.new(hlsl, :color => :float, :start_angle => :float, :end_angle => :float, :flag => :float, :length => :float)
shader = Shader.new(core)

image = Image.new(300,300)

shader.color = [255, 255, 255, 255]
shader.start_angle = 0
shader.end_angle = 0
shader.flag = 1
shader.length = 0.4

x = 1

Window.loop do
  x += 5
  x = 0 if x > 360
  shader.start_angle = x
  Window.draw_shader(100, 100, image, shader)
end