在Unity中制作MMD舞蹈视频

  1. 1.
    1. 1.1. 准备工作
    2. 1.2. 处理MMD文件
    3. 1.3. 更新


准备工作

  1. 创建工程。这里我用的是2019.4.31f1c1版本,有较好的插件兼容性
  2. 导入自己的场景和人物模型
  3. 导入插件包:
  • Dynamic Bone 1.2.0即动骨插件,建议使用高版本,方便后续替换成PhysBone
  • VRCSDK3-AVATAR-2021.11.24.16.20_PublicVRC官网下,建议使用高版本,方便后续替换成PhysBone
  • VRC工具箱v1.2.5_by如梦来自B站 ,必须最后导入否则报错
  1. 布置场景

处理MMD文件

  1. 导入vmd模型动作文件
  2. 导入vmd镜头动作文件,并在文件名后面加上.bytes,即 xxx.vmd.bytes
  3. 导入音乐文件
  4. 使用菜单中的VRC工具箱 —— MMD动作转换 转换获得动作(不含表情)
  5. 复制一份动作出来方便修改k帧(不复制出来的是只读),取消勾选Bake Into Pose
  6. 创建 Animator Controller并给角色加上,增加一个Layer用来放表情动画,并加上Mask保险
  7. 导入MMD4UnityTools插件,用处是获得表情动画 下载地址:https://github.com/ShiinaManatsu/MMD4UnityTools
  8. 导入完插件后修改MMDExtensionsEditor第300行附近的代码
    修改前
    修改后
    其中var chara = GameObject.Find(“Body”); 是获取表情的形态键所在的游戏物体
    以下为我的目录结构,仅供参考
  9. 修改完后就能够右键vmd文件创建表情动作了
  10. 将表情动作加到Animator中,双击点开表情动作,再点击场景中的人物模型,再在左上角下拉框选择表情动作,会发现下面的Missing提示

    这时只需手动按形态键名字添加上再复制关键帧即可
    只有一帧的关键帧可以先删除
    批量添加操作为:Ctrl按住+左键加选,选完后右键Add
    添加完后删除最后一帧,Ctrl+C复制Missing的所有帧,选中新添加的所有第一帧,Ctrl+V粘贴即可
  11. 添加相机动画:只需一个脚本,视频教程:https://www.bilibili.com/video/BV16r4y1n7pz
    不想看可忽略,继续看下文
    以下为该脚本
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    using UnityEngine;
    using System.Runtime.InteropServices;

    public class MMD_VmdCameraLoad : MonoBehaviour
    {

    public TextAsset Select_VMD; //vmdファイル
    public GameObject CameraCenter; //カメラ中心
    public Camera MainCamera; //カメラ
    public GameObject MMD_model; //MMDモデル

    private bool success = false; //データの準備完了フラグ
    CameraData[] Cam_m;
    private int t = 0;
    private float t_f = 0.0f;
    private Animator target_animator;
    private float nowframe = 0.0f;

    [Header("相机视野远近")]
    [Range(0f,120f)]
    [SerializeField] private float camFieldOfView = 60f;
    const int HEADER = 50; //ヘッダーの桁数
    const int MOTIONCOUNT = 4; //モーションレコード数
    const int MOTIONDATA = 111; //モーションデータ(レコード数分)
    const int SKINCOUNT = 4; //スキンレコード数
    const int SKINDATA = 23; //スキンデータ(レコード数分)
    const int CAMERACOUNT = 4; //カメラレコード数
    const int CAMERADATA = 61; //カメラデータ(レコード数分)
    const int ILLUMINATIONCOUNT = 4; //照明レコード数
    const int ILLUMINATIONDATA = 28; //照明データ(レコード数分)
    const int SHADOWCOUNT = 4; //シャドウレコード数
    const int SHADOWDATA = 9; //シャドウデータ(レコード数分)

    [StructLayout(LayoutKind.Explicit)]
    public struct Union
    {
    [FieldOffset(0)]
    public float float0;

    [FieldOffset(0)]
    public int int0;
    }

    //カメラ情報
    public struct CameraData
    {
    public int frame; //対象フレーム
    public float distans; //距離

    public float Pos_x; //カメラ中心のx座標
    public float Pos_y; //カメラ中心のy座標
    public float Pos_z; //カメラ中心のz座標

    public float Rot_x; //カメラ中心のx軸回転
    public float Rot_y; //カメラ中心のy軸回転
    public float Rot_z; //カメラ中心のz軸回転

    public float viewAngle; //視野角

    public int[] Bezier; //ベジェ曲線の補間パラメータ
    public bool originalframe;
    }
    void Awake()
    {
    target_animator = MMD_model.GetComponent<Animator>();
    }
    // Use this for initialization
    void Start()
    {
    Camera.main.transform.localPosition = new Vector3(0f, 0f, 0f);
    Quaternion rotation = Quaternion.Euler(0f,0f,0f);
    Camera.main.transform.localRotation = rotation;

    byte[] raw_data_org = Select_VMD.bytes;
    byte[] frameSum = new byte[4];
    int frameSum_int = 0;
    byte[] frame_data = new byte[4];
    byte[] frame_data_1byte = new byte[1];

    int index = HEADER + MOTIONCOUNT + SKINCOUNT; //カメラレコード数を格納している位置まで飛ばす

    //レコード数の取得
    frameSum[0] = raw_data_org[index++];
    frameSum[1] = raw_data_org[index++];
    frameSum[2] = raw_data_org[index++];
    frameSum[3] = raw_data_org[index++];
    frameSum_int = System.BitConverter.ToInt32(frameSum, 0);
    CameraData[] Cam = new CameraData[frameSum_int]; //レコード数分要素を用意

    // データ取得
    for (int i = 0; i < frameSum_int; i++)
    {
    //フレーム
    frame_data[0] = raw_data_org[index++];
    frame_data[1] = raw_data_org[index++];
    frame_data[2] = raw_data_org[index++];
    frame_data[3] = raw_data_org[index++];
    Cam[i].frame = System.BitConverter.ToInt32(frame_data, 0);
    //距離
    Cam[i].distans = getVmdCamera(ref index, raw_data_org);
    //位置
    Cam[i].Pos_x = getVmdCamera(ref index, raw_data_org);
    Cam[i].Pos_y = getVmdCamera(ref index, raw_data_org);
    Cam[i].Pos_z = getVmdCamera(ref index, raw_data_org);
    Cam[i].Rot_x = getVmdCamera(ref index, raw_data_org);
    //角度(ラジアンから変換)
    conversionAngle(ref Cam[i].Rot_x);
    Cam[i].Rot_y = getVmdCamera(ref index, raw_data_org);
    conversionAngle(ref Cam[i].Rot_y);
    Cam[i].Rot_z = getVmdCamera(ref index, raw_data_org);
    conversionAngle(ref Cam[i].Rot_z);
    //ベジェ曲線
    Cam[i].Bezier = new int[24];
    for (int j = 0; j < 24; j++)
    {
    frame_data_1byte[0] = raw_data_org[index++];
    Cam[i].Bezier[j] = System.Convert.ToInt32(System.BitConverter.ToString(frame_data_1byte, 0), 16);
    }
    //視野角
    frame_data[0] = raw_data_org[index++];
    frame_data[1] = raw_data_org[index++];
    frame_data[2] = raw_data_org[index++];
    frame_data[3] = raw_data_org[index++];
    Cam[i].viewAngle = System.BitConverter.ToInt32(frame_data, 0);
    index += 1; //パース分1バイト飛ばす
    }

    //並び順バラバラなのでソート
    Qsort(ref Cam, 0, Cam.Length - 1);
    //以降補間処理
    Cam_m = new CameraData[Cam[frameSum_int - 1].frame + 1]; //最終フレーム分用意

    //3次ベジェ曲線補間
    Cam_m[0] = Cam[0];//1レコード目をコピー
    Cam_m[0].originalframe = true;
    int Addframe = 0;
    int wIndex = 1;

    for (int i = 0; i < frameSum_int - 1; i++)
    {
    Addframe = Cam[i + 1].frame - Cam[i].frame;
    for (int j = 1; j < Addframe; j++)
    {

    Cam_m[wIndex].frame = wIndex;
    Cam_m[wIndex].Pos_x = Cam[i].Pos_x + (Cam[i + 1].Pos_x - Cam[i].Pos_x) *
    (BezierCurve(new Vector2(0, 0), new Vector2(Cam[i + 1].Bezier[0], Cam[i + 1].Bezier[2]),
    new Vector2(Cam[i + 1].Bezier[1], Cam[i + 1].Bezier[3]), new Vector2(127, 127),
    (float)(1.0 * j / (Addframe))).y) / 127;
    Cam_m[wIndex].Pos_y = Cam[i].Pos_y + (Cam[i + 1].Pos_y - Cam[i].Pos_y) *
    (BezierCurve(new Vector2(0, 0), new Vector2(Cam[i + 1].Bezier[4], Cam[i + 1].Bezier[6]),
    new Vector2(Cam[i + 1].Bezier[5], Cam[i + 1].Bezier[7]), new Vector2(127, 127),
    (float)(1.0 * j / (Addframe))).y) / 127;
    Cam_m[wIndex].Pos_z = Cam[i].Pos_z + (Cam[i + 1].Pos_z - Cam[i].Pos_z) *
    (BezierCurve(new Vector2(0, 0), new Vector2(Cam[i + 1].Bezier[8], Cam[i + 1].Bezier[10]),
    new Vector2(Cam[i + 1].Bezier[9], Cam[i + 1].Bezier[11]), new Vector2(127, 127),
    (float)(1.0 * j / (Addframe))).y) / 127;
    Cam_m[wIndex].Rot_x = Cam[i].Rot_x + (Cam[i + 1].Rot_x - Cam[i].Rot_x) *
    (BezierCurve(new Vector2(0, 0), new Vector2(Cam[i + 1].Bezier[12], Cam[i + 1].Bezier[14]),
    new Vector2(Cam[i + 1].Bezier[13], Cam[i + 1].Bezier[15]), new Vector2(127, 127),
    (float)(1.0 * j / (Addframe))).y) / 127;
    Cam_m[wIndex].Rot_y = Cam[i].Rot_y + (Cam[i + 1].Rot_y - Cam[i].Rot_y) *
    (BezierCurve(new Vector2(0, 0), new Vector2(Cam[i + 1].Bezier[12], Cam[i + 1].Bezier[14]),
    new Vector2(Cam[i + 1].Bezier[13], Cam[i + 1].Bezier[15]), new Vector2(127, 127),
    (float)(1.0 * j / (Addframe))).y) / 127;
    Cam_m[wIndex].Rot_z = Cam[i].Rot_z + (Cam[i + 1].Rot_z - Cam[i].Rot_z) *
    (BezierCurve(new Vector2(0, 0), new Vector2(Cam[i + 1].Bezier[12], Cam[i + 1].Bezier[14]),
    new Vector2(Cam[i + 1].Bezier[13], Cam[i + 1].Bezier[15]), new Vector2(127, 127),
    (float)(1.0 * j / (Addframe))).y) / 127;
    Cam_m[wIndex].distans = Cam[i].distans + (Cam[i + 1].distans - Cam[i].distans) *
    (BezierCurve(new Vector2(0, 0), new Vector2(Cam[i + 1].Bezier[16], Cam[i + 1].Bezier[18]),
    new Vector2(Cam[i + 1].Bezier[17], Cam[i + 1].Bezier[19]), new Vector2(127, 127),
    (float)(1.0 * j / (Addframe))).y) / 127;
    Cam_m[wIndex].viewAngle = Cam[i].viewAngle + (Cam[i + 1].viewAngle - Cam[i].viewAngle) *
    (int)(BezierCurve(new Vector2(0, 0), new Vector2(Cam[i + 1].Bezier[20], Cam[i + 1].Bezier[22]),
    new Vector2(Cam[i + 1].Bezier[21], Cam[i + 1].Bezier[23]), new Vector2(127, 127),
    (float)(1.0 * j / (Addframe))).y) / 127;
    wIndex++;
    }
    Cam_m[wIndex] = Cam[i + 1];
    Cam_m[wIndex++].originalframe = true;
    }
    success = true;
    }

    private void Update()
    {
    //カメラ情報が完成したらカメラスタート
    if (success)
    {
    if (t != (int)nowframe)
    {
    //新しいフレームの処理
    t = (int)nowframe;
    //最終フレームを超えないようにする処理
    if (t < Cam_m.Length - 1)
    {
    CameraCenter.transform.localPosition = new Vector3(Cam_m[t].Pos_x / 12.5f + MMD_model.transform.localPosition.x,
    Cam_m[t].Pos_y / 12.5f + MMD_model.transform.localPosition.y,
    Cam_m[t].Pos_z / 12.5f + MMD_model.transform.localPosition.z);
    this.transform.localPosition = new Vector3(this.transform.localPosition.x, this.transform.localPosition.y, Cam_m[t].distans / 12.5f);
    CameraCenter.transform.rotation = Quaternion.Euler(-Cam_m[t].Rot_x, -Cam_m[t].Rot_y, -Cam_m[t].Rot_z);
    MainCamera.fieldOfView = camFieldOfView;//Cam_m[t].viewAngle; sxqn&lianai:60 SingMe:18
    }
    }
    else
    {
    //同じフレームが再度処理された場合の処理
    if (t + 1 < Cam_m.Length - 1 && !Cam_m[t + 1].originalframe)
    {
    t_f = nowframe - (int)nowframe;
    CameraCenter.transform.localPosition = new Vector3(((Cam_m[t + 1].Pos_x - Cam_m[t].Pos_x) * t_f + Cam_m[t].Pos_x) / 12.5f + MMD_model.transform.localPosition.x,
    ((Cam_m[t + 1].Pos_y - Cam_m[t].Pos_y) * t_f + Cam_m[t].Pos_y) / 12.5f + MMD_model.transform.localPosition.y,
    ((Cam_m[t + 1].Pos_z - Cam_m[t].Pos_z) * t_f + Cam_m[t].Pos_z) / 12.5f + MMD_model.transform.localPosition.z);
    this.transform.localPosition = new Vector3(this.transform.localPosition.x, this.transform.localPosition.y, Cam_m[t].distans / 12.5f);
    CameraCenter.transform.rotation = Quaternion.Euler((-Cam_m[t + 1].Rot_x - -Cam_m[t].Rot_x) * t_f + -Cam_m[t].Rot_x,
    (-Cam_m[t + 1].Rot_y - -Cam_m[t].Rot_y) * t_f + -Cam_m[t].Rot_y,
    (-Cam_m[t + 1].Rot_z - -Cam_m[t].Rot_z) * t_f + -Cam_m[t].Rot_z);
    MainCamera.fieldOfView = camFieldOfView;// (Cam_m[t + 1].viewAngle - Cam_m[t].viewAngle) * t_f + Cam_m[t].viewAngle;
    }
    }
    }
    nowframe = getAnimationFrame();
    }

    private float getAnimationFrame()
    {

    float returnValue = 0.0f;

    var clipInfoList = target_animator.GetCurrentAnimatorClipInfo(0);
    if (clipInfoList != null)
    {
    var clip = clipInfoList[0].clip;
    var stateInfo = target_animator.GetCurrentAnimatorStateInfo(0);
    returnValue = clip.length * stateInfo.normalizedTime * clip.frameRate;
    }

    return returnValue;
    }

    float getVmdCamera(ref int index, byte[] data)
    {
    Union union = new Union();
    byte[] raw_data = new byte[4];

    raw_data[0] = data[index++];
    raw_data[1] = data[index++];
    raw_data[2] = data[index++];
    raw_data[3] = data[index++];
    union.int0 = System.BitConverter.ToInt32(raw_data, 0);

    return (union.float0);
    }
    // ラジアンから角度を取得
    void conversionAngle(ref float rot)
    {
    rot = (float)(rot * 180 / System.Math.PI);
    }
    //クイックソート
    void Qsort(ref CameraData[] data, int left, int right)
    {
    int i, j;
    int pivot;
    CameraData tmp;

    i = left; j = right;
    pivot = data[(left + right) / 2].frame;
    do
    {
    while ((i < right) && (data[i].frame < pivot)) i++;
    while ((j > left) && (pivot < data[j].frame)) j--;
    if (i <= j)
    {
    tmp = data[i];
    data[i] = data[j];
    data[j] = tmp;
    i++; j--;
    }
    } while (i <= j);
    if (left < j) Qsort(ref data, left, j);
    if (i < right) Qsort(ref data, i, right);
    }

    //以降ベジェ曲線に関する関数
    float BezierCurveX(float x1, float x2, float x3, float x4, float t)
    {
    return Mathf.Pow(1 - t, 3) * x1 + 3 * Mathf.Pow(1 - t, 2) * t * x2 + 3 * (1 - t) * Mathf.Pow(t, 2) * x3 + Mathf.Pow(t, 3) * x4;
    }
    float BezierCurveY(float y1, float y2, float y3, float y4, float t)
    {
    return Mathf.Pow(1 - t, 3) * y1 + 3 * Mathf.Pow(1 - t, 2) * t * y2 + 3 * (1 - t) * Mathf.Pow(t, 2) * y3 + Mathf.Pow(t, 3) * y4;
    }
    Vector2 BezierCurve(Vector2 p1, Vector2 p2, Vector2 p3, Vector2 p4, float t)
    {
    return new Vector2(
    BezierCurveX(p1.x, p2.x, p3.x, p4.x, t),
    BezierCurveY(p1.y, p2.y, p3.y, p4.y, t));
    }

    }


    说明:其中有部分内容是经过我修改过的,在此指出:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16

    //Start()中的这三行
    Camera.main.transform.localPosition = new Vector3(0f, 0f, 0f);
    Quaternion rotation = Quaternion.Euler(0f,0f,0f);
    Camera.main.transform.localRotation = rotation;
    //以及
    [Header("相机视野远近")]
    [Range(0f,120f)]
    [SerializeField] private float camFieldOfView = 60f;
    .
    .
    .
    MainCamera.fieldOfView = camFieldOfView;//Cam_m[t].viewAngle; sxqn&lianai:60 SingMe:18
    .
    .
    MainCamera.fieldOfView = camFieldOfView;// (Cam_m[t + 1].viewAngle - Cam_m[t].viewAngle) * t_f + Cam_m[t].viewAngle;

将该脚本添加到下图所示物体上,并按对应的命名来设置相机的目录结构,VMDCAM为空节点,Camera为正常的主相机
并为脚本附上对应的物体:.bytes文件、节点、相机、人物模型

16. 最后为相机加上Audio Source组件加上音乐就完成啦

17. 最后的最后推荐一个AssetStore的资源,叫做Unity Recorder,它能在运行游戏时帮你录制Game窗口的画面,并能直接录制4K画质,使用方便
教程:https://www.bilibili.com/video/BV1Lt411H7bp


  1. PhysBone比Dynamic Bone的物理性能高10倍;
  2. 最终若想发布则不能使用PhysBone因为需配合VRCSDK一起使用,而VRCSDK无法导出;
  3. 可以移除VRCSDK后再导出,当然也只能使用Dynamic Bone了。

更新


2023/11/21更新


更新相机脚本,使相机不会离模型过远,完整脚本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
using UnityEngine;
using System.Runtime.InteropServices;

public class MMD_VmdCameraLoad : MonoBehaviour
{

public TextAsset Select_VMD; //vmdファイル
[SerializeField] private GameObject CameraCenter; //カメラ中心
[SerializeField] private Camera MainCamera; //カメラ
[SerializeField] public GameObject MMD_model; //MMDモデル

private bool success = false; //データの準備完了フラグ
CameraData[] Cam_m;
private int t = 0;
private float t_f = 0.0f;
private Animator target_animator;
private float nowframe = 0.0f;

[Header("相机视野远近")]
[Range(0f,120f)]
[SerializeField] public float camFieldOfView = 60f;
const int HEADER = 50; //ヘッダーの桁数
const int MOTIONCOUNT = 4; //モーションレコード数
const int MOTIONDATA = 111; //モーションデータ(レコード数分)
const int SKINCOUNT = 4; //スキンレコード数
const int SKINDATA = 23; //スキンデータ(レコード数分)
const int CAMERACOUNT = 4; //カメラレコード数
const int CAMERADATA = 61; //カメラデータ(レコード数分)
const int ILLUMINATIONCOUNT = 4; //照明レコード数
const int ILLUMINATIONDATA = 28; //照明データ(レコード数分)
const int SHADOWCOUNT = 4; //シャドウレコード数
const int SHADOWDATA = 9; //シャドウデータ(レコード数分)

[StructLayout(LayoutKind.Explicit)]
public struct Union
{
[FieldOffset(0)]
public float float0;

[FieldOffset(0)]
public int int0;
}

//カメラ情報
public struct CameraData
{
public int frame; //対象フレーム
public float distans; //距離

public float Pos_x; //カメラ中心のx座標
public float Pos_y; //カメラ中心のy座標
public float Pos_z; //カメラ中心のz座標

public float Rot_x; //カメラ中心のx軸回転
public float Rot_y; //カメラ中心のy軸回転
public float Rot_z; //カメラ中心のz軸回転

public float viewAngle; //視野角

public int[] Bezier; //ベジェ曲線の補間パラメータ
public bool originalframe;
}
void Awake()
{
target_animator = MMD_model.GetComponent<Animator>();
}
// Use this for initialization
public void Start()
{
Camera.main.transform.localPosition = new Vector3(0f, 0f, 0f);
Quaternion rotation = Quaternion.Euler(0f,0f,0f);
Camera.main.transform.localRotation = rotation;

byte[] raw_data_org = Select_VMD.bytes;
byte[] frameSum = new byte[4];
int frameSum_int = 0;
byte[] frame_data = new byte[4];
byte[] frame_data_1byte = new byte[1];

int index = HEADER + MOTIONCOUNT + SKINCOUNT; //カメラレコード数を格納している位置まで飛ばす

//レコード数の取得
frameSum[0] = raw_data_org[index++];
frameSum[1] = raw_data_org[index++];
frameSum[2] = raw_data_org[index++];
frameSum[3] = raw_data_org[index++];
frameSum_int = System.BitConverter.ToInt32(frameSum, 0);
CameraData[] Cam = new CameraData[frameSum_int]; //レコード数分要素を用意

// データ取得
for (int i = 0; i < frameSum_int; i++)
{
//フレーム
frame_data[0] = raw_data_org[index++];
frame_data[1] = raw_data_org[index++];
frame_data[2] = raw_data_org[index++];
frame_data[3] = raw_data_org[index++];
Cam[i].frame = System.BitConverter.ToInt32(frame_data, 0);
//距離
Cam[i].distans = getVmdCamera(ref index, raw_data_org);
//位置
Cam[i].Pos_x = getVmdCamera(ref index, raw_data_org);
Cam[i].Pos_y = getVmdCamera(ref index, raw_data_org);
Cam[i].Pos_z = getVmdCamera(ref index, raw_data_org);
Cam[i].Rot_x = getVmdCamera(ref index, raw_data_org);
//角度(ラジアンから変換)
conversionAngle(ref Cam[i].Rot_x);
Cam[i].Rot_y = getVmdCamera(ref index, raw_data_org);
conversionAngle(ref Cam[i].Rot_y);
Cam[i].Rot_z = getVmdCamera(ref index, raw_data_org);
conversionAngle(ref Cam[i].Rot_z);
//ベジェ曲線
Cam[i].Bezier = new int[24];
for (int j = 0; j < 24; j++)
{
frame_data_1byte[0] = raw_data_org[index++];
Cam[i].Bezier[j] = System.Convert.ToInt32(System.BitConverter.ToString(frame_data_1byte, 0), 16);
}
//視野角
frame_data[0] = raw_data_org[index++];
frame_data[1] = raw_data_org[index++];
frame_data[2] = raw_data_org[index++];
frame_data[3] = raw_data_org[index++];
Cam[i].viewAngle = System.BitConverter.ToInt32(frame_data, 0);
index += 1; //パース分1バイト飛ばす
}

//並び順バラバラなのでソート
Qsort(ref Cam, 0, Cam.Length - 1);
//以降補間処理
Cam_m = new CameraData[Cam[frameSum_int - 1].frame + 1]; //最終フレーム分用意

//3次ベジェ曲線補間
Cam_m[0] = Cam[0];//1レコード目をコピー
Cam_m[0].originalframe = true;
int Addframe = 0;
int wIndex = 1;

for (int i = 0; i < frameSum_int - 1; i++)
{
Addframe = Cam[i + 1].frame - Cam[i].frame;
for (int j = 1; j < Addframe; j++)
{

Cam_m[wIndex].frame = wIndex;
Cam_m[wIndex].Pos_x = Cam[i].Pos_x + (Cam[i + 1].Pos_x - Cam[i].Pos_x) *
(BezierCurve(new Vector2(0, 0), new Vector2(Cam[i + 1].Bezier[0], Cam[i + 1].Bezier[2]),
new Vector2(Cam[i + 1].Bezier[1], Cam[i + 1].Bezier[3]), new Vector2(127, 127),
(float)(1.0 * j / (Addframe))).y) / 127;
Cam_m[wIndex].Pos_y = Cam[i].Pos_y + (Cam[i + 1].Pos_y - Cam[i].Pos_y) *
(BezierCurve(new Vector2(0, 0), new Vector2(Cam[i + 1].Bezier[4], Cam[i + 1].Bezier[6]),
new Vector2(Cam[i + 1].Bezier[5], Cam[i + 1].Bezier[7]), new Vector2(127, 127),
(float)(1.0 * j / (Addframe))).y) / 127;
Cam_m[wIndex].Pos_z = Cam[i].Pos_z + (Cam[i + 1].Pos_z - Cam[i].Pos_z) *
(BezierCurve(new Vector2(0, 0), new Vector2(Cam[i + 1].Bezier[8], Cam[i + 1].Bezier[10]),
new Vector2(Cam[i + 1].Bezier[9], Cam[i + 1].Bezier[11]), new Vector2(127, 127),
(float)(1.0 * j / (Addframe))).y) / 127;
Cam_m[wIndex].Rot_x = Cam[i].Rot_x + (Cam[i + 1].Rot_x - Cam[i].Rot_x) *
(BezierCurve(new Vector2(0, 0), new Vector2(Cam[i + 1].Bezier[12], Cam[i + 1].Bezier[14]),
new Vector2(Cam[i + 1].Bezier[13], Cam[i + 1].Bezier[15]), new Vector2(127, 127),
(float)(1.0 * j / (Addframe))).y) / 127;
Cam_m[wIndex].Rot_y = Cam[i].Rot_y + (Cam[i + 1].Rot_y - Cam[i].Rot_y) *
(BezierCurve(new Vector2(0, 0), new Vector2(Cam[i + 1].Bezier[12], Cam[i + 1].Bezier[14]),
new Vector2(Cam[i + 1].Bezier[13], Cam[i + 1].Bezier[15]), new Vector2(127, 127),
(float)(1.0 * j / (Addframe))).y) / 127;
Cam_m[wIndex].Rot_z = Cam[i].Rot_z + (Cam[i + 1].Rot_z - Cam[i].Rot_z) *
(BezierCurve(new Vector2(0, 0), new Vector2(Cam[i + 1].Bezier[12], Cam[i + 1].Bezier[14]),
new Vector2(Cam[i + 1].Bezier[13], Cam[i + 1].Bezier[15]), new Vector2(127, 127),
(float)(1.0 * j / (Addframe))).y) / 127;
Cam_m[wIndex].distans = Cam[i].distans + (Cam[i + 1].distans - Cam[i].distans) *
(BezierCurve(new Vector2(0, 0), new Vector2(Cam[i + 1].Bezier[16], Cam[i + 1].Bezier[18]),
new Vector2(Cam[i + 1].Bezier[17], Cam[i + 1].Bezier[19]), new Vector2(127, 127),
(float)(1.0 * j / (Addframe))).y) / 127;
Cam_m[wIndex].viewAngle = Cam[i].viewAngle + (Cam[i + 1].viewAngle - Cam[i].viewAngle) *
(int)(BezierCurve(new Vector2(0, 0), new Vector2(Cam[i + 1].Bezier[20], Cam[i + 1].Bezier[22]),
new Vector2(Cam[i + 1].Bezier[21], Cam[i + 1].Bezier[23]), new Vector2(127, 127),
(float)(1.0 * j / (Addframe))).y) / 127;
wIndex++;
}
Cam_m[wIndex] = Cam[i + 1];
Cam_m[wIndex++].originalframe = true;
}
success = true;
}

private void Update()
{
//if (Vector3.Distance(MMD_model.transform.position, MainCamera.transform.position) > 6.5f)
//{
// MainCamera.transform.position = Vector3.MoveTowards(MainCamera.transform.position, MainCamera.transform.position + MainCamera.transform.forward *.5f , 10f);
// //MainCamera.transform.LookAt(MMD_model.transform);
// Debug.Log("1");
//}
//else
//{
// Camera.main.transform.localPosition = new Vector3(.05f, -.4f, .58f);
//}
//カメラ情報が完成したらカメラスタート
if (success)
{
if (t != (int)nowframe)
{
//新しいフレームの処理
t = (int)nowframe;
//最終フレームを超えないようにする処理
if (t < Cam_m.Length - 1)
{



CameraCenter.transform.localPosition = new Vector3(Cam_m[t].Pos_x / 12.5f + MMD_model.transform.localPosition.x,
Cam_m[t].Pos_y / 12.5f + MMD_model.transform.localPosition.y,
Cam_m[t].Pos_z / 12.5f + MMD_model.transform.localPosition.z);

//if (Vector3.Distance(MMD_model.transform.position, MainCamera.transform.position) < 6.5f)
// this.transform.localPosition = new Vector3(this.transform.localPosition.x, this.transform.localPosition.y, Cam_m[t].distans / 12.5f);
//else
// this.transform.localPosition = new Vector3(this.transform.localPosition.x, this.transform.localPosition.y, Cam_m[t].distans / 16f);
this.transform.localPosition = (Vector3.Distance(MMD_model.transform.position, new Vector3(this.transform.localPosition.x, this.transform.localPosition.y, Cam_m[t].distans / 12.5f)) < 6f) ? new Vector3(this.transform.localPosition.x, this.transform.localPosition.y, Cam_m[t].distans / 12.5f) : new Vector3(this.transform.localPosition.x, this.transform.localPosition.y, Cam_m[t].distans / 24f);
CameraCenter.transform.rotation = Quaternion.Euler(-Cam_m[t].Rot_x, -Cam_m[t].Rot_y, -Cam_m[t].Rot_z);
MainCamera.fieldOfView = camFieldOfView;//Cam_m[t].viewAngle; sxqn&lianai:60 SingMe:18
}
}
else
{
//同じフレームが再度処理された場合の処理
if (t + 1 < Cam_m.Length - 1 && !Cam_m[t + 1].originalframe)
{
t_f = nowframe - (int)nowframe;


CameraCenter.transform.localPosition = new Vector3(((Cam_m[t + 1].Pos_x - Cam_m[t].Pos_x) * t_f + Cam_m[t].Pos_x) / 12.5f + MMD_model.transform.localPosition.x,
((Cam_m[t + 1].Pos_y - Cam_m[t].Pos_y) * t_f + Cam_m[t].Pos_y) / 12.5f + MMD_model.transform.localPosition.y,
((Cam_m[t + 1].Pos_z - Cam_m[t].Pos_z) * t_f + Cam_m[t].Pos_z) / 12.5f + MMD_model.transform.localPosition.z);
//if (Vector3.Distance(MMD_model.transform.position, MainCamera.transform.position) < 6.5f)
// this.transform.localPosition = new Vector3(this.transform.localPosition.x, this.transform.localPosition.y, Cam_m[t].distans / 12.5f);
//else
// this.transform.localPosition = new Vector3(this.transform.localPosition.x, this.transform.localPosition.y, Cam_m[t].distans / 16f);

this.transform.localPosition = (Vector3.Distance(MMD_model.transform.position, new Vector3(this.transform.localPosition.x, this.transform.localPosition.y, Cam_m[t].distans / 12.5f)) < 6f) ? new Vector3(this.transform.localPosition.x, this.transform.localPosition.y, Cam_m[t].distans / 12.5f) : new Vector3(this.transform.localPosition.x, this.transform.localPosition.y, Cam_m[t].distans / 24f);

CameraCenter.transform.rotation = Quaternion.Euler((-Cam_m[t + 1].Rot_x - -Cam_m[t].Rot_x) * t_f + -Cam_m[t].Rot_x,
(-Cam_m[t + 1].Rot_y - -Cam_m[t].Rot_y) * t_f + -Cam_m[t].Rot_y,
(-Cam_m[t + 1].Rot_z - -Cam_m[t].Rot_z) * t_f + -Cam_m[t].Rot_z);

MainCamera.fieldOfView = camFieldOfView;// (Cam_m[t + 1].viewAngle - Cam_m[t].viewAngle) * t_f + Cam_m[t].viewAngle;
}
}
}
nowframe = getAnimationFrame();
}

private void FixedUpdate()
{

}
private float getAnimationFrame()
{

float returnValue = 0.0f;

var clipInfoList = target_animator.GetCurrentAnimatorClipInfo(0);
if (clipInfoList != null)
{
var clip = clipInfoList[0].clip;
var stateInfo = target_animator.GetCurrentAnimatorStateInfo(0);
returnValue = clip.length * stateInfo.normalizedTime * clip.frameRate;
}

return returnValue;
}

float getVmdCamera(ref int index, byte[] data)
{
Union union = new Union();
byte[] raw_data = new byte[4];

raw_data[0] = data[index++];
raw_data[1] = data[index++];
raw_data[2] = data[index++];
raw_data[3] = data[index++];
union.int0 = System.BitConverter.ToInt32(raw_data, 0);

return (union.float0);
}
// ラジアンから角度を取得
void conversionAngle(ref float rot)
{
rot = (float)(rot * 180 / System.Math.PI);
}
//クイックソート
void Qsort(ref CameraData[] data, int left, int right)
{
int i, j;
int pivot;
CameraData tmp;

i = left; j = right;
pivot = data[(left + right) / 2].frame;
do
{
while ((i < right) && (data[i].frame < pivot)) i++;
while ((j > left) && (pivot < data[j].frame)) j--;
if (i <= j)
{
tmp = data[i];
data[i] = data[j];
data[j] = tmp;
i++; j--;
}
} while (i <= j);
if (left < j) Qsort(ref data, left, j);
if (i < right) Qsort(ref data, i, right);
}

//以降ベジェ曲線に関する関数
float BezierCurveX(float x1, float x2, float x3, float x4, float t)
{
return Mathf.Pow(1 - t, 3) * x1 + 3 * Mathf.Pow(1 - t, 2) * t * x2 + 3 * (1 - t) * Mathf.Pow(t, 2) * x3 + Mathf.Pow(t, 3) * x4;
}
float BezierCurveY(float y1, float y2, float y3, float y4, float t)
{
return Mathf.Pow(1 - t, 3) * y1 + 3 * Mathf.Pow(1 - t, 2) * t * y2 + 3 * (1 - t) * Mathf.Pow(t, 2) * y3 + Mathf.Pow(t, 3) * y4;
}
Vector2 BezierCurve(Vector2 p1, Vector2 p2, Vector2 p3, Vector2 p4, float t)
{
return new Vector2(
BezierCurveX(p1.x, p2.x, p3.x, p4.x, t),
BezierCurveY(p1.y, p2.y, p3.y, p4.y, t));
}

}


  • 附加记录一个类似Scene窗口的相机控制脚本:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CameraController : MonoBehaviour
{
[Tooltip("code auto get")]
[SerializeField]
private Camera mainCamera;
private Transform cameraTrans;
[SerializeField] private Slider CamDisSlider;

private int xAxisCoefficient = 1;
private int yAsixCoefficient = 1;

private string _mouseXString = "Mouse X";
private string _mouseYString = "Mouse Y";
private string _mouseScrollWheel = "Mouse ScrollWheel";
private Vector3 _currentMouseRotate = Vector3.zero;

public float fieldOfViewMin = 20.0f;
public float fieldOfviewMax = 100.0f;
public float moveSpeed = 3;
public float sensitivityDrag = 1;
public float sensitivityRotate = 3;
public float sensitivetyMouseWheel = 30;

[SerializeField] float moveSpeedWhenStop = .2f;

#region 面板修改该选项后要重新启动
public bool xAxisinversion = false;
public bool yAsixinversion = false;
#endregion

private void Start()
{
mainCamera = gameObject.GetComponent<Camera>();
if (mainCamera == null)
{
Debug.LogErrorFormat("摄像机为空!");
return;
}
cameraTrans = mainCamera.transform;

xAxisCoefficient = xAxisinversion ? -1 : 1;
yAsixCoefficient = yAsixinversion ? -1 : 1;
}

private void LateUpdate()
{
if (cameraTrans == null) return;

KeyBoardControl();

MouseLeftDragControl();

MouseRightRotateControl();

MouseScrollwheelScale();
}

/// <summary>
/// 键盘移动
/// </summary>
private void KeyBoardControl()
{
if (Input.GetKey(KeyCode.W))
{
if (Time.timeScale != 0f)
cameraTrans.Translate(Vector3.forward * Time.deltaTime * moveSpeed);
else
{
cameraTrans.Translate(Vector3.forward * moveSpeedWhenStop * moveSpeed /4);
}
}

if (Input.GetKey(KeyCode.S))
{
if (Time.timeScale != 0f)
cameraTrans.Translate(Vector3.back * Time.deltaTime * moveSpeed);
else
{
cameraTrans.Translate(Vector3.back * moveSpeedWhenStop * moveSpeed / 4);
}
}

if (Input.GetKey(KeyCode.A))
{
if (Time.timeScale != 0f)
cameraTrans.Translate(Vector3.left * Time.deltaTime * moveSpeed);
else
{
cameraTrans.Translate(Vector3.left * moveSpeedWhenStop * moveSpeed / 4);
}
}

if (Input.GetKey(KeyCode.D))
{
if (Time.timeScale != 0f)
cameraTrans.Translate(Vector3.right * Time.deltaTime * moveSpeed);
else
{
cameraTrans.Translate(Vector3.right * moveSpeedWhenStop * moveSpeed / 4);
}
}
}

/// <summary>
/// 鼠标左键拖拽摄像机
/// </summary>
private void MouseLeftDragControl()
{
if (Input.GetMouseButton(0))
{
if(Time.timeScale!=0f)
{
Vector3 p1 = cameraTrans.position - cameraTrans.right * Input.GetAxisRaw(_mouseXString) * sensitivityDrag * Time.timeScale;
Vector3 p2 = p1 - cameraTrans.up * Input.GetAxisRaw(_mouseYString) * sensitivityDrag * Time.timeScale;
cameraTrans.position = p2;
}
else
{
Vector3 p1 = cameraTrans.position - cameraTrans.right * Input.GetAxisRaw(_mouseXString) * sensitivityDrag * moveSpeedWhenStop;
Vector3 p2 = p1 - cameraTrans.up * Input.GetAxisRaw(_mouseYString) * sensitivityDrag * moveSpeedWhenStop;
cameraTrans.position = p2;
}

}
}

/// <summary>
/// 鼠标右键旋转
/// </summary>
private void MouseRightRotateControl()
{
if (Input.GetMouseButton(1))
{
_currentMouseRotate.x = -(Input.GetAxis(_mouseYString) * sensitivityRotate) * xAxisCoefficient;
_currentMouseRotate.y = -(Input.GetAxis(_mouseXString) * sensitivityRotate) * yAsixCoefficient;

cameraTrans.rotation = Quaternion.Euler(cameraTrans.eulerAngles + _currentMouseRotate);
}
}

/// <summary>
/// 鼠标滚轮缩放
/// </summary>
private void MouseScrollwheelScale()
{
if (Input.GetAxis(_mouseScrollWheel) == 0) return;

mainCamera.fieldOfView = mainCamera.fieldOfView - Input.GetAxis(_mouseScrollWheel) * sensitivetyMouseWheel;
mainCamera.fieldOfView = Mathf.Clamp(mainCamera.fieldOfView, fieldOfViewMin, fieldOfviewMax);
CamDisSlider.value = mainCamera.fieldOfView;
}
}

  • 项目主要控制器脚本:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class UIScript : MonoBehaviour
{
[Header("模型")]
[SerializeField] private GameObject playerEl;
[SerializeField] private GameObject playerSp;
[SerializeField] private GameObject playerRust;

[Header("正在播放")]
[SerializeField] private TextMeshProUGUI tmpText;

[Header("环境光")]
[SerializeField] private Slider _LightSlider;
[SerializeField] private Light _DirectionalLight;
[SerializeField] private Light centerPointLight;

[Header("衣服颜色 For Elinyaa")]
[SerializeField] private Material Cloth_Material;
[SerializeField] private Texture texGold;
[SerializeField] private Texture texPurple;
[SerializeField] private Texture texBlue;
[SerializeField] private Texture texPink;

[Header("衣服 For Elinyaa")]
[SerializeField] private SkinnedMeshRenderer eyeHeart;
[SerializeField] private GameObject cloth;
[SerializeField] private GameObject clothCollar;
[SerializeField] private GameObject Clothpants;
[SerializeField] private GameObject clothSleeve;
[SerializeField] private GameObject ClothUnder;
[SerializeField] private GameObject Boots;
[SerializeField] private GameObject hhat;
[SerializeField] private GameObject necklace;
[SerializeField] private GameObject Hairpin;
[SerializeField] private GameObject socks;
[Header("衣服 For Sapphy")]
[SerializeField] private SkinnedMeshRenderer eyeHeart_sp;
[SerializeField] private GameObject cloth_sp;
[SerializeField] private GameObject clothCollar_sp;
[SerializeField] private GameObject Clothpants_sp;
[SerializeField] private GameObject clothSleeve_sp;
[SerializeField] private GameObject ClothUnder_sp;
[SerializeField] private GameObject Boots_sp;
[SerializeField] private GameObject hhat_sp;
[SerializeField] private GameObject socks_sp;
[Header("衣服 For Rust")]
[SerializeField] private SkinnedMeshRenderer eyeHeart_Rust;
[SerializeField] private GameObject cloth_Rust;
[SerializeField] private GameObject socks_Rust;
[SerializeField] private GameObject boots_Rust;


[Header("相机距离")]
[SerializeField] private MMD_VmdCameraLoad mmdCameraDistance;
[SerializeField] private MMD_VmdCameraLoad_Sap mmdCameraDistanceSap;
[SerializeField] private MMD_VmdCameraLoad_Rust mmdCameraDistanceRust;
[SerializeField] private Slider _CamDistanceSlider;
[SerializeField] private Slider _CamUpDownSlider;
[SerializeField] private Transform CamUpDownObj;

[Header("镜头动作开关")]
[SerializeField] private CameraController CamType2;

[Header("场景切换开关")]
[SerializeField] private List<GameObject> Stages;
//[SerializeField] private GameObject Stage_1_Sakura;
//[SerializeField] private GameObject Stage_2_yyl;
//[SerializeField] private GameObject Stage_3_candy;


[Header("MMD选择")]
[SerializeField] private AudioSource _audiosource;
[SerializeField] private Animator anim;
[SerializeField] private Animator animSp;
[SerializeField] private Animator animRust;
[SerializeField] private Transform vmdCam;
[SerializeField] MMD_VmdCameraLoad MMD_VMD;
[SerializeField] MMD_VmdCameraLoad_Sap MMD_VMD_Sap;
[SerializeField] MMD_VmdCameraLoad_Rust MMD_VMD_Rust;
// MMD
[SerializeField] private AudioClip sound_koi; // 恋爱循环
[SerializeField] private TextAsset VMD_koi;
[SerializeField] private AudioClip sound_SingMeToSleep; // Sing Me To Sleep
[SerializeField] private TextAsset VMD_SingMeToSleep;
[SerializeField] private AudioClip sound_Gwiyomi; // Gwiyomi
[SerializeField] private TextAsset VMD_Gwiyomi;
[SerializeField] private AudioClip sound_HappyHalloween; // Happy Halloween
[SerializeField] private TextAsset VMD_HappyHalloween;
[SerializeField] private AudioClip sound_ldsd; // 劣等上等
[SerializeField] private TextAsset VMD_ldsd;
[SerializeField] private AudioClip sound_jljt; // 极乐净土
[SerializeField] private TextAsset VMD_jljt;
[SerializeField] private AudioClip sound_PinkCat; // PinkCat
[SerializeField] private TextAsset VMD_PinkCat;
[SerializeField] private AudioClip sound_ELECT; // ELECT
[SerializeField] private TextAsset VMD_ELECT;
[SerializeField] private AudioClip sound_DreamingChuchu; // Dreaming Chuchu
[SerializeField] private TextAsset VMD_DreamingChuchu;
[SerializeField] private AudioClip sound_Necomimi; // 猫耳开关
[SerializeField] private TextAsset VMD_Necomimi;
[SerializeField] private AudioClip sound_Yumeyume; // Yumeyume
[SerializeField] private TextAsset VMD_Yumeyume;
[SerializeField] private AudioClip sound_Linngo; // Linngo
[SerializeField] private TextAsset VMD_Linngo;
[SerializeField] private AudioClip sound_lkyb; // 恋空予报
[SerializeField] private TextAsset VMD_lkyb;
[SerializeField] private AudioClip sound_badbadWater; // 我的悲伤是水做的
[SerializeField] private TextAsset VMD_badbadWater;
[SerializeField] private AudioClip sound_ツギハギスタッカート; // ツギハギスタッカート
[SerializeField] private TextAsset VMD_ツギハギスタッカート;

// R18MMD
[SerializeField] private AudioClip sound_Conqueror; // Conqueror
[SerializeField] private TextAsset VMD_Conqueror;
[SerializeField] private AudioClip sound_PacmanGhosts; // Pac-manGhosts
[SerializeField] private TextAsset VMD_PacmanGhosts;
[SerializeField] private AudioClip sound_FullBodied; // FullBodied
[SerializeField] private TextAsset VMD_FullBodied;
[SerializeField] private AudioClip sound_Applepie; // Apple pie
[SerializeField] private TextAsset VMD_Applepie;
[SerializeField] private AudioClip sound_BubbleButt; // BubbleButt
[SerializeField] private TextAsset VMD_BubbleButt;
[SerializeField] private AudioClip sound_SwingDance; // SwingDance
[SerializeField] private TextAsset VMD_SwingDance;
[SerializeField] private AudioClip sound_GoodNightKiss; // GoodNightKiss
[SerializeField] private TextAsset VMD_GoodNightKiss;
[SerializeField] private AudioClip sound_DojaCat; // Doja Cat
[SerializeField] private TextAsset VMD_DojaCat;
[SerializeField] private AudioClip sound_DeepBlueTown; // DeepBlueTown
[SerializeField] private TextAsset VMD_DeepBlueTown;
[SerializeField] private AudioClip sound_Mister; // Mister
[SerializeField] private TextAsset VMD_Mister;
[SerializeField] private AudioClip sound_Lupin; // Lupin
[SerializeField] private TextAsset VMD_Lupin;
[SerializeField] private AudioClip sound_HoldDance; // Hold Dance
[SerializeField] private TextAsset VMD_HoldDance;
[SerializeField] private AudioClip sound_Samsara; // Samsara
[SerializeField] private TextAsset VMD_Samsara;
[SerializeField] private AudioClip sound_Fireball; // Fireball
[SerializeField] private TextAsset VMD_Fireball;
[SerializeField] private AudioClip sound_BaamFull; // Baam full
[SerializeField] private TextAsset VMD_BaamFull;
[SerializeField] private AudioClip sound_DestinationCalabria; // Destination Calabria
[SerializeField] private TextAsset VMD_DestinationCalabria;
[SerializeField] private AudioClip sound_Lamb; // Lamb
[SerializeField] private TextAsset VMD_Lamb;
[SerializeField] private AudioClip sound_ExcuseMe; // ExcuseMe
[SerializeField] private TextAsset VMD_ExcuseMe;
[SerializeField] private AudioClip sound_KillerB; // KillerB
[SerializeField] private TextAsset VMD_KillerB;
[SerializeField] private AudioClip sound_PushIt; // PushIt
[SerializeField] private TextAsset VMD_PushIt;


[SerializeField] private Canvas _canvas;

private void Awake()
{
Application.targetFrameRate = 60;
// 暂停状态初始化 录制时注释
Time.timeScale = 1f;
UIShow();
}
private void Start()
{
// 光照值滑块初始化
_LightSlider.value = _DirectionalLight.intensity;

//Camera.main.GetComponent<AudioSource>().Stop();
// 相机距离滑块初始化
_CamDistanceSlider.value = mmdCameraDistance.camFieldOfView;
_CamUpDownSlider.value = CamUpDownObj.localPosition.y;
// NowPlaying初始化
tmpText.text = "Now:" + anim.runtimeAnimatorController.name;
// 开始时播放的是哪个mmd
MMD_9_DreamingChuchu();

//_audiosource.Play();
//ChangeModel();
// 衣服 录制时开启
//CloPre3_All_ForMovie();
// 录制时若有延迟开启---------------
Invoke("Playmusic", 1.2f);
//Playmusic();

}
private void Update()
{
// 按空格键控制UI显隐
if (Input.GetKeyDown(KeyCode.Space))
{
UIShow();
}
}

public void UIShow()
{
if (_canvas.gameObject.activeSelf)
{
_canvas.gameObject.SetActive(false);
}
else
{
_canvas.gameObject.SetActive(true);
}
}
private void Playmusic()
{
_audiosource.Play();
}
// 环境光照强度
public void setLight()
{
_DirectionalLight.intensity = _LightSlider.value;
}
public void setPointLight()
{
if (centerPointLight.enabled)
{
centerPointLight.enabled = false;
}
else if (!centerPointLight.enabled)
{
centerPointLight.enabled = true;
}
}
// 衣服颜色
public void ColorGold()
{
Cloth_Material.SetTexture("_MainTex", texGold);
}
public void ColorPurple()
{
Cloth_Material.SetTexture("_MainTex", texPurple);
}
public void ColorBlue()
{
Cloth_Material.SetTexture("_MainTex", texBlue);
}
public void ColorPink()
{
Cloth_Material.SetTexture("_MainTex", texPink);
}
// 播放/暂停
public void StartOrStop()
{
if (Time.timeScale == 0)
{
Time.timeScale = 1f;
_audiosource.Play();
}

else if (Time.timeScale == 1f)
{
Time.timeScale = 0f;
_audiosource.Pause();
}

}
// 衣服
public void EyeHeart()
{
if (playerEl.activeSelf)
{
if (eyeHeart.GetBlendShapeWeight(62) == 0)
eyeHeart.SetBlendShapeWeight(62, 100);
else if (eyeHeart.GetBlendShapeWeight(62) == 100)
eyeHeart.SetBlendShapeWeight(62, 0);
}
else if(playerSp.activeSelf)
{
if (eyeHeart_sp.GetBlendShapeWeight(67) == 0)
eyeHeart_sp.SetBlendShapeWeight(67, 100);
else if (eyeHeart_sp.GetBlendShapeWeight(67) == 100)
eyeHeart_sp.SetBlendShapeWeight(67, 0);
}
else if (playerRust.activeSelf)
{
if (eyeHeart_Rust.GetBlendShapeWeight(173) == 0)
eyeHeart_Rust.SetBlendShapeWeight(173, 100);
else if (eyeHeart_Rust.GetBlendShapeWeight(173) == 100)
eyeHeart_Rust.SetBlendShapeWeight(173, 0);

//for (int i = 0; i < 249; i++)
//{
// if (eyeHeart_Rust.GetBlendShapeWeight(i) == 100)
// Debug.Log(i);

//}
}
}
public void Clothes_cloth()
{
if (playerEl.gameObject.activeSelf)
{
if (cloth.activeSelf)
cloth.SetActive(false);
else if (!cloth.activeSelf)
cloth.SetActive(true);
}
else if (playerSp.gameObject.activeSelf)
{
if (cloth_sp.activeSelf)
cloth_sp.SetActive(false);
else if (!cloth_sp.activeSelf)
cloth_sp.SetActive(true);
}
else if (playerRust.gameObject.activeSelf)
{
if (cloth_Rust.activeSelf)
cloth_Rust.SetActive(false);
else if (!cloth_Rust.activeSelf)
cloth_Rust.SetActive(true);
}
}
public void Clothes_hat()
{
if (playerEl.gameObject.activeSelf)
{
if (hhat.activeSelf)
hhat.SetActive(false);
else if (!hhat.activeSelf)
hhat.SetActive(true);
}
else if (playerSp.gameObject.activeSelf)
{
if (hhat_sp.activeSelf)
hhat_sp.SetActive(false);
else if (!hhat_sp.activeSelf)
hhat_sp.SetActive(true);
}
else if (playerRust.gameObject.activeSelf)
{
return;
}
}
public void Clothes_sleeve()
{
if (playerEl.gameObject.activeSelf)
{
if (clothSleeve.activeSelf)
clothSleeve.SetActive(false);
else if (!clothSleeve.activeSelf)
clothSleeve.SetActive(true);
}
else if (playerSp.gameObject.activeSelf)
{
if (clothSleeve_sp.activeSelf)
clothSleeve_sp.SetActive(false);
else if (!clothSleeve_sp.activeSelf)
clothSleeve_sp.SetActive(true);
}
else if (playerRust.gameObject.activeSelf)
{
return;
}
}
public void Clothes_socks()
{
if (playerEl.gameObject.activeSelf)
{
if (socks.activeSelf)
socks.SetActive(false);
else if (!socks.activeSelf)
socks.SetActive(true);
}
else if (playerSp.gameObject.activeSelf)
{
if (socks_sp.activeSelf)
socks_sp.SetActive(false);
else if (!socks_sp.activeSelf)
socks_sp.SetActive(true);
}
else if (playerRust.gameObject.activeSelf)
{
if (socks_Rust.activeSelf)
socks_Rust.SetActive(false);
else if (!socks_Rust.activeSelf)
socks_Rust.SetActive(true);
}
}
public void Clothes_clothpants()
{
if (playerEl.gameObject.activeSelf)
{
if (Clothpants.activeSelf)
Clothpants.SetActive(false);
else if (!Clothpants.activeSelf)
Clothpants.SetActive(true);
}
else if (playerSp.gameObject.activeSelf)
{
if (Clothpants_sp.activeSelf)
Clothpants_sp.SetActive(false);
else if (!Clothpants_sp.activeSelf)
Clothpants_sp.SetActive(true);
}
else if (playerRust.gameObject.activeSelf)
{
return;
}
}
public void Clothes_boots()
{
if (playerEl.gameObject.activeSelf)
{
if (Boots.activeSelf)
Boots.SetActive(false);
else if (!Boots.activeSelf)
Boots.SetActive(true);
}
else if (playerSp.gameObject.activeSelf)
{
if (Boots_sp.activeSelf)
Boots_sp.SetActive(false);
else if (!Boots_sp.activeSelf)
Boots_sp.SetActive(true);
}
else if (playerRust.gameObject.activeSelf)
{
if (boots_Rust.activeSelf)
boots_Rust.SetActive(false);
else if (!boots_Rust.activeSelf)
boots_Rust.SetActive(true);
}
}
public void Clothes_under()
{
if (playerEl.gameObject.activeSelf)
{
if (ClothUnder.activeSelf)
ClothUnder.SetActive(false);
else if (!ClothUnder.activeSelf)
ClothUnder.SetActive(true);
}
else if (playerSp.gameObject.activeSelf)
{
if (ClothUnder_sp.activeSelf)
ClothUnder_sp.SetActive(false);
else if (!ClothUnder_sp.activeSelf)
ClothUnder_sp.SetActive(true);
}
else if (playerRust.gameObject.activeSelf)
{
return;
}
}
public void Clothes_collar()
{
if (playerEl.gameObject.activeSelf)
{
if (clothCollar.activeSelf)
clothCollar.SetActive(false);
else if (!clothCollar.activeSelf)
clothCollar.SetActive(true);
}
else if (playerSp.gameObject.activeSelf)
{
if (clothCollar_sp.activeSelf)
clothCollar_sp.SetActive(false);
else if (!clothCollar_sp.activeSelf)
clothCollar_sp.SetActive(true);
}
else if (playerRust.gameObject.activeSelf)
{
return;
}
}
public void CloPre1()
{
if (playerEl.activeSelf)
{
cloth.SetActive(true);
clothCollar.SetActive(true);
Clothpants.SetActive(true);
clothSleeve.SetActive(true);
ClothUnder.SetActive(true);
Boots.SetActive(true);
hhat.SetActive(true);
necklace.SetActive(true);
Hairpin.SetActive(true);
socks.SetActive(true);
}
else if (playerSp.activeSelf)
{
cloth_sp.SetActive(true);
clothCollar_sp.SetActive(true);
Clothpants_sp.SetActive(true);
clothSleeve_sp.SetActive(true);
ClothUnder_sp.SetActive(true);
Boots_sp.SetActive(true);
hhat_sp.SetActive(true);
socks_sp.SetActive(true);
}
else if (playerRust.gameObject.activeSelf)
{
cloth_Rust.SetActive(true);
socks_Rust.SetActive(true);
boots_Rust.SetActive(true);
}
}
public void CloPre2()
{
if (playerEl.activeSelf)
{
cloth.SetActive(false);
clothCollar.SetActive(false);
Clothpants.SetActive(false);
clothSleeve.SetActive(false);
ClothUnder.SetActive(true);
Boots.SetActive(false);
hhat.SetActive(false);
necklace.SetActive(false);
Hairpin.SetActive(false);
socks.SetActive(false);
}
else if (playerSp.activeSelf)
{
cloth_sp.SetActive(false);
clothCollar_sp.SetActive(false);
Clothpants_sp.SetActive(false);
clothSleeve_sp.SetActive(false);
ClothUnder_sp.SetActive(true);
Boots_sp.SetActive(false);
hhat_sp.SetActive(true);
socks_sp.SetActive(false);
}
else if (playerRust.gameObject.activeSelf)
{
cloth_Rust.SetActive(false);
socks_Rust.SetActive(false);
boots_Rust.SetActive(false);
}
}

private void CloPre3_All_ForMovie()
{
if (playerEl.activeSelf)
{
cloth.SetActive(false);
clothCollar.SetActive(false);
Clothpants.SetActive(false);
clothSleeve.SetActive(false);
ClothUnder.SetActive(false);
Boots.SetActive(false);
hhat.SetActive(false);
necklace.SetActive(false);
Hairpin.SetActive(false);
socks.SetActive(false);
}
else if (playerSp.activeSelf)
{
cloth_sp.SetActive(false);
clothCollar_sp.SetActive(false);
Clothpants_sp.SetActive(false);
clothSleeve_sp.SetActive(false);
ClothUnder_sp.SetActive(false);
Boots_sp.SetActive(false);
hhat_sp.SetActive(true);
socks_sp.SetActive(false);
}
else if (playerRust.gameObject.activeSelf)
{
cloth_Rust.SetActive(false);
socks_Rust.SetActive(false);
boots_Rust.SetActive(false);
}
}
// 相机距离
public void SetCamDistance()
{
if(mmdCameraDistance.enabled)
{
mmdCameraDistance.camFieldOfView = _CamDistanceSlider.value;
}
else if(mmdCameraDistanceSap.enabled)
{
mmdCameraDistanceSap.camFieldOfView = _CamDistanceSlider.value;
}
else if(mmdCameraDistanceRust.enabled)
{
mmdCameraDistanceRust.camFieldOfView = _CamDistanceSlider.value;
}

}
public void SetCamUpDown()
{
CamUpDownObj.localPosition = new Vector3(CamUpDownObj.localPosition.x, _CamUpDownSlider.value, CamUpDownObj.localPosition.z);
}
// 镜头动作开关
public void CamChoose()
{
if (!CamType2.enabled)
{
if(playerEl.activeSelf)
mmdCameraDistance.enabled = false;
else if(playerSp.activeSelf)
mmdCameraDistanceSap.enabled = false;
else if (playerRust.activeSelf)
mmdCameraDistanceRust.enabled = false;

CamType2.enabled = true;
}
else if (CamType2.enabled)
{
if(playerEl.activeSelf)
mmdCameraDistance.enabled= true;
else if(playerSp.activeSelf)
mmdCameraDistanceSap.enabled = true;
else if (playerRust.activeSelf)
mmdCameraDistanceRust.enabled = true;
CamType2.enabled = false;
}
}
// 场景切换
public void StageChoose()
{
//if (Stage_1_Sakura.activeSelf)
//{
// Stage_2_yyl.SetActive(true);
// Stage_1_Sakura.SetActive(false);
//}
//else if(Stage_2_yyl.activeSelf)
//{
// Stage_3_candy.SetActive(true);
// Stage_2_yyl.SetActive(false);
//}
//else if (Stage_3_candy.activeSelf)
//{
// Stage_1_Sakura.SetActive(true);
// Stage_3_candy.SetActive(false);
//}

for (int i = 0; i < Stages.Count; i++)
{
if (Stages[i].activeSelf)
{
Stages[(i == Stages.Count - 1) ? 0 : i + 1].SetActive(true);
Stages[i].SetActive(false);
return;
}
}
}
// 模型切换
public void ChangeModel()
{
if (playerEl.activeSelf)
{
MMD_VMD.enabled = false;

playerSp.SetActive(true);
playerSp.transform.localPosition = new Vector3(0, 0, 0);
playerSp.transform.localRotation = Quaternion.Euler(0f, 180f, 0f);
animSp.runtimeAnimatorController = null;
RuntimeAnimatorController animator = (RuntimeAnimatorController)Resources.Load(anim.runtimeAnimatorController.name);
animSp.runtimeAnimatorController = (RuntimeAnimatorController)animator;

playerEl.SetActive(false);
//playerRust.SetActive(false);

MMD_VMD_Sap.enabled = true;

if(Time.timeScale == 1f)
{
_audiosource.Stop();
_audiosource.Play();
}
else if(Time.timeScale == 0f)
{
_audiosource.Stop();
}
mmdChange(animSp.runtimeAnimatorController.name, _audiosource.clip, MMD_VMD.Select_VMD);
}
else if (playerSp.activeSelf)
{
MMD_VMD_Sap.enabled = false;

playerRust.SetActive(true);
playerRust.transform.localPosition = new Vector3(0, 0, 0);
playerRust.transform.localRotation = Quaternion.Euler(0f, 180f, 0f);
animRust.runtimeAnimatorController = null;
RuntimeAnimatorController animator = (RuntimeAnimatorController)Resources.Load(animSp.runtimeAnimatorController.name);
animRust.runtimeAnimatorController = (RuntimeAnimatorController)animator;
playerSp.SetActive(false);
//playerRust.SetActive(false);

MMD_VMD_Rust.enabled = true;
if (Time.timeScale == 1f)
{
_audiosource.Stop();
_audiosource.Play();
}
else if (Time.timeScale == 0f)
{
_audiosource.Stop();
}
mmdChange(animRust.runtimeAnimatorController.name, _audiosource.clip, MMD_VMD_Sap.Select_VMD);
}
else if (playerRust.activeSelf)
{
MMD_VMD_Rust.enabled = false;

playerEl.SetActive(true);
playerEl.transform.localPosition = new Vector3(0, 0, 0);
playerEl.transform.localRotation = Quaternion.Euler(0f, 180f, 0f);
anim.runtimeAnimatorController = null;
RuntimeAnimatorController animator = (RuntimeAnimatorController)Resources.Load(animRust.runtimeAnimatorController.name);
anim.runtimeAnimatorController = (RuntimeAnimatorController)animator;
//playerSp.SetActive(false);
playerRust.SetActive(false);

MMD_VMD.enabled = true;
if (Time.timeScale == 1f)
{
_audiosource.Stop();
_audiosource.Play();
}
else if (Time.timeScale == 0f)
{
_audiosource.Stop();
}
mmdChange(anim.runtimeAnimatorController.name, _audiosource.clip, MMD_VMD_Rust.Select_VMD);
}
}
// MMD选择 只需传递Resources路径、音频片段、vmd文件
public void MMD_1_koi()
{
mmdChange("恋爱循环", sound_koi,VMD_koi);
}
public void MMD_2_SingMeToSleep()
{
mmdChange("SingMeToSleep", sound_SingMeToSleep,VMD_SingMeToSleep);
}
public void MMD_3_Gwiyomi()
{
mmdChange("Gwiyomi", sound_Gwiyomi, VMD_Gwiyomi);
}
public void MMD_4_HappyHalloween()
{
mmdChange("HappyHalloween", sound_HappyHalloween, VMD_HappyHalloween);
}
public void MMD_5_ldsd()
{
mmdChange("劣等上等", sound_ldsd, VMD_ldsd);
}
public void MMD_6_jljt()
{
mmdChange("极乐净土", sound_jljt, VMD_jljt);
}
public void MMD_7_PinkCat()
{
mmdChange("PinkCat", sound_PinkCat, VMD_PinkCat);
}
public void MMD_8_ELECT()
{
mmdChange("Elect", sound_ELECT, VMD_ELECT);
}
public void MMD_9_DreamingChuchu()
{
mmdChange("DreamingChuchu", sound_DreamingChuchu, VMD_DreamingChuchu);
}
public void MMD_10_Necomimi()
{
mmdChange("猫耳开关", sound_Necomimi, VMD_Necomimi);
}
public void MMD_11_Yumeyume()
{
mmdChange("Yumeyume", sound_Yumeyume, VMD_Yumeyume);
}
public void MMD_12_Linngo()
{
mmdChange("林檎花火とソーダの海", sound_Linngo, VMD_Linngo);
}
public void MMD_13_lkyb()
{
mmdChange("恋空予报", sound_lkyb, VMD_lkyb);
}
public void MMD_14_badbadWater()
{
mmdChange("我的悲伤是水做的", sound_badbadWater, VMD_badbadWater);
}
public void MMD_15_ツギハギスタッカート()
{
mmdChange("ツギハギスタッカート", sound_ツギハギスタッカート, VMD_ツギハギスタッカート);
}



// R18MMD
public void R18MMD_1_Conqueror()
{
mmdChange("Conqueror", sound_Conqueror, VMD_Conqueror);
}
public void R18MMD_2_PacmanGhosts()
{
mmdChange("PacmanGhosts", sound_PacmanGhosts, VMD_PacmanGhosts);
}
public void R18MMD_3_FullBodied()
{
mmdChange("FullBodied", sound_FullBodied, VMD_FullBodied);
}
public void R18MMD_4_Applepie()
{
mmdChange("Applepie", sound_Applepie, VMD_Applepie);
}
public void R18MMD_5_BubbleButt()
{
mmdChange("BubbleButt", sound_BubbleButt, VMD_BubbleButt);
}
public void R18MMD_6_SwingDance()
{
mmdChange("SwingDance", sound_SwingDance, VMD_SwingDance);
}
public void R18MMD_7_GoodNightKiss()
{
mmdChange("GoodNightKiss", sound_GoodNightKiss, VMD_GoodNightKiss);
}
public void R18MMD_8_DojaCat()
{
mmdChange("DojaCat", sound_DojaCat, VMD_DojaCat);
}
public void R18MMD_9_DeepBlueTown()
{
mmdChange("DeepBlueTown", sound_DeepBlueTown, VMD_DeepBlueTown);
}
public void R18MMD_10_Mister()
{
mmdChange("Mister", sound_Mister, VMD_Mister);
}
public void R18MMD_11_Lupin()
{
mmdChange("Lupin", sound_Lupin, VMD_Lupin);
}
public void R18MMD_12_HoldDance()
{
mmdChange("HoldDance", sound_HoldDance, VMD_HoldDance);
}
public void R18MMD_13_Samsara()
{
mmdChange("Samsara", sound_Samsara, VMD_Samsara);
}
public void R18MMD_14_Fireball()
{
mmdChange("Fireball", sound_Fireball, VMD_Fireball);
}
public void R18MMD_15_BaamFull()
{
mmdChange("BaamFull", sound_BaamFull, VMD_BaamFull);
}
public void R18MMD_16_DestinationCalabria()
{
mmdChange("DestinationCalabria", sound_DestinationCalabria, VMD_DestinationCalabria);
}
public void R18MMD_17_Lamb()
{
mmdChange("Lamb", sound_Lamb, VMD_Lamb);
}
public void R18MMD_18_ExcuseMe()
{
mmdChange("ExcuseMe", sound_ExcuseMe, VMD_ExcuseMe);
}
public void R18MMD_19_KillerB()
{
mmdChange("KillerB", sound_KillerB, VMD_KillerB);
}
public void R18MMD_20_PushIt()
{
mmdChange("PushIt", sound_PushIt, VMD_PushIt);
}

// Motions
public void Motions_1_motion1()
{
motionChange("motion1");
if (playerEl.activeSelf)
mmdCameraDistance.enabled = false;
else if (playerSp.activeSelf)
mmdCameraDistanceSap.enabled = false;
else if (playerRust.activeSelf)
mmdCameraDistanceRust.enabled = false;

CamType2.enabled = true;

Camera.main.transform.localPosition = new Vector3(0, .38f, -.64f);
Quaternion rot = Quaternion.Euler(11.51f, 0f, 0f);
Camera.main.transform.localRotation = rot;
}

private void mmdChange(string strPath, AudioClip a, TextAsset vmdtext)
{
// 如果MMD_VMD_Rust的distanceOffset改变了,能被检测到,执行两次mmdChange()
float tempDistance = MMD_VMD_Rust.distanceOffset;
if (playerEl.activeSelf)
{
for(int i = 0;i<86;i++)
{
if (i == 62) continue;
eyeHeart.SetBlendShapeWeight(i, 0);
}
tmpText.text = "Now:" + strPath;
playerEl.transform.localPosition = new Vector3(0, 0, 0);
playerEl.transform.localRotation = Quaternion.Euler(0f, 180f, 0f);
if (strPath == "HoldDance")
{
playerEl.transform.localPosition = new Vector3(0, 0.3f, 0);
}
anim.runtimeAnimatorController = null;
RuntimeAnimatorController animator = (RuntimeAnimatorController)Resources.Load(strPath);
anim.runtimeAnimatorController = (RuntimeAnimatorController)animator;
_audiosource.Stop();
_audiosource.clip = a;
if (Time.timeScale == 1f)
{
_audiosource.Play();
}
MMD_VMD.Select_VMD = vmdtext;
MMD_VMD.Start();
if (CamType2.enabled) return;
vmdCam.localPosition = new Vector3(0, 0, 0);
vmdCam.GetChild(0).localPosition = new Vector3(0, 0, 0);
Quaternion rotation = Quaternion.Euler(0f, 0f, 0f);
vmdCam.localRotation = rotation;
vmdCam.GetChild(0).localRotation = rotation;
Camera.main.transform.localPosition = new Vector3(0f, 0f, 0f);
Camera.main.transform.localRotation = rotation;
if (strPath == "Applepie")
{
Camera.main.transform.localPosition = new Vector3(.14f, 0, 5.44f);
Quaternion rot = Quaternion.Euler(0f, 180f, 0f);
Camera.main.transform.localRotation = rot;
}
else if (strPath == "Conqueror")
{
Camera.main.transform.localPosition = new Vector3(0f, -0.3076369f, 0f);
}
else if (strPath == "Gwiyomi")
{
Camera.main.transform.localPosition = new Vector3(0f, .6f, -1.24f);
}
else if (strPath == "DeepBlueTown")
{
Camera.main.transform.localPosition = new Vector3(0f, -.54f, 0f);
}
else if (strPath == "Mister")
{
Camera.main.transform.localPosition = new Vector3(0f, -.38f, 0f);
}
else if (strPath == "Lupin")
{
Camera.main.transform.localPosition = new Vector3(0f, -.1f, 0f);
}
else if (strPath == "SingMeToSleep")
{
Camera.main.transform.localPosition = new Vector3(0f, -.3f, 0f);
}
else if (strPath == "Samsara")
{
Camera.main.transform.localPosition = new Vector3(0f, -.2f, 0f);
}
else if (strPath == "Fireball")
{
Camera.main.transform.localPosition = new Vector3(0f, -.05f, 0f);
}
else if (strPath == "Elect")
{
Camera.main.transform.localPosition = new Vector3(0f, -.25f, 7.35f);
Quaternion rot = Quaternion.Euler(0f, 180f, 0f);
Camera.main.transform.localRotation = rot;
}
else if (strPath == "恋空予报")
{
Camera.main.transform.localPosition = new Vector3(0f, -.2f, 0f);
}
else if(strPath == "BaamFull")
{
Camera.main.transform.localPosition = new Vector3(0f, -.2f, 0f);
}
else if(strPath == "DestinationCalabria")
{
Camera.main.transform.localPosition = new Vector3(0f, -.22f, 0f);
}
else if (strPath == "Lamb")
{
Camera.main.transform.localPosition = new Vector3(0f, -.22f, 6.8f);
Quaternion rot = Quaternion.Euler(0f, 180f, 0f);
Camera.main.transform.localRotation = rot;
}
else if (strPath == "ExcuseMe")
{
Camera.main.transform.localPosition = new Vector3(0f, -.15f, 0f);
}
else if (strPath == "KillerB")
{
Camera.main.transform.localPosition = new Vector3(0f, -.2f, 4f);
Quaternion rot = Quaternion.Euler(0f, 180f, 0f);
Camera.main.transform.localRotation = rot;
}
else if (strPath == "我的悲伤是水做的")
{
Camera.main.transform.localPosition = new Vector3(0f, -.15f, .81f);
}
else if (strPath == "ツギハギスタッカート")
{
Camera.main.transform.localPosition = new Vector3(.05f, -.4f, .58f);
Invoke("ChangeCam", 8f);
}
else if (strPath == "DreamingChuchu")
{
Camera.main.transform.localPosition = new Vector3(0f, -.15f, 0.2f);
Invoke("ChangeCam", 9f);
}
}
else if(playerSp.activeSelf)
{
for (int i = 0; i < 76; i++)
{
if (i == 67) continue;
eyeHeart_sp.SetBlendShapeWeight(i, 0);
}
tmpText.text = "Now:" + strPath;
playerSp.transform.localPosition = new Vector3(0, 0, 0);
playerSp.transform.localRotation = Quaternion.Euler(0f, 180f, 0f);
if (strPath == "HoldDance")
{
playerSp.transform.localPosition = new Vector3(0, 0.3f, 0);
}
animSp.runtimeAnimatorController = null;
RuntimeAnimatorController animator = (RuntimeAnimatorController)Resources.Load(strPath);
animSp.runtimeAnimatorController = (RuntimeAnimatorController)animator;
_audiosource.Stop();
_audiosource.clip = a;
if (Time.timeScale == 1f)
{
_audiosource.Play();
}
MMD_VMD_Sap.Select_VMD = vmdtext;
MMD_VMD_Sap.Start();
if (CamType2.enabled) return;
vmdCam.localPosition = new Vector3(0, 0, 0);
vmdCam.GetChild(0).localPosition = new Vector3(0, 0, 0);
Quaternion rotation = Quaternion.Euler(0f, 0f, 0f);
vmdCam.localRotation = rotation;
vmdCam.GetChild(0).localRotation = rotation;
Camera.main.transform.localPosition = new Vector3(0f, 0f, 0f);
Camera.main.transform.localRotation = rotation;
if (strPath == "Applepie")
{
Camera.main.transform.localPosition = new Vector3(.14f, 0, 5.44f);
Quaternion rot = Quaternion.Euler(0f, 180f, 0f);
Camera.main.transform.localRotation = rot;
}
else if (strPath == "Conqueror")
{
Camera.main.transform.localPosition = new Vector3(0f, -0.1f, 0f);
}
else if (strPath == "Gwiyomi")
{
Camera.main.transform.localPosition = new Vector3(0f, .8f, -1.24f);
}
else if (strPath == "DeepBlueTown")
{
Camera.main.transform.localPosition = new Vector3(0f, -.54f, 0f);
}
else if (strPath == "Mister")
{
Camera.main.transform.localPosition = new Vector3(0f, -.52f, 0f);
}
else if (strPath == "Lupin")
{
Camera.main.transform.localPosition = new Vector3(0f, -.1f, 0f);
}
else if (strPath == "SingMeToSleep")
{
Camera.main.transform.localPosition = new Vector3(0f, -.3f, 0f);
}
else if (strPath == "Samsara")
{
Camera.main.transform.localPosition = new Vector3(0f, -.2f, 0f);
}
else if (strPath == "Fireball")
{
Camera.main.transform.localPosition = new Vector3(0f, -.05f, 0f);
}
else if(strPath == "FullBodied")
{
Camera.main.transform.localPosition = new Vector3(0f, .14f, 0f);
}
else if (strPath == "SwingDance")
{
Camera.main.transform.localPosition = new Vector3(0f, .15f, 0f);
}
else if (strPath == "Elect")
{
Camera.main.transform.localPosition = new Vector3(0f, -.15f, 7.35f);
Quaternion rot = Quaternion.Euler(0f, 180f, 0f);
Camera.main.transform.localRotation = rot;
}
else if (strPath == "恋空予报")
{
Camera.main.transform.localPosition = new Vector3(0f, -.2f, 0f);
}
else if (strPath == "BaamFull")
{
Camera.main.transform.localPosition = new Vector3(0f, -.1f, 0f);
}
else if (strPath == "DestinationCalabria")
{
Camera.main.transform.localPosition = new Vector3(0f, -.1f, 0f);
}
else if (strPath == "Lamb")
{
Camera.main.transform.localPosition = new Vector3(0f, -.1f, 6.8f);
Quaternion rot = Quaternion.Euler(0f, 180f, 0f);
Camera.main.transform.localRotation = rot;
}
else if (strPath == "KillerB")
{
Camera.main.transform.localPosition = new Vector3(0f, -.1f, 4f);
Quaternion rot = Quaternion.Euler(0f, 180f, 0f);
Camera.main.transform.localRotation = rot;
}
else if (strPath == "我的悲伤是水做的")
{
Camera.main.transform.localPosition = new Vector3(0f, 0f, .81f);
}
else if (strPath == "ツギハギスタッカート")
{
Camera.main.transform.localPosition = new Vector3(.05f, -.24f, .55f);
}
}
else if (playerRust.activeSelf)
{
for (int i = 0; i < 249; i++)
{
if (i == 173) continue; // heart_eye
eyeHeart_sp.SetBlendShapeWeight(i, 0);
}
tmpText.text = "Now:" + strPath;
playerRust.transform.localPosition = new Vector3(0, 0, 0);
playerRust.transform.localRotation = Quaternion.Euler(0f, 180f, 0f);
if (strPath == "HoldDance")
{
playerRust.transform.localPosition = new Vector3(0, 0.3f, 0);
}
animRust.runtimeAnimatorController = null;
RuntimeAnimatorController animator = (RuntimeAnimatorController)Resources.Load(strPath);
animRust.runtimeAnimatorController = (RuntimeAnimatorController)animator;
_audiosource.Stop();
_audiosource.clip = a;
if (Time.timeScale == 1f)
{
_audiosource.Play();
}
MMD_VMD_Rust.Select_VMD = vmdtext;
MMD_VMD_Rust.Start();
if (CamType2.enabled) return;
vmdCam.localPosition = new Vector3(0, 0, 0);
vmdCam.GetChild(0).localPosition = new Vector3(0, 0, 0);
Quaternion rotation = Quaternion.Euler(0f, 0f, 0f);
vmdCam.localRotation = rotation;
vmdCam.GetChild(0).localRotation = rotation;
Camera.main.transform.localPosition = new Vector3(0f, 0f, 0f);
Camera.main.transform.localRotation = rotation;
MMD_VMD_Rust.distanceOffset = 1f;
if (strPath == "Applepie")
{
Camera.main.transform.localPosition = new Vector3(.14f, 0, 5.44f);
Quaternion rot = Quaternion.Euler(0f, 180f, 0f);
Camera.main.transform.localRotation = rot;
}
else if (strPath == "Conqueror")
{
Camera.main.transform.localPosition = new Vector3(0f, -0.3076369f, 0f);
}
else if (strPath == "Gwiyomi")
{
Camera.main.transform.localPosition = new Vector3(0f, .6f, -1.24f);
}
else if (strPath == "DeepBlueTown")
{
Camera.main.transform.localPosition = new Vector3(0f, -.54f, 0f);
}
else if (strPath == "Mister")
{
Camera.main.transform.localPosition = new Vector3(0f, -.38f, 0f);
}
else if (strPath == "Lupin")
{
Camera.main.transform.localPosition = new Vector3(0f, -.1f, 0f);
}
else if (strPath == "SingMeToSleep")
{
Camera.main.transform.localPosition = new Vector3(0f, -.3f, 0f);
}
else if (strPath == "Samsara")
{
Camera.main.transform.localPosition = new Vector3(0f, -.2f, 0f);
}
else if (strPath == "Fireball")
{
Camera.main.transform.localPosition = new Vector3(0f, -.05f, 0f);
}
else if (strPath == "Elect")
{
Camera.main.transform.localPosition = new Vector3(0f, -.25f, 7.35f);
Quaternion rot = Quaternion.Euler(0f, 180f, 0f);
Camera.main.transform.localRotation = rot;
}
else if (strPath == "恋空予报")
{
Camera.main.transform.localPosition = new Vector3(0f, -.2f, 0f);
}
else if (strPath == "BaamFull")
{
Camera.main.transform.localPosition = new Vector3(0f, -.2f, 0f);
}
else if (strPath == "DestinationCalabria")
{
Camera.main.transform.localPosition = new Vector3(0f, -.22f, 0f);
}
else if (strPath == "Lamb")
{
Camera.main.transform.localPosition = new Vector3(0f, -.22f, 6.8f);
Quaternion rot = Quaternion.Euler(0f, 180f, 0f);
Camera.main.transform.localRotation = rot;
}
else if (strPath == "ExcuseMe")
{
Camera.main.transform.localPosition = new Vector3(0f, -.15f, 0f);
}
else if (strPath == "KillerB")
{
Camera.main.transform.localPosition = new Vector3(0f, -.2f, 4f);
Quaternion rot = Quaternion.Euler(0f, 180f, 0f);
Camera.main.transform.localRotation = rot;
}
else if (strPath == "我的悲伤是水做的")
{
Camera.main.transform.localPosition = new Vector3(0f, -.15f, .81f);
}
else if (strPath == "ツギハギスタッカート")
{
MMD_VMD_Rust.distanceOffset = 2.1f;
Camera.main.transform.localPosition = new Vector3(.05f, -.39f, .27f);
}
}

if(playerRust.activeSelf && tempDistance != MMD_VMD_Rust.distanceOffset)
{
mmdChange(tmpText.text.Substring(4, tmpText.text.Length - 4), _audiosource.clip, MMD_VMD_Rust.Select_VMD);
}
// Debug.Log(MMD_VMD_Rust.distanceOffset);
}

private void motionChange(string strPath)
{
if (playerEl.activeSelf)
{
for (int i = 0; i < 86; i++)
{
if (i == 62) continue;
eyeHeart.SetBlendShapeWeight(i, 0);
}
tmpText.text = "Now:" + strPath;
playerEl.transform.localPosition = new Vector3(0, 0, 0);
playerEl.transform.localRotation = Quaternion.Euler(0f, 180f, 0f);

anim.runtimeAnimatorController = null;
RuntimeAnimatorController animator = (RuntimeAnimatorController)Resources.Load(strPath);
anim.runtimeAnimatorController = (RuntimeAnimatorController)animator;
_audiosource.Stop();

if (CamType2.enabled) return;
vmdCam.localPosition = new Vector3(0, 0, 0);
vmdCam.GetChild(0).localPosition = new Vector3(0, 0, 0);
Quaternion rotation = Quaternion.Euler(0f, 0f, 0f);
vmdCam.localRotation = rotation;
vmdCam.GetChild(0).localRotation = rotation;
Camera.main.transform.localPosition = new Vector3(0f, 0f, 0f);
Camera.main.transform.localRotation = rotation;
}
else if (playerSp.activeSelf)
{
for (int i = 0; i < 76; i++)
{
if (i == 67) continue;
eyeHeart_sp.SetBlendShapeWeight(i, 0);
}
tmpText.text = "Now:" + strPath;
playerSp.transform.localPosition = new Vector3(0, 0, 0);
playerSp.transform.localRotation = Quaternion.Euler(0f, 180f, 0f);

animSp.runtimeAnimatorController = null;
RuntimeAnimatorController animator = (RuntimeAnimatorController)Resources.Load(strPath);
animSp.runtimeAnimatorController = (RuntimeAnimatorController)animator;
_audiosource.Stop();

if (CamType2.enabled) return;
vmdCam.localPosition = new Vector3(0, 0, 0);
vmdCam.GetChild(0).localPosition = new Vector3(0, 0, 0);
Quaternion rotation = Quaternion.Euler(0f, 0f, 0f);
vmdCam.localRotation = rotation;
vmdCam.GetChild(0).localRotation = rotation;
Camera.main.transform.localPosition = new Vector3(0f, 0f, 0f);
Camera.main.transform.localRotation = rotation;

}
else if (playerRust.activeSelf)
{
for (int i = 0; i < 249; i++)
{
if (i == 173) continue; // heart_eye
eyeHeart_sp.SetBlendShapeWeight(i, 0);
}
tmpText.text = "Now:" + strPath;
playerRust.transform.localPosition = new Vector3(0, 0, 0);
playerRust.transform.localRotation = Quaternion.Euler(0f, 180f, 0f);

animRust.runtimeAnimatorController = null;
RuntimeAnimatorController animator = (RuntimeAnimatorController)Resources.Load(strPath);
animRust.runtimeAnimatorController = (RuntimeAnimatorController)animator;
_audiosource.Stop();

if (CamType2.enabled) return;
vmdCam.localPosition = new Vector3(0, 0, 0);
vmdCam.GetChild(0).localPosition = new Vector3(0, 0, 0);
Quaternion rotation = Quaternion.Euler(0f, 0f, 0f);
vmdCam.localRotation = rotation;
vmdCam.GetChild(0).localRotation = rotation;
Camera.main.transform.localPosition = new Vector3(0f, 0f, 0f);
Camera.main.transform.localRotation = rotation;
MMD_VMD_Rust.distanceOffset = 1f;

}
}

void ChangeCam()
{
// Camera.main.transform.localPosition = new Vector3(.05f, -.3f, .8f); // kato
Camera.main.transform.localPosition = new Vector3(0f, -.5f, 0.2f); // DreamChuChu
}
}