グラデーションフォント

NScripter2の記事を見ていてこういう描画ができたらいいなーと思ったので試してみた。
http://naokitakahashi-dev.blogspot.jp/2012/08/nscripter2.html?zx=670ac5de0cb5d31b


今回は文字列を扱うからRuby1.9限定になる。1.8の人はちょっといじる必要があるかもしれないが、もう忘れてしまったので各自でどうぞ。
グラデーションの角度の指定とか、袋文字+グラデーションとか、そういうのはまた気が向いたら。
あとDXRubyの文字描画関連は結構遅いみたいで、イメージの内部キャッシュとかの仕組みを作る必要がありそうだ。

#!ruby -Ks
require 'dxruby'

hlsl = <<EOS
texture tex0;
float3 start_color, end_color;

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

float4 PS(float2 input : TEXCOORD0) : COLOR0
{
  float4 output;
  output = tex2D( Samp0, input);
  output.rgb = start_color * (1 - (input.x + input.y) / 2) + end_color * (input.x + input.y) / 2;

  return output;
}

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

core = DXRuby::Shader::Core.new(hlsl, {:start_color=>:float, :end_color=>:float, :angle=>:float})
shader = Shader.new(core)
shader.start_color = [1.2,1.2,1.2]
shader.end_color = [0.3,0.3,1]

text = "グラデーションフォントでーす"
font = Font.new(32)
i = 0

Window.loop do

  text.each_char.with_index do |t, i|
    image = Image.new(font.get_width(t), font.size).draw_font(0, 0, t, font)
    Window.draw_shader(i * 32, 0, image, shader)
    image.delayed_dispose
  end

  break if Input.key_push?(K_ESCAPE)
end