OSDN Git Service

add Savable support
[mikumikustudio/MikuMikuStudio.git] / src / projectkyoto / mmd / file / PMDModel.java
1 /*
2  * Copyright (c) 2011 Kazuhiko Kobayashi All rights reserved.
3  * <p/>
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  * 
7  * * Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  * <p/>
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * <p/>
14  * * Neither the name of 'MMDLoaderJME' nor the names of its contributors
15  * may be used to endorse or promote products derived from this software
16  * without specific prior written permission.
17  * <p/>
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 package projectkyoto.mmd.file;
31
32 import java.io.BufferedInputStream;
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.io.Serializable;
36 import java.net.URL;
37
38 /**
39  *
40  * @author Kazuhiko Kobayashi
41  */
42 public class PMDModel implements Serializable{
43     // PMD Header
44
45     private String id; // char[3] "Pmd"
46     private float version; // float 0x00 0x00 0x80 0x3F == 1.00;
47     private String modelName; // char[20] モデル名
48     private String comment; // char[256] コメント
49     // 頂点リスト
50     private int vertCount; // 頂点数
51     private PMDVertex[] vertexList;
52     private int faceVertCount;
53     private int faceVertIndex[];
54     private int materialCount;
55     private PMDMaterial[] material;
56     private PMDBoneList boneList;
57     private PMDIKList ikList;
58     private int skinCount;
59     private PMDSkinData skinData[];
60     private PMDSkinDispList skinDispList;
61     private PMDBoneDispNameList boneDispNameList;
62     private PMDBoneDispList boneDispList;
63     private PMDHeaderEnglish headerEnglish;
64     private PMDToonTextureList toonTextureList;
65     private PMDRigidBodyList rigidBodyList;
66     private PMDJointList jointList;
67
68     public PMDModel() {
69     }
70
71     public PMDModel(URL url) throws IOException {
72         readFromFile(url.openStream());
73     }
74     public PMDModel(InputStream is) throws IOException {
75         readFromFile(is);
76     }
77     public void readFromFile(InputStream is) throws IOException {
78         DataInputStreamLittleEndian dis = null;
79         try {
80 //            is = new DataInputStreamLittleEndian(new BufferedInputStream(
81 //                    new FileInputStream(
82 //                    file)));
83             dis = new DataInputStreamLittleEndian(new BufferedInputStream(is));
84             readFromStream(dis);
85             dis.close();
86 //            this.url = url;
87         } finally {
88             if (dis != null) {
89                 dis.close();
90                 dis = null;
91             }
92         }
93 //        System.out.println(toString());
94     }
95
96     public void readFromStream(DataInputStreamLittleEndian is) throws
97             IOException {
98         id = is.readString(3);
99         if (!"Pmd".equals(id)) {
100             throw new InvalidPMDFileException("Invalid ID:" + id);
101         }
102         version = is.readFloat();
103         modelName = is.readString(20);
104         comment = is.readString(256);
105         vertCount = is.readInt();
106         vertexList = new PMDVertex[vertCount];
107         for (int i = 0; i < vertCount; i++) {
108             vertexList[i] = new PMDVertex(is);
109         }
110         faceVertCount = is.readInt();
111         faceVertIndex = new int[faceVertCount];
112         for (int i = 0; i < faceVertCount; i++) {
113             faceVertIndex[i] = is.readUnsignedShort();
114         }
115         // 逆にする。
116         for (int i = 0; i < faceVertCount; i += 3) {
117             int tmp = faceVertIndex[i];
118             faceVertIndex[i] = faceVertIndex[i + 1];
119             faceVertIndex[i + 1] = tmp;
120         }
121         materialCount = is.readInt();
122         material = new PMDMaterial[materialCount];
123         for (int i = 0; i < materialCount; i++) {
124             material[i] = new PMDMaterial(is);
125         }
126         boneList = new PMDBoneList(is);
127         ikList = new PMDIKList(is);
128         skinCount = is.readShort();
129         skinData = new PMDSkinData[skinCount];
130         for (int i = 0; i < skinCount; i++) {
131             skinData[i] = new PMDSkinData(is);
132         }
133         skinDispList = new PMDSkinDispList(is);
134         boneDispNameList = new PMDBoneDispNameList(is);
135         boneDispList = new PMDBoneDispList(is);
136         headerEnglish = new PMDHeaderEnglish(this, is);
137         toonTextureList = new PMDToonTextureList(is);
138         rigidBodyList = new PMDRigidBodyList(is);
139         jointList = new PMDJointList(is);
140 //        toonTextureList = new PMDToonTextureList();
141 //        rigidBodyList = new PMDRigidBodyList();
142 //        jointList = new PMDJointList();
143     }
144
145     @Override
146     public String toString() {
147         StringBuffer sb = new StringBuffer();
148         sb.append("{id = " + id
149                 + " version = " + version
150                 + " modelName = " + modelName
151                 + " comment = " + comment);
152         sb.append(" vertexCount = " + vertCount);
153         for (PMDVertex vertex : vertexList) {
154             sb.append(vertex.toString());
155         }
156         sb.append(" faceVertCount = " + faceVertCount);
157         sb.append(" faceVertIndex = {");
158         for (int i : faceVertIndex) {
159             sb.append(i).append(" ");
160         }
161         sb.append("}");
162         sb.append(" materialCount = ").append(materialCount);
163         sb.append(" PMDMaterial = {");
164         for (PMDMaterial m : material) {
165             sb.append(m).append(" ");
166         }
167         sb.append("}\n");
168         sb.append("boneList = ");
169         sb.append(boneList.toString());
170         sb.append("\nikList = " + ikList);
171         sb.append("\nskinData = {");
172
173         for (int i = 0; i < skinCount; i++) {
174             sb.append(skinData[i]);
175             sb.append("\n");
176         }
177         sb.append("}\n}\n");
178
179         sb.append(rigidBodyList.toString());
180         return sb.toString();
181     }
182
183     public String getComment() {
184         return comment;
185     }
186
187     public void setComment(String comment) {
188         this.comment = comment;
189     }
190
191     public int getFaceVertCount() {
192         return faceVertCount;
193     }
194
195     public void setFaceVertCount(int faceVertCount) {
196         this.faceVertCount = faceVertCount;
197     }
198
199     public int[] getFaceVertIndex() {
200         return faceVertIndex;
201     }
202
203     public void setFaceVertIndex(int[] faceVertIndex) {
204         this.faceVertIndex = faceVertIndex;
205     }
206
207     public String getId() {
208         return id;
209     }
210
211     public void setId(String id) {
212         this.id = id;
213     }
214
215     public PMDMaterial[] getMaterial() {
216         return material;
217     }
218
219     public void setMaterial(PMDMaterial[] material) {
220         this.material = material;
221     }
222
223     public int getMaterialCount() {
224         return materialCount;
225     }
226
227     public void setMaterialCount(int materialCount) {
228         this.materialCount = materialCount;
229     }
230
231     public String getModelName() {
232         return modelName;
233     }
234
235     public void setModelName(String modelName) {
236         this.modelName = modelName;
237     }
238
239     public float getVersion() {
240         return version;
241     }
242
243     public void setVersion(float version) {
244         this.version = version;
245     }
246
247     public int getVertCount() {
248         return vertCount;
249     }
250
251     public void setVertCount(int vertCount) {
252         this.vertCount = vertCount;
253     }
254
255     public PMDVertex[] getVertexList() {
256         return vertexList;
257     }
258
259     public void setVertexList(PMDVertex[] vertexList) {
260         this.vertexList = vertexList;
261     }
262
263     public PMDBoneList getBoneList() {
264         return boneList;
265     }
266
267     public void setBoneList(PMDBoneList boneList) {
268         this.boneList = boneList;
269     }
270
271     public int getSkinCount() {
272         return skinCount;
273     }
274
275     public void setSkinCount(int skinCount) {
276         this.skinCount = skinCount;
277     }
278
279     public PMDSkinData[] getSkinData() {
280         return skinData;
281     }
282
283     public void setSkinData(PMDSkinData[] skinData) {
284         this.skinData = skinData;
285     }
286
287     public PMDIKList getIkList() {
288         return ikList;
289     }
290
291     public void setIkList(PMDIKList ikList) {
292         this.ikList = ikList;
293     }
294
295     public PMDBoneDispList getBoneDispList() {
296         return boneDispList;
297     }
298
299     public void setBoneDispList(PMDBoneDispList boneDispList) {
300         this.boneDispList = boneDispList;
301     }
302
303     public PMDBoneDispNameList getBoneDispNameList() {
304         return boneDispNameList;
305     }
306
307     public void setBoneDispNameList(PMDBoneDispNameList boneDispNameList) {
308         this.boneDispNameList = boneDispNameList;
309     }
310
311     public PMDHeaderEnglish getHeaderEnglish() {
312         return headerEnglish;
313     }
314
315     public void setHeaderEnglish(PMDHeaderEnglish headerEnglish) {
316         this.headerEnglish = headerEnglish;
317     }
318
319     public PMDSkinDispList getSkinDispList() {
320         return skinDispList;
321     }
322
323     public void setSkinDispList(PMDSkinDispList skinDispList) {
324         this.skinDispList = skinDispList;
325     }
326
327     public PMDToonTextureList getToonTextureList() {
328         return toonTextureList;
329     }
330
331     public void setToonTextureList(PMDToonTextureList toonTextureList) {
332         this.toonTextureList = toonTextureList;
333     }
334
335     public PMDJointList getJointList() {
336         return jointList;
337     }
338
339     public void setJointList(PMDJointList jointList) {
340         this.jointList = jointList;
341     }
342
343     public PMDRigidBodyList getRigidBodyList() {
344         return rigidBodyList;
345     }
346
347     public void setRigidBodyList(PMDRigidBodyList rigidBodyList) {
348         this.rigidBodyList = rigidBodyList;
349     }
350 }