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;
34 import java.io.FileOutputStream;
35 import java.io.IOException;
36 import java.io.InputStream;
37 import java.io.RandomAccessFile;
38 import java.io.Serializable;
40 import java.nio.ByteBuffer;
41 import java.nio.ByteOrder;
42 import java.nio.channels.FileChannel.MapMode;
43 import projectkyoto.mmd.file.util2.BufferUtil;
47 * @author Kazuhiko Kobayashi
49 public class PMDModel implements Serializable{
52 private String id; // char[3] "Pmd"
53 private float version; // float 0x00 0x00 0x80 0x3F == 1.00;
54 private String modelName; // char[20] モデル名
55 private String comment; // char[256] コメント
57 private int vertCount; // 頂点数
58 // private PMDVertex[] vertexList;
59 private ByteBuffer vertexBuffer;
60 private int faceVertCount;
61 private int faceVertIndex[];
62 private int materialCount;
63 private PMDMaterial[] material;
64 private PMDBoneList boneList;
65 private PMDIKList ikList;
66 private int skinCount;
67 private PMDSkinData skinData[];
68 private PMDSkinDispList skinDispList;
69 private PMDBoneDispNameList boneDispNameList;
70 private PMDBoneDispList boneDispList;
71 private PMDHeaderEnglish headerEnglish;
72 private PMDToonTextureList toonTextureList;
73 private PMDRigidBodyList rigidBodyList;
74 private PMDJointList jointList;
79 public PMDModel(URL url) throws IOException {
80 readFromFile(url.openStream());
82 public PMDModel(InputStream is) throws IOException {
85 public void readFromFile(InputStream is) throws IOException {
86 DataInputStreamLittleEndian dis = null;
88 // is = new DataInputStreamLittleEndian(new BufferedInputStream(
89 // new FileInputStream(
91 dis = new DataInputStreamLittleEndian(new BufferedInputStream(is));
101 // System.out.println(toString());
104 public void readFromStream(DataInputStreamLittleEndian is) throws
106 id = is.readString(3);
107 if (!"Pmd".equals(id)) {
108 throw new InvalidPMDFileException("Invalid ID:" + id);
110 version = is.readFloat();
111 modelName = is.readString(20);
112 comment = is.readString(256);
113 vertCount = is.readInt();
114 // vertexList = new PMDVertex[vertCount];
115 // vertexBuffer = ByteBuffer.allocateDirect(PMDVertex.size() * vertCount);
116 vertexBuffer = BufferUtil.createByteBuffer(PMDVertex.size() * vertCount);
117 vertexBuffer.order(ByteOrder.nativeOrder());
118 PMDVertex tmpVertex = new PMDVertex();
119 for (int i = 0; i < vertCount; i++) {
120 tmpVertex.readFromStream(is);
121 tmpVertex.writeToBuffer(vertexBuffer);
124 faceVertCount = is.readInt();
125 faceVertIndex = new int[faceVertCount];
126 for (int i = 0; i < faceVertCount; i++) {
127 faceVertIndex[i] = is.readUnsignedShort();
130 for (int i = 0; i < faceVertCount; i += 3) {
131 int tmp = faceVertIndex[i];
132 faceVertIndex[i] = faceVertIndex[i + 1];
133 faceVertIndex[i + 1] = tmp;
135 materialCount = is.readInt();
136 material = new PMDMaterial[materialCount];
137 for (int i = 0; i < materialCount; i++) {
138 material[i] = new PMDMaterial(is);
140 boneList = new PMDBoneList(is);
141 ikList = new PMDIKList(is);
142 skinCount = is.readShort();
143 skinData = new PMDSkinData[skinCount];
144 for (int i = 0; i < skinCount; i++) {
145 skinData[i] = new PMDSkinData(is);
147 skinDispList = new PMDSkinDispList(is);
148 boneDispNameList = new PMDBoneDispNameList(is);
149 boneDispList = new PMDBoneDispList(is);
150 headerEnglish = new PMDHeaderEnglish(this, is);
151 toonTextureList = new PMDToonTextureList(is);
152 rigidBodyList = new PMDRigidBodyList(is);
153 jointList = new PMDJointList(is);
154 // toonTextureList = new PMDToonTextureList();
155 // rigidBodyList = new PMDRigidBodyList();
156 // jointList = new PMDJointList();
158 public PMDVertex getVertex(int i) {
159 return getVertex(i, new PMDVertex());
161 public PMDVertex getVertex(int i, PMDVertex in) {
162 vertexBuffer.position(PMDVertex.size() * i);
163 in.readFromBuffer(vertexBuffer);
167 public String toString() {
168 StringBuffer sb = new StringBuffer();
169 sb.append("{id = " + id
170 + " version = " + version
171 + " modelName = " + modelName
172 + " comment = " + comment);
173 sb.append(" vertexCount = " + vertCount);
174 PMDVertex tmpVertex = new PMDVertex();
175 vertexBuffer.position(0);
176 for (int i=0;i<vertCount;i++) {
177 tmpVertex.readFromBuffer(vertexBuffer);
178 sb.append(tmpVertex.toString());
180 sb.append(" faceVertCount = " + faceVertCount);
181 sb.append(" faceVertIndex = {");
182 for (int i : faceVertIndex) {
183 sb.append(i).append(" ");
186 sb.append(" materialCount = ").append(materialCount);
187 sb.append(" PMDMaterial = {");
188 for (PMDMaterial m : material) {
189 sb.append(m).append(" ");
192 sb.append("boneList = ");
193 sb.append(boneList.toString());
194 sb.append("\nikList = " + ikList);
195 sb.append("\nskinData = {");
197 for (int i = 0; i < skinCount; i++) {
198 sb.append(skinData[i]);
203 sb.append(rigidBodyList.toString());
204 return sb.toString();
207 public String getComment() {
211 public void setComment(String comment) {
212 this.comment = comment;
215 public int getFaceVertCount() {
216 return faceVertCount;
219 public void setFaceVertCount(int faceVertCount) {
220 this.faceVertCount = faceVertCount;
223 public int[] getFaceVertIndex() {
224 return faceVertIndex;
227 public void setFaceVertIndex(int[] faceVertIndex) {
228 this.faceVertIndex = faceVertIndex;
231 public String getId() {
235 public void setId(String id) {
239 public PMDMaterial[] getMaterial() {
243 public void setMaterial(PMDMaterial[] material) {
244 this.material = material;
247 public int getMaterialCount() {
248 return materialCount;
251 public void setMaterialCount(int materialCount) {
252 this.materialCount = materialCount;
255 public String getModelName() {
259 public void setModelName(String modelName) {
260 this.modelName = modelName;
263 public float getVersion() {
267 public void setVersion(float version) {
268 this.version = version;
271 public int getVertCount() {
275 public void setVertCount(int vertCount) {
276 this.vertCount = vertCount;
279 public ByteBuffer getVertexBuffer() {
283 public void setVertexBuffer(ByteBuffer vertexBuffer) {
284 this.vertexBuffer = vertexBuffer;
288 public PMDBoneList getBoneList() {
292 public void setBoneList(PMDBoneList boneList) {
293 this.boneList = boneList;
296 public int getSkinCount() {
300 public void setSkinCount(int skinCount) {
301 this.skinCount = skinCount;
304 public PMDSkinData[] getSkinData() {
308 public void setSkinData(PMDSkinData[] skinData) {
309 this.skinData = skinData;
312 public PMDIKList getIkList() {
316 public void setIkList(PMDIKList ikList) {
317 this.ikList = ikList;
320 public PMDBoneDispList getBoneDispList() {
324 public void setBoneDispList(PMDBoneDispList boneDispList) {
325 this.boneDispList = boneDispList;
328 public PMDBoneDispNameList getBoneDispNameList() {
329 return boneDispNameList;
332 public void setBoneDispNameList(PMDBoneDispNameList boneDispNameList) {
333 this.boneDispNameList = boneDispNameList;
336 public PMDHeaderEnglish getHeaderEnglish() {
337 return headerEnglish;
340 public void setHeaderEnglish(PMDHeaderEnglish headerEnglish) {
341 this.headerEnglish = headerEnglish;
344 public PMDSkinDispList getSkinDispList() {
348 public void setSkinDispList(PMDSkinDispList skinDispList) {
349 this.skinDispList = skinDispList;
352 public PMDToonTextureList getToonTextureList() {
353 return toonTextureList;
356 public void setToonTextureList(PMDToonTextureList toonTextureList) {
357 this.toonTextureList = toonTextureList;
360 public PMDJointList getJointList() {
364 public void setJointList(PMDJointList jointList) {
365 this.jointList = jointList;
368 public PMDRigidBodyList getRigidBodyList() {
369 return rigidBodyList;
372 public void setRigidBodyList(PMDRigidBodyList rigidBodyList) {
373 this.rigidBodyList = rigidBodyList;