示例#1
0
    public void createOGJModel(string filePath, string fileName)
    {
        OBJModel objmodel = new OBJModel(filePath, fileName);

        objmodel.transform.localScale = new Vector3(0.2f, 0.2f, 0.2f);
        objmodel.transform.parent     = father.transform;
        objmodel.transform.position   = spawn.position;
        objmodel.transform.rotation   = spawn.rotation;
        Global.objDict.Add(objmodel.Id, objmodel);
    }
示例#2
0
    public void createSixAngle()
    {
        OBJModel objmodel = new OBJModel(Application.dataPath + "//Resources//", "sixSide.obj");

        objmodel.transform.localScale = new Vector3(0.2f, 0.2f, 0.2f);
        objmodel.transform.parent     = father.transform;
        objmodel.transform.position   = spawn.transform.position;
        objmodel.transform.rotation   = spawn.transform.rotation;
        Global.objDict.Add(objmodel.Id, objmodel);
    }
示例#3
0
        /// <summary>
        /// Loads a nif model
        /// </summary>
        /// <param name="nif"></param>
        public NIFModel(Stream nif)
        {
            NiFile nifile;

            using (var r = new BinaryReader(nif)) {
                nifile = new NiFile(r);
            }
            var obj = Ni2Obj.ToObj(nifile);

            using (var ms = new MemoryStream()) {
                obj.Save(new StreamWriter(ms));
                ms.Position = 0;
                _objModel   = OBJLoader.Load(new StreamReader(ms));
            }
        }
示例#4
0
 private void OnBtnClick(string name)
 {
     print("Result " + name);
     if (name.EndsWith(".stl"))
     {
         STLModel stlobj = new STLModel(Application.dataPath + "//Resources//Models//", name);
         stlobj.BaseObj.name = "newobject";
         stlobj.BaseObj.AddComponent <Rigidbody>();
         stlobj.BaseObj.GetComponent <Rigidbody>().useGravity  = false;
         stlobj.BaseObj.GetComponent <Rigidbody>().isKinematic = false;
         stlobj.BaseObj.AddComponent <ModelEditor>();
         // stlobj.BaseObj.GetComponent<ModelEditor>().enabled = false;
         stlobj.transform.parent = Global.sceneSet[Global.sceneSet.Count - 1].transform;
         Global.objDict.Add(stlobj.Id, stlobj);
     }
     else if (name.EndsWith(".off"))
     {
         OFFModel offobj = new OFFModel(Application.dataPath + "//Resources//Models//", name);
         // offobj.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);
         // offobj.transform.position = spawn.transform.position;
         // offobj.transform.rotation = spawn.transform.rotation;
         offobj.BaseObj.name = "newobject";
         offobj.BaseObj.AddComponent <Rigidbody>();
         offobj.BaseObj.GetComponent <Rigidbody>().useGravity  = false;
         offobj.BaseObj.GetComponent <Rigidbody>().isKinematic = false;
         offobj.BaseObj.AddComponent <ModelEditor>();
         // offobj.BaseObj.GetComponent<ModelEditor>().enabled = false;
         offobj.transform.parent = Global.sceneSet[Global.sceneSet.Count - 1].transform;
         Global.objDict.Add(offobj.Id, offobj);
     }
     else if (name.EndsWith(".obj"))
     {
         OBJModel objobj = new OBJModel(Application.dataPath + "//Resources//Models//", name);
         // objobj.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);
         // objobj.transform.position = spawn.transform.position;
         // objobj.transform.rotation = spawn.transform.rotation;
         objobj.BaseObj.name = "newobject";
         objobj.BaseObj.AddComponent <Rigidbody>();
         objobj.BaseObj.GetComponent <Rigidbody>().useGravity  = false;
         objobj.BaseObj.GetComponent <Rigidbody>().isKinematic = false;
         objobj.BaseObj.AddComponent <ModelEditor>();
         // objobj.BaseObj.GetComponent<ModelEditor>().enabled = false;
         objobj.transform.parent = Global.sceneSet[Global.sceneSet.Count - 1].transform;
         Global.objDict.Add(objobj.Id, objobj);
     }
     this.gameObject.SetActive(false);
 }
示例#5
0
    static public OBJModel LoadModel(string path)
    {
        if (!File.Exists(path))
        {
            return(null);
        }

        string[] lines = File.ReadAllLines(path);

        List <Vector3> positionList = new List <Vector3>();
        List <Vector2> texcoordList = new List <Vector2>();
        List <Vector3> normalList   = new List <Vector3>();

        MeshBuilder     builder = new MeshBuilder();
        List <OBJGroup> groups  = new List <OBJGroup>();

        var materials = LoadMTL(Path.GetDirectoryName(path) + "/" + Path.GetFileNameWithoutExtension(path) + ".mtl");

        foreach (string line in lines)
        {
            if (line.Length == 0)
            {
                continue;
            }
            if (line[0] == '#')
            {
                continue;
            }

            string[] tokens = line.Split(' ');
            if (tokens[0] == "v")
            {
                positionList.Add(ParseVector3(tokens));
            }
            else if (tokens[0] == "vt")
            {
                texcoordList.Add(ParseVector2(tokens) * new Vector2(1.0f, -1.0f));
            }
            else if (tokens[0] == "vn")
            {
                normalList.Add(ParseVector3(tokens));
            }
            else if (tokens[0] == "g")
            {
                if (builder.vertices.Count > 0)
                {
                    groups.Add(builder.Build(materials));
                }
                builder          = new MeshBuilder();
                builder.meshName = tokens[1];
            }
            else if (tokens[0] == "usemtl")
            {
                builder.mtlName = tokens[1];
            }
            else if (tokens[0] == "f")
            {
                for (int i = 1; i < tokens.Length; i++)
                {
                    string[] index = tokens[i].Split('/');

                    if (index.Length >= 1)
                    {
                        builder.vertices.Add(positionList[int.Parse(index[0]) - 1] * 0.01f);
                    }
                    if (index.Length >= 2)
                    {
                        builder.uv.Add(texcoordList[int.Parse(index[1]) - 1]);
                    }
                    if (index.Length >= 3)
                    {
                        builder.normals.Add(normalList[int.Parse(index[2]) - 1]);
                    }
                }

                int vertPerFace = tokens.Length - 1;
                int offset      = builder.vertices.Count - vertPerFace;
                if (vertPerFace == 3)
                {
                    builder.triangles.Add(offset + 0);
                    builder.triangles.Add(offset + 1);
                    builder.triangles.Add(offset + 2);
                }
                else if (vertPerFace == 4)
                {
                    builder.triangles.Add(offset + 0);
                    builder.triangles.Add(offset + 1);
                    builder.triangles.Add(offset + 2);
                    builder.triangles.Add(offset + 0);
                    builder.triangles.Add(offset + 2);
                    builder.triangles.Add(offset + 3);
                }
            }
        }

        if (builder.vertices.Count > 0)
        {
            groups.Add(builder.Build(materials));
        }

        var loadedObj = new OBJModel();

        loadedObj.name      = Path.GetFileNameWithoutExtension(path);
        loadedObj.groups    = groups.ToArray();
        loadedObj.materials = materials.ToArray();
        return(loadedObj);
    }