#!ruby -Ks # -*- mode: ruby; encoding: sjis -*- # Last updated: <2014/01/13 07:36:15 +0900> # # Shaderを使ってラスタスクロールのテスト # 天井、壁、床をラスタースクロールさせるっぽい感じの処理 require 'dxruby' hlsl = <; // こっちだと画面がチラチラする (´・ω・`) // MinFilter = LINEAR; // MagFilter = LINEAR; // MipFilter = LINEAR; // こっちならチラチラしない MinFilter = POINT; MagFilter = POINT; MipFilter = NONE; AddressU = WRAP; AddressV = WRAP; }; struct PixelIn { float2 UV : TEXCOORD0; }; struct PixelOut { float4 Color : COLOR0; }; PixelOut PS1(PixelIn input) { PixelOut output; input.UV.x = input.UV.x + ((d + (d * input.UV.y * dd)) / size.x); output.Color = tex2D( Samp0, input.UV ); return output; } PixelOut PS2(PixelIn input) { PixelOut output; input.UV.x = input.UV.x + ((d + (d * (1.0 - input.UV.y) * dd)) / size.x); output.Color = tex2D( Samp0, input.UV ); return output; } PixelOut PS3(PixelIn input) { PixelOut output; input.UV.x = input.UV.x + d / size.x; output.Color = tex2D( Samp0, input.UV ); return output; } // float4 PS(float2 input : TEXCOORD0) : COLOR0 // { // float4 output; // // input.x -= ((d / size.x) + ((d / size.x) * input.y * dd)); // input.x = input.x - ((d + (d * input.y * dd)) / size.x); // output = tex2D( Samp0, input); // return output; // } technique FloorScroll { pass P0 { PixelShader = compile ps_2_0 PS1(); } } technique CeilingScroll { pass P0 { PixelShader = compile ps_2_0 PS2(); } } technique WallScroll { pass P0 { PixelShader = compile ps_2_0 PS3(); } } EOS core = Shader::Core.new(hlsl, {:size=>:float, :d=>:float, :dd=>:float}) shader1 = Shader.new(core, "CeilingScroll") shader2 = Shader.new(core, "WallScroll") shader3 = Shader.new(core, "FloorScroll") # 画像に対してランダムにドットを打つ def plot_random_dot(image) dt = [[10, [255, 0, 64, 255]], [5, [255, 0, 255, 255]], [3, [255, 255, 255, 255]]] srand(0) image.height.times do |y| # next if y % 2 != 0 dt.each do |d| d[0].times {|i| image[rand(image.width), y] = d[1]} end end end image1 = Image.new(640, 150, [0, 0, 0, 0]) # 天井 image2 = Image.new(640, 180, [0, 0, 0, 0]) # 壁 image3 = Image.new(640, 150, [0, 0, 0, 0]) # 床 plot_random_dot(image1) plot_random_dot(image2) plot_random_dot(image3) shader1.size = [image1.width, image1.height] shader1.d = 0 shader1.dd = 2.0 # yが1ドット進むごとにつける角度 shader2.size = [image2.width, image2.height] shader2.d = 0 shader2.dd = 0 shader3.size = [image3.width, image3.height] shader3.d = 0 shader3.dd = 16.0 # Window.fps = 10 x = 0 Window.loop do break if Input.keyPush?(K_ESCAPE) x += 1 shader1.d = x # スクロール量を指定 shader2.d = x shader3.d = x y = 0 Window.draw_shader(0, y, image1, shader1) y += image1.height Window.draw_shader(0, y, image2, shader2) y += image2.height Window.draw_shader(0, y, image3, shader3) end