• R/O
  • SSH

MMDLoaderJME: 提交

Java版MikuMikuDanceライブラリ


Commit MetaInfo

修訂ad69521155ab2f9705ee17162ea71381d57acb90 (tree)
時間2012-10-17 17:21:47
作者Kazuhiko Kobayashi <chototsu_ <moushinp@yaho...>
CommiterKazuhiko Kobayashi <chototsu_

Log Message

Add pmdcache support.

Change Summary

差異

diff -r c6451e912509 -r ad69521155ab src/projectkyoto/jme3/mmd/PMDLoaderGLSLSkinning2.java
--- a/src/projectkyoto/jme3/mmd/PMDLoaderGLSLSkinning2.java Thu Oct 11 15:17:57 2012 +0900
+++ b/src/projectkyoto/jme3/mmd/PMDLoaderGLSLSkinning2.java Wed Oct 17 17:21:47 2012 +0900
@@ -34,13 +34,10 @@
3434 import com.jme3.asset.AssetInfo;
3535 import com.jme3.asset.AssetLoader;
3636 import com.jme3.asset.AssetManager;
37-import com.jme3.asset.AssetNotFoundException;
3837 import com.jme3.asset.DesktopAssetManager;
3938 import com.jme3.material.Material;
4039 import com.jme3.material.RenderState.BlendMode;
41-import com.jme3.material.RenderState.FaceCullMode;
4240 import com.jme3.math.ColorRGBA;
43-import com.jme3.math.FastMath;
4441 import com.jme3.math.Matrix4f;
4542 import com.jme3.math.Quaternion;
4643 import com.jme3.math.Vector3f;
@@ -53,13 +50,12 @@
5350 import com.jme3.scene.debug.SkeletonWire;
5451 import com.jme3.scene.shape.Box;
5552 import com.jme3.shader.VarType;
56-import com.jme3.system.JmeSystem;
5753 import com.jme3.texture.Texture;
5854 import com.jme3.util.BufferUtils;
5955 import com.jme3.util.TempVars;
60-import com.sun.jmx.remote.util.OrderClassLoaders;
56+import java.io.BufferedInputStream;
6157 import java.io.IOException;
62-import java.nio.ByteBuffer;
58+import java.io.InputStream;
6359 import java.nio.FloatBuffer;
6460 import java.nio.ShortBuffer;
6561 import java.util.ArrayList;
@@ -69,11 +65,11 @@
6965 import java.util.StringTokenizer;
7066 import java.util.logging.Level;
7167 import java.util.logging.Logger;
72-import org.lwjgl.opengl.GL11;
7368 import projectkyoto.mmd.file.*;
74-import projectkyoto.mmd.file.pmn.PMNData;
7569 import projectkyoto.mmd.file.util2.MeshConverter;
7670 import projectkyoto.mmd.file.util2.MeshData;
71+import projectkyoto.mmd.file.util2.PMDFileUtil;
72+import projectkyoto.mmd.file.util2.SkinMeshData;
7773
7874 /**
7975 *
@@ -173,13 +169,13 @@
173169 // go.optimize3();
174170 createSkinCommonVertData();
175171 int numBones = meshConverter.getMaxBoneSize();
176- if (meshConverter.getSkinMeshData().getBoneList().size() > numBones) {
177- if (meshConverter.getSkinMeshData().getBoneList().size() > 56) {
178- throw new TooManyBonesException(Integer.toString(meshConverter.getSkinMeshData().getBoneList().size()));
172+ if (meshConverter.getSkinMeshData().skinIndexArray.length > numBones) {
173+ if (meshConverter.getSkinMeshData().skinIndexArray.length > 56) {
174+ throw new TooManyBonesException(Integer.toString(meshConverter.getSkinMeshData().skinIndexArray.length));
179175 }
180- numBones = meshConverter.getSkinMeshData().getBoneList().size();
176+ numBones = meshConverter.getSkinMeshData().skinIndexArray.length;
181177 }
182- for (PMDMaterial pmdMaterial : meshConverter.getSkinMeshData().getIndexMap().keySet()) {
178+ for (PMDMaterial pmdMaterial : meshConverter.getSkinMeshData().indexShortBufferMap.keySet()) {
183179 PMDSkinMesh mesh = createSkinMesh(pmdMaterial);
184180 PMDGeometry geom = new PMDGeometry("geom" + meshCount++);
185181 geom.setMesh(mesh);
@@ -221,71 +217,38 @@
221217 }
222218
223219 void createSkinCommonVertData() {
220+ SkinMeshData smd = meshConverter.getSkinMeshData();
224221 skinvb = new VertexBuffer(VertexBuffer.Type.Position);
225- FloatBuffer skinvfb = BufferUtils.createFloatBuffer(meshConverter.getSkinMeshData().getVertexList().size() * 3);
222+ FloatBuffer skinvfb = smd.skinvfb;
226223 skinvb.setupData(VertexBuffer.Usage.Dynamic, 3, VertexBuffer.Format.Float, skinvfb);
227224
228225 skinvb2 = new VertexBuffer(VertexBuffer.Type.Position);
229- FloatBuffer skinvfb2 = BufferUtils.createFloatBuffer(meshConverter.getSkinMeshData().getVertexList().size() * 3);
226+ FloatBuffer skinvfb2 = smd.skinvfb2;
230227 skinvb2.setupData(VertexBuffer.Usage.Dynamic, 3, VertexBuffer.Format.Float, skinvfb2);
231228
232229 skinnb = new VertexBuffer(VertexBuffer.Type.Normal);
233- FloatBuffer skinnfb = BufferUtils.createFloatBuffer(meshConverter.getSkinMeshData().getVertexList().size() * 3);
230+ FloatBuffer skinnfb = smd.skinnfb;
234231 skinnb.setupData(VertexBuffer.Usage.Static, 3, VertexBuffer.Format.Float, skinnfb);
235232
236-// skinnb2 = new VertexBuffer(VertexBuffer.Type.Normal);
237-// FloatBuffer skinnfb2 = BufferUtils.createFloatBuffer(meshConverter.getSkinMeshData().getVertexList().size() * 3);
238-// skinnb2.setupData(VertexBuffer.Usage.Static, 3, VertexBuffer.Format.Float, skinnfb2);
239-
240233 skintb = new VertexBuffer(VertexBuffer.Type.TexCoord);
241- FloatBuffer skintfb = BufferUtils.createFloatBuffer(meshConverter.getSkinMeshData().getVertexList().size() * 2);
234+ FloatBuffer skintfb = smd.skintfb;
242235 skintb.setupData(VertexBuffer.Usage.Static, 2, VertexBuffer.Format.Float, skintfb);
243236
244237 skinbib = new VertexBuffer(VertexBuffer.Type.BoneIndex);
245- ShortBuffer skinbisb = BufferUtils.createShortBuffer(meshConverter.getSkinMeshData().getVertexList().size() * 2);
238+ ShortBuffer skinbisb = smd.skinbisb;
246239 skinbib.setupData(VertexBuffer.Usage.Static, 2, VertexBuffer.Format.UnsignedShort, skinbisb);
247240
248241 skinwb = new VertexBuffer(VertexBuffer.Type.BoneWeight);
249- FloatBuffer wfb = BufferUtils.createFloatBuffer(meshConverter.getSkinMeshData().getVertexList().size() * 2);
242+ FloatBuffer wfb = smd.wfb;
250243 skinwb.setupData(VertexBuffer.Usage.Static, 2, VertexBuffer.Format.Float, wfb);
251244
252- for (PMDVertex v : meshConverter.getSkinMeshData().getVertexList()) {
253- skinvfb.put(v.getPos().x).put(v.getPos().y).put(v.getPos().z);
254- skinnfb.put(v.getNormal().x).put(v.getNormal().y).put(v.getNormal().z);
255- float f1 = v.getUv().getU();
256- float f2 = v.getUv().getV();
257-// tfb.put(v.getUv().getU()).put(1f - v.getUv().getV());
258- f1 = f1 - FastMath.floor(f1);
259- f2 = f2 - FastMath.floor(f2);
260- f2 = 1 - f2;
261- skintfb.put(f1).put(f2);
262-// skintfb.put(v.getUv().getU()).put(1f - v.getUv().getV());
263-// skinbisb.put((short) meshConverter.getSkinMeshData()
264-// .getBoneList().indexOf(v.getBoneNum1()))
265-// .put((short) meshConverter.getSkinMeshData()
266-// .getBoneList().indexOf(v.getBoneNum2()));
267- short b1 = (short)meshConverter.getSkinMeshData().getBoneList().indexOf(v.getBoneNum1());
268- short b2 = (short)meshConverter.getSkinMeshData().getBoneList().indexOf(v.getBoneNum2());
269- if (b1 < 0) b1 = 0;
270- if (b2 < 0) b2 = 0;
271- skinbisb.put(b1).put(b2);
272- float weight = (float) v.getBoneWeight() / 100.0f;
273- wfb.put(weight).put(1f - weight);
274- }
275245 skinvfb.position(0);
276246 skinvfb2.position(0);
277247 skinvfb2.put(skinvfb);
278248 skinnfb.position(0);
279249 // skinnfb2.position(0);
280250 // skinnfb2.put(skinnfb);
281- skinIndexArray = new int[meshConverter.getSkinMeshData().getBoneList().size()];
282- for (int i = 0; i < skinIndexArray.length; i++) {
283- if (i < meshConverter.getSkinMeshData().getBoneList().size()) {
284- skinIndexArray[i] = meshConverter.getSkinMeshData().getBoneList().get(i).shortValue();
285- } else {
286- skinIndexArray[i] = 0;
287- }
288- }
251+ skinIndexArray = smd.skinIndexArray;
289252 }
290253
291254 PMDSkinMesh createSkinMesh(PMDMaterial pmdMaterial) {
@@ -294,7 +257,6 @@
294257 textureFlag = false;
295258 }
296259 PMDSkinMesh mesh = new PMDSkinMesh();
297- List<Integer> indexList = meshConverter.getSkinMeshData().getIndexMap().get(pmdMaterial);
298260 mesh.setMode(Mesh.Mode.Triangles);
299261 mesh.setBuffer(skinvb);
300262 mesh.setSkinvb2(skinvb2);
@@ -306,10 +268,7 @@
306268 mesh.setBuffer(skinbib);
307269 mesh.setBuffer(skinwb);
308270 VertexBuffer ib = new VertexBuffer(VertexBuffer.Type.Index);
309- ShortBuffer isb = BufferUtils.createShortBuffer(indexList.size());
310- for (Integer index : indexList) {
311- isb.put(index.shortValue());
312- }
271+ ShortBuffer isb = meshConverter.getSkinMeshData().indexShortBufferMap.get(pmdMaterial);
313272 ib.setupData(VertexBuffer.Usage.Static, 1, VertexBuffer.Format.UnsignedShort, isb);
314273 mesh.setBuffer(ib);
315274 mesh.setBoneIndexArray(skinIndexArray);
@@ -336,76 +295,27 @@
336295 PMDMesh mesh = new PMDMesh();
337296 mesh.setMode(Mesh.Mode.Triangles);
338297 VertexBuffer vb = new VertexBuffer(VertexBuffer.Type.Position);
339- FloatBuffer vfb = BufferUtils.createFloatBuffer(md.getVertIndexList().size() * 3);
340298 VertexBuffer nb = new VertexBuffer(VertexBuffer.Type.Normal);
341- FloatBuffer nfb = BufferUtils.createFloatBuffer(md.getVertIndexList().size() * 3);
342-
343-// VertexBuffer bvb = new VertexBuffer(VertexBuffer.Type.BindPosePosition);
344-// FloatBuffer bvfb = BufferUtils.createFloatBuffer(md.getVertexList().size() * 3);
345-// VertexBuffer bnb = new VertexBuffer(VertexBuffer.Type.BindPoseNormal);
346-// FloatBuffer bnfb = BufferUtils.createFloatBuffer(md.getVertexList().size() * 3);
347299
348300 VertexBuffer tb = new VertexBuffer(VertexBuffer.Type.TexCoord);
349301
350- FloatBuffer tfb = null;
351- if (textureFlag ) {
352- tfb = BufferUtils.createFloatBuffer(md.getVertIndexList().size() * 2);
353- }
354302 VertexBuffer wb = new VertexBuffer(VertexBuffer.Type.BoneWeight);
355- FloatBuffer wfb = BufferUtils.createFloatBuffer(md.getVertIndexList().size() * 2);
356303 VertexBuffer ib = new VertexBuffer(VertexBuffer.Type.Index);
357- ShortBuffer isb = BufferUtils.createShortBuffer(md.getIndexList().size()/*md.getMaterial().getFaceVertCount()*/);
358304 VertexBuffer bib = new VertexBuffer(VertexBuffer.Type.BoneIndex);
359- ShortBuffer bisb = BufferUtils.createShortBuffer(md.getVertIndexList().size() * 2);
360305 PMDVertex v = new PMDVertex();
361- for (Integer vertIndex : md.getVertIndexList()) {
362- model.getVertex(vertIndex, v);
363- vfb.put(v.getPos().x).put(v.getPos().y).put(v.getPos().z);
364- nfb.put(v.getNormal().x).put(v.getNormal().y).put(v.getNormal().z);
365-
366-// bvfb.put(v.getPos().x).put(v.getPos().y).put(v.getPos().z);
367-// bnfb.put(v.getNormal().x).put(v.getNormal().y).put(v.getNormal().z);
368- if (textureFlag) {
369- float f1 = v.getUv().getU();
370- float f2 = v.getUv().getV();
371-// tfb.put(v.getUv().getU()).put(1f - v.getUv().getV());
372- f1 = f1 - FastMath.floor(f1);
373- f2 = f2 - FastMath.floor(f2);
374- f2 = 1 - f2;
375- tfb.put(f1).put(f2);
376- }
377- float weight = (float) v.getBoneWeight() / 100.0f;
378- wfb.put(weight).put(1f - weight);
379- short b1 = (short)md.getBoneList().indexOf(v.getBoneNum1());
380- short b2 = (short)md.getBoneList().indexOf(v.getBoneNum2());
381- if (b1 < 0) b1 = 0;
382- if (b2 < 0) b2 = 0;
383- bisb.put(b1).put(b2);
384-// bisb.put((short) md.getBoneList().indexOf(v.getBoneNum1())).put((short) md.getBoneList().indexOf(v.getBoneNum2()));
385-// if (( weight != 0 && md.getBoneList().indexOf(v.getBoneNum1()) < 0)
386-// || (weight != 1 && md.getBoneList().indexOf(v.getBoneNum2())<0)){
387-// System.out.println("ERROR!! "+v.getBoneNum1()+" "+v.getBoneNum2());
388-// System.out.println(""+md.getBoneList().indexOf(v.getBoneNum1())+" "+md.getBoneList().indexOf(v.getBoneNum2()));
389-// System.out.println("weight = "+weight);
390-// }
391- }
392- for (Integer index : md.getIndexList()) {
393- isb.put(index.shortValue());
394-// System.out.println("index = "+index);
395- }
396306 // System.out.println("isb.capacity() = " + isb.capacity());
397307 // System.out.println("isb.capacity() = " + md.getIndexList().size());
398- vb.setupData(VertexBuffer.Usage.Static, 3, VertexBuffer.Format.Float, vfb);
399- nb.setupData(VertexBuffer.Usage.Static, 3, VertexBuffer.Format.Float, nfb);
308+ vb.setupData(VertexBuffer.Usage.Static, 3, VertexBuffer.Format.Float, md.vfb);
309+ nb.setupData(VertexBuffer.Usage.Static, 3, VertexBuffer.Format.Float, md.nfb);
400310
401311 // bvb.setupData(VertexBuffer.Usage.CpuOnly, 3, VertexBuffer.Format.Float, bvfb);
402312 // bnb.setupData(VertexBuffer.Usage.CpuOnly, 3, VertexBuffer.Format.Float, bnfb);
403313 if (textureFlag) {
404- tb.setupData(VertexBuffer.Usage.Static, 2, VertexBuffer.Format.Float, tfb);
314+ tb.setupData(VertexBuffer.Usage.Static, 2, VertexBuffer.Format.Float, md.tfb);
405315 }
406- wb.setupData(VertexBuffer.Usage.Static, 2, VertexBuffer.Format.Float, wfb);
407- ib.setupData(VertexBuffer.Usage.Static, 1, VertexBuffer.Format.UnsignedShort, isb);
408- bib.setupData(VertexBuffer.Usage.Static, 2, VertexBuffer.Format.UnsignedShort, bisb);
316+ wb.setupData(VertexBuffer.Usage.Static, 2, VertexBuffer.Format.Float, md.wfb);
317+ ib.setupData(VertexBuffer.Usage.Static, 1, VertexBuffer.Format.UnsignedShort, md.isb);
318+ bib.setupData(VertexBuffer.Usage.Static, 2, VertexBuffer.Format.UnsignedShort, md.bisb);
409319 mesh.setBuffer(vb);
410320 mesh.setBuffer(nb);
411321
@@ -420,18 +330,9 @@
420330 mesh.setBuffer(wb);
421331 mesh.setBuffer(ib);
422332 mesh.setBuffer(bib);
423- int[] indexArray = new int[md.getBoneList().size()];
424- ShortBuffer indexBuffer = BufferUtils.createShortBuffer(md.getBoneList().size());
425- for (int i = 0; i < indexArray.length; i++) {
426- if (i < md.getBoneList().size()) {
427- indexArray[i] = md.getBoneList().get(i).shortValue();
428- } else {
429- indexArray[i] = 0;
430- }
431- indexBuffer.put((short)indexArray[i]);
432- }
333+ int[] indexArray = md.indexArray;
433334 mesh.setBoneIndexArray(indexArray);
434- mesh.setBoneIndexBuffer(indexBuffer);
335+ mesh.setBoneIndexBuffer(md.indexBuffer);
435336 FloatBuffer boneMatrixBuffer = BufferUtils.createFloatBuffer(16 * indexArray.length);
436337 mesh.setBoneMatrixArray(new Matrix4f[indexArray.length]);
437338 mesh.setBoneMatrixBuffer(boneMatrixBuffer);
@@ -511,7 +412,7 @@
511412 mat.setFloat("Shininess", m.getMaterial().getPower());
512413 if (m.getTextureFileName().length() > 0) {
513414 StringTokenizer st = new StringTokenizer(m.getTextureFileName().replace("\\", "/"), "*");
514- System.out.println("m.getTextureFileName() = " + m.getTextureFileName());
415+// System.out.println("m.getTextureFileName() = " + m.getTextureFileName());
515416 while (st.hasMoreElements()) {
516417 String fileName = st.nextToken();
517418 // System.out.println("fileName = " + fileName);
@@ -725,7 +626,13 @@
725626 for(;;) {
726627 try {
727628 PMDLoaderGLSLSkinning2 loader = new PMDLoaderGLSLSkinning2();
728- return loader.load2(ai);
629+ Object result;
630+ if (ai.getKey().getName().toLowerCase().endsWith(".pmd")) {
631+ result = loader.load2(ai);
632+ } else {
633+ result = loader.load3(ai);
634+ }
635+ return result;
729636 }catch(OutOfMemoryError ex) {
730637 if (errFlag) {
731638 throw ex;
@@ -749,18 +656,40 @@
749656 // model.setVertexList(null);
750657 model.setFaceVertIndex(null);
751658 PMDNode pmdNode = createNode(ai.getKey().getName());
752- if (JmeSystem.getFullName().indexOf("Android") == -1) {
753- try {
754- String vendor = GL11.glGetString(GL11.GL_VENDOR);
755- if (vendor != null && vendor.toLowerCase().contains("intel")) {
756- pmdNode.setGlslSkinning(false);
757- } else {
758- pmdNode.setGlslSkinning(true);
759- }
760- } catch(Exception ex) {
761- pmdNode.setGlslSkinning(false);
762- }
763- }
659+// if (JmeSystem.getFullName().indexOf("Android") == -1) {
660+// try {
661+// String vendor = GL11.glGetString(GL11.GL_VENDOR);
662+// if (vendor != null && vendor.toLowerCase().contains("intel")) {
663+// pmdNode.setGlslSkinning(false);
664+// } else {
665+// pmdNode.setGlslSkinning(true);
666+// }
667+// } catch(Exception ex) {
668+// pmdNode.setGlslSkinning(false);
669+// }
670+// }
671+ return pmdNode;
672+ }
673+ private Object load3(AssetInfo ai) throws IOException {
674+ this.assetManager = ai.getManager();
675+ folderName = ai.getKey().getFolder();
676+ InputStream is = ai.openStream();
677+ meshConverter = PMDFileUtil.readPMDCache1(is);
678+ is.close();
679+ model = meshConverter.getModel();
680+ PMDNode pmdNode = createNode(ai.getKey().getName());
681+// if (JmeSystem.getFullName().indexOf("Android") == -1) {
682+// try {
683+// String vendor = GL11.glGetString(GL11.GL_VENDOR);
684+// if (vendor != null && vendor.toLowerCase().contains("intel")) {
685+// pmdNode.setGlslSkinning(false);
686+// } else {
687+// pmdNode.setGlslSkinning(true);
688+// }
689+// } catch(Exception ex) {
690+// pmdNode.setGlslSkinning(false);
691+// }
692+// }
764693 return pmdNode;
765694 }
766695
@@ -780,7 +709,7 @@
780709 }
781710 return tex;
782711 } catch(Exception ex) {
783- logger.log(Level.WARNING,"Txture "+name+" not found.");
712+ logger.log(Level.FINE,"Txture "+name+" not found.");
784713 return null;
785714 }
786715 }
diff -r c6451e912509 -r ad69521155ab src/projectkyoto/jme3/mmd/PMDNode.java
--- a/src/projectkyoto/jme3/mmd/PMDNode.java Thu Oct 11 15:17:57 2012 +0900
+++ b/src/projectkyoto/jme3/mmd/PMDNode.java Wed Oct 17 17:21:47 2012 +0900
@@ -88,13 +88,7 @@
8888 PMDGeometry[] pmdGeometryArray;
8989 Map<String, Skin> skinMap = new HashMap<String, Skin>();
9090 Skin[] skinArray = new Skin[0];
91-// javax.vecmath.Vector3f skinPosArray[];
92-// javax.vecmath.Vector3f skinNormalArray[];
93- javax.vecmath.Vector3f skinPosArrayOrig[];
94-// javax.vecmath.Vector3f skinNormalArrayOrig[];
9591 FloatBuffer skinPosBuffer;
96-// float skinBoneWeightArray[];
97-// int skinBoneArray[];
9892 AssetManager assetManager;
9993 Matrix4f[] offsetMatrices;
10094 FloatBuffer offsetMatrixbuffer;
@@ -244,23 +238,6 @@
244238 for (Skin skin : skinAray) {
245239 skinMap.put(skin.getSkinName(), skin);
246240 }
247- int skinVertSize = skinVertexList.size();
248-// skinPosArray = new javax.vecmath.Vector3f[skinVertSize];
249-// skinNormalArray = new javax.vecmath.Vector3f[skinVertSize];
250- skinPosArrayOrig = new javax.vecmath.Vector3f[skinVertSize];
251-// skinNormalArrayOrig = new javax.vecmath.Vector3f[skinVertSize];
252-// skinBoneWeightArray = new float[skinVertSize];
253-// skinBoneArray = new int[skinVertSize * 2];
254- for (int i = 0; i < skinVertSize; i++) {
255- PMDVertex v = skinVertexList.get(i);
256- skinPosArrayOrig[i] = v.getPos();
257-// skinPosArray[i] = new javax.vecmath.Vector3f(v.getPos());
258-// skinNormalArrayOrig[i] = v.getNormal();
259-// skinNormalArray[i] = new javax.vecmath.Vector3f(v.getNormal());
260-// skinBoneWeightArray[i] = (float) v.getBoneWeight() / 100f;
261-// skinBoneArray[i * 2] = v.getBoneNum1();
262-// skinBoneArray[i * 2 + 1] = v.getBoneNum2();
263- }
264241 }
265242
266243 public PMDModel getPmdModel() {
diff -r c6451e912509 -r ad69521155ab src/projectkyoto/mmd/file/Coords2d.java
--- a/src/projectkyoto/mmd/file/Coords2d.java Thu Oct 11 15:17:57 2012 +0900
+++ b/src/projectkyoto/mmd/file/Coords2d.java Wed Oct 17 17:21:47 2012 +0900
@@ -31,6 +31,8 @@
3131 */
3232 package projectkyoto.mmd.file;
3333
34+import java.io.DataInput;
35+import java.io.DataOutput;
3436 import java.io.IOException;
3537 import java.io.Serializable;
3638 import java.nio.ByteBuffer;
@@ -54,15 +56,19 @@
5456 this.u = u;
5557 this.v = v;
5658 }
57- public Coords2d(DataInputStreamLittleEndian is) throws IOException {
59+ public Coords2d(DataInput is) throws IOException {
5860 u = is.readFloat();
5961 v = is.readFloat();
6062 }
61- public Coords2d readFromStream(DataInputStreamLittleEndian is) throws IOException {
63+ public Coords2d readFromStream(DataInput is) throws IOException {
6264 u = is.readFloat();
6365 v = is.readFloat();
6466 return this;
6567 }
68+ public void writeToStream(DataOutput os) throws IOException {
69+ os.writeFloat(u);
70+ os.writeFloat(v);
71+ }
6672 public Coords2d readFromBuffer(ByteBuffer bb) {
6773 u = bb.getFloat();
6874 v = bb.getFloat();
diff -r c6451e912509 -r ad69521155ab src/projectkyoto/mmd/file/DataInputStreamLittleEndian.java
--- a/src/projectkyoto/mmd/file/DataInputStreamLittleEndian.java Thu Oct 11 15:17:57 2012 +0900
+++ b/src/projectkyoto/mmd/file/DataInputStreamLittleEndian.java Wed Oct 17 17:21:47 2012 +0900
@@ -32,6 +32,7 @@
3232 package projectkyoto.mmd.file;
3333
3434 import java.io.BufferedInputStream;
35+import java.io.DataInput;
3536 import java.io.DataInputStream;
3637 import java.io.File;
3738 import java.io.FileInputStream;
@@ -45,39 +46,49 @@
4546 *
4647 * @author Kazuhiko Kobayashi
4748 */
48-public class DataInputStreamLittleEndian extends FilterInputStream {
49+public class DataInputStreamLittleEndian extends FilterInputStream implements DataInput {
4950 // public URL url;
51+
5052 private DataInputStream dis;
5153 byte[] buf;
5254
5355 public DataInputStreamLittleEndian(InputStream in) {
5456 super(in);
55- dis = new DataInputStream(this);
57+ dis = new DataInputStream(in);
5658 }
59+
5760 public DataInputStreamLittleEndian(URL url) throws IOException {
5861 super(new BufferedInputStream(url.openStream()));
5962 // this.url = url;
6063 dis = new DataInputStream(this);
6164 }
65+
6266 final byte[] getBuf(int size) {
6367 if (buf == null || buf.length < size) {
6468 buf = new byte[size];
6569 }
6670 return buf;
6771 }
72+
73+ @Override
6874 public final int readInt() throws IOException {
6975 return Integer.reverseBytes(dis.readInt());
7076 }
7177
78+ @Override
7279 public final short readShort() throws IOException {
7380 return Short.reverseBytes(dis.readShort());
7481 }
82+
83+ @Override
7584 public final int readUnsignedShort() throws IOException {
7685 short shortValue = readShort();
7786 int intValue = shortValue;
7887 intValue = intValue & 0xffff;
7988 return intValue;
8089 }
90+
91+ @Override
8192 public final int readUnsignedByte() throws IOException {
8293 byte byteValue = readByte();
8394 int intValue = byteValue;
@@ -85,30 +96,84 @@
8596 return intValue;
8697 }
8798
99+ @Override
88100 public final long readLong() throws IOException {
89101 return Long.reverseBytes(dis.readLong());
90102 }
91103
104+ @Override
92105 public final float readFloat() throws IOException {
93106 return Float.intBitsToFloat(readInt());
94107 }
95108
109+ @Override
96110 public final double readDouble() throws IOException {
97111 return Double.longBitsToDouble(readLong());
98112 }
113+
99114 public final String readString(int size) throws IOException {
100115 byte[] buf = getBuf(size);
101- read(buf,0,size);
102- for(int i=0;i<size;i++) {
116+ read(buf, 0, size);
117+ for (int i = 0; i < size; i++) {
103118 if (buf[i] == 0) {
104- return new String(buf,0,i,"Shift_JIS").intern();
119+ return new String(buf, 0, i, "Shift_JIS").intern();
105120 }
106121 }
107- return new String(buf,"Shift_JIS").intern();
122+ return new String(buf, 0, size, "Shift_JIS").intern();
108123 }
109- public final byte readByte() throws IOException{
124+
125+ @Override
126+ public final byte readByte() throws IOException {
110127 byte[] buf = getBuf(1);
111- read(buf,0,1);
128+ read(buf, 0, 1);
112129 return buf[0];
113130 }
131+
132+ @Override
133+ public long skip(long l) throws IOException {
134+ long l2 = l;
135+ while (l2 > 0) {
136+ long l3 = dis.skip(l2);
137+ l2 = l2 - l3;
138+ if (l3 == 0) {
139+ break;
140+ }
141+ }
142+ return l;
143+ }
144+
145+ @Override
146+ public void readFully(byte[] bytes) throws IOException {
147+ dis.readFully(bytes);
148+ }
149+
150+ @Override
151+ public void readFully(byte[] bytes, int i, int i1) throws IOException {
152+ dis.readFully(bytes, i, i1);
153+ }
154+
155+ @Override
156+ public int skipBytes(int i) throws IOException {
157+ return dis.skipBytes(i);
158+ }
159+
160+ @Override
161+ public boolean readBoolean() throws IOException {
162+ return dis.readBoolean();
163+ }
164+
165+ @Override
166+ public char readChar() throws IOException {
167+ return dis.readChar();
168+ }
169+
170+ @Override
171+ public String readLine() throws IOException {
172+ return dis.readLine();
173+ }
174+
175+ @Override
176+ public String readUTF() throws IOException {
177+ return dis.readUTF();
178+ }
114179 }
diff -r c6451e912509 -r ad69521155ab src/projectkyoto/mmd/file/DataOutputStreamLittleEndian.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/projectkyoto/mmd/file/DataOutputStreamLittleEndian.java Wed Oct 17 17:21:47 2012 +0900
@@ -0,0 +1,88 @@
1+/*
2+ * To change this template, choose Tools | Templates
3+ * and open the template in the editor.
4+ */
5+package projectkyoto.mmd.file;
6+
7+import java.io.DataOutput;
8+import java.io.DataOutputStream;
9+import java.io.FilterOutputStream;
10+import java.io.IOException;
11+import java.io.OutputStream;
12+
13+/**
14+ *
15+ * @author kobayasi
16+ */
17+public class DataOutputStreamLittleEndian extends FilterOutputStream implements DataOutput{
18+ DataOutputStream dos;
19+ public DataOutputStreamLittleEndian(OutputStream out) {
20+ super(out);
21+ dos = new DataOutputStream(out);
22+ }
23+
24+ @Override
25+ public void writeBoolean(boolean bln) throws IOException {
26+ dos.writeBoolean(bln);
27+ }
28+
29+ @Override
30+ public void writeByte(int i) throws IOException {
31+ dos.writeByte(i);
32+ }
33+
34+ @Override
35+ public void writeShort(int i) throws IOException {
36+ dos.writeShort(Short.reverseBytes((short)i));
37+ }
38+
39+ @Override
40+ public void writeChar(int i) throws IOException {
41+ dos.writeChar(i);
42+ }
43+
44+ @Override
45+ public void writeInt(int i) throws IOException {
46+ dos.writeInt(Integer.reverseBytes(i));
47+ }
48+
49+ @Override
50+ public void writeLong(long l) throws IOException {
51+ dos.writeLong(Long.reverseBytes(l));
52+ }
53+
54+ @Override
55+ public void writeFloat(float f) throws IOException {
56+ writeInt(Float.floatToIntBits(f));
57+ }
58+
59+ @Override
60+ public void writeDouble(double d) throws IOException {
61+ writeLong(Double.doubleToLongBits(d));
62+ }
63+
64+ @Override
65+ public void writeBytes(String string) throws IOException {
66+ dos.writeBytes(string);
67+ }
68+
69+ @Override
70+ public void writeChars(String string) throws IOException {
71+ dos.writeChars(string);
72+ }
73+
74+ @Override
75+ public void writeUTF(String string) throws IOException {
76+ dos.writeUTF(string);
77+ }
78+
79+ @Override
80+ public void flush() throws IOException {
81+ dos.flush();
82+ }
83+
84+ @Override
85+ public void close() throws IOException {
86+ dos.close();
87+ }
88+}
diff -r c6451e912509 -r ad69521155ab src/projectkyoto/mmd/file/PMDBone.java
--- a/src/projectkyoto/mmd/file/PMDBone.java Thu Oct 11 15:17:57 2012 +0900
+++ b/src/projectkyoto/mmd/file/PMDBone.java Wed Oct 17 17:21:47 2012 +0900
@@ -31,6 +31,7 @@
3131 */
3232 package projectkyoto.mmd.file;
3333
34+import java.io.DataOutput;
3435 import java.io.IOException;
3536 import java.io.Serializable;
3637 import java.nio.ByteBuffer;
@@ -67,6 +68,9 @@
6768 public PMDBone() {
6869 }
6970 public PMDBone(DataInputStreamLittleEndian is) throws IOException {
71+ readFromStream(is);
72+ }
73+ public PMDBone readFromStream(DataInputStreamLittleEndian is) throws IOException {
7074 boneName = is.readString(20);
7175 parentBoneIndex = is.readUnsignedShort();
7276 tailPosBoneIndex = is.readUnsignedShort();
@@ -79,6 +83,17 @@
7983 } else {
8084 hiza = false;
8185 }
86+ return this;
87+ }
88+ public void writeToStream(DataOutput os) throws IOException {
89+ PMDUtil.writeString(os, boneName, 20);
90+ os.writeShort(parentBoneIndex);
91+ os.writeShort(tailPosBoneIndex);
92+ os.writeByte(boneType);
93+ os.writeShort(targetBone);
94+ os.writeFloat(boneHeadPos.x);
95+ os.writeFloat(boneHeadPos.y);
96+ os.writeFloat(-boneHeadPos.z);
8297 }
8398
8499 public void readFromBuffer(ByteBuffer bb) {
diff -r c6451e912509 -r ad69521155ab src/projectkyoto/mmd/file/PMDBoneDisp.java
--- a/src/projectkyoto/mmd/file/PMDBoneDisp.java Thu Oct 11 15:17:57 2012 +0900
+++ b/src/projectkyoto/mmd/file/PMDBoneDisp.java Wed Oct 17 17:21:47 2012 +0900
@@ -32,6 +32,7 @@
3232
3333 package projectkyoto.mmd.file;
3434
35+import java.io.DataOutput;
3536 import java.io.IOException;
3637 import java.io.Serializable;
3738 import java.nio.ByteBuffer;
@@ -49,6 +50,10 @@
4950 boneIndex = is.readUnsignedShort();
5051 boneDispFrameIndex = is.readUnsignedByte();
5152 }
53+ public void writeToStream(DataOutput os) throws IOException {
54+ os.writeShort(boneIndex);
55+ os.writeByte(boneDispFrameIndex);
56+ }
5257 public void readFromBuffer(ByteBuffer bb) {
5358 boneIndex = bb.getShort();
5459 boneDispFrameIndex = bb.get();
diff -r c6451e912509 -r ad69521155ab src/projectkyoto/mmd/file/PMDBoneDispList.java
--- a/src/projectkyoto/mmd/file/PMDBoneDispList.java Thu Oct 11 15:17:57 2012 +0900
+++ b/src/projectkyoto/mmd/file/PMDBoneDispList.java Wed Oct 17 17:21:47 2012 +0900
@@ -32,6 +32,7 @@
3232
3333 package projectkyoto.mmd.file;
3434
35+import java.io.DataOutput;
3536 import java.io.IOException;
3637 import java.io.Serializable;
3738 import java.nio.ByteBuffer;
@@ -53,6 +54,12 @@
5354 boneDispArray[i] = new PMDBoneDisp(is);
5455 }
5556 }
57+ public void writeToStream(DataOutput os) throws IOException {
58+ os.writeInt(boneDispCount);
59+ for(int i=0;i<boneDispCount;i++) {
60+ boneDispArray[i].writeToStream(os);
61+ }
62+ }
5663 public void readFromBuffer(ByteBuffer bb) {
5764 boneDispCount = bb.getInt();
5865 boneDispArray = new PMDBoneDisp[boneDispCount];
diff -r c6451e912509 -r ad69521155ab src/projectkyoto/mmd/file/PMDBoneDispNameList.java
--- a/src/projectkyoto/mmd/file/PMDBoneDispNameList.java Thu Oct 11 15:17:57 2012 +0900
+++ b/src/projectkyoto/mmd/file/PMDBoneDispNameList.java Wed Oct 17 17:21:47 2012 +0900
@@ -32,6 +32,7 @@
3232
3333 package projectkyoto.mmd.file;
3434
35+import java.io.DataOutput;
3536 import java.io.IOException;
3637 import java.io.Serializable;
3738 import java.nio.ByteBuffer;
@@ -54,6 +55,12 @@
5455 dispNameArray[i] = is.readString(50);
5556 }
5657 }
58+ public void writeToStream(DataOutput os) throws IOException {
59+ os.writeByte(boneDispNameCount);
60+ for(String dispName : dispNameArray) {
61+ PMDUtil.writeString(os, dispName, 50);
62+ }
63+ }
5764 public void readFromBuffer(ByteBuffer bb) {
5865 boneDispNameCount = bb.get();
5966 dispNameArray = new String[boneDispNameCount];
diff -r c6451e912509 -r ad69521155ab src/projectkyoto/mmd/file/PMDBoneList.java
--- a/src/projectkyoto/mmd/file/PMDBoneList.java Thu Oct 11 15:17:57 2012 +0900
+++ b/src/projectkyoto/mmd/file/PMDBoneList.java Wed Oct 17 17:21:47 2012 +0900
@@ -32,6 +32,7 @@
3232
3333 package projectkyoto.mmd.file;
3434
35+import java.io.DataOutput;
3536 import java.io.IOException;
3637 import java.io.Serializable;
3738
@@ -49,6 +50,12 @@
4950 bones[i] = new PMDBone(is);
5051 }
5152 }
53+ public void writeToStream(DataOutput os) throws IOException {
54+ os.writeShort(boneCount);
55+ for(PMDBone bone : bones) {
56+ bone.writeToStream(os);
57+ }
58+ }
5259 @Override
5360 public String toString() {
5461 StringBuffer sb = new StringBuffer();
diff -r c6451e912509 -r ad69521155ab src/projectkyoto/mmd/file/PMDHeaderEnglish.java
--- a/src/projectkyoto/mmd/file/PMDHeaderEnglish.java Thu Oct 11 15:17:57 2012 +0900
+++ b/src/projectkyoto/mmd/file/PMDHeaderEnglish.java Wed Oct 17 17:21:47 2012 +0900
@@ -29,9 +29,9 @@
2929 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
3030 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3131 */
32-
3332 package projectkyoto.mmd.file;
3433
34+import java.io.DataOutput;
3535 import java.io.IOException;
3636 import java.io.Serializable;
3737
@@ -39,7 +39,7 @@
3939 *
4040 * @author kobayasi
4141 */
42-public class PMDHeaderEnglish implements Serializable{
42+public class PMDHeaderEnglish implements Serializable {
4343
4444 private int englishNameCompatibility;
4545 private String modelName;
@@ -51,8 +51,8 @@
5151 public PMDHeaderEnglish(PMDModel model, DataInputStreamLittleEndian is) throws IOException {
5252 englishNameCompatibility = is.readUnsignedByte();
5353 if (englishNameCompatibility == 1) {
54- modelName = is.readString(20);
55- comment = is.readString(256);
54+ modelName = is.readString(20);
55+ comment = is.readString(256);
5656 boneNameEnglish = new String[model.getBoneList().getBoneCount()];
5757 for (int i = 0; i < boneNameEnglish.length; i++) {
5858 boneNameEnglish[i] = is.readString(20);
@@ -63,12 +63,29 @@
6363 }
6464
6565 dispNameEnglish = new String[model.getBoneDispNameList().getBoneDispNameCount()];
66- for(int i=0;i<dispNameEnglish.length;i++) {
66+ for (int i = 0; i < dispNameEnglish.length; i++) {
6767 dispNameEnglish[i] = is.readString(50);
6868 }
6969 }
7070 }
7171
72+ public void writeToStream(DataOutput os) throws IOException {
73+ os.writeByte(englishNameCompatibility);
74+ if (englishNameCompatibility == 1) {
75+ PMDUtil.writeString(os, modelName, 20);
76+ PMDUtil.writeString(os, comment, 256);
77+ for(String boneName : boneNameEnglish) {
78+ PMDUtil.writeString(os, boneName, 20);
79+ }
80+ for(String skinName : skinNameEnglish) {
81+ PMDUtil.writeString(os, skinName, 20);
82+ }
83+ for(String dispName : dispNameEnglish) {
84+ PMDUtil.writeString(os, dispName, 50);
85+ }
86+ }
87+ }
88+
7289 @Override
7390 public String toString() {
7491 StringBuilder sb = new StringBuilder();
@@ -79,19 +96,19 @@
7996 sb.append("modelName = ").append(modelName).append('\n');
8097 sb.append("comment = ").append(comment).append('\n');
8198 sb.append("boneNameEnglish = ").append("{\n");
82- for(int i=0;i<boneNameEnglish.length;i++) {
99+ for (int i = 0; i < boneNameEnglish.length; i++) {
83100 sb.append(i);
84101 sb.append(" ").append(boneNameEnglish[i]).append('\n');
85102 }
86103 sb.append("}\n");
87104 sb.append("skinNameEnglish = ").append("{\n");
88- for(int i=0;i<skinNameEnglish.length;i++) {
105+ for (int i = 0; i < skinNameEnglish.length; i++) {
89106 sb.append(i);
90107 sb.append(" ").append(skinNameEnglish[i]).append('\n');
91108 }
92109 sb.append("}\n");
93110 sb.append("dispNameEnglish = ").append("{\n");
94- for(int i=0;i<dispNameEnglish.length;i++) {
111+ for (int i = 0; i < dispNameEnglish.length; i++) {
95112 sb.append(i);
96113 sb.append(" ").append(dispNameEnglish[i]).append('\n');
97114 }
diff -r c6451e912509 -r ad69521155ab src/projectkyoto/mmd/file/PMDIKData.java
--- a/src/projectkyoto/mmd/file/PMDIKData.java Thu Oct 11 15:17:57 2012 +0900
+++ b/src/projectkyoto/mmd/file/PMDIKData.java Wed Oct 17 17:21:47 2012 +0900
@@ -29,9 +29,9 @@
2929 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
3030 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3131 */
32-
3332 package projectkyoto.mmd.file;
3433
34+import java.io.DataOutput;
3535 import java.io.IOException;
3636 import java.io.Serializable;
3737
@@ -39,14 +39,15 @@
3939 *
4040 * @author kobayasi
4141 */
42-public class PMDIKData implements Serializable{
42+public class PMDIKData implements Serializable {
43+
4344 private int ikBoneIndex;
4445 private int ikTargetBoneIndex;
4546 private int ikChainLength;
4647 private int iterations;
4748 private float controlWeight;
4849 private int[] ikChildBoneIndex;
49-
50+
5051 public PMDIKData(DataInputStreamLittleEndian is) throws IOException {
5152 ikBoneIndex = is.readUnsignedShort();
5253 ikTargetBoneIndex = is.readUnsignedShort();
@@ -54,22 +55,33 @@
5455 iterations = is.readShort();
5556 controlWeight = is.readFloat();
5657 ikChildBoneIndex = new int[ikChainLength];
57- for(int i=0;i<ikChainLength;i++) {
58+ for (int i = 0; i < ikChainLength; i++) {
5859 ikChildBoneIndex[i] = is.readUnsignedShort();
5960 }
6061 }
6162
63+ public void writeToStream(DataOutput os) throws IOException {
64+ os.writeShort(ikBoneIndex);
65+ os.writeShort(ikTargetBoneIndex);
66+ os.writeByte(ikChainLength);
67+ os.writeShort(iterations);
68+ os.writeFloat(controlWeight);
69+ for (int s : ikChildBoneIndex) {
70+ os.writeShort(s);
71+ }
72+ }
73+
6274 @Override
6375 public String toString() {
6476 StringBuffer sb = new StringBuffer();
65- sb.append("{ikBoneIndex = "+ikBoneIndex);
66- sb.append("\n").append("ikTargetBoneIndex = "+ikTargetBoneIndex);
67- sb.append(" ikChainLength = "+ikChainLength);
68- sb.append(" \niterations = "+iterations);
69- sb.append("\ncontrolWeight = "+controlWeight);
77+ sb.append("{ikBoneIndex = " + ikBoneIndex);
78+ sb.append("\n").append("ikTargetBoneIndex = " + ikTargetBoneIndex);
79+ sb.append(" ikChainLength = " + ikChainLength);
80+ sb.append(" \niterations = " + iterations);
81+ sb.append("\ncontrolWeight = " + controlWeight);
7082 sb.append("\n{");
71- for(int i=0;i<ikChainLength;i++) {
72- sb.append("ikChildBoneIndex = "+ikChildBoneIndex[i]);
83+ for (int i = 0; i < ikChainLength; i++) {
84+ sb.append("ikChildBoneIndex = " + ikChildBoneIndex[i]);
7385 }
7486 sb.append("}");
7587 return sb.toString();
@@ -122,5 +134,4 @@
122134 public void setIterations(int iterations) {
123135 this.iterations = iterations;
124136 }
125-
126137 }
diff -r c6451e912509 -r ad69521155ab src/projectkyoto/mmd/file/PMDIKList.java
--- a/src/projectkyoto/mmd/file/PMDIKList.java Thu Oct 11 15:17:57 2012 +0900
+++ b/src/projectkyoto/mmd/file/PMDIKList.java Wed Oct 17 17:21:47 2012 +0900
@@ -32,6 +32,7 @@
3232
3333 package projectkyoto.mmd.file;
3434
35+import java.io.DataOutput;
3536 import java.io.IOException;
3637 import java.io.Serializable;
3738
@@ -50,6 +51,12 @@
5051 pmdIKData[i] = new PMDIKData(is);
5152 }
5253 }
54+ public void writeToStream(DataOutput os) throws IOException {
55+ os.writeShort(ikDataCount);
56+ for(PMDIKData ikData : pmdIKData) {
57+ ikData.writeToStream(os);
58+ }
59+ }
5360
5461 @Override
5562 public String toString() {
diff -r c6451e912509 -r ad69521155ab src/projectkyoto/mmd/file/PMDJoint.java
--- a/src/projectkyoto/mmd/file/PMDJoint.java Thu Oct 11 15:17:57 2012 +0900
+++ b/src/projectkyoto/mmd/file/PMDJoint.java Wed Oct 17 17:21:47 2012 +0900
@@ -31,6 +31,7 @@
3131 */
3232 package projectkyoto.mmd.file;
3333
34+import java.io.DataOutput;
3435 import java.io.IOException;
3536 import java.io.Serializable;
3637 import javax.vecmath.Vector3f;
@@ -102,55 +103,42 @@
102103 constRot2 = new Vector3f(-is.readFloat(), -is.readFloat(), is.readFloat());
103104
104105 float tmp;
105-// if (constPos1.x > constPos2.x) {
106-// tmp = constPos1.x;
107-// constPos1.x = constPos2.x;
108-// constPos2.x = tmp;
109-// }
110-// if (constPos1.y > constPos2.y) {
111-// tmp = constPos1.y;
112-// constPos1.y = constPos2.y;
113-// constPos2.y = tmp;
114-// }
115-// if (constPos1.z > constPos2.z) {
116-// tmp = constPos1.z;
117-// constPos1.z = constPos2.z;
118-// constPos2.z = tmp;
119-// }
120-
121-// float tmp;
122106 tmp = constPos1.z;
123107 constPos1.z = constPos2.z;
124108 constPos2.z = tmp;
125-//
126-// tmp = constRot1.x;
127-// constRot1.x = -constRot2.x;
128-// constRot2.x = -tmp;
129-// tmp = constRot1.y;
130-// constRot1.y = -constRot2.y;
131-// constRot2.y = -tmp;
132-//
133-// Vector3f tmpV = constRot1;
134-// constRot1 = constRot2;
135-// constRot1 = constRot2;
136-// constRot2 = tmpV;
137-// constRot1.x *= -1f;
138-// constRot2.x *= -1f;
139-// constRot1.y *= -1f;
140-// constRot2.y *= -1f;
141109 swapConst(constRot1, constRot2);
142-
143- // tmp = constRot1.z;
144- // constRot1.z = -constRot2.z;
145- // constRot2.z = -tmp;
146- // springPos = new Vector3f(is.readFloat(), is.readFloat(), -is.readFloat());
147- // springRot = new Vector3f(-is.readFloat(), -is.readFloat(), is.readFloat());
148110 for (int i = 0; i < 6; i++) {
149111 stiffness[i] = is.readFloat();
150112 }
151-// stiffness[2] = -stiffness[2];
152-// stiffness[3] = -stiffness[3];
153-// stiffness[4] = -stiffness[4];
113+ }
114+ public void writeToStream(DataOutput os) throws IOException {
115+ PMDUtil.writeString(os, jointName, 20);
116+ os.writeInt(rigidBodyA);
117+ os.writeInt(rigidBodyB);
118+ PMDUtil.writeVector3f(os, jointPos);
119+
120+ os.writeFloat(-jointRot.x);
121+ os.writeFloat(-jointRot.y);
122+ os.writeFloat(jointRot.z);
123+
124+ os.writeFloat(constPos1.x);
125+ os.writeFloat(constPos1.y);
126+ os.writeFloat(-constPos2.z);
127+
128+ os.writeFloat(constPos2.x);
129+ os.writeFloat(constPos2.y);
130+ os.writeFloat(-constPos1.z);
131+
132+ os.writeFloat(-constRot2.x);
133+ os.writeFloat(-constRot2.y);
134+ os.writeFloat(constRot1.z);
135+
136+ os.writeFloat(-constRot1.x);
137+ os.writeFloat(-constRot1.y);
138+ os.writeFloat(constRot2.z);
139+ for(float f : stiffness) {
140+ os.writeFloat(f);
141+ }
154142 }
155143
156144 @Override
diff -r c6451e912509 -r ad69521155ab src/projectkyoto/mmd/file/PMDJointList.java
--- a/src/projectkyoto/mmd/file/PMDJointList.java Thu Oct 11 15:17:57 2012 +0900
+++ b/src/projectkyoto/mmd/file/PMDJointList.java Wed Oct 17 17:21:47 2012 +0900
@@ -32,6 +32,7 @@
3232
3333 package projectkyoto.mmd.file;
3434
35+import java.io.DataOutput;
3536 import java.io.IOException;
3637 import java.io.Serializable;
3738
@@ -55,7 +56,12 @@
5556 jointArray[i] = new PMDJoint(is);
5657 }
5758 }
58-
59+ public void writeToStream(DataOutput os) throws IOException {
60+ os.writeInt(jointCount);
61+ for(PMDJoint joint : jointArray) {
62+ joint.writeToStream(os);
63+ }
64+ }
5965 @Override
6066 public String toString() {
6167 StringBuilder sb = new StringBuilder();
diff -r c6451e912509 -r ad69521155ab src/projectkyoto/mmd/file/PMDMaterial.java
--- a/src/projectkyoto/mmd/file/PMDMaterial.java Thu Oct 11 15:17:57 2012 +0900
+++ b/src/projectkyoto/mmd/file/PMDMaterial.java Wed Oct 17 17:21:47 2012 +0900
@@ -32,6 +32,7 @@
3232 package projectkyoto.mmd.file;
3333
3434 import java.io.ByteArrayOutputStream;
35+import java.io.DataOutput;
3536 import java.io.IOException;
3637 import java.io.InputStream;
3738 import java.io.Serializable;
@@ -47,6 +48,7 @@
4748 private byte edgeFlag;
4849 private int faceVertCount;
4950 private String textureFileName; // 20文字
51+ private int materialNo;
5052 // private byte[] textureData;
5153
5254 public PMDMaterial() {
@@ -57,33 +59,13 @@
5759 edgeFlag = is.readByte();
5860 faceVertCount = is.readInt();
5961 textureFileName = is.readString(20);
60-// if (textureFileName.length() != 0) {
61-// texture = TextureIO.newTexture(new URL(is.url ,textureFileName), true,"bmp");
62-// }
63-// if ( false && !textureFileName.isEmpty()) {
64-// InputStream textureIs = null;
65-// try {
66-// textureIs = new URL(is.url ,textureFileName).openStream();
67-// ByteArrayOutputStream os = new ByteArrayOutputStream();
68-// byte[] buf = new byte[4096];
69-// for(;;) {
70-// int size = textureIs.read(buf);
71-// if (size <= 0) {
72-// break;
73-// }
74-// os.write(buf,0,size);
75-// }
76-// os.close();
77-// textureData = os.toByteArray();
78-// } catch(IOException ex) {
79-// ex.printStackTrace();
80-// } finally {
81-// if (textureIs != null) {
82-// textureIs.close();
83-// textureIs = null;
84-// }
85-// }
86-// }
62+ }
63+ public void writeToStream(DataOutput os) throws IOException {
64+ material.writeToStream(os);
65+ os.writeByte(toonIndex);
66+ os.writeByte(edgeFlag);
67+ os.writeInt(faceVertCount);
68+ PMDUtil.writeString(os, textureFileName, 20);
8769 }
8870
8971 public byte getEdgeFlag() {
@@ -126,6 +108,14 @@
126108 this.toonIndex = toonIndex;
127109 }
128110
111+ public int getMaterialNo() {
112+ return materialNo;
113+ }
114+
115+ public void setMaterialNo(int materialNo) {
116+ this.materialNo = materialNo;
117+ }
118+
129119 // public byte[] getTextureData() {
130120 // return textureData;
131121 // }
diff -r c6451e912509 -r ad69521155ab src/projectkyoto/mmd/file/PMDModel.java
--- a/src/projectkyoto/mmd/file/PMDModel.java Thu Oct 11 15:17:57 2012 +0900
+++ b/src/projectkyoto/mmd/file/PMDModel.java Wed Oct 17 17:21:47 2012 +0900
@@ -30,6 +30,7 @@
3030 package projectkyoto.mmd.file;
3131
3232 import java.io.BufferedInputStream;
33+import java.io.DataOutput;
3334 import java.io.EOFException;
3435 import java.io.File;
3536 import java.io.FileOutputStream;
@@ -104,6 +105,10 @@
104105
105106 public void readFromStream(DataInputStreamLittleEndian is) throws
106107 IOException {
108+ readFromStream(is, false);
109+ }
110+ public void readFromStream(DataInputStreamLittleEndian is, boolean skipVertFlag) throws
111+ IOException {
107112 id = is.readString(3);
108113 if (!"Pmd".equals(id)) {
109114 throw new InvalidPMDFileException("Invalid ID:" + id);
@@ -114,29 +119,51 @@
114119 vertCount = is.readInt();
115120 // vertexList = new PMDVertex[vertCount];
116121 // vertexBuffer = ByteBuffer.allocateDirect(PMDVertex.size() * vertCount);
117- vertexBuffer = BufferUtil.createByteBuffer(PMDVertex.size() * vertCount);
118- vertexBuffer.order(ByteOrder.nativeOrder());
119- PMDVertex tmpVertex = new PMDVertex();
120- for (int i = 0; i < vertCount; i++) {
121- tmpVertex.readFromStream(is);
122- tmpVertex.writeToBuffer(vertexBuffer);
123-
122+ if (skipVertFlag) {
123+// vertexBuffer = BufferUtil.createByteBuffer(PMDVertex.size() * vertCount);
124+// vertexBuffer.order(ByteOrder.nativeOrder());
125+// PMDVertex tmpVertex = new PMDVertex();
126+// for (int i = 0; i < vertCount; i++) {
127+// tmpVertex.readFromStream(is);
128+//// tmpVertex.writeToBuffer(vertexBuffer);
129+//
130+// }
131+ is.skip(38 * vertCount);
132+ } else {
133+ vertexBuffer = BufferUtil.createByteBuffer(PMDVertex.size() * vertCount);
134+ vertexBuffer.order(ByteOrder.nativeOrder());
135+ PMDVertex tmpVertex = new PMDVertex();
136+ for (int i = 0; i < vertCount; i++) {
137+ tmpVertex.readFromStream(is);
138+ tmpVertex.writeToBuffer(vertexBuffer);
139+ }
124140 }
125141 faceVertCount = is.readInt();
126- faceVertIndex = new int[faceVertCount];
127- for (int i = 0; i < faceVertCount; i++) {
128- faceVertIndex[i] = is.readUnsignedShort();
129- }
130- // 逆にする。
131- for (int i = 0; i < faceVertCount; i += 3) {
132- int tmp = faceVertIndex[i];
133- faceVertIndex[i] = faceVertIndex[i + 1];
134- faceVertIndex[i + 1] = tmp;
142+ if (skipVertFlag) {
143+// for (int i = 0; i < faceVertCount; i++) {
144+// is.readUnsignedShort();
145+// }
146+ long skip = is.skip(faceVertCount * 2);
147+ if (skip != faceVertCount * 2) {
148+ throw new IllegalArgumentException("skip = "+skip+" "+faceVertCount * 2);
149+ }
150+ } else {
151+ faceVertIndex = new int[faceVertCount];
152+ for (int i = 0; i < faceVertCount; i++) {
153+ faceVertIndex[i] = is.readUnsignedShort();
154+ }
155+ // 逆にする。
156+ for (int i = 0; i < faceVertCount; i += 3) {
157+ int tmp = faceVertIndex[i];
158+ faceVertIndex[i] = faceVertIndex[i + 1];
159+ faceVertIndex[i + 1] = tmp;
160+ }
135161 }
136162 materialCount = is.readInt();
137163 material = new PMDMaterial[materialCount];
138164 for (int i = 0; i < materialCount; i++) {
139165 material[i] = new PMDMaterial(is);
166+ material[i].setMaterialNo(i);
140167 }
141168 boneList = new PMDBoneList(is);
142169 ikList = new PMDIKList(is);
@@ -161,6 +188,43 @@
161188 // rigidBodyList = new PMDRigidBodyList();
162189 // jointList = new PMDJointList();
163190 }
191+ public void writeToStream(DataOutput os) throws IOException {
192+ PMDUtil.writeString(os, "Pmd", 3);
193+ os.writeFloat(version);
194+ PMDUtil.writeString(os, modelName, 20);
195+ PMDUtil.writeString(os, comment, 256);
196+ os.writeInt(vertCount);
197+ System.out.print("vertCount out = "+vertCount);
198+ for (int i = 0; i < vertCount; i++) {
199+ PMDVertex tmpVertex = new PMDVertex();
200+ getVertex(i, tmpVertex);
201+ tmpVertex.writeToStream(os);
202+ }
203+ os.writeInt(faceVertCount);
204+ for (int i = 0; i < faceVertCount; i += 3) {
205+ os.writeShort(faceVertIndex[i+1]);
206+ os.writeShort(faceVertIndex[i]);
207+ os.writeShort(faceVertIndex[i+2]);
208+ }
209+ os.writeInt(materialCount);
210+ for(PMDMaterial mat : material) {
211+ mat.writeToStream(os);
212+ }
213+ boneList.writeToStream(os);
214+ ikList.writeToStream(os);
215+ os.writeShort(skinCount);
216+ for(PMDSkinData skin : skinData) {
217+ skin.writeToStream(os);
218+ }
219+ skinDispList.writeToStream(os);
220+ boneDispNameList.writeToStream(os);
221+ boneDispList.writeToStream(os);
222+ headerEnglish.writeToStream(os);
223+ toonTextureList.writeToStream(os);
224+ rigidBodyList.writeToStream(os);
225+ jointList.writeToStream(os);
226+
227+ }
164228 public PMDVertex getVertex(int i) {
165229 return getVertex(i, new PMDVertex());
166230 }
diff -r c6451e912509 -r ad69521155ab src/projectkyoto/mmd/file/PMDRigidBody.java
--- a/src/projectkyoto/mmd/file/PMDRigidBody.java Thu Oct 11 15:17:57 2012 +0900
+++ b/src/projectkyoto/mmd/file/PMDRigidBody.java Wed Oct 17 17:21:47 2012 +0900
@@ -32,6 +32,7 @@
3232
3333 package projectkyoto.mmd.file;
3434
35+import java.io.DataOutput;
3536 import java.io.IOException;
3637 import java.io.Serializable;
3738 import javax.vecmath.Vector3f;
@@ -75,6 +76,26 @@
7576 friction = is.readFloat();
7677 rigidBodyType = is.readUnsignedByte();
7778 }
79+ public void writeToStream(DataOutput os) throws IOException {
80+ PMDUtil.writeString(os, rigidBodyName, 20);
81+ os.writeShort(relBoneIndex);
82+ os.writeByte(rigidBodyGroupIndex);
83+ os.writeShort(rigidBodyGroupTarget);
84+ os.writeByte(shapeType);
85+ os.writeFloat(shapeW);
86+ os.writeFloat(shapeH);
87+ os.writeFloat(shapeD);
88+ PMDUtil.writeVector3f(os, pos);
89+ os.writeFloat(-rot.x);
90+ os.writeFloat(-rot.y);
91+ os.writeFloat(rot.z);
92+ os.writeFloat(weight);
93+ os.writeFloat(posDim);
94+ os.writeFloat(rotDim);
95+ os.writeFloat(recoil);
96+ os.writeFloat(friction);
97+ os.writeByte(rigidBodyType);
98+ }
7899
79100 @Override
80101 public String toString() {
diff -r c6451e912509 -r ad69521155ab src/projectkyoto/mmd/file/PMDRigidBodyList.java
--- a/src/projectkyoto/mmd/file/PMDRigidBodyList.java Thu Oct 11 15:17:57 2012 +0900
+++ b/src/projectkyoto/mmd/file/PMDRigidBodyList.java Wed Oct 17 17:21:47 2012 +0900
@@ -24,6 +24,7 @@
2424 */
2525 package projectkyoto.mmd.file;
2626
27+import java.io.DataOutput;
2728 import java.io.IOException;
2829 import java.io.Serializable;
2930
@@ -48,6 +49,12 @@
4849 rigidBodyArray[i] = new PMDRigidBody(is);
4950 }
5051 }
52+ public void writeToStream(DataOutput os) throws IOException {
53+ os.writeInt(rigidBodyCount);
54+ for(PMDRigidBody rigidBody : rigidBodyArray) {
55+ rigidBody.writeToStream(os);
56+ }
57+ }
5158
5259 @Override
5360 public String toString() {
diff -r c6451e912509 -r ad69521155ab src/projectkyoto/mmd/file/PMDSkinData.java
--- a/src/projectkyoto/mmd/file/PMDSkinData.java Thu Oct 11 15:17:57 2012 +0900
+++ b/src/projectkyoto/mmd/file/PMDSkinData.java Wed Oct 17 17:21:47 2012 +0900
@@ -33,6 +33,7 @@
3333 package projectkyoto.mmd.file;
3434
3535 import com.jme3.util.BufferUtils;
36+import java.io.DataOutput;
3637 import java.io.IOException;
3738 import java.io.Serializable;
3839 import java.nio.FloatBuffer;
@@ -68,6 +69,19 @@
6869 skinBuf.put(-is.readFloat());
6970 }
7071 }
72+ public void writeToStream(DataOutput os) throws IOException {
73+ PMDUtil.writeString(os, skinName, 20);
74+ os.writeInt(skinVertCount);
75+ os.writeByte(skinType);
76+ indexBuf.position(0);
77+ skinBuf.position(0);
78+ for(int i=0;i<skinVertCount;i++) {
79+ os.writeInt(indexBuf.get() & 0xffff);
80+ os.writeFloat(skinBuf.get());
81+ os.writeFloat(skinBuf.get());
82+ os.writeFloat(-skinBuf.get());
83+ }
84+ }
7185
7286 @Override
7387 public String toString() {
diff -r c6451e912509 -r ad69521155ab src/projectkyoto/mmd/file/PMDSkinDispList.java
--- a/src/projectkyoto/mmd/file/PMDSkinDispList.java Thu Oct 11 15:17:57 2012 +0900
+++ b/src/projectkyoto/mmd/file/PMDSkinDispList.java Wed Oct 17 17:21:47 2012 +0900
@@ -32,6 +32,7 @@
3232
3333 package projectkyoto.mmd.file;
3434
35+import java.io.DataOutput;
3536 import java.io.IOException;
3637 import java.io.Serializable;
3738
@@ -49,6 +50,12 @@
4950 skinIndexArray[i] = is.readShort();
5051 }
5152 }
53+ public void writeToStream(DataOutput os) throws IOException {
54+ os.writeByte(skinDispCount);
55+ for(short s : skinIndexArray) {
56+ os.writeShort(s);
57+ }
58+ }
5259
5360 @Override
5461 public String toString() {
diff -r c6451e912509 -r ad69521155ab src/projectkyoto/mmd/file/PMDSkinVertData.java
--- a/src/projectkyoto/mmd/file/PMDSkinVertData.java Thu Oct 11 15:17:57 2012 +0900
+++ b/src/projectkyoto/mmd/file/PMDSkinVertData.java Wed Oct 17 17:21:47 2012 +0900
@@ -32,6 +32,7 @@
3232
3333 package projectkyoto.mmd.file;
3434
35+import java.io.DataOutput;
3536 import java.io.IOException;
3637 import java.io.Serializable;
3738 import javax.vecmath.Vector3f;
@@ -55,7 +56,12 @@
5556 skinVertPos.y = is.readFloat();
5657 skinVertPos.z = -is.readFloat();
5758 }
58-
59+ public void writeToStream(DataOutput os) throws IOException {
60+ os.writeInt(skinVertIndex);
61+ os.writeFloat(skinVertPos.x);
62+ os.writeFloat(skinVertPos.y);
63+ os.writeFloat(-skinVertPos.z);
64+ }
5965 public int getSkinVertIndex() {
6066 return skinVertIndex;
6167 }
diff -r c6451e912509 -r ad69521155ab src/projectkyoto/mmd/file/PMDToonTextureList.java
--- a/src/projectkyoto/mmd/file/PMDToonTextureList.java Thu Oct 11 15:17:57 2012 +0900
+++ b/src/projectkyoto/mmd/file/PMDToonTextureList.java Wed Oct 17 17:21:47 2012 +0900
@@ -32,6 +32,7 @@
3232
3333 package projectkyoto.mmd.file;
3434
35+import java.io.DataOutput;
3536 import java.io.IOException;
3637 import java.io.Serializable;
3738
@@ -47,12 +48,16 @@
4748 toonFileName[i] = "";
4849 }
4950 }
50-
5151 public PMDToonTextureList(DataInputStreamLittleEndian is) throws IOException {
5252 for(int i=0;i<10;i++) {
5353 toonFileName[i] = is.readString(100);
5454 }
5555 }
56+ public void writeToStream(DataOutput os) throws IOException {
57+ for(int i=0;i<10;i++) {
58+ PMDUtil.writeString(os, toonFileName[i], 100);
59+ }
60+ }
5661
5762 @Override
5863 public String toString() {
diff -r c6451e912509 -r ad69521155ab src/projectkyoto/mmd/file/PMDUtil.java
--- a/src/projectkyoto/mmd/file/PMDUtil.java Thu Oct 11 15:17:57 2012 +0900
+++ b/src/projectkyoto/mmd/file/PMDUtil.java Wed Oct 17 17:21:47 2012 +0900
@@ -31,6 +31,7 @@
3131 */
3232 package projectkyoto.mmd.file;
3333
34+import java.io.DataOutput;
3435 import java.io.IOException;
3536 import java.nio.ByteBuffer;
3637 import javax.vecmath.Vector3f;
@@ -61,4 +62,21 @@
6162 bb.putFloat(v.z);
6263 return v;
6364 }
65+ public static void writeString(DataOutput os, String s, int len) throws IOException {
66+ byte[] buf = s.getBytes("Shift_JIS");
67+ int l = buf.length;
68+ if (l > len) {
69+ os.write(buf, 0, len);
70+ } else {
71+ os.write(buf);
72+ for(;l < len;l++) {
73+ os.writeByte(0);
74+ }
75+ }
76+ }
77+ public static void writeVector3f(DataOutput os, Vector3f v) throws IOException{
78+ os.writeFloat(v.x);
79+ os.writeFloat(v.y);
80+ os.writeFloat(-v.z);
81+ }
6482 }
diff -r c6451e912509 -r ad69521155ab src/projectkyoto/mmd/file/PMDVertex.java
--- a/src/projectkyoto/mmd/file/PMDVertex.java Thu Oct 11 15:17:57 2012 +0900
+++ b/src/projectkyoto/mmd/file/PMDVertex.java Wed Oct 17 17:21:47 2012 +0900
@@ -32,6 +32,7 @@
3232
3333 package projectkyoto.mmd.file;
3434
35+import java.io.DataOutput;
3536 import java.io.IOException;
3637 import java.io.Serializable;
3738 import java.nio.ByteBuffer;
@@ -79,6 +80,15 @@
7980 edgeFlag = is.readByte();
8081 return this;
8182 }
83+ public void writeToStream(DataOutput os) throws IOException {
84+ PMDUtil.writeVector3f(os, pos);
85+ PMDUtil.writeVector3f(os, normal);
86+ uv.writeToStream(os);
87+ os.writeShort(boneNum1);
88+ os.writeShort(boneNum2);
89+ os.writeByte(boneWeight);
90+ os.writeByte(edgeFlag);
91+ }
8292 public PMDVertex readFromBuffer(ByteBuffer bb) {
8393 PMDUtil.readVector3f(bb, pos);
8494 PMDUtil.readVector3f(bb, normal);
diff -r c6451e912509 -r ad69521155ab src/projectkyoto/mmd/file/XColorRGB.java
--- a/src/projectkyoto/mmd/file/XColorRGB.java Thu Oct 11 15:17:57 2012 +0900
+++ b/src/projectkyoto/mmd/file/XColorRGB.java Wed Oct 17 17:21:47 2012 +0900
@@ -32,6 +32,7 @@
3232
3333 package projectkyoto.mmd.file;
3434
35+import java.io.DataOutput;
3536 import java.io.IOException;
3637 import java.io.Serializable;
3738
@@ -85,6 +86,11 @@
8586 green = is.readFloat();
8687 blue = is.readFloat();
8788 }
89+ public void writeToStream(DataOutput os) throws IOException {
90+ os.writeFloat(red);
91+ os.writeFloat(green);
92+ os.writeFloat(blue);
93+ }
8894 @Override
8995 public String toString() {
9096 return "{red = "+red
diff -r c6451e912509 -r ad69521155ab src/projectkyoto/mmd/file/XColorRGBA.java
--- a/src/projectkyoto/mmd/file/XColorRGBA.java Thu Oct 11 15:17:57 2012 +0900
+++ b/src/projectkyoto/mmd/file/XColorRGBA.java Wed Oct 17 17:21:47 2012 +0900
@@ -32,6 +32,7 @@
3232
3333 package projectkyoto.mmd.file;
3434
35+import java.io.DataOutput;
3536 import java.io.IOException;
3637 import java.io.Serializable;
3738
@@ -55,6 +56,10 @@
5556 super(is);
5657 alpha = is.readFloat();
5758 }
59+ public void writeToStream(DataOutput os) throws IOException {
60+ super.writeToStream(os);
61+ os.writeFloat(alpha);
62+ }
5863
5964 public float getAlpha() {
6065 return alpha;
diff -r c6451e912509 -r ad69521155ab src/projectkyoto/mmd/file/XMaterial.java
--- a/src/projectkyoto/mmd/file/XMaterial.java Thu Oct 11 15:17:57 2012 +0900
+++ b/src/projectkyoto/mmd/file/XMaterial.java Wed Oct 17 17:21:47 2012 +0900
@@ -32,6 +32,7 @@
3232
3333 package projectkyoto.mmd.file;
3434
35+import java.io.DataOutput;
3536 import java.io.IOException;
3637 import java.io.Serializable;
3738
@@ -53,6 +54,12 @@
5354 specularColor = new XColorRGB(is);
5455 ambientColor = new XColorRGB(is);
5556 }
57+ public void writeToStream(DataOutput os) throws IOException {
58+ faceColor.writeToStream(os);
59+ os.writeFloat(power);
60+ specularColor.writeToStream(os);
61+ ambientColor.writeToStream(os);
62+ }
5663
5764 @Override
5865 public String toString() {
diff -r c6451e912509 -r ad69521155ab src/projectkyoto/mmd/file/util2/BufferUtil.java
--- a/src/projectkyoto/mmd/file/util2/BufferUtil.java Thu Oct 11 15:17:57 2012 +0900
+++ b/src/projectkyoto/mmd/file/util2/BufferUtil.java Wed Oct 17 17:21:47 2012 +0900
@@ -4,8 +4,12 @@
44 */
55 package projectkyoto.mmd.file.util2;
66
7+import com.jme3.util.BufferUtils;
8+import java.io.DataInputStream;
9+import java.io.DataOutputStream;
710 import java.io.File;
811 import java.io.IOException;
12+import java.io.OutputStream;
913 import java.io.RandomAccessFile;
1014 import java.io.UnsupportedEncodingException;
1115 import java.nio.ByteBuffer;
@@ -24,7 +28,7 @@
2428 */
2529 public class BufferUtil {
2630 public static File tmpDir = null;
27- public static final Logger logger = Logger.getLogger(BufferUtil.class.getName());
31+ private static final Logger logger = Logger.getLogger(BufferUtil.class.getName());
2832 public static ByteBuffer createByteBuffer2(int size) {
2933 ByteBuffer bb = ByteBuffer.allocateDirect(size);
3034 bb.order(ByteOrder.nativeOrder());
@@ -120,4 +124,32 @@
120124 bb.putFloat(q.z);
121125 bb.putFloat(q.w);
122126 }
127+ public static void write(ByteBuffer bb, DataOutputStream os, byte[] buf) throws IOException {
128+ bb.position(0);
129+ final int capacity = bb.capacity();
130+ os.writeInt(capacity);
131+ while(bb.position() < capacity) {
132+ int size = capacity - bb.position();
133+ if (size > buf.length) {
134+ size = buf.length;
135+ }
136+ bb.get(buf, 0, size);
137+ os.write(buf, 0, size);
138+ }
139+ }
140+ public static ByteBuffer read(DataInputStream is, byte[]buf) throws IOException {
141+ final int capacity = is.readInt();
142+ ByteBuffer bb = BufferUtils.createByteBuffer(capacity);
143+ while(bb.position() < capacity) {
144+ int size = capacity - bb.position();
145+ if (size > buf.length) {
146+ size = buf.length;
147+ }
148+ is.read(buf, 0, size);
149+ bb.put(buf, 0, size);
150+ }
151+ bb.position(0);
152+ return bb;
153+ }
154+
123155 }
diff -r c6451e912509 -r ad69521155ab src/projectkyoto/mmd/file/util2/MeshConverter.java
--- a/src/projectkyoto/mmd/file/util2/MeshConverter.java Thu Oct 11 15:17:57 2012 +0900
+++ b/src/projectkyoto/mmd/file/util2/MeshConverter.java Wed Oct 17 17:21:47 2012 +0900
@@ -32,6 +32,11 @@
3232
3333 package projectkyoto.mmd.file.util2;
3434
35+import java.io.DataInputStream;
36+import java.io.DataOutputStream;
37+import java.io.IOException;
38+import java.io.InputStream;
39+import java.io.OutputStream;
3540 import java.io.Serializable;
3641 import java.net.URL;
3742 import java.nio.ByteBuffer;
@@ -67,10 +72,11 @@
6772 HashMap<Integer, Integer> meshTmpVertMap = new HashMap<Integer, Integer>();
6873 HashMap<Integer, Integer> skinTmpVertMap = new HashMap<Integer, Integer>();
6974 public ByteBuffer interleavedBuffer;
70- public ArrayList<ByteBuffer> skinBufferList;
71- public ByteBuffer skinIndexBuffer;
7275 int currentVertIndex = 0;
7376 PMNData pmnData;
77+ public MeshConverter() {
78+
79+ }
7480 public MeshConverter(PMDModel model) {
7581 this.model = model;
7682 skinMeshData = new SkinMeshData(this, model);
@@ -111,7 +117,7 @@
111117 meshTmpVertMap.clear();
112118 PMDMaterial material = model.getMaterial()[materialNo];
113119 // find same material
114- MeshData meshData = new MeshData(model, maxBoneSize, material, currentVertIndex);
120+ MeshData meshData = new MeshData(model, maxBoneSize, material);
115121 for(int meshIndex = meshDataList.size()-1;meshIndex >=0;meshIndex--) {
116122 PMDMaterial material2 = meshDataList.get(meshIndex).getMaterial();
117123 if (material.equals(material2)) {
@@ -137,7 +143,7 @@
137143 addSkinTriangle(material, i1, i2, i3);
138144 } else {
139145 if (!meshData.addTriangle(this, i1, i2, i3)) {
140- meshData = new MeshData(model, maxBoneSize, material, currentVertIndex);
146+ meshData = new MeshData(model, maxBoneSize, material);
141147 meshTmpVertMap.clear();
142148 meshDataList.add(meshData);
143149 meshData.addTriangle(this, i1, i2, i3);
@@ -156,8 +162,13 @@
156162 MeshData md = it.next();
157163 if (md.getIndexList().size() == 0) {
158164 it.remove();
165+ } else {
166+ md.createMesh();
159167 }
160168 }
169+ skinMeshData.createSkinCommonVertData();
170+ meshTmpVertMap = null;
171+ skinTmpVertMap = null;
161172 }
162173 void removeUnusedSkinVertex() {
163174 HashSet<Integer> tmpSet = new HashSet<Integer>();
@@ -374,6 +385,31 @@
374385 }
375386 return size;
376387 }
388+ public void write(OutputStream os) throws IOException {
389+ DataOutputStream dos = new DataOutputStream(os);
390+ byte[] buf = new byte[1024 * 16];
391+ dos.writeInt(meshDataList.size());
392+ for(int i=0;i<meshDataList.size();i++) {
393+ MeshData md = meshDataList.get(i);
394+ md.write(dos, buf);
395+ }
396+ skinMeshData.write(dos, buf);
397+ dos.flush();
398+ }
399+ public void read(InputStream is) throws IOException {
400+ DataInputStream dis = new DataInputStream(is);
401+ byte[] buf = new byte[1024 * 16];
402+ meshDataList = new ArrayList<MeshData>();
403+ int meshDataSize = dis.readInt();
404+ for(int i=0;i<meshDataSize;i++) {
405+ MeshData md = new MeshData(model, maxBoneSize, null);
406+ md.read(dis, buf);
407+ meshDataList.add(md);
408+ }
409+ skinMeshData = new SkinMeshData();
410+ skinMeshData.model = model;
411+ skinMeshData.read(dis, buf);
412+ }
377413 public int getMaxBoneSize() {
378414 return maxBoneSize;
379415 }
diff -r c6451e912509 -r ad69521155ab src/projectkyoto/mmd/file/util2/MeshData.java
--- a/src/projectkyoto/mmd/file/util2/MeshData.java Thu Oct 11 15:17:57 2012 +0900
+++ b/src/projectkyoto/mmd/file/util2/MeshData.java Wed Oct 17 17:21:47 2012 +0900
@@ -32,8 +32,16 @@
3232
3333 package projectkyoto.mmd.file.util2;
3434
35+import com.jme3.math.FastMath;
36+import com.jme3.util.BufferUtils;
37+import java.io.DataInputStream;
38+import java.io.DataOutputStream;
39+import java.io.IOException;
40+import java.io.OutputStream;
3541 import java.nio.ByteBuffer;
3642 import java.nio.ByteOrder;
43+import java.nio.FloatBuffer;
44+import java.nio.ShortBuffer;
3745 import java.util.ArrayList;
3846 import java.util.HashSet;
3947 import java.util.List;
@@ -56,16 +64,13 @@
5664 List<Integer> indexList = new ArrayList<Integer>();
5765 // public ByteBuffer indexBuffer;
5866 List<Integer> vertIndexList = new ArrayList<Integer>();
59- public int offset;
6067 private PMDVertex tmpVert = new PMDVertex();
61- public MeshData(PMDModel model, int maxBoneSize, PMDMaterial material
62- , int offset) {
68+ public MeshData(PMDModel model, int maxBoneSize, PMDMaterial material) {
6369 this.model = model;
6470 this.maxBoneSize = maxBoneSize;
6571 this.material = material;
6672 // indexBuffer = ByteBuffer.allocateDirect(material.getFaceVertCount() * 2);
6773 // indexBuffer.order(ByteOrder.nativeOrder());
68- this.offset = offset;
6974 }
7075 public boolean addTriangle(MeshConverter mc, int i1,int i2,int i3) {
7176 int boneListSizeBefore = boneList.size();
@@ -118,7 +123,150 @@
118123 // indexBuffer.putShort((short)newVertIndex);
119124 indexList.add(newVertIndex);
120125 }
121- public List<Integer> getBoneList() {
126+ ByteBuffer vfbb;
127+ ByteBuffer nfbb;
128+ ByteBuffer tfbb;
129+ ByteBuffer wfbb;
130+ ByteBuffer isbb;
131+ ByteBuffer bisbb;
132+ ByteBuffer indexBufferb;
133+
134+ public FloatBuffer vfb;
135+ public FloatBuffer nfb;
136+ public FloatBuffer tfb;
137+ public FloatBuffer wfb;
138+ public ShortBuffer isb;
139+ public ShortBuffer bisb;
140+ public ShortBuffer indexBuffer;
141+ public int[] indexArray;
142+ public void write(DataOutputStream os, byte[] buf) throws IOException {
143+ os.writeInt(material.getMaterialNo());
144+ BufferUtil.write(vfbb, os, buf);
145+ BufferUtil.write(nfbb, os, buf);
146+ if (tfbb != null) {
147+ os.writeBoolean(true);
148+ BufferUtil.write(tfbb, os, buf);
149+ } else {
150+ os.writeBoolean(false);
151+ }
152+ BufferUtil.write(wfbb, os, buf);
153+ BufferUtil.write(isbb, os, buf);
154+ BufferUtil.write(bisbb, os, buf);
155+ BufferUtil.write(indexBufferb, os, buf);
156+ os.writeInt(indexArray.length);
157+ for(int i : indexArray) {
158+ os.writeInt(i);
159+ }
160+ }
161+ public void read(DataInputStream is, byte[] buf) throws IOException {
162+ material = model.getMaterial()[is.readInt()];
163+ vfbb = BufferUtil.read(is, buf);
164+ vfb = vfbb.asFloatBuffer();
165+ nfbb = BufferUtil.read(is, buf);
166+ nfb = nfbb.asFloatBuffer();
167+ if (is.readBoolean()) {
168+ tfbb = BufferUtil.read(is, buf);
169+ tfb = tfbb.asFloatBuffer();
170+ }
171+ wfbb = BufferUtil.read(is, buf);
172+ wfb = wfbb.asFloatBuffer();
173+ isbb = BufferUtil.read(is, buf);
174+ isb = isbb.asShortBuffer();
175+ bisbb = BufferUtil.read(is, buf);
176+ bisb = bisbb.asShortBuffer();
177+ indexBufferb = BufferUtil.read(is, buf);
178+ indexBuffer = indexBufferb.asShortBuffer();
179+ int length = is.readInt();
180+ indexArray = new int[length];
181+ for(int i=0;i<length;i++) {
182+ indexArray[i] = is.readInt();
183+ }
184+ }
185+ void createMesh() {
186+ boolean textureFlag = true;
187+ if (getMaterial().getTextureFileName().length() == 0) {
188+ textureFlag = false;
189+ }
190+ vfbb = BufferUtils.createByteBuffer(4 * getVertIndexList().size() * 3);
191+ vfb = vfbb.asFloatBuffer();
192+// vfb = BufferUtils.createFloatBuffer(getVertIndexList().size() * 3);
193+ nfbb = BufferUtils.createByteBuffer(4 * getVertIndexList().size() * 3);
194+ nfb = nfbb.asFloatBuffer();
195+// nfb = BufferUtils.createFloatBuffer(getVertIndexList().size() * 3);
196+
197+ tfb = null;
198+ if (textureFlag ) {
199+ tfbb = BufferUtils.createByteBuffer(4 * getVertIndexList().size() * 2);
200+ tfb = tfbb.asFloatBuffer();
201+// tfb = BufferUtils.createFloatBuffer(getVertIndexList().size() * 2);
202+ }
203+ wfbb = BufferUtils.createByteBuffer(4 * getVertIndexList().size() * 2);
204+ wfb = wfbb.asFloatBuffer();
205+// wfb = BufferUtils.createFloatBuffer(getVertIndexList().size() * 2);
206+ isbb = BufferUtils.createByteBuffer(2 * getIndexList().size());
207+ isb = isbb.asShortBuffer();
208+// isb = BufferUtils.createShortBuffer(getIndexList().size()/*md.getMaterial().getFaceVertCount()*/);
209+ bisbb = BufferUtils.createByteBuffer(2 * getVertIndexList().size() * 2);
210+ bisb = bisbb.asShortBuffer();
211+// bisb = BufferUtils.createShortBuffer(getVertIndexList().size() * 2);
212+ PMDVertex v = new PMDVertex();
213+ for (Integer vertIndex : getVertIndexList()) {
214+ model.getVertex(vertIndex, v);
215+ vfb.put(v.getPos().x).put(v.getPos().y).put(v.getPos().z);
216+ nfb.put(v.getNormal().x).put(v.getNormal().y).put(v.getNormal().z);
217+
218+// bvfb.put(v.getPos().x).put(v.getPos().y).put(v.getPos().z);
219+// bnfb.put(v.getNormal().x).put(v.getNormal().y).put(v.getNormal().z);
220+ if (textureFlag) {
221+ float f1 = v.getUv().getU();
222+ float f2 = v.getUv().getV();
223+// tfb.put(v.getUv().getU()).put(1f - v.getUv().getV());
224+ f1 = f1 - FastMath.floor(f1);
225+ f2 = f2 - FastMath.floor(f2);
226+ f2 = 1 - f2;
227+ tfb.put(f1).put(f2);
228+ }
229+ float weight = (float) v.getBoneWeight() / 100.0f;
230+ wfb.put(weight).put(1f - weight);
231+ short b1 = (short)getBoneList().indexOf(v.getBoneNum1());
232+ short b2 = (short)getBoneList().indexOf(v.getBoneNum2());
233+ if (b1 < 0) b1 = 0;
234+ if (b2 < 0) b2 = 0;
235+ bisb.put(b1).put(b2);
236+// bisb.put((short) md.getBoneList().indexOf(v.getBoneNum1())).put((short) md.getBoneList().indexOf(v.getBoneNum2()));
237+// if (( weight != 0 && md.getBoneList().indexOf(v.getBoneNum1()) < 0)
238+// || (weight != 1 && md.getBoneList().indexOf(v.getBoneNum2())<0)){
239+// System.out.println("ERROR!! "+v.getBoneNum1()+" "+v.getBoneNum2());
240+// System.out.println(""+md.getBoneList().indexOf(v.getBoneNum1())+" "+md.getBoneList().indexOf(v.getBoneNum2()));
241+// System.out.println("weight = "+weight);
242+// }
243+ }
244+ for (Integer index : getIndexList()) {
245+ isb.put(index.shortValue());
246+// System.out.println("index = "+index);
247+ }
248+// System.out.println("isb.capacity() = " + isb.capacity());
249+// System.out.println("isb.capacity() = " + md.getIndexList().size());
250+
251+// bvb.setupData(VertexBuffer.Usage.CpuOnly, 3, VertexBuffer.Format.Float, bvfb);
252+// bnb.setupData(VertexBuffer.Usage.CpuOnly, 3, VertexBuffer.Format.Float, bnfb);
253+ indexArray = new int[getBoneList().size()];
254+ indexBufferb = BufferUtils.createByteBuffer(2 * getBoneList().size());
255+ indexBuffer = indexBufferb.asShortBuffer();
256+// indexBuffer = BufferUtils.createShortBuffer(getBoneList().size());
257+ for (int i = 0; i < indexArray.length; i++) {
258+ if (i < getBoneList().size()) {
259+ indexArray[i] = getBoneList().get(i).shortValue();
260+ } else {
261+ indexArray[i] = 0;
262+ }
263+ indexBuffer.put((short)indexArray[i]);
264+ }
265+ boneList = null;
266+ vertIndexList = null;
267+ vertIndexList = null;
268+ }
269+ List<Integer> getBoneList() {
122270 return boneList;
123271 }
124272
@@ -154,11 +302,11 @@
154302 // return indexBuffer;
155303 // }
156304
157- public List<Integer> getVertIndexList() {
305+ List<Integer> getVertIndexList() {
158306 return vertIndexList;
159307 }
160308
161- public List<Integer> getIndexList() {
309+ List<Integer> getIndexList() {
162310 return indexList;
163311 }
164312
diff -r c6451e912509 -r ad69521155ab src/projectkyoto/mmd/file/util2/PMDFileUtil.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/projectkyoto/mmd/file/util2/PMDFileUtil.java Wed Oct 17 17:21:47 2012 +0900
@@ -0,0 +1,123 @@
1+/*
2+ * To change this template, choose Tools | Templates
3+ * and open the template in the editor.
4+ */
5+package projectkyoto.mmd.file.util2;
6+
7+import java.io.BufferedInputStream;
8+import java.io.BufferedOutputStream;
9+import java.io.File;
10+import java.io.FileInputStream;
11+import java.io.FileNotFoundException;
12+import java.io.FileOutputStream;
13+import java.io.IOException;
14+import java.io.InputStream;
15+import java.io.OutputStream;
16+import java.util.ArrayList;
17+import java.util.List;
18+import java.util.StringTokenizer;
19+import projectkyoto.mmd.file.DataInputStreamLittleEndian;
20+import projectkyoto.mmd.file.DataOutputStreamLittleEndian;
21+import projectkyoto.mmd.file.PMDModel;
22+import projectkyoto.mmd.file.PMDUtil;
23+
24+/**
25+ *
26+ * @author kobayasi
27+ */
28+public class PMDFileUtil {
29+ public static final String PMDCACHE1HEADER = "PMDCACHEVer1";
30+ public static void makeMeshCache(PMDModel pmdModel, OutputStream os) throws IOException {
31+ MeshConverter mc = new MeshConverter(pmdModel);
32+ mc.convertMesh();
33+ }
34+ public static MeshConverter readPMDCache1(InputStream is) throws IOException {
35+ DataInputStreamLittleEndian dis = null;
36+ try {
37+ dis = new DataInputStreamLittleEndian(new BufferedInputStream(is));
38+ String version = dis.readString(20);
39+ if (!PMDCACHE1HEADER.equals(version)) {
40+ throw new IllegalArgumentException("Invalid header "+version);
41+ }
42+ int maxBoneSize = dis.readInt();
43+ PMDModel model = new PMDModel();
44+ model.readFromStream(dis);
45+ MeshConverter mc = new MeshConverter();
46+ mc.setModel(model);
47+ mc.read(dis);
48+ mc.setMaxBoneSize(maxBoneSize);
49+ return mc;
50+ } finally {
51+// if (dis != null) {
52+// dis.close();
53+// }
54+ }
55+ }
56+ public static void writePMDCache1(PMDModel model, File file) throws IOException {
57+ MeshConverter mc = new MeshConverter(model);
58+ model.setFaceVertCount(0);
59+ model.setVertCount(0);
60+ DataOutputStreamLittleEndian dos = null;
61+ try {
62+ dos = new DataOutputStreamLittleEndian(new BufferedOutputStream(new FileOutputStream(file)));
63+ // write header
64+ PMDUtil.writeString(dos, PMDCACHE1HEADER, 20);
65+ dos.writeInt(mc.getMaxBoneSize());
66+ model.writeToStream(dos);
67+ mc.convertMesh();
68+ mc.write(dos);
69+ } finally {
70+ if (dos != null) {
71+ dos.close();
72+ }
73+ }
74+ }
75+ public static void createPmdcache1(File file) throws IOException{
76+ List<String> list = new ArrayList<String>();
77+ StringTokenizer st = new StringTokenizer(file.getName(), ".");
78+ while(st.hasMoreElements()) {
79+ String s = st.nextToken();
80+ list.add(s);
81+ }
82+ if (list.size() < 3) {
83+ throw new FileNotFoundException(file.getAbsolutePath());
84+ }
85+ if (!list.get(list.size()-1).equals("pmdcache1")) {
86+ throw new FileNotFoundException(file.getAbsolutePath());
87+ }
88+ File pmdFile = null;
89+ FileInputStream is = null;
90+ try {
91+ int boneSize = Integer.parseInt(list.get(list.size()-2));
92+ String fileName = file.getAbsolutePath();
93+ int delmCount = 0;
94+ for(int i=fileName.length()-1;i>0;i--) {
95+ if (fileName.charAt(i) == '.') {
96+ delmCount++;
97+ if (delmCount == 2) {
98+ fileName = fileName.substring(0, i);
99+ pmdFile = new File(fileName);
100+ break;
101+ }
102+ }
103+ }
104+ if (pmdFile != null) {
105+ System.out.println("pmdFile = "+pmdFile.getAbsolutePath());
106+ if (!pmdFile.exists()) {
107+ throw new FileNotFoundException(file.getAbsolutePath());
108+ }
109+ MeshConverter.DEFAULT_MAX_BONE_SIZE = boneSize;
110+ is = new FileInputStream(pmdFile);
111+ PMDModel pmdModel = new PMDModel(is);
112+ writePMDCache1(pmdModel, file);
113+ return;
114+ }
115+ } catch(NumberFormatException ex) {
116+ } finally {
117+ if (is != null) {
118+ is.close();
119+ }
120+ }
121+ throw new FileNotFoundException(file.getAbsolutePath());
122+ }
123+}
diff -r c6451e912509 -r ad69521155ab src/projectkyoto/mmd/file/util2/SkinMeshData.java
--- a/src/projectkyoto/mmd/file/util2/SkinMeshData.java Thu Oct 11 15:17:57 2012 +0900
+++ b/src/projectkyoto/mmd/file/util2/SkinMeshData.java Wed Oct 17 17:21:47 2012 +0900
@@ -29,10 +29,18 @@
2929 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
3030 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3131 */
32-
3332 package projectkyoto.mmd.file.util2;
3433
34+import com.jme3.math.FastMath;
35+import com.jme3.util.BufferUtils;
36+import java.io.DataInputStream;
37+import java.io.DataOutputStream;
38+import java.io.IOException;
3539 import java.io.Serializable;
40+import java.nio.ByteBuffer;
41+import java.nio.ByteOrder;
42+import java.nio.FloatBuffer;
43+import java.nio.ShortBuffer;
3644 import java.util.ArrayList;
3745 import java.util.HashMap;
3846 import java.util.List;
@@ -46,24 +54,27 @@
4654 *
4755 * @author kobayasi
4856 */
49-public class SkinMeshData implements Serializable{
57+public class SkinMeshData implements Serializable {
5058
5159 PMDModel model;
5260 List<Integer> boneList = new ArrayList<Integer>();
5361 List<PMDVertex> vertexList = new ArrayList<PMDVertex>();
5462 Map<PMDMaterial, List<Integer>> indexMap = new HashMap<PMDMaterial, List<Integer>>();
55-
63+ public SkinMeshData() {
64+
65+ }
5666 public SkinMeshData(MeshConverter mc, PMDModel model) {
5767 this.model = model;
58- for(PMDSkinData sd : model.getSkinData()) {
68+ if (model.getVertexBuffer() != null)
69+ for (PMDSkinData sd : model.getSkinData()) {
5970 if (sd.getSkinType() == 0) {
60- for(int i=0;i<sd.getSkinVertCount();i++) {
71+ for (int i = 0; i < sd.getSkinVertCount(); i++) {
6172 int skinVertIndex = sd.getIndexBuf().get(i) & 0xffff;
6273 try {
6374 PMDVertex v = model.getVertex(skinVertIndex);
6475 vertexList.add(v);
6576 mc.skinTmpVertMap.put(skinVertIndex, i);
66- } catch(Exception ex) {
77+ } catch (Exception ex) {
6778 ex.printStackTrace();
6879 }
6980 }
@@ -81,9 +92,9 @@
8192 indexList = new ArrayList<Integer>();
8293 indexMap.put(material, indexList);
8394 }
84- addVertex(mc, indexList,i1);
85- addVertex(mc, indexList,i2);
86- addVertex(mc, indexList,i3);
95+ addVertex(mc, indexList, i1);
96+ addVertex(mc, indexList, i2);
97+ addVertex(mc, indexList, i3);
8798 }
8899
89100 private void addBoneList(int vertIndex) {
@@ -96,7 +107,7 @@
96107 }
97108 }
98109
99- private void addVertex(MeshConverter mc, List<Integer>indexList, int vertIndex) {
110+ private void addVertex(MeshConverter mc, List<Integer> indexList, int vertIndex) {
100111 PMDVertex v = model.getVertex(vertIndex);
101112 Integer index = mc.skinTmpVertMap.get(vertIndex);
102113 int newVertIndex;
@@ -110,8 +121,160 @@
110121 }
111122 indexList.add(index/*newVertIndex*/);
112123 }
124+ public ByteBuffer skinvfbb;
125+ public ByteBuffer skinnfbb;
126+ public ByteBuffer skintfbb;
127+ public ByteBuffer skinbisbb;
128+ public ByteBuffer wfbb;
129+ public FloatBuffer skinvfb;
130+ public FloatBuffer skinvfb2;
131+ public FloatBuffer skinnfb;
132+ public FloatBuffer skintfb;
133+ public ShortBuffer skinbisb;
134+ public FloatBuffer wfb;
135+ public int[] skinIndexArray;
136+ public Map<PMDMaterial, ShortBuffer> indexShortBufferMap = new HashMap<PMDMaterial, ShortBuffer>();
113137
114- public List<Integer> getBoneList() {
138+ public void write(DataOutputStream os, byte[] buf) throws IOException {
139+ BufferUtil.write(skinvfbb, os, buf);
140+ BufferUtil.write(skinnfbb, os, buf);
141+ if (skintfbb != null) {
142+ os.writeBoolean(true);
143+ BufferUtil.write(skintfbb, os, buf);
144+ } else {
145+ os.writeBoolean(false);
146+ }
147+ BufferUtil.write(wfbb, os, buf);
148+ BufferUtil.write(skinbisbb, os, buf);
149+ os.writeInt(skinIndexArray.length);
150+ for (int i : skinIndexArray) {
151+ os.writeInt(i);
152+ }
153+ os.writeInt(indexShortBufferMap.size());
154+ for (PMDMaterial mat : indexShortBufferMap.keySet()) {
155+ os.writeInt(mat.getMaterialNo());
156+ ShortBuffer sb = indexShortBufferMap.get(mat);
157+ os.writeInt(sb.capacity() * 2);
158+ sb.position(0);
159+ if (ByteOrder.nativeOrder().equals(ByteOrder.LITTLE_ENDIAN)) {
160+ for (int i = 0; i < sb.capacity(); i++) {
161+ short s = sb.get();
162+ os.writeByte(s);
163+ os.writeByte(s >> 8);
164+ }
165+ } else {
166+ for (int i = 0; i < sb.capacity(); i++) {
167+ short s = sb.get();
168+ os.writeByte(s >> 8);
169+ os.writeByte(s);
170+ }
171+ }
172+ }
173+ }
174+
175+ public void read(DataInputStream is, byte[] buf) throws IOException {
176+ skinvfbb = BufferUtil.read(is, buf);
177+ skinvfb = skinvfbb.asFloatBuffer();
178+ skinnfbb = BufferUtil.read(is, buf);
179+ skinnfb = skinnfbb.asFloatBuffer();
180+ if (is.readBoolean()) {
181+ skintfbb = BufferUtil.read(is, buf);
182+ skintfb = skintfbb.asFloatBuffer();
183+ }
184+ wfbb = BufferUtil.read(is, buf);
185+ wfb = wfbb.asFloatBuffer();
186+ skinbisbb = BufferUtil.read(is, buf);
187+ skinbisb = skinbisbb.asShortBuffer();
188+ int length = is.readInt();
189+ skinIndexArray = new int[length];
190+ for (int i = 0; i < length; i++) {
191+ skinIndexArray[i] = is.readInt();
192+ }
193+ int size = is.readInt();
194+ indexShortBufferMap = new HashMap<PMDMaterial, ShortBuffer>();
195+ for(int i=0;i<size;i++) {
196+ PMDMaterial mat = model.getMaterial()[is.readInt()];
197+ ShortBuffer sb = BufferUtil.read(is, buf).asShortBuffer();
198+ indexShortBufferMap.put(mat, sb);
199+ }
200+ skinvfb2 = BufferUtils.createFloatBuffer(skinvfb.capacity());
201+ skinvfb.position(0);
202+ skinvfb2.put(skinvfb);
203+ }
204+
205+ void createSkinCommonVertData() {
206+ skinvfbb = BufferUtils.createByteBuffer(getVertexList().size() * 3 * 4);
207+ skinvfb = skinvfbb.asFloatBuffer();
208+
209+ skinvfb2 = BufferUtils.createFloatBuffer(getVertexList().size() * 3);
210+
211+ skinnfbb = BufferUtils.createByteBuffer(getVertexList().size() * 3 * 4);
212+ skinnfb = skinnfbb.asFloatBuffer();
213+
214+ skintfbb = BufferUtils.createByteBuffer(getVertexList().size() * 2 * 4);
215+ skintfb = skintfbb.asFloatBuffer();
216+
217+ skinbisbb = BufferUtils.createByteBuffer(getVertexList().size() * 2 * 2);
218+ skinbisb = skinbisbb.asShortBuffer();
219+
220+ wfbb = BufferUtils.createByteBuffer(getVertexList().size() * 2 * 4);
221+ wfb = wfbb.asFloatBuffer();
222+
223+ for (PMDVertex v : getVertexList()) {
224+ skinvfb.put(v.getPos().x).put(v.getPos().y).put(v.getPos().z);
225+ skinnfb.put(v.getNormal().x).put(v.getNormal().y).put(v.getNormal().z);
226+ float f1 = v.getUv().getU();
227+ float f2 = v.getUv().getV();
228+// tfb.put(v.getUv().getU()).put(1f - v.getUv().getV());
229+ f1 = f1 - FastMath.floor(f1);
230+ f2 = f2 - FastMath.floor(f2);
231+ f2 = 1 - f2;
232+ skintfb.put(f1).put(f2);
233+// skintfb.put(v.getUv().getU()).put(1f - v.getUv().getV());
234+// skinbisb.put((short) meshConverter.getSkinMeshData()
235+// .getBoneList().indexOf(v.getBoneNum1()))
236+// .put((short) meshConverter.getSkinMeshData()
237+// .getBoneList().indexOf(v.getBoneNum2()));
238+ short b1 = (short) getBoneList().indexOf(v.getBoneNum1());
239+ short b2 = (short) getBoneList().indexOf(v.getBoneNum2());
240+ if (b1 < 0) {
241+ b1 = 0;
242+ }
243+ if (b2 < 0) {
244+ b2 = 0;
245+ }
246+ skinbisb.put(b1).put(b2);
247+ float weight = (float) v.getBoneWeight() / 100.0f;
248+ wfb.put(weight).put(1f - weight);
249+ }
250+ skinvfb.position(0);
251+ skinvfb2.position(0);
252+ skinvfb2.put(skinvfb);
253+ skinnfb.position(0);
254+// skinnfb2.position(0);
255+// skinnfb2.put(skinnfb);
256+ skinIndexArray = new int[getBoneList().size()];
257+ for (int i = 0; i < skinIndexArray.length; i++) {
258+ if (i < getBoneList().size()) {
259+ skinIndexArray[i] = getBoneList().get(i).shortValue();
260+ } else {
261+ skinIndexArray[i] = 0;
262+ }
263+ }
264+ for (PMDMaterial key : indexMap.keySet()) {
265+ List<Integer> indexList = indexMap.get(key);
266+ ShortBuffer isb = BufferUtils.createShortBuffer(indexList.size());
267+ for (Integer index : indexList) {
268+ isb.put(index.shortValue());
269+ }
270+ indexShortBufferMap.put(key, isb);
271+ }
272+ indexMap = null;
273+ boneList = null;
274+ vertexList = null;
275+ }
276+
277+ List<Integer> getBoneList() {
115278 return boneList;
116279 }
117280
@@ -119,7 +282,6 @@
119282 this.boneList = boneList;
120283 }
121284
122-
123285 public PMDModel getModel() {
124286 return model;
125287 }
@@ -136,7 +298,7 @@
136298 this.vertexList = vertexList;
137299 }
138300
139- public Map<PMDMaterial, List<Integer>> getIndexMap() {
301+ Map<PMDMaterial, List<Integer>> getIndexMap() {
140302 return indexMap;
141303 }
142304
Show on old repository browser