2 * Copyright (c) 2011 Kazuhiko Kobayashi All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
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.
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.
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.
30 package projectkyoto.mmd.file;
32 import java.io.BufferedInputStream;
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.io.Serializable;
40 * @author Kazuhiko Kobayashi
42 public class PMDModel implements Serializable{
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] コメント
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;
71 public PMDModel(URL url) throws IOException {
72 readFromFile(url.openStream());
74 public PMDModel(InputStream is) throws IOException {
77 public void readFromFile(InputStream is) throws IOException {
78 DataInputStreamLittleEndian dis = null;
80 // is = new DataInputStreamLittleEndian(new BufferedInputStream(
81 // new FileInputStream(
83 dis = new DataInputStreamLittleEndian(new BufferedInputStream(is));
93 // System.out.println(toString());
96 public void readFromStream(DataInputStreamLittleEndian is) throws
98 id = is.readString(3);
99 if (!"Pmd".equals(id)) {
100 throw new InvalidPMDFileException("Invalid ID:" + id);
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);
110 faceVertCount = is.readInt();
111 faceVertIndex = new int[faceVertCount];
112 for (int i = 0; i < faceVertCount; i++) {
113 faceVertIndex[i] = is.readUnsignedShort();
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;
121 materialCount = is.readInt();
122 material = new PMDMaterial[materialCount];
123 for (int i = 0; i < materialCount; i++) {
124 material[i] = new PMDMaterial(is);
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);
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();
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());
156 sb.append(" faceVertCount = " + faceVertCount);
157 sb.append(" faceVertIndex = {");
158 for (int i : faceVertIndex) {
159 sb.append(i).append(" ");
162 sb.append(" materialCount = ").append(materialCount);
163 sb.append(" PMDMaterial = {");
164 for (PMDMaterial m : material) {
165 sb.append(m).append(" ");
168 sb.append("boneList = ");
169 sb.append(boneList.toString());
170 sb.append("\nikList = " + ikList);
171 sb.append("\nskinData = {");
173 for (int i = 0; i < skinCount; i++) {
174 sb.append(skinData[i]);
179 sb.append(rigidBodyList.toString());
180 return sb.toString();
183 public String getComment() {
187 public void setComment(String comment) {
188 this.comment = comment;
191 public int getFaceVertCount() {
192 return faceVertCount;
195 public void setFaceVertCount(int faceVertCount) {
196 this.faceVertCount = faceVertCount;
199 public int[] getFaceVertIndex() {
200 return faceVertIndex;
203 public void setFaceVertIndex(int[] faceVertIndex) {
204 this.faceVertIndex = faceVertIndex;
207 public String getId() {
211 public void setId(String id) {
215 public PMDMaterial[] getMaterial() {
219 public void setMaterial(PMDMaterial[] material) {
220 this.material = material;
223 public int getMaterialCount() {
224 return materialCount;
227 public void setMaterialCount(int materialCount) {
228 this.materialCount = materialCount;
231 public String getModelName() {
235 public void setModelName(String modelName) {
236 this.modelName = modelName;
239 public float getVersion() {
243 public void setVersion(float version) {
244 this.version = version;
247 public int getVertCount() {
251 public void setVertCount(int vertCount) {
252 this.vertCount = vertCount;
255 public PMDVertex[] getVertexList() {
259 public void setVertexList(PMDVertex[] vertexList) {
260 this.vertexList = vertexList;
263 public PMDBoneList getBoneList() {
267 public void setBoneList(PMDBoneList boneList) {
268 this.boneList = boneList;
271 public int getSkinCount() {
275 public void setSkinCount(int skinCount) {
276 this.skinCount = skinCount;
279 public PMDSkinData[] getSkinData() {
283 public void setSkinData(PMDSkinData[] skinData) {
284 this.skinData = skinData;
287 public PMDIKList getIkList() {
291 public void setIkList(PMDIKList ikList) {
292 this.ikList = ikList;
295 public PMDBoneDispList getBoneDispList() {
299 public void setBoneDispList(PMDBoneDispList boneDispList) {
300 this.boneDispList = boneDispList;
303 public PMDBoneDispNameList getBoneDispNameList() {
304 return boneDispNameList;
307 public void setBoneDispNameList(PMDBoneDispNameList boneDispNameList) {
308 this.boneDispNameList = boneDispNameList;
311 public PMDHeaderEnglish getHeaderEnglish() {
312 return headerEnglish;
315 public void setHeaderEnglish(PMDHeaderEnglish headerEnglish) {
316 this.headerEnglish = headerEnglish;
319 public PMDSkinDispList getSkinDispList() {
323 public void setSkinDispList(PMDSkinDispList skinDispList) {
324 this.skinDispList = skinDispList;
327 public PMDToonTextureList getToonTextureList() {
328 return toonTextureList;
331 public void setToonTextureList(PMDToonTextureList toonTextureList) {
332 this.toonTextureList = toonTextureList;
335 public PMDJointList getJointList() {
339 public void setJointList(PMDJointList jointList) {
340 this.jointList = jointList;
343 public PMDRigidBodyList getRigidBodyList() {
344 return rigidBodyList;
347 public void setRigidBodyList(PMDRigidBodyList rigidBodyList) {
348 this.rigidBodyList = rigidBodyList;