2 * building shape information
\r
4 * License : The MIT License
\r
5 * Copyright(c) 2010 MikuToga Partners
\r
8 package jp.sourceforge.mikutoga.pmd.pmdloader;
\r
10 import jp.sourceforge.mikutoga.corelib.ListUtil;
\r
11 import java.util.Iterator;
\r
12 import java.util.List;
\r
13 import java.util.RandomAccess;
\r
14 import jp.sourceforge.mikutoga.parser.ParseStage;
\r
15 import jp.sourceforge.mikutoga.parser.pmd.PmdShapeHandler;
\r
16 import jp.sourceforge.mikutoga.pmd.BoneInfo;
\r
17 import jp.sourceforge.mikutoga.pmd.PmdModel;
\r
18 import jp.sourceforge.mikutoga.pmd.Pos2d;
\r
19 import jp.sourceforge.mikutoga.pmd.Pos3d;
\r
20 import jp.sourceforge.mikutoga.pmd.Surface;
\r
21 import jp.sourceforge.mikutoga.pmd.Vec3d;
\r
22 import jp.sourceforge.mikutoga.pmd.Vertex;
\r
25 * モデル形状に関する通知をパーサから受け取る。
\r
27 class ShapeBuilder implements PmdShapeHandler {
\r
29 private final List<Vertex> vertexList;
\r
30 private final List<BoneInfo> boneList;
\r
31 private final List<Surface> surfaceList;
\r
33 private Iterator<Vertex> vertexIt;
\r
34 private Vertex currentVertex = null;
\r
36 private Iterator<Surface> surfaceIt;
\r
37 private Surface currentSurface = null;
\r
43 ShapeBuilder(PmdModel model){
\r
46 this.vertexList = model.getVertexList();
\r
47 this.boneList = model.getBoneList();
\r
48 this.surfaceList = model.getSurfaceList();
\r
50 assert this.vertexList instanceof RandomAccess;
\r
51 assert this.surfaceList instanceof RandomAccess;
\r
52 assert this.boneList instanceof RandomAccess;
\r
59 * すでに指定位置にボーンがあればなにもしない。
\r
60 * @param id 0から始まるボーン番号
\r
63 private BoneInfo prepareBone(int id){
\r
64 ListUtil.extendCollection(this.boneList, id + 1);
\r
65 BoneInfo bone = this.boneList.get(id);
\r
67 bone = new BoneInfo();
\r
68 bone.setSerialNumber(id);
\r
69 this.boneList.set(id, bone);
\r
76 * @param stage {@inheritDoc}
\r
77 * @param loops {@inheritDoc}
\r
79 public void loopStart(ParseStage stage, int loops){
\r
80 if(stage == PmdShapeHandler.VERTEX_LIST){
\r
81 ListUtil.prepareDefConsList(this.vertexList, Vertex.class, loops);
\r
82 ListUtil.assignIndexedSerial(this.vertexList);
\r
84 this.vertexIt = this.vertexList.iterator();
\r
85 if(this.vertexIt.hasNext()){
\r
86 this.currentVertex = this.vertexIt.next();
\r
88 }else if(stage == PmdShapeHandler.SURFACE_LIST){
\r
89 ListUtil.prepareDefConsList(this.surfaceList,
\r
90 Surface.class, loops );
\r
91 ListUtil.assignIndexedSerial(this.surfaceList);
\r
93 this.surfaceIt = this.surfaceList.iterator();
\r
94 if(this.surfaceIt.hasNext()){
\r
95 this.currentSurface = this.surfaceIt.next();
\r
99 throw new AssertionError();
\r
106 * @param stage {@inheritDoc}
\r
108 public void loopNext(ParseStage stage){
\r
109 if(stage == PmdShapeHandler.VERTEX_LIST){
\r
110 if(this.vertexIt.hasNext()){
\r
111 this.currentVertex = this.vertexIt.next();
\r
113 }else if(stage == PmdShapeHandler.SURFACE_LIST){
\r
114 if(this.surfaceIt.hasNext()){
\r
115 this.currentSurface = this.surfaceIt.next();
\r
119 throw new AssertionError();
\r
126 * @param stage {@inheritDoc}
\r
128 public void loopEnd(ParseStage stage){
\r
134 * @param xPos {@inheritDoc}
\r
135 * @param yPos {@inheritDoc}
\r
136 * @param zPos {@inheritDoc}
\r
138 public void pmdVertexPosition(float xPos, float yPos, float zPos){
\r
139 Pos3d position = this.currentVertex.getPosition();
\r
140 position.setXPos(xPos);
\r
141 position.setYPos(yPos);
\r
142 position.setZPos(zPos);
\r
148 * @param xVec {@inheritDoc}
\r
149 * @param yVec {@inheritDoc}
\r
150 * @param zVec {@inheritDoc}
\r
152 public void pmdVertexNormal(float xVec, float yVec, float zVec){
\r
153 Vec3d normal = this.currentVertex.getNormal();
\r
154 normal.setXVal(xVec);
\r
155 normal.setYVal(yVec);
\r
156 normal.setZVal(zVec);
\r
162 * @param uVal {@inheritDoc}
\r
163 * @param vVal {@inheritDoc}
\r
165 public void pmdVertexUV(float uVal, float vVal){
\r
166 Pos2d uv = this.currentVertex.getUVPosition();
\r
174 * @param boneId1 {@inheritDoc}
\r
175 * @param boneId2 {@inheritDoc}
\r
176 * @param weightForB1 {@inheritDoc}
\r
178 public void pmdVertexWeight(int boneId1, int boneId2, int weightForB1){
\r
179 BoneInfo bone1 = prepareBone(boneId1);
\r
180 BoneInfo bone2 = prepareBone(boneId2);
\r
182 this.currentVertex.setBonePair(bone1, bone2);
\r
183 this.currentVertex.setWeightA(weightForB1);
\r
190 * @param hideEdge {@inheritDoc}
\r
192 public void pmdVertexEdge(boolean hideEdge){
\r
193 this.currentVertex.setEdgeAppearance( ! hideEdge );
\r
199 * @param vertexId1 {@inheritDoc}
\r
200 * @param vertexId2 {@inheritDoc}
\r
201 * @param vertexId3 {@inheritDoc}
\r
203 public void pmdSurfaceTriangle(int vertexId1,
\r
206 Vertex vtx1 = this.vertexList.get(vertexId1);
\r
207 Vertex vtx2 = this.vertexList.get(vertexId2);
\r
208 Vertex vtx3 = this.vertexList.get(vertexId3);
\r
210 this.currentSurface.setTriangle(vtx1, vtx2, vtx3);
\r