Ejemplo n.º 1
0
    public static  void readTRIS(CoreMesh surf, int surfaceId,int vtStar)
    {

     //   trace("READ TRIS");


        int brushid = file.ReadInt32();
        int TriangleCount =(int)(getChunkSize() / 12);
        bool showwarning = false;
        var vertex_id = new int[3];


        B3DBone bone= new B3DBone();

        while (getChunkSize() != 0)
        {
            vertex_id[0] = file.ReadInt32();
            vertex_id[1] = file.ReadInt32();
            vertex_id[2] = file.ReadInt32();

       
            vertex_id[0] += vtStar;
            vertex_id[1] += vtStar;
            vertex_id[2] += vtStar;

            for (int i=0; i<3;i++)
            {
                if (vertex_id[i] >= AnimatedVertices_VertexID.Count)
                {
                    trace("Illegal vertex index found");
                    return ;
                }

                if (AnimatedVertices_VertexID[ vertex_id[i] ] != -1)
                {
                    if ( AnimatedVertices_BufferID[ vertex_id[i] ] != surfaceId ) //If this vertex is linked in a different meshbuffer
                    {
                        AnimatedVertices_VertexID[ vertex_id[i] ] = -1;
                        AnimatedVertices_BufferID[ vertex_id[i] ] = -1;
                        showwarning = true;

                    }
                }

                if (AnimatedVertices_VertexID[ vertex_id[i] ] == -1) //If this vertex is not in the meshbuffer
                {

                    var vertex = listVertex[ vertex_id[i] ];
                    surf.addVertex(vertex.Pos);
                    surf.addVertexColor(vertex.color);

                    if (vertex.useUv1)
                        surf.addTexCoords(vertex.TCoords0, 0);
                    
                    if (vertex.useUv2)
                        surf.addTexCoords(vertex.TCoords1, 1);
                    
                    if (vertex.useNormals)
                        surf.addNormal(vertex.Normal);


                    
                    
                    bone.vertex.Add(vertex);


                    //create vertex id to meshbuffer index link:
                    AnimatedVertices_VertexID[ vertex_id[i] ] = surf.vertices.Count-1;
                    AnimatedVertices_BufferID[ vertex_id[i] ] = surfaceId;

                }




            }
            surf.addFace(
                AnimatedVertices_VertexID[ vertex_id[0] ],
                AnimatedVertices_VertexID[ vertex_id[1] ],
                AnimatedVertices_VertexID[ vertex_id[2] ]);


          

        }


        if (showwarning)
        {
            Debug.LogWarning("Warning, different meshbuffers linking to the same vertex, this will cause problems with animated meshes");
        }



       
      
        Bones.Add(bone);

      //  surf.material.clone(brushes[brushid]);
      

    }
Ejemplo n.º 2
0
    private static void readMesh(string path, string filename, string texturepath)
    {
        

        if (File.Exists(path + "/" + filename))
        {
                string nm = Path.GetFileNameWithoutExtension(filename);
                importingAssetsDir = "Assets/Prefabs/" + nm + "/";

                if (saveAssets)
                {
                if (!Directory.Exists(importingAssetsDir))
                {
                    Directory.CreateDirectory(importingAssetsDir);
                }
               
                }


            trace("load file :"+path + "/" + filename);

            using (FileStream fs = File.OpenRead(path + "/" + filename))
            {

              
                file = new BinaryReader(fs);
                file.BaseStream.Position = 0;


                string id =readUTFBytes(10);
                int version = readInt();

                Debug.Log("ID:" + id + ", version:" + version);


                int numVerts = (int)file.ReadInt16();

                Debug.Log("Numer of vertex:" + numVerts);
                List<MS3DVertex> vertices = new List<MS3DVertex>();
                for (int i=0; i<numVerts; i++)
                {
                    MS3DVertex vertex = new MS3DVertex();
                    vertices.Add(vertex);
                    
                }

                List<MS3DTriangle> triangles = new List<MS3DTriangle>();
                int numTriangles = (int)file.ReadInt16();

                Debug.Log("Numer of triangles:" + numTriangles);
                for (int i=0; i<numTriangles; i++)
                {
                    MS3DTriangle tri = new MS3DTriangle();
                    triangles.Add(tri);

                }
                 

                int numMeshes=(int)file.ReadInt16();
                Debug.Log("Numer of meshes:" + numMeshes);

                List<MS3DMesh> meshes = new List<MS3DMesh>();
                for (int i = 0; i < numMeshes; i++)
                {
                    MS3DMesh mesh = new MS3DMesh();
                    meshes.Add(mesh);
                }

                int numMaterials=(int)file.ReadInt16();
                Debug.Log("Number  of Materials:" + numMaterials);
                List<MS3DMaterial> materials = new List<MS3DMaterial>();
                for (int i = 0; i < numMaterials; i++)
                {
                    MS3DMaterial material = new MS3DMaterial();

                    if (File.Exists(path + "/" + material.alphaMap))
                    {
                        material.textureDetail = loadexture(path + "/" + material.alphaMap);
                    }
                        
                    if (File.Exists(path + "/" + material.textureMap))
                    {
                        material.texture = loadexture(path + "/" + material.textureMap);
                    }



                    materials.Add(material);
                }

                framesPerSecond = file.ReadSingle();
                float currentTime =file.ReadSingle();
                NumFrames =file.ReadInt32();
                int numJoints = file.ReadInt16();


                Debug.Log("fps:"+framesPerSecond+", time:"+currentTime+", total frames:"+NumFrames+", num joints:"+numJoints);

              
                GameObject ObjectRoot = new GameObject(nm);
                GameObject meshContainer = new GameObject("Surfaces");
                meshContainer.transform.parent = ObjectRoot.transform;

                if (numJoints > 1)
                {
                    AnimationClip clip = new AnimationClip();
                    clip.name = nm + "take00";
                    clip.wrapMode = WrapMode.Loop;

               
                    for (int i = 0; i < numJoints; i++)
                    {
                        isStatic = false;

                        CoreJoint Joint = new CoreJoint();
                        byte flags = file.ReadByte();
                        char[] name = file.ReadChars(32);
                        char[] parentName = file.ReadChars(32);

                   
                        Joint.Name = "";
                        for (int k = 0; k < 32; k++)
                        {
                            if (name[k] == (char)0)
                                break;
                            Joint.Name += name[k];
                        }
                        Joint.ParentName = "";
                        for (int k = 0; k < 32; k++)
                        {
                            if (parentName[k] == (char)0)
                                break;
                            Joint.ParentName += parentName[k];
                        }

                        Joint.joint.name = Joint.Name;


                        //       Debug.Log("Joint name:" + Joint.Name + " , Join Parent:" + Joint.ParentName);

                  
                   
                        Vector3 rotation = Vector3.zero;
                        rotation.x = file.ReadSingle();
                        rotation.y = file.ReadSingle();
                        rotation.z = file.ReadSingle();

                        Joint.position = Vector3.zero;
                        Joint.position.x = file.ReadSingle();
                        Joint.position.y = file.ReadSingle();
                        Joint.position.z = file.ReadSingle();
                        Joint.rotation = QuaternionCreate(rotation);


                        CoreJoint parent = getBoneByName(Joint.ParentName);
                        if (parent != null)
                        {
                            Joint.parent = parent;
                            Joint.joint.transform.parent = parent.joint.transform;    
                            Joint.Path += parent.Path + "/";

                            //    Debug.Log("Bone:"+ Joint.Name+" Parent"+ Joint.ParentName); 

                        }
                        else
                        {
                            Joint.joint.transform.parent = ObjectRoot.transform;
                            //  Debug.LogWarning("Bone:"+ Joint.Name+" dont have parent"); 
                        }
                        Joint.Path += Joint.Name;

                        Joint.joint.transform.localPosition = Joint.position;
                        Joint.joint.transform.localRotation = Joint.rotation;


                 


                 

                        // Debug.Log("Joint: "+Joint.Name+", Position:" + Joint.position +", Rotation: "+rotation);
                        //    Debug.Log("Joint :"+Joint.Name+" , Path:"+Joint.Path);


           
              
                        Joint.numRotKeyFrames = file.ReadInt16();
                        Joint.numPosKeyFrames = file.ReadInt16();
                  
                        float fps = 1.0f/ framesPerSecond;
                    
                        for (int k = 0; k < Joint.numRotKeyFrames; k++)
                        {
                       
                            float time = file.ReadSingle();
                            Vector3 rot = ReadVector3();
                            Quaternion qrot = QuaternionCreate(rot) * Joint.rotation;
                            Joint.addRotationCurve(qrot, time );


                       //     Debug.Log("Frame:" + time + " , time:" + (time*fps));

                        }
                        for (int k = 0; k < Joint.numPosKeyFrames; k++)
                        {
                            float time = file.ReadSingle();
                            Vector3 pos = Joint.position + ReadVector3();
                            Joint.addPositionCurve(pos.x, pos.y, pos.z, time );

                

                           


                        }
                        Joint.addCurves(clip);
                       
                        if (Joint.numRotKeyFrames != Joint.numPosKeyFrames)
                        {
                            Debug.LogError(Joint.numPosKeyFrames + " != " + Joint.numRotKeyFrames);
                        }
                  



                  

               
                        listJoints.Add(Joint);



                   
                    }

                    clip.frameRate = framesPerSecond;
                    clip.legacy = true;
                    Animation anim = (UnityEngine.Animation)ObjectRoot.AddComponent(typeof(Animation));
                    anim.AddClip(clip, clip.name);
                    anim.clip = clip;
                    anim.playAutomatically = true;
         
                    if (saveAssets)
                    {

                        string clipAssetPath = AssetDatabase.GenerateUniqueAssetPath(importingAssetsDir + clip.name + ".asset");
                        AssetDatabase.CreateAsset(clip, clipAssetPath);

                    }
                }//JOINTS

             
                for (int i = 0; i < meshes.Count; i++)
                {
                    MS3DMesh mesh = meshes[i];

                    CoreMesh cmesh = new CoreMesh(meshContainer, ObjectRoot, mesh.name);


                    if (mesh.MaterialIndex >= 0 && mesh.MaterialIndex <= materials.Count)
                    {
                        MS3DMaterial material = materials[mesh.MaterialIndex];    
                        bool isDetail = material.textureDetail != null;
                        if (isDetail)
                        {
                        }
                        else
                        {
                            cmesh.material=new Material(Shader.Find("Diffuse"));
                            cmesh.material.name = material.name;
                            if (material.texture != null)
                            {
                                cmesh.material.mainTexture = material.texture;
                            }

                            if (saveAssets)
                            {
                                string meshAssetPath = AssetDatabase.GenerateUniqueAssetPath(importingAssetsDir + cmesh.material.name + ".asset");
                       
                                AssetDatabase.CreateAsset(cmesh.material, meshAssetPath);
                            }

                        }


                    }



                    for (int j = 0; j < mesh.numTriangles; j++)
                    {
                        if(!isStatic)
                        {
                   
                            VertexBone vtx0 = new VertexBone();
                            cmesh.vertex.Add(vtx0);

                            VertexBone vtx1 = new VertexBone();
                  
                            cmesh.vertex.Add(vtx1);

                            VertexBone vtx2 = new VertexBone();
                     
                            cmesh.vertex.Add(vtx2);

                        }

                        
                        int index0 = triangles[mesh.TriangleIndices[j]].indice0;
                        Vector3 v0 = vertices[index0].Vertex;
                        Vector3 n0 = triangles[mesh.TriangleIndices[j]].normal0;
                        float u0 = triangles[mesh.TriangleIndices[j]].s.x;
                        float t0 = 1f * -triangles[mesh.TriangleIndices[j]].t.x;

                        int index1 = triangles[mesh.TriangleIndices[j]].indice1;
                        Vector3 v1 = vertices[index1].Vertex;
                        Vector3 n1 = triangles[mesh.TriangleIndices[j]].normal1;
                        float u1 = triangles[mesh.TriangleIndices[j]].s.y;
                        float t1 = 1f * -triangles[mesh.TriangleIndices[j]].t.y;


                        int index2 = triangles[mesh.TriangleIndices[j]].indice2;
                        Vector3 v2 = vertices[index2].Vertex;
                        Vector3 n2 = triangles[mesh.TriangleIndices[j]].normal2;
                        float u2 = triangles[mesh.TriangleIndices[j]].s.z;
                        float t2 = 1f * -triangles[mesh.TriangleIndices[j]].t.z;

                        int f0 = cmesh.addVertex(v0);
                                 cmesh.addNormal(n0);
                                 cmesh.addTexCoords(new Vector2(u0, t0), 0);


                        int f1 = cmesh.addVertex(v1);
                        cmesh.addNormal(n1);
                        cmesh.addTexCoords(new Vector2(u1, t1), 0);


                        int f2 = cmesh.addVertex(v2);
                        cmesh.addNormal(n2);
                        cmesh.addTexCoords(new Vector2(u2, t2), 0);

                        cmesh.addFace(f0, f1, f2);

                        if (!isStatic)
                        {
                            
                            int Bone0 = vertices[index0].boneId;
                            int Bone1 = vertices[index1].boneId;
                            int Bone2 = vertices[index2].boneId;

                            cmesh.vertex[f0].addBone(Bone0, 1);
                            cmesh.vertex[f1].addBone(Bone1, 1);
                            cmesh.vertex[f2].addBone(Bone2, 1);

                        }

                     

                    

                    }
                   
                    surfaces.Add(cmesh);
                }

                for (int i = 0; i < meshes.Count; i++)
                {
                    MS3DMesh mesh = meshes[i];
                    CoreMesh cmesh = surfaces[i];

                    if (!isStatic)
                    {
                        
                        for (int j = 0; j < cmesh.vertex.Count; j++)
                        {
                            isStatic = false;

                            VertexBone vertex = cmesh.vertex[j];
                            //  Debug.Log("Num Bone:" + vertex.numBones);

                            BoneWeight b = new BoneWeight();
                            b.boneIndex0 = vertex.bones[0].boneId;
                            b.weight0 = vertex.bones[0].Weight;

                      

                            cmesh.addBone(b);


                        }
                    }
                    cmesh.build();
                    if (saveAssets)
                    {
                        string meshAssetPath = AssetDatabase.GenerateUniqueAssetPath(importingAssetsDir + cmesh.name+"_"+i + ".asset");
                        AssetDatabase.CreateAsset(cmesh.geometry, meshAssetPath);
                    }
                }



                if (saveAssets)
                {

                    string prefabPath = AssetDatabase.GenerateUniqueAssetPath(importingAssetsDir + filename + ".prefab");
                    var prefab = PrefabUtility.CreateEmptyPrefab(prefabPath);
                    PrefabUtility.ReplacePrefab(ObjectRoot, prefab, ReplacePrefabOptions.ConnectToPrefab);
                    AssetDatabase.Refresh();
                }

                materials.Clear();
                meshes.Clear();
                triangles.Clear();
                vertices.Clear();

                materials = null;
                meshes = null;
                triangles = null;
                vertices = null;


               




            
            }//file open

            Debug.Log(path + "/" + filename + " Imported ;) ");
        }

    }