SlimMMDXの描画システムをとりあえず構築。次からデモを作ってデバッグ
@@ -18,10 +18,10 @@ | ||
18 | 18 | /// <param name="world">ワールドマトリクス</param> |
19 | 19 | void SetParams(ref Matrix world); |
20 | 20 | /// <summary> |
21 | - /// ボーンの設定 | |
21 | + /// スキン行列の設定 | |
22 | 22 | /// </summary> |
23 | - /// <param name="bones">ボーン配列</param> | |
24 | - void SetBone(Matrix[] bones); | |
23 | + /// <param name="bones">スキン行列配列</param> | |
24 | + void SetSkinMatrix(Matrix[] skinTransforms); | |
25 | 25 | |
26 | 26 | /// <summary> |
27 | 27 | /// モデルパーツの描画 |
@@ -111,7 +111,7 @@ | ||
111 | 111 | } |
112 | 112 | protected virtual void SetBone(Matrix[] skinTransforms) |
113 | 113 | { |
114 | - System.Threading.Tasks.Parallel.ForEach(Parts, (part) => part.SetBone(skinTransforms)); | |
114 | + System.Threading.Tasks.Parallel.ForEach(Parts, (part) => part.SetSkinMatrix(skinTransforms)); | |
115 | 115 | /*foreach (var part in Parts) |
116 | 116 | part.SetBone(BoneManager.SkinTransforms);*/ |
117 | 117 | } |
@@ -11,7 +11,7 @@ | ||
11 | 11 | |
12 | 12 | namespace MikuMikuDance.Core.Model |
13 | 13 | { |
14 | - static class SkinningHelpers | |
14 | + public static class SkinningHelpers | |
15 | 15 | { |
16 | 16 | #if SlimDX |
17 | 17 | public static void SkinVertex(Matrix[] bones, MMDVertex vertin, out Vector4 outPosition) |
@@ -83,11 +83,11 @@ | ||
83 | 83 | { |
84 | 84 | effect = Effect.FromFile(SlimMMDXCore.Instance.Device, file, |
85 | 85 | #if DEBUG |
86 | - ShaderFlags.OptimizationLevel0 | ShaderFlags.Debug | |
86 | + ShaderFlags.OptimizationLevel0 | ShaderFlags.Debug | |
87 | 87 | #else |
88 | 88 | ShaderFlags.OptimizationLevel3 |
89 | 89 | #endif |
90 | - ); | |
90 | +); | |
91 | 91 | } |
92 | 92 | } |
93 | 93 | if (effect == null) |
@@ -94,11 +94,11 @@ | ||
94 | 94 | { |
95 | 95 | effect = Effect.FromMemory(SlimMMDXCore.Instance.Device, MMDXResource.MMDWinEffect, |
96 | 96 | #if DEBUG |
97 | - ShaderFlags.OptimizationLevel0 | ShaderFlags.Debug | |
97 | + ShaderFlags.OptimizationLevel0 | ShaderFlags.Debug | |
98 | 98 | #else |
99 | 99 | ShaderFlags.OptimizationLevel3 |
100 | 100 | #endif |
101 | - ); | |
101 | +); | |
102 | 102 | } |
103 | 103 | //マテリアル設定 |
104 | 104 | effect.SetValue<Vector3>("DiffuseColor", MMDXMath.ToVector3(material.DiffuseColor)); |
@@ -107,7 +107,7 @@ | ||
107 | 107 | effect.SetValue("SpecularColor", MMDXMath.ToVector3(material.SpecularColor)); |
108 | 108 | effect.SetValue("SpecularPower", material.Specularity); |
109 | 109 | int shaderIndex; |
110 | - //テクスチャ設定 | |
110 | + //テクスチャ設定 | |
111 | 111 | if (!string.IsNullOrEmpty(material.TextureFileName)) |
112 | 112 | { |
113 | 113 | //テクスチャを読み込んではめ込み |
@@ -155,8 +155,8 @@ | ||
155 | 155 | //シェーダインデックス設定 |
156 | 156 | effect.SetValue("ShaderIndex", shaderIndex); |
157 | 157 | //インデックスバッファ(頂点は組む必要ない。頂点ストリームはセットしたままにしてインデックスバッファだけ変えてドローするといい) |
158 | - IndexBuffer indexbuffer = new IndexBuffer(SlimMMDXCore.Instance.Device, (int)(sizeof(ushort) * material.FaceVertCount), Usage.None, Pool.Default, true); | |
159 | - result.Add(new MMDModelPart(effect, indexbuffer)); | |
158 | + IndexBuffer indexbuffer = new IndexBuffer(SlimMMDXCore.Instance.Device, (int)(sizeof(ushort) * material.FaceVertCount), Usage.None, Pool.Managed, true); | |
159 | + result.Add(new MMDModelPart((int)material.FaceVertCount, effect, indexbuffer)); | |
160 | 160 | } |
161 | 161 | return result; |
162 | 162 | } |
@@ -5,6 +5,9 @@ | ||
5 | 5 | using MikuMikuDance.Core.Model; |
6 | 6 | using MikuMikuDance.Core.Motion; |
7 | 7 | using MikuMikuDance.Core.Model.Physics; |
8 | +using SlimDX.Direct3D9; | |
9 | +using System.Runtime.InteropServices; | |
10 | +using SlimDX; | |
8 | 11 | |
9 | 12 | namespace MikuMikuDance.SlimDX.Model |
10 | 13 | { |
@@ -14,6 +17,9 @@ | ||
14 | 17 | public class SlimMMDModel : MMDModel |
15 | 18 | { |
16 | 19 | MMDVertexNmTx[] m_vertex; |
20 | + VertexPNmTx[] verticesSource; | |
21 | + VertexBuffer vertexBuffer; | |
22 | + | |
17 | 23 | /// <summary> |
18 | 24 | /// コンストラクタ |
19 | 25 | /// </summary> |
@@ -26,7 +32,18 @@ | ||
26 | 32 | : base(modelParts, boneManager, attachedMotion, rigids, joints) |
27 | 33 | { |
28 | 34 | m_vertex = vertex; |
29 | - | |
35 | + //データのコピー | |
36 | + verticesSource = new VertexPNmTx[m_vertex.LongLength]; | |
37 | + for (long i = 0; i < m_vertex.LongLength; ++i) | |
38 | + { | |
39 | + verticesSource[i].Position = new Vector4(m_vertex[i].Position, 0); | |
40 | + verticesSource[i].Normal = m_vertex[i].Normal; | |
41 | + verticesSource[i].Texture = m_vertex[i].TextureCoordinate; | |
42 | + } | |
43 | + vertexBuffer = new VertexBuffer(SlimMMDXCore.Instance.Device, m_vertex.Length * Marshal.SizeOf(typeof(VertexPNmTx)), Usage.Dynamic, VertexPNmTx.Format, Pool.Managed); | |
44 | + DataStream stream = vertexBuffer.Lock(0, 0, LockFlags.None); | |
45 | + stream.WriteRange(verticesSource); | |
46 | + vertexBuffer.Unlock(); | |
30 | 47 | } |
31 | 48 | |
32 | 49 | /// <summary> |
@@ -34,10 +51,21 @@ | ||
34 | 51 | /// </summary> |
35 | 52 | protected override void BeforeDraw() |
36 | 53 | { |
54 | + //頂点バッファの更新 | |
55 | + DataStream stream = vertexBuffer.Lock(0, 0, LockFlags.None); | |
56 | + stream.WriteRange(verticesSource); | |
57 | + vertexBuffer.Unlock(); | |
37 | 58 | //頂点バッファのセット処理 |
38 | - | |
59 | + SlimMMDXCore.Instance.Device.SetStreamSource(0, vertexBuffer, 0, Marshal.SizeOf(typeof(VertexPNmTx))); | |
60 | + SlimMMDXCore.Instance.Device.VertexFormat = VertexPNmTx.Format; | |
39 | 61 | base.BeforeDraw(); |
40 | - throw new NotImplementedException(); | |
41 | 62 | } |
63 | + | |
64 | + protected override void SetBone(Matrix[] skinTransforms) | |
65 | + { | |
66 | + System.Threading.Tasks.Parallel.For(0, m_vertex.Length, | |
67 | + (i) => SkinningHelpers.SkinVertex(skinTransforms, m_vertex[i], out verticesSource[i].Position)); | |
68 | + //base.SetBone(skinTransforms);(こっちでは呼ばない | |
69 | + } | |
42 | 70 | } |
43 | 71 | } |
@@ -0,0 +1,19 @@ | ||
1 | +using System; | |
2 | +using System.Collections.Generic; | |
3 | +using System.Linq; | |
4 | +using System.Text; | |
5 | +using System.Runtime.InteropServices; | |
6 | +using SlimDX.Direct3D9; | |
7 | +using SlimDX; | |
8 | + | |
9 | +namespace MikuMikuDance.SlimDX.Model | |
10 | +{ | |
11 | + [StructLayout(LayoutKind.Sequential)] | |
12 | + struct VertexPNmTx | |
13 | + { | |
14 | + public const VertexFormat Format = VertexFormat.Position | VertexFormat.Normal | VertexFormat.Texture0; | |
15 | + public Vector4 Position; | |
16 | + public Vector3 Normal; | |
17 | + public Vector2 Texture; | |
18 | + } | |
19 | +} |
@@ -4,35 +4,57 @@ | ||
4 | 4 | using System.Text; |
5 | 5 | using MikuMikuDance.Core.Model; |
6 | 6 | using SlimDX; |
7 | +using SlimDX.Direct3D9; | |
7 | 8 | |
8 | 9 | namespace MikuMikuDance.SlimDX.Model |
9 | 10 | { |
10 | 11 | public class MMDModelPart : IMMDModelPart |
11 | 12 | { |
12 | - private global::SlimDX.Direct3D9.Effect effect; | |
13 | - private global::SlimDX.Direct3D9.IndexBuffer indexbuffer; | |
14 | - | |
15 | - public MMDModelPart(global::SlimDX.Direct3D9.Effect effect, global::SlimDX.Direct3D9.IndexBuffer indexbuffer) | |
13 | + protected Effect effect; | |
14 | + protected IndexBuffer indexbuffer; | |
15 | + protected int vertexCount; | |
16 | + public MMDModelPart(int vertexCount, Effect effect, IndexBuffer indexbuffer) | |
16 | 17 | { |
17 | - // TODO: Complete member initialization | |
18 | + this.vertexCount = vertexCount; | |
18 | 19 | this.effect = effect; |
19 | 20 | this.indexbuffer = indexbuffer; |
20 | 21 | } |
21 | 22 | #region IMMDModelPart メンバー |
22 | 23 | |
24 | + /// <summary> | |
25 | + /// エフェクトにマトリックスを適用 | |
26 | + /// </summary> | |
27 | + /// <param name="world">ワールド</param> | |
23 | 28 | public void SetParams(ref Matrix world) |
24 | 29 | { |
25 | - throw new NotImplementedException(); | |
26 | - } | |
30 | + Matrix view, projection; | |
31 | + //カメラ情報の取得 | |
32 | + Viewport viewport = effect.Device.Viewport; | |
33 | + float aspectRatio = (float)viewport.Width / (float)viewport.Height; | |
34 | + SlimMMDXCore.Instance.Camera.GetCameraParam(aspectRatio, out view, out projection); | |
27 | 35 | |
28 | - public void SetBone(Matrix[] bones) | |
29 | - { | |
30 | - throw new NotImplementedException(); | |
36 | + //マトリクス処理 | |
37 | + effect.SetValue("World", world); | |
38 | + effect.SetValue("View", view); | |
39 | + effect.SetValue("Projection", projection); | |
40 | + effect.SetValue("EyePosition", SlimMMDXCore.Instance.Camera.Position); | |
41 | + //ライティング処理 | |
42 | + Vector3 color, dir; | |
43 | + SlimMMDXCore.Instance.Light.GetLightParam(out color, out dir); | |
44 | + effect.SetValue("AmbientLightColor", color); | |
45 | + effect.SetValue("DirLight0Direction", dir); | |
46 | + effect.Technique = "MMDEffect"; | |
31 | 47 | } |
32 | 48 | |
49 | + public void SetSkinMatrix(Matrix[] bones) { }//こちらではなにもしない | |
50 | + | |
33 | 51 | public void Draw() |
34 | 52 | { |
35 | - throw new NotImplementedException(); | |
53 | + effect.Begin(); | |
54 | + effect.BeginPass(0); | |
55 | + SlimMMDXCore.Instance.Device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, vertexCount, 0, vertexCount / 3); | |
56 | + effect.EndPass(); | |
57 | + effect.End(); | |
36 | 58 | } |
37 | 59 | |
38 | 60 | #endregion |