Example #1
0
    void Start()
    {
        theChosenFaceIdx = -1;
        //--------------------------------------------------------------
        HSpheres  = new List <List <GameObject> >();
        m_spheres = new GameObject[3];
        for (int i = 0; i < 3; i++)
        {
            m_spheres[i] = GameObject.CreatePrimitive(PrimitiveType.Sphere);
            m_spheres[i].transform.position   = new Vector3(0, 0, 0);
            m_spheres[i].transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);
        }
        //--------------------------------------------------------------
        HSpheres = new List <List <GameObject> >();
        //--------------------------------------------------------------
        objPath      = "D:/repo/NewUnityProject/Assets/Resources/objWithUv.obj";
        pts_         = new List <Vector3>();
        faces_       = new List <int>();
        uvs_         = new List <Vector2>();
        vn_          = new List <Vector3>();
        faceCenters_ = new List <Vector3>();
        FileStream   aFile   = new FileStream(objPath, FileMode.Open);
        StreamReader reader  = new StreamReader(aFile);
        string       strLine = reader.ReadLine();

        while (true)
        {
            strLine = reader.ReadLine();
            if (strLine == null)
            {
                break;
            }
            if (strLine.StartsWith("v "))
            {
                string[] sArray = Regex.Split(strLine, " ", RegexOptions.IgnoreCase);
                float    x      = float.Parse(sArray[1]);
                float    y      = float.Parse(sArray[2]);
                float    z      = float.Parse(sArray[3]);
                Vector3  thisPt = new Vector3(x, y, z);
                pts_.Add(thisPt);
            }
            else if (strLine.StartsWith("vt "))
            {
                string[] sArray = Regex.Split(strLine, " ", RegexOptions.IgnoreCase);
                double   x      = double.Parse(sArray[1]);
                double   y      = double.Parse(sArray[2]);
                Vector2  thisUv = new Vector2((float)x, (float)y);
                uvs_.Add(thisUv);
            }
            else if (strLine.StartsWith("#vn "))
            {
                string[] sArray = Regex.Split(strLine, " ", RegexOptions.IgnoreCase);
                double   x      = double.Parse(sArray[1]);
                double   y      = double.Parse(sArray[2]);
                double   z      = double.Parse(sArray[3]);
                Vector3  thisN  = new Vector3((float)x, (float)y, (float)z);
                vn_.Add(thisN);
            }
            else if (strLine.StartsWith("#faceCenter "))
            {
                string[] sArray         = Regex.Split(strLine, " ", RegexOptions.IgnoreCase);
                double   x              = double.Parse(sArray[1]);
                double   y              = double.Parse(sArray[2]);
                double   z              = double.Parse(sArray[3]);
                Vector3  thisFaceCenter = new Vector3((float)x, (float)y, (float)z);
                faceCenters_.Add(thisFaceCenter);
            }
            else if (strLine.StartsWith("f "))
            {
                int      ptA    = 0;
                int      ptB    = 0;
                int      ptC    = 0;
                string[] sArray = Regex.Split(strLine, " ", RegexOptions.IgnoreCase);
                {
                    string[] ptAndUvStrs = Regex.Split(sArray[1], "/", RegexOptions.IgnoreCase);
                    ptA = int.Parse(ptAndUvStrs[0]) - 1;
                }
                {
                    string[] ptAndUvStrs = Regex.Split(sArray[2], "/", RegexOptions.IgnoreCase);
                    ptB = int.Parse(ptAndUvStrs[0]) - 1;
                }
                {
                    string[] ptAndUvStrs = Regex.Split(sArray[3], "/", RegexOptions.IgnoreCase);
                    ptC = int.Parse(ptAndUvStrs[0]) - 1;
                }
                faces_.Add(ptA);
                faces_.Add(ptB);
                faces_.Add(ptC);
                //faces_.Add(ptA);
                //faces_.Add(ptC);
                //faces_.Add(ptB);
            }
            else
            {
                continue;
            }
        }
        reader.Close();
        Vector3[] pts   = pts_.ToArray();
        int[]     faces = faces_.ToArray();
        Vector2[] uvs   = uvs_.ToArray();
        int       num   = pts.Length;

        faceNum = num;


        List <Vector3> ptsOrderByX = new List <Vector3>();

        ptsOrderByX = pts_.OrderBy(tt => (tt.x)).ToList();
        List <Vector3> ptsOrderByY = new List <Vector3>();

        ptsOrderByY = pts_.OrderBy(tt => (tt.y)).ToList();
        List <Vector3> ptsOrderByZ = new List <Vector3>();

        ptsOrderByZ = pts_.OrderBy(tt => (tt.z)).ToList();

        Vector3 meshCenter = new Vector3(0.5f * (ptsOrderByX[0].x + ptsOrderByX[num - 1].x),
                                         0.5f * (ptsOrderByY[0].y + ptsOrderByY[num - 1].y),
                                         0.5f * (ptsOrderByZ[0].z + ptsOrderByZ[num - 1].z));

        CameraMove.setRotationCenter(meshCenter);

        obj      = new GameObject();
        obj.name = "back";
        MeshFilter   mfilter_ = obj.AddComponent <MeshFilter>();
        Texture2D    t2d      = Resources.Load("objWithUv", typeof(Texture2D)) as Texture2D;
        MeshRenderer render_  = obj.AddComponent <MeshRenderer>();

        render_.material             = new Material(Shader.Find("Diffuse"));
        render_.material.mainTexture = t2d;
        Mesh mesh = new Mesh();

        mfilter_.mesh = mesh;
        Color[] colors   = new Color[num];
        int[]   indecies = new int[num];
        for (int i = 0; i < num; ++i)
        {
            indecies[i] = i;
            colors[i]   = Color.white;
        }
        mfilter_.mesh.vertices = pts;
        mfilter_.mesh.uv       = uvs;
        mfilter_.mesh.colors   = colors;
        //mfilter_.mesh.SetIndices(indecies, MeshTopology.Points, 0);
        mfilter_.mesh.triangles = faces;
        //Debug.Log("done");

        MeshCollider collider = obj.AddComponent <MeshCollider>();

        collider.sharedMesh = mfilter_.mesh;
        //--------------------------------------------------------------
        Hxx = new HxxSpheres(pts_, faces_, vn_, faceCenters_, 5);
        for (int i = 0; i < Hxx.HxxUs.Count; i++)
        {
            List <GameObject> thisStrand = new List <GameObject> ();
            for (int j = 0; j < Hxx.HxxUs[i].Count; j++)
            {
                GameObject hs = GameObject.CreatePrimitive(PrimitiveType.Sphere);
                hs.transform.position   = Hxx.HxxUs[i][j].pos;
                hs.transform.localScale = new Vector3(0.03f, 0.03f, 0.03f);
                hs.GetComponent <Renderer>().material.color = Color.white;
                thisStrand.Add(hs);
            }
            HSpheres.Add(thisStrand);
        }
    }
Example #2
0
    void Start()
    {
        //List<List<Node>> test0 = new List<List<Node>>();
        //List<Node> test1 = new List<Node>();
        //Node testJson = new Node();
        //testJson.pos = Vector3Obj.ToVector3Obj(new Vector3(1, 2, 3));
        //test1.Add(testJson);
        //test0.Add(test1);
        //string str = JsonConvert.SerializeObject(test0);
        //Debug.Log(str);



        string configPath = Application.dataPath + "/config.json";

        if (System.IO.File.Exists(configPath))
        {
            ParseParm pp        = new ParseParm();
            string    theParams = File.ReadAllText(configPath);
            pp = (ParseParm)JsonConvert.DeserializeObject(theParams, typeof(ParseParm));
            if (pp.initialSphereNum > 0)
            {
                initialSphereNum = pp.initialSphereNum;
            }
            inc1 = Mathf.Abs((float)pp.inc1);
            inc2 = Mathf.Abs((float)pp.inc2);
            Debug.Log(initialSphereNum);
            Debug.Log(inc1);
            Debug.Log(inc2);
        }
        else
        {
        }


        while (true)
        {
            {
                OpenFileName openFileName = new OpenFileName();
                openFileName.structSize = Marshal.SizeOf(openFileName);
                //openFileName.filter = "Excel文件(*.xlsx)\0*.xlsx";
                openFileName.filter       = "(*.*)\0*.*";
                openFileName.file         = new string(new char[256]);
                openFileName.maxFile      = openFileName.file.Length;
                openFileName.fileTitle    = new string(new char[64]);
                openFileName.maxFileTitle = openFileName.fileTitle.Length;
                openFileName.initialDir   = Application.streamingAssetsPath.Replace('/', '\\');//默认路径
                openFileName.title        = "窗口标题";
                openFileName.flags        = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000008;

                if (LocalDialog.GetOpenFileName(openFileName))
                {
                    Debug.Log(openFileName.file);
                    Debug.Log(openFileName.fileTitle);
                }
                objPath = openFileName.file;
            }
            if (objPath.EndsWith(".obj"))
            {
                string pPath = System.IO.Path.GetDirectoryName(objPath); //获取文件路径
                string pName = System.IO.Path.GetFileName(objPath);
                Debug.Log(pPath);
                Debug.Log(pName);
                infoPath = objPath + ".json";
                {
                    preData = new List <List <Node> >();
                    preData.Add(new List <Node>());
                    if (System.IO.File.Exists(infoPath) == false)
                    {
                        print("文件不存在");
                    }
                    else
                    {
                        print("文件存在");
                        string infos = File.ReadAllText(infoPath);
                        preData    = (List <List <Node> >)JsonConvert.DeserializeObject(infos, typeof(List <List <Node> >));
                        hasPreData = true;
                    }
                }
                break;
            }
        }
        theChosenFaceIdx = -1;
        //--------------------------------------------------------------
        HSpheres  = new List <List <GameObject> >();
        m_spheres = new GameObject[3];
        for (int i = 0; i < 3; i++)
        {
            m_spheres[i] = GameObject.CreatePrimitive(PrimitiveType.Sphere);
            m_spheres[i].transform.position   = new Vector3(0, 0, 0);
            m_spheres[i].transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);
        }
        //--------------------------------------------------------------
        HSpheres = new List <List <GameObject> >();
        //--------------------------------------------------------------

        pts_         = new List <Vector3>();
        faces_       = new List <int>();
        uvs_         = new List <Vector2>();
        vn_          = new List <Vector3>();
        faceCenters_ = new List <Vector3>();
        FileStream   aFile   = new FileStream(objPath, FileMode.Open);
        StreamReader reader  = new StreamReader(aFile);
        string       strLine = reader.ReadLine();

        while (true)
        {
            strLine = reader.ReadLine();
            if (strLine == null)
            {
                break;
            }
            if (strLine.StartsWith("v "))
            {
                string[] sArray = Regex.Split(strLine, " ", RegexOptions.IgnoreCase);
                float    x      = float.Parse(sArray[1]);
                float    y      = float.Parse(sArray[2]);
                float    z      = float.Parse(sArray[3]);
                Vector3  thisPt = new Vector3(x, y, z);
                pts_.Add(thisPt);
            }
            else if (strLine.StartsWith("vt "))
            {
                string[] sArray = Regex.Split(strLine, " ", RegexOptions.IgnoreCase);
                double   x      = double.Parse(sArray[1]);
                double   y      = double.Parse(sArray[2]);
                Vector2  thisUv = new Vector2((float)x, (float)y);
                uvs_.Add(thisUv);
            }
            else if (strLine.StartsWith("#vn "))
            {
                string[] sArray = Regex.Split(strLine, " ", RegexOptions.IgnoreCase);
                double   x      = double.Parse(sArray[1]);
                double   y      = double.Parse(sArray[2]);
                double   z      = double.Parse(sArray[3]);
                Vector3  thisN  = new Vector3((float)x, (float)y, (float)z);
                vn_.Add(thisN);
            }
            else if (strLine.StartsWith("#faceCenter "))
            {
                string[] sArray         = Regex.Split(strLine, " ", RegexOptions.IgnoreCase);
                double   x              = double.Parse(sArray[1]);
                double   y              = double.Parse(sArray[2]);
                double   z              = double.Parse(sArray[3]);
                Vector3  thisFaceCenter = new Vector3((float)x, (float)y, (float)z);
                faceCenters_.Add(thisFaceCenter);
            }
            else if (strLine.StartsWith("f "))
            {
                int      ptA    = 0;
                int      ptB    = 0;
                int      ptC    = 0;
                string[] sArray = Regex.Split(strLine, " ", RegexOptions.IgnoreCase);
                {
                    string[] ptAndUvStrs = Regex.Split(sArray[1], "/", RegexOptions.IgnoreCase);
                    ptA = int.Parse(ptAndUvStrs[0]) - 1;
                }
                {
                    string[] ptAndUvStrs = Regex.Split(sArray[2], "/", RegexOptions.IgnoreCase);
                    ptB = int.Parse(ptAndUvStrs[0]) - 1;
                }
                {
                    string[] ptAndUvStrs = Regex.Split(sArray[3], "/", RegexOptions.IgnoreCase);
                    ptC = int.Parse(ptAndUvStrs[0]) - 1;
                }
                faces_.Add(ptA);
                faces_.Add(ptB);
                faces_.Add(ptC);
                //faces_.Add(ptA);
                //faces_.Add(ptC);
                //faces_.Add(ptB);
            }
            else
            {
                continue;
            }
        }
        reader.Close();
        Vector3[] pts   = pts_.ToArray();
        int[]     faces = faces_.ToArray();
        Vector2[] uvs   = uvs_.ToArray();
        int       num   = pts.Length;

        faceNum = num;


        List <Vector3> ptsOrderByX = new List <Vector3>();

        ptsOrderByX = pts_.OrderBy(tt => (tt.x)).ToList();
        List <Vector3> ptsOrderByY = new List <Vector3>();

        ptsOrderByY = pts_.OrderBy(tt => (tt.y)).ToList();
        List <Vector3> ptsOrderByZ = new List <Vector3>();

        ptsOrderByZ = pts_.OrderBy(tt => (tt.z)).ToList();

        Vector3 meshCenter = new Vector3(0.5f * (ptsOrderByX[0].x + ptsOrderByX[num - 1].x),
                                         0.5f * (ptsOrderByY[0].y + ptsOrderByY[num - 1].y),
                                         0.5f * (ptsOrderByZ[0].z + ptsOrderByZ[num - 1].z));
        CameraMove CameraMoveIns = Camera.main.GetComponent <CameraMove>();

        CameraMoveIns.setRotationCenter(meshCenter);

        obj      = new GameObject();
        obj.name = "back";
        MeshFilter   mfilter_ = obj.AddComponent <MeshFilter>();
        Texture2D    t2d      = Resources.Load("objWithUv", typeof(Texture2D)) as Texture2D;
        MeshRenderer render_  = obj.AddComponent <MeshRenderer>();

        render_.material             = new Material(Shader.Find("Diffuse"));
        render_.material.mainTexture = t2d;
        Mesh mesh = new Mesh();

        mfilter_.mesh = mesh;
        Color[] colors   = new Color[num];
        int[]   indecies = new int[num];
        for (int i = 0; i < num; ++i)
        {
            indecies[i] = i;
            colors[i]   = Color.white;
        }
        mfilter_.mesh.vertices = pts;
        mfilter_.mesh.uv       = uvs;
        mfilter_.mesh.colors   = colors;
        //mfilter_.mesh.SetIndices(indecies, MeshTopology.Points, 0);
        mfilter_.mesh.triangles = faces;
        //Debug.Log("done");

        MeshCollider collider = obj.AddComponent <MeshCollider>();

        collider.sharedMesh = mfilter_.mesh;
        //--------------------------------------------------------------
        Hxx = new HxxSpheres(pts_, faces_, vn_, faceCenters_, initialSphereNum);

        if (hasPreData)
        {
            Debug.Log(preData.Count);
            Hxx.HxxUs = preData;
        }
        for (int i = 0; i < Hxx.HxxUs.Count; i++)
        {
            List <GameObject> thisStrand = new List <GameObject> ();
            for (int j = 0; j < Hxx.HxxUs[i].Count; j++)
            {
                GameObject hs = GameObject.CreatePrimitive(PrimitiveType.Sphere);
                hs.transform.position   = Hxx.HxxUs[i][j].pos.ToVector3();
                hs.transform.localScale = new Vector3(0.03f, 0.03f, 0.03f);
                hs.GetComponent <Renderer>().material.color = Color.white;
                thisStrand.Add(hs);
            }
            HSpheres.Add(thisStrand);
        }
    }