示例#1
0
    void Start()
    {
        prefAnimation.onClick.AddListener(clickPref);
        nextAnimation.onClick.AddListener(clickNext);


        animation1 = model1.GetComponent("MD2Model") as MD2Model;
        animation2 = model2.GetComponent("MD2Model") as MD2Model;
        animation3 = model3.GetComponent("MD2Model") as MD2Model;

        if (!animation1)
        {
            Debug.LogError("game object dont have md2");
        }
        if (!animation2)
        {
            Debug.LogError("game object dont have md2");
        }
        if (!animation3)
        {
            Debug.LogError("game object dont have md2");
        }
    }
示例#2
0
	void Start () 
	{
		prefAnimation.onClick.AddListener (clickPref);
		nextAnimation.onClick.AddListener(clickNext);


		animation1 = model1.GetComponent("MD2Model") as MD2Model;
		animation2 = model2.GetComponent("MD2Model") as MD2Model;
		animation3 = model3.GetComponent("MD2Model") as MD2Model;

		if (!animation1) 
		{
			Debug.LogError("game object dont have md2");
		}
		if (!animation2) 
		{
			Debug.LogError("game object dont have md2");
		}
		if (!animation3) 
		{
			Debug.LogError("game object dont have md2");
		}

	}
 /// <summary>
 /// Initiates loading of the model through the content pipeline using
 /// the content importer and processor.  Also loads texture through
 /// content pipeline. (Must use an XNA friendly texture format like
 /// *.tga, *.png, *.bmp, *.jpg. Native Quake *.pcx files won't load.
 /// </summary>
 /// <param name="graphics">Graphics device handle needed to initialize vertex buffer.</param>
 /// <param name="model">Name of model and file path.</param>
 /// <param name="skin">Name of texture and file path.</param>
 /// <param name="content">Content manager object for loading through pipeline.</param>
 public void LoadModel(GraphicsDevice graphics, string model, 
     string skin, ContentManager content)
 {
     md2         = content.Load<MD2Model>(model);
     numFaces    = md2.numFaces;
     texture     = content.Load<Texture2D>(skin);
     InitializeVertexBuffer(graphics);
 }
示例#4
0
    public static void readMesh(string path, string filename)
    {
        if (File.Exists(path + "/" + filename))
        {
            string     nm         = Path.GetFileNameWithoutExtension(filename);
            GameObject ObjectRoot = new GameObject(nm);
            MD2Model   model      = (MD2Model)ObjectRoot.AddComponent(typeof(MD2Model));
            model.setup();



            int            vertex_Count;
            int            uv_count;
            int            face_Count;
            int            frames_Count;
            List <Face>    faces    = new List <Face> ();
            List <Face>    tfaces   = new List <Face> ();
            List <Vector2> uvCoords = new List <Vector2> ();


            using (FileStream fs = File.OpenRead(path + "/" + filename)) {
                BinaryReader file = new BinaryReader(fs);

                int Magic            = file.ReadInt32();
                int Version          = file.ReadInt32();
                int SkinWidth        = file.ReadInt32();
                int SkinHeight       = file.ReadInt32();
                int FrameSize        = file.ReadInt32();
                int NumSkins         = file.ReadInt32();
                int NumVertices      = file.ReadInt32();
                int NumTexCoords     = file.ReadInt32();
                int NumTriangles     = file.ReadInt32();
                int NumGlCommands    = file.ReadInt32();
                int NumFrames        = file.ReadInt32();
                int OffsetSkins      = file.ReadInt32();
                int OffsetTexCoords  = file.ReadInt32();
                int OffsetTriangles  = file.ReadInt32();
                int OffsetFrames     = file.ReadInt32();
                int OffsetGlCommands = file.ReadInt32();
                int OffsetEnd        = file.ReadInt32();

                frames_Count = NumFrames;
                vertex_Count = NumVertices;
                face_Count   = NumTriangles;
                uv_count     = NumTexCoords;


                file.BaseStream.Seek(OffsetTriangles, SeekOrigin.Begin);
                Debug.Log("Num triangles" + NumTriangles);
                for (int i = 0; i < NumTriangles; i++)
                {
                    int v0, v1, v2;

                    //vertex face
                    v0 = (int)file.ReadUInt16();
                    v1 = (int)file.ReadUInt16();
                    v2 = (int)file.ReadUInt16();

                    faces.Add(new Face(v0, v1, v2));

                    //texture faces
                    v0 = (int)file.ReadUInt16();
                    v1 = (int)file.ReadUInt16();
                    v2 = (int)file.ReadUInt16();
                    tfaces.Add(new Face(v0, v1, v2));
                }

                file.BaseStream.Seek(OffsetTexCoords, SeekOrigin.Begin);
                Debug.Log("Num TextureCoord" + NumTexCoords);
                for (int i = 0; i < NumTexCoords; i++)
                {
                    float u = (float)file.ReadInt16() / SkinWidth;
                    float v = (float)1 * -file.ReadInt16() / SkinWidth;
                    uvCoords.Add(new Vector2(u, v));
                }

                //**************************************************
                file.BaseStream.Seek(OffsetFrames, SeekOrigin.Begin);
                Debug.Log("Num Frames" + OffsetFrames);
                for (int i = 0; i < NumFrames; i++)
                {
                    Vector3 Scale     = Vector3.zero;
                    Vector3 Translate = Vector3.zero;

                    Scale.x = file.ReadSingle();
                    Scale.y = file.ReadSingle();
                    Scale.z = file.ReadSingle();

                    Translate.x = file.ReadSingle();
                    Translate.y = file.ReadSingle();
                    Translate.z = file.ReadSingle();

                    char[] name = file.ReadChars(16);


                    //Debug.LogWarning(new string(name));

                    for (int j = 0; j < NumVertices; j++)
                    {
                        byte x = file.ReadByte();
                        byte y = file.ReadByte();
                        byte z = file.ReadByte();
                        byte w = file.ReadByte();
                        //	System.BitConverter.ToSingle(buffer, 0);


                        float sx = Scale.x * x + Translate.x;
                        float sy = Scale.z * z + Translate.z;
                        float sz = Scale.y * y + Translate.y;

                        model.vertex.Add(new Vector3(sx, sy, sz));
                    }
                }

                //meshFilter.sharedMesh =Surface.createCube ("cube");
                for (int f = 0; f < NumFrames; f++)
                {
                    Surface surface = new Surface("frame");
                    for (int i = 0; i < faces.Count; i++)
                    {
                        Vector3 v1  = model.vertex[f * vertex_Count + faces[i].v0];
                        Vector3 v2  = model.vertex[f * vertex_Count + faces[i].v1];
                        Vector3 v3  = model.vertex[f * vertex_Count + faces[i].v2];
                        Vector2 uv1 = uvCoords[0 * uv_count + tfaces[i].v0];
                        Vector2 uv2 = uvCoords[0 * uv_count + tfaces[i].v1];
                        Vector2 uv3 = uvCoords[0 * uv_count + tfaces[i].v2];
                        surface.addFace(v1, v2, v3, uv1, uv2, uv3);
                    }
                    surface.build();
                    surface.RecalculateNormals();
                    surface.Optimize();
                    model.Frames.Add(surface.getMesh());
                }

                model.meshFilter.sharedMesh = model.Frames[0];
                model.ready = true;

                Debug.LogWarning("read ok");
            }
        }
        else
        {
            Debug.LogError(filename + " dont exits");
        }
    }