• R/O
  • SSH
  • HTTPS

mmdx: Commit


Commit MetaInfo

Revision57 (tree)
Zeit2010-03-20 17:54:02
Autorwilfrem

Log Message

ライトの修正。独自形式をMMDに近づけ、質感、処理速度が向上

Ändern Zusammenfassung

Diff

--- trunk/MikuMikuDanceXNA/Model/MMDModel.cs (revision 56)
+++ trunk/MikuMikuDanceXNA/Model/MMDModel.cs (revision 57)
@@ -172,7 +172,7 @@
172172 public override void Draw(GameTime gameTime)
173173 {
174174 Draw();
175- base.Draw(gameTime);
175+ //base.Draw(gameTime);
176176 }
177177 /// <summary>
178178 /// モデルの描画
@@ -215,6 +215,10 @@
215215
216216 private void ModelDraw(GraphicsDevice graphics, string EffectTechniqueName)
217217 {
218+#if TRACE
219+ if (mmdXNA.TimeRular != null && UseTimeRular)
220+ mmdXNA.TimeRular.BeginMark(1, "ModelDraw-Prepare", Color.PaleGreen);
221+#endif
218222 //ビューとプロジェクション取得
219223 Matrix view = mmdXNA.Camera.GetViewMatrix();
220224 Matrix projection = mmdXNA.Camera.GetProjectionMatrix(graphics);
@@ -223,12 +227,13 @@
223227 translationTexture.Flip();
224228 faceTexture.Flip();
225229
230+ //TODO: GetSkinRotation、GetSkinTranslationがボトルネック。原因はO(200)程度の処理量……
226231 //スキンアニメーション用テクスチャ
227232 rotationTexture.Texture.SetData<Quaternion>(BoneManager.GetSkinRotation());
228233 translationTexture.Texture.SetData<Vector4>(BoneManager.GetSkinTranslation());
229234 //フェイステクスチャ
230235 faceTexture.Texture.SetData<Vector4>(FaceManager.GetFaceTranslation());
231-
236+
232237 Vector2 textureSize = new Vector2(rotationTexture.Texture.Width,
233238 rotationTexture.Texture.Height);
234239 //フェイステクスチャのサイズ
@@ -235,30 +240,42 @@
235240 Vector2 faceTextureSize = new Vector2(faceTexture.Texture.Width, faceTexture.Texture.Height);
236241
237242 //ライティング設定の取得
238- Vector3[] LightVectors, LightColors;
239- mmdXNA.LightManager.GetParameters(out LightVectors, out LightColors);
243+ Vector3 LightVector, LightColor;
244+ mmdXNA.LightManager.GetParameters(out LightVector, out LightColor);
240245 //モデルのCullModeを変更
241246 CullMode mode = graphics.RenderState.CullMode;
242247 graphics.RenderState.CullMode = CullMode.None;
243-
248+
249+#if TRACE
250+ if (mmdXNA.TimeRular != null && UseTimeRular)
251+ {
252+ mmdXNA.TimeRular.EndMark(1, "ModelDraw-Prepare");
253+ mmdXNA.TimeRular.BeginMark(1, "ModelDraw-Core", Color.RosyBrown);
254+ }
255+#endif
244256 foreach (ModelMesh mesh in ModelData.ModelData.Meshes)
245257 {
246- foreach (Effect effect in mesh.Effects)
258+#if TRACE
259+ if (mmdXNA.TimeRular != null && UseTimeRular)
260+ mmdXNA.TimeRular.BeginMark(2, "ModelDraw-SetData", Color.RosyBrown);//メモ:この中の実行コードが時間くってる
261+#endif
262+ foreach (Effect effect in mesh.Effects)//メモ:エフェクト17回ループで処理時間食ってる
247263 {
264+ mmdXNA.TimeRular.BeginMark(3, "ModelDraw-SetData1", Color.RosyBrown);
265+
266+
248267 //エフェクトルーチン。調整が必要
249268 //テクニックセット
250269 effect.CurrentTechnique = effect.Techniques[EffectTechniqueName];
251- //ライティングセット(すげーアホなコード書いてるが、ToStringするとGC発生しちゃうのでやむなく……)
252- effect.Parameters["DirLight0Direction"].SetValue(LightVectors[0]);
253- effect.Parameters["DirLight0DiffuseColor"].SetValue(LightColors[0]);
254- //effect.Parameters["DirLight0SpecularColor"].SetValue(LightSpeculars[0]);
255- effect.Parameters["DirLight1Direction"].SetValue(LightVectors[1]);
256- effect.Parameters["DirLight1DiffuseColor"].SetValue(LightColors[1]);
257- //effect.Parameters["DirLight1SpecularColor"].SetValue(LightSpeculars[1]);
258- effect.Parameters["DirLight2Direction"].SetValue(LightVectors[2]);
259- effect.Parameters["DirLight2DiffuseColor"].SetValue(LightColors[2]);
260- //effect.Parameters["DirLight2SpecularColor"].SetValue(LightSpeculars[2]);
270+ //ライティングセット
271+ effect.Parameters["AmbientLightColor"].SetValue(mmdXNA.LightManager.AmbientLight.ToVector3());
272+ effect.Parameters["DirLight0Direction"].SetValue(LightVector);
273+ effect.Parameters["DirLight0DiffuseColor"].SetValue(LightColor);
261274
275+ mmdXNA.TimeRular.EndMark(3, "ModelDraw-SetData1");
276+ mmdXNA.TimeRular.BeginMark(3, "ModelDraw-SetData2", Color.RosyBrown);
277+
278+
262279 //ボーン設定
263280 effect.Parameters["BoneRotationTexture"].SetValue(
264281 rotationTexture.Texture);
@@ -269,6 +286,11 @@
269286 //表情設定
270287 effect.Parameters["FaceTranslationTexture"].SetValue(faceTexture.Texture);
271288 effect.Parameters["FaceTextureSize"].SetValue(faceTextureSize);
289+
290+ mmdXNA.TimeRular.EndMark(3, "ModelDraw-SetData2");
291+ mmdXNA.TimeRular.BeginMark(3, "ModelDraw-SetData3", Color.RosyBrown);
292+
293+
272294 //トゥーンライティング設定
273295 effect.Parameters["UseToonLighting"].SetValue(UseToon ? 1.0f : 0.0f);
274296 effect.Parameters["ToonThresholds"].SetValue(ToonThresholds);
@@ -278,10 +300,30 @@
278300 effect.Parameters["World"].SetValue(World);
279301 effect.Parameters["View"].SetValue(view);
280302 effect.Parameters["Projection"].SetValue(projection);
303+
304+ mmdXNA.TimeRular.EndMark(3, "ModelDraw-SetData3");
281305
282306 }
307+#if TRACE
308+ if (mmdXNA.TimeRular != null && UseTimeRular)
309+ {
310+ mmdXNA.TimeRular.EndMark(2, "ModelDraw-SetData");
311+ mmdXNA.TimeRular.BeginMark(2, "ModelDraw-Draw", Color.RoyalBlue);
312+ }
313+#endif
283314 mesh.Draw();
315+#if TRACE
316+ if (mmdXNA.TimeRular != null && UseTimeRular)
317+ mmdXNA.TimeRular.EndMark(2, "ModelDraw-Draw");
318+#endif
284319 }
320+#if TRACE
321+ if (mmdXNA.TimeRular != null && UseTimeRular)
322+ {
323+ mmdXNA.TimeRular.EndMark(1, "ModelDraw-Core");
324+ mmdXNA.TimeRular.BeginMark(1, "ModelDraw-After", Color.OrangeRed);
325+ }
326+#endif
285327 //CullModeの変更を戻す
286328 graphics.RenderState.CullMode = mode;
287329 //アクセサリの描画
@@ -296,6 +338,11 @@
296338 }
297339 }
298340 }
341+#if TRACE
342+ if (mmdXNA.TimeRular != null && UseTimeRular)
343+ mmdXNA.TimeRular.EndMark(1, "ModelDraw-After");
344+#endif
345+
299346 }
300347
301348 }
--- trunk/MikuMikuDanceXNA/Stages/MMDLightManager.cs (revision 56)
+++ trunk/MikuMikuDanceXNA/Stages/MMDLightManager.cs (revision 57)
@@ -12,29 +12,48 @@
1212 /// </summary>
1313 public class MMDLightManager
1414 {
15- const int NumLight = 3;
16- Vector3[] m_dir = new Vector3[NumLight], m_color = new Vector3[NumLight], m_spec = new Vector3[NumLight];
15+ //const int NumLight = 3;
16+ //Vector3[] m_dir = new Vector3[NumLight], m_color = new Vector3[NumLight], m_spec = new Vector3[NumLight];
1717
1818 /// <summary>
19+ /// 環境光の色
20+ /// </summary>
21+ /// <remarks>MMDにおける照明色はこの項目が相当</remarks>
22+ public Color AmbientLight = new Color(154, 154, 154);
23+
24+ /// <summary>
1925 /// キーライト
2026 /// </summary>
21- public MMDLight KeyLight = new MMDLight(new Vector3(1, -1, 1),
22- new Color(154, 145, 106));
27+ /// <remarks>MMDの照明方向はこの項目が相当</remarks>
28+ public MMDLight KeyLight = new MMDLight(new Vector3(-0.5f, -1f, -0.5f), new Color(80, 80, 80), LightType.DirectionalLight);//new Color(154, 154, 154));
29+
30+
31+ internal void GetParameters(out Vector3 dir, out Vector3 color)
32+ {
33+ KeyLight.SetParameters(out dir, out color);
34+ }
35+
36+
37+ //TODO: 1.1aには削除
38+ #region MMDのライトアニメーションに合わせるため、廃止
2339 /// <summary>
2440 /// フィルライト
2541 /// </summary>
42+ [Obsolete("Lightに統合", true)]
2643 public MMDLight FillLight = new MMDLight(new Vector3(-1, 1, -1),
27- new Color(148, 139, 103));
44+ new Color(148, 139, 103),LightType.DirectionalLight);
2845 /// <summary>
2946 /// バックライト
3047 /// </summary>
48+ [Obsolete("Lightに統合", true)]
3149 public MMDLight BackLight = new MMDLight(new Vector3(-1, -1, -1),
32- new Color(82, 92, 100));
50+ new Color(82, 92, 100), LightType.DirectionalLight);
3351 /// <summary>
3452 /// ライト
3553 /// </summary>
3654 /// <param name="i">0=キー、1=フィル、2=バック</param>
3755 /// <returns>ライト構造体</returns>
56+ [Obsolete("Lightを使用して下さい", true)]
3857 public MMDLight this[int i]
3958 {
4059 get
@@ -54,14 +73,8 @@
5473
5574
5675
57- internal void GetParameters(out Vector3[] dir, out Vector3[] color)
58- {
59- dir = m_dir;
60- color = m_color;
61- for (int i = 0; i < 3; i++)
62- {
63- this[i].SetParameters(out dir[i], out color[i]);
64- }
65- }
76+
77+ #endregion
78+
6679 }
6780 }
--- trunk/MikuMikuDanceXNA/Stages/MMDLight.cs (revision 56)
+++ trunk/MikuMikuDanceXNA/Stages/MMDLight.cs (revision 57)
@@ -8,8 +8,22 @@
88 namespace MikuMikuDance.XNA.Stages
99 {
1010 /// <summary>
11- /// ライト
11+ /// ライトの種別
1212 /// </summary>
13+ public enum LightType
14+ {
15+ /// <summary>
16+ /// ディレクショナルライト
17+ /// </summary>
18+ DirectionalLight = 0,
19+ /// <summary>
20+ /// スポットライト(未実装)
21+ /// </summary>
22+ SpotLight = 1,
23+ }
24+ /// <summary>
25+ /// ライトクラス
26+ /// </summary>
1327 public struct MMDLight
1428 {
1529 /// <summary>
@@ -20,10 +34,17 @@
2034 /// カラー
2135 /// </summary>
2236 public Color Color;
23- internal MMDLight(Vector3 dir, Color color)
37+
38+ /// <summary>
39+ /// ライトの種別
40+ /// </summary>
41+ public LightType Type;
42+
43+ internal MMDLight(Vector3 dir, Color color, LightType type)
2444 {
2545 Direction = dir;
2646 Color = color;
47+ Type = type;
2748 }
2849 internal void SetParameters(out Vector3 dir, out Vector3 color)
2950 {
--- trunk/MikuMikuDanceXNA/Accessory/MMDAccessory.cs (revision 56)
+++ trunk/MikuMikuDanceXNA/Accessory/MMDAccessory.cs (revision 57)
@@ -23,6 +23,10 @@
2323 /// このモデルに適応するワールド座標系
2424 /// </summary>
2525 public Matrix World { get; set; }
26+ /// <summary>
27+ /// MikuMikuDanceXNA.TimeRularをこのアクセサリが呼び出すかどうか
28+ /// </summary>
29+ public bool UseTimeRular { get; set; }
2630
2731 /// <summary>
2832 /// コンストラクタ
@@ -37,6 +41,7 @@
3741 this.mmd = mmd;
3842 ModelData = model;
3943 World = world;
44+ UseTimeRular = true;
4045 game.Components.Add(this);
4146 }
4247 /// <summary>
@@ -48,7 +53,7 @@
4853 //親がいない場合は自分で描画する。親がいる場合は親のタイミングで描画される
4954 if (parent == null)
5055 Draw(Matrix.Identity);
51- base.Draw(gameTime);
56+ //base.Draw(gameTime);
5257 }
5358 /// <summary>
5459 /// 内部描画ルーチン
@@ -56,36 +61,48 @@
5661 /// <param name="baseTransform">基準トランスフォーム</param>
5762 internal void Draw(Matrix baseTransform)
5863 {
64+#if TRACE
65+ if (mmd.TimeRular != null && UseTimeRular)
66+ {
67+ mmd.TimeRular.BeginMark(1, "AccessoryDraw", Color.Red);
68+ mmd.TimeRular.BeginMark(2, "AccessoryDraw-Prepare", Color.Gray);
69+ }
70+#endif
5971 //アクセサリ描画用に設定
6072 Game.GraphicsDevice.RenderState.AlphaBlendEnable = true;
6173 Game.GraphicsDevice.RenderState.BlendFunction = BlendFunction.Add;
6274 Game.GraphicsDevice.RenderState.SourceBlend = Blend.SourceAlpha;
6375 Game.GraphicsDevice.RenderState.DestinationBlend = Blend.InverseSourceAlpha;
76+ Game.GraphicsDevice.RenderState.AlphaTestEnable = true;
77+ Game.GraphicsDevice.RenderState.ReferenceAlpha = 1;
78+ Game.GraphicsDevice.RenderState.AlphaFunction = CompareFunction.GreaterEqual;
79+
6480 //モデルのCullModeを変更
6581 CullMode mode = Game.GraphicsDevice.RenderState.CullMode;
66- Game.GraphicsDevice.RenderState.CullMode = CullMode.None;
67- //ステージの描画
82+ Game.GraphicsDevice.RenderState.CullMode = CullMode.CullCounterClockwiseFace;
83+#if TRACE
84+ if (mmd.TimeRular != null && UseTimeRular)
85+ {
86+ mmd.TimeRular.EndMark(2, "AccessoryDraw-Prepare");
87+ mmd.TimeRular.BeginMark(2, "AccessoryDraw-Core", Color.Green);
88+ }
89+#endif
90+ //アクセサリの描画
6891 foreach (ModelMesh mesh in ModelData.Meshes)
6992 {
7093 foreach (BasicEffect effect in mesh.Effects)
7194 {
7295 effect.LightingEnabled = true;
73- //effect.EnableDefaultLighting();
96+ effect.AmbientLightColor = mmd.LightManager.AmbientLight.ToVector3();
7497 effect.DirectionalLight0.Direction = mmd.LightManager.KeyLight.Direction;
7598 effect.DirectionalLight0.Direction.Normalize();
76- effect.DirectionalLight0.DiffuseColor = mmd.LightManager.KeyLight.Color.ToVector3();
77- effect.DirectionalLight0.SpecularColor = Vector3.Zero;
99+ effect.DirectionalLight0.DiffuseColor = mmd.LightManager.KeyLight.Color.ToVector3();//new Vector3(0.2f, 0.2f, 0.2f);
100+ effect.DirectionalLight0.SpecularColor = mmd.LightManager.KeyLight.Color.ToVector3();
78101 effect.DirectionalLight0.Enabled = true;
102+ effect.DirectionalLight1.Enabled = false;
103+ effect.DirectionalLight2.Enabled = false;
104+ effect.PreferPerPixelLighting = true;
79105
80- effect.DirectionalLight1.Direction = mmd.LightManager.FillLight.Direction;
81- effect.DirectionalLight1.DiffuseColor = mmd.LightManager.FillLight.Color.ToVector3();
82- effect.DirectionalLight1.SpecularColor = Vector3.Zero;
83- effect.DirectionalLight1.Enabled = true;
84- effect.DirectionalLight2.Direction = mmd.LightManager.BackLight.Direction;
85- effect.DirectionalLight2.DiffuseColor = mmd.LightManager.BackLight.Color.ToVector3();
86- effect.DirectionalLight2.SpecularColor = Vector3.Zero;
87- effect.DirectionalLight2.Enabled = true;
88-
89106 effect.World = baseTransform * World;
90107 effect.View = mmd.Camera.GetViewMatrix();
91108 effect.Projection = mmd.Camera.GetProjectionMatrix(Game.GraphicsDevice);
@@ -97,8 +114,15 @@
97114 Game.GraphicsDevice.RenderState.AlphaBlendEnable = false;
98115 //CullModeの変更を戻す
99116 Game.GraphicsDevice.RenderState.CullMode = mode;
100-
101117
118+#if TRACE
119+ if (mmd.TimeRular != null && UseTimeRular)
120+ {
121+ mmd.TimeRular.EndMark(2, "AccessoryDraw-Core");
122+ mmd.TimeRular.EndMark(1, "AccessoryDraw");
123+ }
124+#endif
125+
102126 }
103127
104128 }
Cannot display: file marked as a binary type.
svn:mime-type = application/octet-stream
Show on old repository browser