PlayStation Mobile Studio
 PlayStation(R)Mobile SDK 1.21.01

■Playstation Mobile 入力 Prev Top Next
関連ページ:なし


今回はキーボード入力です。解説はなしで。

Common.cs 共通クラスとか
Sprite.vcg バーテックスシェーダーのcgファイル
Sprite.fcg フラグメントシェーダーのcgファイル
Sprite.cs スプライトシェーダークラス
SpriteMesh.cs スプライトメッシュクラス
DebugFont.cs デバッグフォントクラス
Lambert.vcg ランバートシェーダーのバーテックスシェーダーのcgファイル
Lambert.fcg ランバートシェーダーのフラグメントシェーダーのcgファイル
Lambert.cs ランバートシェーダークラス
F14Mesh.cs F14メッシュクラス
ValcanMesh.cs 弾メッシュクラス
AppMain.cs main関数があるソースファイル


---F14Mesh.cs---  ↑

using System;
using Sce.PlayStation.Core;
using Sce.PlayStation.Core.Graphics;
using Sce.PlayStation.Core.Input;
using Sce.PlayStation.HighLevel.Model;

public class F14Mesh : IDisposable
{
   // 3Dメッシュ
   private BasicModel model;

   Vector3 speed;
   Vector3 position;
   float rotationY;

   public F14Mesh ()
   {
      // XY平面上を移動するだけなのでZ値は固定
      const float z = 100;

      speed = new Vector3( 0, 0, 0 );
      position = new Vector3( 0, 0, z );
      rotationY = 0;

      // 3Dメッシュロード
      model = new BasicModel( "/Application/resources/F14.mdx", 0 );
   }

   public void Dispose()
   {
      model.Dispose();
   }

   public void Update( Matrix4 viewMatrix, Matrix4 projMatrix )
   {
      // キーボード入力
      GamePadData gamePadData = GamePad.GetData(0);

      if((gamePadData.Buttons & GamePadButtons.Left) != 0)
      {
         speed.X -= 0.5f;
         rotationY -= 0.1f;
      }
      else if((gamePadData.Buttons & GamePadButtons.Right) != 0)
      {
         speed.X += 0.5f;
         rotationY += 0.1f;
      }

      if((gamePadData.Buttons & GamePadButtons.Up) != 0)
      {
         speed.Y += 0.5f;
      }
      else if((gamePadData.Buttons & GamePadButtons.Down) != 0)
      {
         speed.Y -= 0.5f;
      }

      speed = speed *= 0.9f;
      position += speed;

      if( rotationY > (float)FMath.PI * 0.3f )
         rotationY = (float)FMath.PI * 0.3f;
      else if( rotationY < -(float)FMath.PI * 0.3f )
         rotationY = -(float)FMath.PI * 0.3f;

      rotationY *= 0.9f;

      // ビュー空間上でのZ値を取得
      float z = viewMatrix.Transform( new Vector4( 0, 0, -position.Z, -1 ) ).Z;
      // 視錐台のXY方向の最大値を計算
      Vector2 XY = projMatrix.Inverse().Transform( new Vector4( z, z, 0, z ) ).Xy;

      if( position.X > XY.X )
         position.X = XY.X;
      else if( position.X < -XY.X )
         position.X = -XY.X;

      if( position.Y > XY.Y )
         position.Y = XY.Y;
      else if( position.Y < -XY.Y )
         position.Y = -XY.Y;

      // ワールド行列

      Matrix4 matScaling = Matrix4.Scale( new Vector3( 10.0f, 10.0f, 10.0f ) );
      Matrix4 matRotationX = Matrix4.RotationX( FMath.PI * 0.5f );
      Matrix4 matRotationY = Matrix4.RotationY( rotationY );

      // position.Yはワールド空間上ではZ値とする
      Matrix4 matTranslation = Matrix4.Translation( position );

      Matrix4 matWorld = matTranslation * matRotationY * matRotationX * matScaling ;

      model.SetWorldMatrix( ref matWorld );
   }

   public Matrix4 GetWorldMatrix()
   {
      return model.WorldMatrix;
   }

   public Vector3 GetPosition()
   {
      return position;
   }

   public void Render( GraphicsContext graphics )
   {
      // 頂点バッファを設定
      graphics.SetVertexBuffer( 0, model.Parts[0].Arrays[0].VertexBuffer );

      // テクスチャーを設定する
      graphics.SetTexture( 0, model.Textures[0].Texture );

      // レンダリング
      graphics.DrawArrays( DrawMode.Triangles, 0, model.Parts[0].Arrays[0].VertexBuffer.IndexCount );
   }
}

---ValcanMesh.cs---  ↑


using System;
using Sce.PlayStation.Core;
using System.Collections.Generic;
using Sce.PlayStation.Core.Graphics;
using Sce.PlayStation.Core.Input;

public class ValcanMesh : IDisposable
{
   private enum MODE
   {
      StandBy = 0,
      Shoot = 1,
   };

   private class DataArray
   {
      public Vector3 position;
      public MODE mode;

      public DataArray( Vector3 position, MODE mode )
      {
         this.position = position;
         this.mode = mode;
      }
   };

   private SpriteMesh valcan;
   private int wait;
   private List<DataArray> dataArray;
   private int index;

   public ValcanMesh ()
   {
      dataArray = new List<DataArray>();

      // 弾の最大数はここで設定
      for( uint i=0; i<10; i++ )
      {
         dataArray.Add ( new DataArray( new Vector3( 0, 0, 0 ), MODE.StandBy ) );
      }

      Texture2D tex = new Texture2D( "/Application/resources/Valcan.png", false );
      valcan = new SpriteMesh( new Vector2( 2, 2 ), tex );
      tex.Dispose();

      wait = 0;
   }

   public void Dispose()
   {
      valcan.Dispose();
   }

   public void Update( Matrix4 viewMatrix, Matrix4 projMatrix, Vector3 ParentPos )
   {
      // キーボード入力
      GamePadData gamePadData = GamePad.GetData(0);

      if( wait == 0 )
      {
         // ○ボタン( キーボード上はDキー )
         if((gamePadData.Buttons & GamePadButtons.Circle) != 0)
         {
            for( int i=0; i<dataArray.Count; i++ )
            {
               // 未処理
               if( dataArray[i].mode == MODE.StandBy )
               {
                  dataArray[i].position = ParentPos;
                  dataArray[i].position.Y += 3.0f;
                  dataArray[i].mode = MODE.Shoot;
                  wait = 2;
                  break;
               }
            }
         }
      }
      else
      {
         if( wait > 0 )
            wait--;
      }

      // ビュー空間上でのZ値を取得
      float z = viewMatrix.Transform( new Vector4( 0, 0, -ParentPos.Z, -1 ) ).Z;
      // 視錐台のXY方向の最大値を計算
      Vector2 XY = projMatrix.Inverse().Transform( new Vector4( z, z, 0, z ) ).Xy;

      for( int i=0; i<dataArray.Count; i++ )
      {
         if( dataArray[i].mode == MODE.Shoot )
         {
            dataArray[i].position.Y += 4.0f;

            if( dataArray[i].position.Y > XY.Y )
            {
               dataArray[i].mode = MODE.StandBy;
            }
         }
      }
   }

   public void Reset()
   {
      index = -1;
   }

   public int MoveNext()
   {
      index++;
      for( int i=index; i<dataArray.Count; i++ )
      {
         if( dataArray[i].mode == MODE.Shoot )
            return index = i;
      }

      return index = -1;
   }

   public Matrix4 GetWorldMatrix(int Index)
   {
      Matrix4 matTranslation = Matrix4.Translation ( dataArray[Index].position );
      return matTranslation;
   }

   public void Render( GraphicsContext graphics )
   {
      valcan.Draw( graphics );
   }
}

---AppMain.cs---  ↑


using System;
using System.Collections.Generic;

using Sce.PlayStation.Core;
using Sce.PlayStation.Core.Environment;
using Sce.PlayStation.Core.Graphics;
using Sce.PlayStation.Core.Imaging;

namespace PSM_Samples
{
   public class AppMain : IDisposable
   {
      private const int Width = 800;
      private const int Height = 480;

      private static GraphicsContext graphics;

      private static Lambert lambert;
      private static Sprite spriteShader;

      private static DebugFont debugFont;

      // メッシュ
      private static F14Mesh f14;
      private static ValcanMesh valcan;

      // 行列
      static Matrix4 projMatrix, viewMatrix;

      // 平行光源の方向ベクトル
      static Vector3 DirectionalLightDir;

      public static void Main (string[] args)
      {
         Initialize ();

         while (true) 
         {
            SystemEvents.CheckEvents ();
            Update ();
            Render ();
         }
      }

      // 初期化
      public static void Initialize ()
      {
         // フレームバッファの解像度はウィンドウの解像度と同じサイズにする
         graphics = new GraphicsContext( Width, Height, PixelFormat.Rgba, PixelFormat.Depth16, MultiSampleMode.None );

         // シェーダーの作成
         lambert = new Lambert();
         spriteShader = new Sprite();

         // デバッグフォントの作成
         debugFont = new DebugFont();

         // 3Dメッシュの作成
         f14 = new F14Mesh();
         valcan = new ValcanMesh();

         // 射影行列
         projMatrix = Matrix4.Perspective( (float)FMath.PI / 5.0f, (float)Width / (float)Height, 10.0f, 1000.0f );

         // ビュー行列
         viewMatrix = Matrix4.LookAt( new Vector3( 0, 0, 300 ), new Vector3( 0, 0, 0 ), new Vector3( 0, 1, 0 ) );

         // 平行光源の方向ベクトル
         DirectionalLightDir = new Vector3( 0, -1, -1 );
      }

      public void Dispose()
      {
         valcan.Dispose();
         f14.Dispose();
         debugFont.Dispose();
         spriteShader.Dispose();
         lambert.Dispose();
         graphics.Dispose();
      }

      // フレーム更新処理
      public static void Update ()
      {
         // キーボード入力
         f14.Update(viewMatrix, projMatrix);
         valcan.Update(viewMatrix, projMatrix, f14.GetPosition());
      }

      // レンダリング
      public static void Render ()
      {
         graphics.SetClearColor (0.2f, 0.2f, 1, 1 );
         graphics.Clear ();

         // カリングモードを有効にする
         graphics.Enable( EnableMode.CullFace, true );
         // 反時計回りで裏面となるポリゴンを描画しない
         graphics.SetCullFace( CullFaceMode.Back, CullFaceDirection.Ccw );
         // アルファブレンド無効
         AlphaBlend.SetAlphaBlend( graphics, AlphaBlend.BLEND_TYPE.NONE );
         // 深度テスト有効
         graphics.Enable( EnableMode.DepthTest, true );

         // Lambertシェーダー
         lambert.Begin( graphics, projMatrix, viewMatrix, DirectionalLightDir );
         {
            lambert.BeginPass( f14.GetWorldMatrix() );
            {
               // F14レンダリング
               f14.Render( graphics );
            }lambert.EndPass();
         }lambert.End( graphics );


         // 加算合成
         AlphaBlend.SetAlphaBlend( graphics, AlphaBlend.BLEND_TYPE.ADD );
         // 深度テスト無効
         graphics.Enable( EnableMode.DepthTest, false );

         // スプライトシェーダー
         spriteShader.Begin( graphics, projMatrix, viewMatrix );
         {
            int index;
            valcan.Reset ();
            while( ( index = valcan.MoveNext() ) >= 0 )
            {
               // 弾レンダリング
               spriteShader.BeginPass( valcan.GetWorldMatrix( index ) );
               {
                  valcan.Render( graphics );
               }spriteShader.EndPass ();
            }
         }spriteShader.End( graphics );

         debugFont.Draw( graphics, "D:Shoot\rArrow Key:Move" );

         // スワップ
         graphics.SwapBuffers ();
      }
   }
}

キーボードの割り当ては以下の通りです。
方向キーの左 カーソルキー:←
方向キーの上 カーソルキー:↑
方向キーの右 カーソルキー:→
方向キーの下 カーソルキー:↓
□ボタン Aキー
△ボタン Wキー
○ボタン Dキー
×ボタン Sキー
SELECT ボタン Zキー
START ボタン Xキー
L ボタン Qキー
R ボタン Eキー


Prev Top Next
inserted by FC2 system