📄 milktojme.java
字号:
int nNumMaterials=inFile.readUnsignedShort();
for (int i=0;i<nNumMaterials;i++){
inFile.skipBytes(32); // Skip the name, unused
MaterialState matState=DisplaySystem.getDisplaySystem().getRenderer().createMaterialState();
matState.setAmbient(getNextColor());
matState.setDiffuse(getNextColor());
matState.setSpecular(getNextColor());
matState.setEmissive(getNextColor());
matState.setShininess(inFile.readFloat());
float alpha = inFile.readFloat();
matState.getDiffuse().a = alpha;
matState.getEmissive().a = alpha;
matState.getAmbient().a = alpha;
inFile.readByte(); // Mode is ignored
inFile.readFully(tempChar,0,128);
TextureState texState=null;
String texFile=cutAtNull(tempChar);
if (texFile.length()!=0){
URL texURL = ResourceLocatorTool.locateResource(
ResourceLocatorTool.TYPE_TEXTURE, texFile);
if (texURL != null) {
texState = DisplaySystem.getDisplaySystem().getRenderer()
.createTextureState();
Texture tempTex = new Texture2D();
tempTex.setTextureKey(new TextureKey(texURL, true,
TextureManager.COMPRESS_BY_DEFAULT ? Image.Format.Guess
: Image.Format.GuessNoCompression));
tempTex.setAnisotropicFilterPercent(0.0f);
tempTex.setMinificationFilter(Texture.MinificationFilter.BilinearNearestMipMap);
tempTex.setMagnificationFilter(Texture.MagnificationFilter.Bilinear);
tempTex.setWrap(Texture.WrapMode.Repeat);
texState.setTexture(tempTex);
}
}
inFile.readFully(tempChar,0,128); // Alpha map, but it is ignored
//TODO: Implement Alpha Maps
applyStates(matState,texState,i);
}
}
/**
* Every child of finalNode (IE the .ms3d file) whos materialIndex is the given index, gets the two RenderStates applied
* @param matState A Material state to apply
* @param texState A TextureState to apply
* @param index The index that a TriMesh should have from <code>materialIndex</code> to get the given
* states
*/
private void applyStates(MaterialState matState, TextureState texState,int index) {
for (int i=0;i<finalNode.getQuantity();i++){
if (materialIndexes[i]==index){
if (matState!=null) ((TriMesh)finalNode.getChild(i)).setRenderState(matState);
if (texState!=null) ((TriMesh)finalNode.getChild(i)).setRenderState(texState);
}
}
}
private ColorRGBA getNextColor() throws IOException {
return new ColorRGBA(
inFile.readFloat(),
inFile.readFloat(),
inFile.readFloat(),
inFile.readFloat()
);
}
private void readGroups() throws IOException {
int nNumGroups=inFile.readUnsignedShort();
materialIndexes=new int[nNumGroups];
for (int i=0;i<nNumGroups;i++){
inFile.readByte(); // Ignore flags
inFile.readFully(tempChar,0,32); // Name
int numTriLocal=inFile.readUnsignedShort();
Vector3f[] meshVerts=new Vector3f[numTriLocal*3],meshNormals=new Vector3f[numTriLocal*3];
Vector3f[] origVerts=new Vector3f[numTriLocal*3],origNormals=new Vector3f[numTriLocal*3];
Vector2f[] meshTexCoords=new Vector2f[numTriLocal*3];
int[] meshIndex=new int[numTriLocal*3];
int[] jointIndex=new int[numTriLocal*3];
JointMesh theMesh=new JointMesh(cutAtNull(tempChar));
for (int j=0;j<numTriLocal;j++){
int triIndex=inFile.readUnsignedShort();
for (int k=0;k<3;k++){
meshVerts [j*3+k]=myVerts[myTris[triIndex].vertIndicies[k]].vertex;
meshNormals [j*3+k]=myTris[triIndex].vertNormals[k];
meshTexCoords [j*3+k]=myTris[triIndex].vertTexCoords[k];
meshIndex [j*3+k]=j*3+k;
origVerts [j*3+k]=meshVerts[j*3+k];
origNormals [j*3+k]=meshNormals[j*3+k];
jointIndex [j*3+k]=myVerts[myTris[triIndex].vertIndicies[k]].boneID;
}
}
theMesh.reconstruct(BufferUtils.createFloatBuffer(meshVerts),
BufferUtils.createFloatBuffer(meshNormals), null,
TexCoords.makeNew(meshTexCoords),
BufferUtils.createIntBuffer(meshIndex));
theMesh.originalNormal=origNormals;
theMesh.originalVertex=origVerts;
theMesh.jointIndex=jointIndex;
finalNode.attachChild(theMesh);
materialIndexes[i]=inFile.readByte();
}
}
private void readTriangles() throws IOException {
nNumTriangles=inFile.readUnsignedShort();
myTris=new MilkTriangle[nNumTriangles];
for (int i=0;i<nNumTriangles;i++){
int j;
myTris[i]=new MilkTriangle();
inFile.readUnsignedShort(); // Ignore flags
for (j=0;j<3;j++)
myTris[i].vertIndicies[j]=inFile.readUnsignedShort();
for (j=0;j<3;j++){
myTris[i].vertNormals[j]=new Vector3f(
inFile.readFloat(),
inFile.readFloat(),
inFile.readFloat()
);
}
for (j=0;j<3;j++){
myTris[i].vertTexCoords[j]=new Vector2f();
myTris[i].vertTexCoords[j].x=inFile.readFloat();
}
for (j=0;j<3;j++)
myTris[i].vertTexCoords[j].y=1-inFile.readFloat();
inFile.readByte(); // Ignore smoothingGroup
inFile.readByte(); // Ignore groupIndex
}
}
private void readVerts() throws IOException {
nNumVertices=inFile.readUnsignedShort();
myVerts=new MilkVertex[nNumVertices];
for (int i=0;i<nNumVertices;i++){
myVerts[i]=new MilkVertex();
inFile.readByte(); // Ignore flags
myVerts[i].vertex=new Vector3f(
inFile.readFloat(),
inFile.readFloat(),
inFile.readFloat()
);
myVerts[i].boneID=inFile.readByte();
inFile.readByte(); // Ignore referenceCount
}
}
private void checkHeader() throws IOException {
inFile.readFully(tempChar,0,10);
if (!"MS3D000000".equals(new String(tempChar,0,10))) throw new JmeException("Wrong File type: not Milkshape file??");
if (inFile.readInt()!=4) throw new JmeException("Wrong file version: Not 4"); // version
}
private static class MilkVertex{
Vector3f vertex;
byte boneID;
}
private static class MilkTriangle{
int[] vertIndicies=new int[3]; // 3 ints
Vector3f[] vertNormals=new Vector3f[3]; // 3 Vector3fs
Vector2f[] vertTexCoords=new Vector2f[3]; // 3 Texture Coords
}
private static String cutAtNull(byte[] inString) {
for (int i=0;i<inString.length;i++)
if (inString[i]==0) return new String(inString,0,i);
return new String(inString);
}
/**
* This function returns the controller of a loaded Milkshape3D model. Will return
* null if a correct JointController could not be found, or if one does not exist.
* @param model The model that was loaded.
* @return The controller for that milkshape model.
*/
public static JointController findController(Node model){
if (model.getQuantity()==0 ||
model.getChild(0).getControllers().size()==0 ||
!(model.getChild(0).getController(0) instanceof JointController))
return null;
return (JointController) (model.getChild(0)).getController(0);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -