コード例 #1
0
    public static void GetObject(string path, UnityAction <GameObject> action)
    {
        var objLoader = new Dummiesman.OBJLoader();

        if (path.StartsWith("http"))
        {
            var www = UnityEngine.Networking.UnityWebRequest.Get(path);
            www.SendWebRequest().completed += x =>
            {
                using (MemoryStream ms = new MemoryStream(www.downloadHandler.data))
                    action.Invoke(objLoader.Load(ms));
            };
        }
        else
        {
            action?.Invoke(objLoader.Load(path));
        }
    }
コード例 #2
0
        static void ObjLoadMenu()
        {
            string pth = EditorUtility.OpenFilePanel("Import OBJ", "", "obj");

            if (!string.IsNullOrEmpty(pth))
            {
                System.Diagnostics.Stopwatch s = new System.Diagnostics.Stopwatch();
                s.Start();

                var loader = new OBJLoader
                {
                    SplitMode = SplitMode.Object,
                };
                loader.Load(pth);

                //Debug.Log($"OBJ import time: {s.ElapsedMilliseconds}ms");
                s.Stop();
            }
        }
コード例 #3
0
 public OBJObjectBuilder(string name, OBJLoader loader)
 {
     _name   = name;
     _loader = loader;
 }
コード例 #4
0
    private void LoadTextMaze(string filePath)
    {
        //Scripts.GameLanguage.CodeRunner runner = new Scripts.GameLanguage.CodeRunner();
        //var result = runner.Compile("for(int i = 0; i < 200; i++){setVelocity(2.0f, 0.0f); wait();}");
        //runner.Run(result);

        Clear();
        string[] textLines = File.ReadAllLines(filePath);

        float x, z;
        float y = transform.position.y;
        float rx = 0, rz = 0, rth = 0;
        bool  changeRobot = false;

        List <Vector4>    walls  = new List <Vector4>();
        List <Vector2>    points = new List <Vector2>();
        List <Vector4>    lines  = new List <Vector4>();
        List <Vector4>    cubes  = new List <Vector4>();
        List <Vector3>    balls  = new List <Vector3>();
        List <DynamicObj> objs   = new List <DynamicObj>();

        string id;
        int    x1, y1, x2, y2, tmp;
        float  s1, s2, s3, rotx, roty, rotz;

        for (int i = 0; i < textLines.Length; i++)
        {
            if (textLines[i].StartsWith("#"))
            {
                continue;
            }
            string[] parts = textLines[i].Split(new[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries)
                             .Select(part => part.Trim()).Where(part => !string.IsNullOrEmpty(part)).ToArray();
            if (parts.Length < 3)
            {
                continue;
            }
            id = parts[0].Trim(' ', '\r', '\t');
            if (id == "R")
            {
                rx = float.Parse(parts[1]);
                rz = float.Parse(parts[2]);
                if (parts.Length > 3)
                {
                    rth = float.Parse(parts[3]);
                }
                else
                {
                    rth = 0;
                }
                changeRobot = true;
            }
            else if (id == "W")
            {
                x1 = int.Parse(parts[1]);
                y1 = int.Parse(parts[2]);
                if (parts.Length == 3)
                {
                    points.Add(new Vector2(x1, y1));
                }
                else if (parts.Length == 5)
                {
                    x2 = int.Parse(parts[3]);
                    y2 = int.Parse(parts[4]);
                    walls.Add(new Vector4(x1, y1, x2, y2));
                }
            }
            else if (id == "L")
            {
                x1 = int.Parse(parts[1]);
                y1 = int.Parse(parts[2]);
                x2 = int.Parse(parts[3]);
                y2 = int.Parse(parts[4]);
                lines.Add(new Vector4(x1, y1, x2, y2));
            }
            else if (id == "C")
            {
                x1 = int.Parse(parts[1]);
                y1 = int.Parse(parts[2]);
                if (parts.Length > 3)
                {
                    s1 = float.Parse(parts[3]);
                    if (parts.Length > 4)
                    {
                        s2 = float.Parse(parts[4]);
                    }
                    else
                    {
                        s2 = s1;
                    }
                }
                else
                {
                    s1 = 1.0f;
                    s2 = 1.0f;
                }
                cubes.Add(new Vector4(x1, y1, s1, s2));
            }
            else if (id == "B")
            {
                x1 = int.Parse(parts[1]);
                y1 = int.Parse(parts[2]);
                if (parts.Length > 3)
                {
                    s1 = float.Parse(parts[3]);
                }
                else
                {
                    s1 = 1.0f;
                }
                balls.Add(new Vector3(x1, y1, s1));
            }
            else if (id == "O")
            {
                DynamicObj obj = new DynamicObj();

                x1 = int.Parse(parts[1]);
                y1 = int.Parse(parts[2]);

                string root = System.IO.Path.GetDirectoryName(filePath);
                string file = parts[3];

                if (parts.Length > 4)
                {
                    rotx = 0;
                    roty = 0;
                    rotz = float.Parse(parts[4]);
                }
                else
                {
                    rotx = 0;
                    roty = 0;
                    rotz = 0;
                }

                if (parts.Length > 5)
                {
                    s1 = float.Parse(parts[5]);
                    if (parts.Length > 6)
                    {
                        s2 = float.Parse(parts[6]);
                        if (parts.Length > 7)
                        {
                            s3 = float.Parse(parts[7]);
                        }
                        else
                        {
                            s3 = s1;
                        }
                    }
                    else
                    {
                        s2 = s1;
                        s3 = s1;
                    }
                }
                else
                {
                    s1 = 1.0f;
                    s2 = 1.0f;
                    s3 = 1.0f;
                }

                obj.Scale    = new Vector3(s1, s2, s3);
                obj.Position = new Vector3(-y1, y + obj.Scale.y, x1);
                obj.Rotation = new Vector3(roty, -rotz * Mathf.Rad2Deg, rotx);
                obj.Path     = System.IO.Path.Combine(root, file);

                objs.Add(obj);
            }
        }

        for (int i = 0; i < points.Count; i++)
        {
            x = -points[i].y;
            z = points[i].x;
            Instantiate(Cell, new Vector3(x, y, z), Quaternion.identity, transform);
        }

        for (int i = 0; i < walls.Count; i++)
        {
            x1 = -(int)walls[i].y;
            y1 = (int)walls[i].x;
            x2 = -(int)walls[i].w;
            y2 = (int)walls[i].z;
            float cx = (x1 + x2) / 2.0f;
            float cy = (y1 + y2) / 2.0f;

            Vector2 p1   = new Vector2(x1, y1);
            Vector2 p2   = new Vector2(x2, y2);
            float   dist = Vector2.Distance(p1, p2) + 1;

            Vector3 scale = new Vector3(1.0f, 2.0f, 1.0f);
            scale.z = dist;

            float angle = (float)Math.Atan2((double)(y2 - y1), (double)(x2 - x1));

            GameObject wall = Instantiate(Cell, new Vector3(cx, y + scale.y * 0.5f, cy), Quaternion.identity, transform);
            wall.transform.localScale = scale;
            wall.transform.rotation   = Quaternion.Euler(0, -angle * Mathf.Rad2Deg - 90, 0);
        }

        for (int i = 0; i < cubes.Count; i++)
        {
            x = -cubes[i].y;
            z = cubes[i].x;
            GameObject cube = Instantiate(Cube, new Vector3(x, y + 1 + cubes[i].w, z), Quaternion.identity, transform);
            cube.transform.localScale = new Vector3(cubes[i].z, cubes[i].w, cubes[i].z);
        }

        for (int i = 0; i < balls.Count; i++)
        {
            x = -balls[i].y;
            z = balls[i].x;
            GameObject ball = Instantiate(Ball, new Vector3(x, y + balls[i].z, z), Quaternion.identity, transform);
            ball.transform.localScale = new Vector3(balls[i].z, balls[i].z, balls[i].z);
        }

        for (int i = 0; i < lines.Count; i++)
        {
            x1 = -(int)lines[i].y;
            y1 = (int)lines[i].x;
            x2 = -(int)lines[i].w;
            y2 = (int)lines[i].z;
            Vector3[] positions = new Vector3[2];
            positions[0].x = lines[i].y;
            positions[0].y = 0;
            positions[0].z = lines[i].x;
            positions[1].x = lines[i].w;
            positions[1].y = 0;
            positions[1].z = lines[i].z;
            GameObject   lineObject   = Instantiate(Line, new Vector3(0, 0, 0), Quaternion.identity, transform);
            LineRenderer lineRenderer = lineObject.GetComponent <LineRenderer>();
            lineRenderer.positionCount = 2;
            lineRenderer.SetPositions(positions);
        }

        for (int i = 0; i < objs.Count; i++)
        {
            DynamicObj obj = objs[i];

            GameObject model = new Dummiesman.OBJLoader().Load(obj.Path);
            model.transform.SetParent(transform);
            model.transform.position   = obj.Position;
            model.transform.localScale = obj.Scale;
            model.transform.rotation   = Quaternion.Euler(obj.Rotation);

            Rigidbody    rb       = model.AddComponent <Rigidbody>();
            MeshCollider collider = model.AddComponent <MeshCollider>();
            collider.convex     = true;
            collider.sharedMesh = model.GetComponentInChildren <MeshFilter>().sharedMesh;
        }

        if (changeRobot)
        {
            Robot.position = new Vector3(-rz, 0.86f, rx);
            Robot.rotation = Quaternion.Euler(0, -rth * Mathf.Rad2Deg, 0);
        }
    }