public void Init(OBJData data, int i)
        {
            this.data   = data;
            lines       = new List <RenderLine>();
            boundingBox = new RenderBoundingBox();
            boundingBox.Init(new BoundingBox(new Vector3(-0.5f), new Vector3(0.5f)));
            boundingBox.SetTransform(Matrix.Translation(data.vertices[i].position));

            if (data.vertices[i].unk3 < data.vertices.Length)
            {
                RenderLine line = new RenderLine();
                Vector3    pos1 = data.vertices[i].position;
                Vector3    pos2 = data.vertices[data.vertices[i].unk3].position;
                line.Init(new Vector3[] { pos1, pos2 });
                lines.Add(line);
            }

            if (data.vertices[i].unk4 < data.vertices.Length)
            {
                RenderLine line = new RenderLine();
                line.Init(new Vector3[] { data.vertices[i].position, data.vertices[data.vertices[i].unk4].position });
                lines.Add(line);
            }

            if (data.vertices[i].unk5 < data.vertices.Length)
            {
                RenderLine line = new RenderLine();
                line.Init(new Vector3[] { data.vertices[i].position, data.vertices[data.vertices[i].unk5].position });
                lines.Add(line);
            }
        }
Beispiel #2
0
    public void SaveObj()
    {
        SDMesh sdMesh = FindObjectOfType <SDMesh>();

        MeshFilter[]      meshFilters = sdMesh.GetComponentsInChildren <MeshFilter>();
        CombineInstance[] combine     = new CombineInstance[meshFilters.Length];
        int i = 0;

        while (i < meshFilters.Length)
        {
            combine[i].mesh      = meshFilters[i].sharedMesh;
            combine[i].transform = meshFilters[i].transform.localToWorldMatrix;
            i++;
        }
        Mesh combinedMesh = new Mesh();

        combinedMesh.CombineMeshes(combine);

        OBJData data     = combinedMesh.EncodeOBJ();
        string  filepath = Application.persistentDataPath + "/Obj_"
                           + UnityEngine.Random.Range(0, 10000) + ".obj";
        var lStream = new FileStream(filepath, FileMode.Create);

        OBJLoader.ExportOBJ(data, lStream);
        lStream.Close();
    }
Beispiel #3
0
    //------------------------------------------------------------------------------------------------------------
    public static void ExportOBJ(OBJData lData, Stream lStream)
    {
        StreamWriter lLineStreamWriter = new StreamWriter(lStream);

        lLineStreamWriter.WriteLine(string.Format("# File exported by Unity3D version {0}", Application.unityVersion));

        for (int lCount = 0; lCount < lData.m_Vertices.Count; ++lCount)
        {
            lLineStreamWriter.WriteLine(string.Format("v {0} {1} {2}",
                                                      lData.m_Vertices[lCount].x.ToString("n8"),
                                                      lData.m_Vertices[lCount].y.ToString("n8"),
                                                      lData.m_Vertices[lCount].z.ToString("n8")));
        }

        for (int lCount = 0; lCount < lData.m_UVs.Count; ++lCount)
        {
            lLineStreamWriter.WriteLine(string.Format("vt {0} {1}",
                                                      lData.m_UVs[lCount].x.ToString("n5"),
                                                      lData.m_UVs[lCount].y.ToString("n5")));
        }

        for (int lCount = 0; lCount < lData.m_UV2s.Count; ++lCount)
        {
            lLineStreamWriter.WriteLine(string.Format("vt2 {0} {1}",
                                                      lData.m_UVs[lCount].x.ToString("n5"),
                                                      lData.m_UVs[lCount].y.ToString("n5")));
        }

        for (int lCount = 0; lCount < lData.m_Normals.Count; ++lCount)
        {
            lLineStreamWriter.WriteLine(string.Format("vn {0} {1} {2}",
                                                      lData.m_Normals[lCount].x.ToString("n8"),
                                                      lData.m_Normals[lCount].y.ToString("n8"),
                                                      lData.m_Normals[lCount].z.ToString("n8")));
        }

        for (int lCount = 0; lCount < lData.m_Colors.Count; ++lCount)
        {
            lLineStreamWriter.WriteLine(string.Format("vc {0} {1} {2} {3}",
                                                      lData.m_Colors[lCount].r.ToString("n8"),
                                                      lData.m_Colors[lCount].g.ToString("n8"),
                                                      lData.m_Colors[lCount].b.ToString("n8"),
                                                      lData.m_Colors[lCount].a.ToString("n8")));
        }

        for (int lGroup = 0; lGroup < lData.m_Groups.Count; ++lGroup)
        {
            lLineStreamWriter.WriteLine(string.Format("g {0}", lData.m_Groups[lGroup].m_Name));

            for (int lFace = 0; lFace < lData.m_Groups[lGroup].Faces.Count; ++lFace)
            {
                lLineStreamWriter.WriteLine(string.Format("f {0} {1} {2}",
                                                          lData.m_Groups[lGroup].Faces[lFace].ToString(0),
                                                          lData.m_Groups[lGroup].Faces[lFace].ToString(1),
                                                          lData.m_Groups[lGroup].Faces[lFace].ToString(2)));
            }
        }

        lLineStreamWriter.Flush();
    }
    public void ExportWheelsForOther()
    {
        //Get the mesh filter component from the car model
        MeshFilter meshFilter = GameObject.Find("Wheels").GetComponent <MeshFilter> ();

        //Open the [Save as..] panel from your OS
        var path = EditorUtility.SaveFilePanel("Save as .obj", "Assets/", meshFilter.name + id + ".obj", "obj");

        //Stop errors from happening if the user cancels the save process
        if (string.IsNullOrEmpty(path))
        {
            return;
        }

        //Create a file stream that will take the path from the OS and open in create mode
        FileStream fileStream = new FileStream(path, FileMode.Create);

        //The obj data contained resulting from the encode obj function
        OBJData objData = meshFilter.sharedMesh.EncodeOBJ();

        //Calling the export obj function taking the obj data and the file stream and close it
        OBJLoader.ExportOBJ(objData, fileStream);
        fileStream.Close();

        //Display some comfirmation text to the screen then call the ienumerator
        successObj.SetActive(true);
        StartCoroutine(HideTextAfterTime(3.0f));
    }
Beispiel #5
0
    public static void ExportOBJ(OBJData lData, StringBuilder sBuilder)
    {
        Debug.Log("building string...");
        sBuilder.Append(string.Format("# File exported by Unity3D version {0}\n", Application.unityVersion));

        for (int lCount = 0; lCount < lData.m_Vertices.Count; ++lCount)
        {
            sBuilder.Append(string.Format("v {0} {1} {2}\n",
                                          lData.m_Vertices[lCount].x.ToString("n8"),
                                          lData.m_Vertices[lCount].y.ToString("n8"),
                                          lData.m_Vertices[lCount].z.ToString("n8")));
        }

        for (int lCount = 0; lCount < lData.m_UVs.Count; ++lCount)
        {
            sBuilder.Append(string.Format("vt {0} {1}\n",
                                          lData.m_UVs[lCount].x.ToString("n5"),
                                          lData.m_UVs[lCount].y.ToString("n5")));
        }

        for (int lCount = 0; lCount < lData.m_UV2s.Count; ++lCount)
        {
            sBuilder.Append(string.Format("vt2 {0} {1}\n",
                                          lData.m_UVs[lCount].x.ToString("n5"),
                                          lData.m_UVs[lCount].y.ToString("n5")));
        }

        for (int lCount = 0; lCount < lData.m_Normals.Count; ++lCount)
        {
            sBuilder.Append(string.Format("vn {0} {1} {2}\n",
                                          lData.m_Normals[lCount].x.ToString("n8"),
                                          lData.m_Normals[lCount].y.ToString("n8"),
                                          lData.m_Normals[lCount].z.ToString("n8")));
        }

        for (int lCount = 0; lCount < lData.m_Colors.Count; ++lCount)
        {
            sBuilder.Append(string.Format("vc {0} {1} {2} {3}\n",
                                          lData.m_Colors[lCount].r.ToString("n8"),
                                          lData.m_Colors[lCount].g.ToString("n8"),
                                          lData.m_Colors[lCount].b.ToString("n8"),
                                          lData.m_Colors[lCount].a.ToString("n8")));
        }

        for (int lGroup = 0; lGroup < lData.m_Groups.Count; ++lGroup)
        {
            sBuilder.Append(string.Format("g {0}\n", lData.m_Groups[lGroup].m_Name));

            for (int lFace = 0; lFace < lData.m_Groups[lGroup].Faces.Count; ++lFace)
            {
                sBuilder.Append(string.Format("f {0} {1} {2}\n",
                                              lData.m_Groups[lGroup].Faces[lFace].ToString(0),
                                              lData.m_Groups[lGroup].Faces[lFace].ToString(1),
                                              lData.m_Groups[lGroup].Faces[lFace].ToString(2)));
            }
        }

        //lLineStreamWriter.Flush();
    }
Beispiel #6
0
        //------------------------------------------------------------------------------------------------------------
        public static OBJData EncodeOBJ(this Mesh lMesh)
        {
            OBJData lData = new OBJData
            {
                m_Vertices = new List <Vector3>(lMesh.vertices),
                m_UVs      = new List <Vector2>(lMesh.uv),
                m_Normals  = new List <Vector3>(lMesh.normals),
                m_UV2s     = new List <Vector2>(lMesh.uv2),
                m_Colors   = new List <Color>(lMesh.colors)
            };

            int[]         lIndices    = null;
            OBJGroup      lGroup      = null;
            OBJFace       lFace       = null;
            OBJFaceVertex lFaceVertex = null;

            for (int lMCount = 0; lMCount < lMesh.subMeshCount; ++lMCount)
            {
                lIndices = lMesh.GetTriangles(lMCount);
                lGroup   = new OBJGroup(lMesh.name + "_" + lMCount.ToString());

                for (int lCount = 0; lCount < lIndices.Length; lCount += 3)
                {
                    lFace = new OBJFace();

                    lFaceVertex = new OBJFaceVertex();
                    lFaceVertex.m_VertexIndex = lData.m_Vertices.Count > 0 ? lIndices[lCount] : -1;
                    lFaceVertex.m_UVIndex     = lData.m_UVs.Count > 0 ? lIndices[lCount] : -1;
                    lFaceVertex.m_NormalIndex = lData.m_Normals.Count > 0 ? lIndices[lCount] : -1;
                    lFaceVertex.m_UV2Index    = lData.m_UV2s.Count > 0 ? lIndices[lCount] : -1;
                    lFaceVertex.m_ColorIndex  = lData.m_Colors.Count > 0 ? lIndices[lCount] : -1;
                    lFace.AddVertex(lFaceVertex);

                    lFaceVertex = new OBJFaceVertex();
                    lFaceVertex.m_VertexIndex = lData.m_Vertices.Count > 0 ? lIndices[lCount + 1] : -1;
                    lFaceVertex.m_UVIndex     = lData.m_UVs.Count > 0 ? lIndices[lCount + 1] : -1;
                    lFaceVertex.m_NormalIndex = lData.m_Normals.Count > 0 ? lIndices[lCount + 1] : -1;
                    lFaceVertex.m_UV2Index    = lData.m_UV2s.Count > 0 ? lIndices[lCount + 1] : -1;
                    lFaceVertex.m_ColorIndex  = lData.m_Colors.Count > 0 ? lIndices[lCount + 1] : -1;
                    lFace.AddVertex(lFaceVertex);

                    lFaceVertex = new OBJFaceVertex();
                    lFaceVertex.m_VertexIndex = lData.m_Vertices.Count > 0 ? lIndices[lCount + 2] : -1;
                    lFaceVertex.m_UVIndex     = lData.m_UVs.Count > 0 ? lIndices[lCount + 2] : -1;
                    lFaceVertex.m_NormalIndex = lData.m_Normals.Count > 0 ? lIndices[lCount + 2] : -1;
                    lFaceVertex.m_UV2Index    = lData.m_UV2s.Count > 0 ? lIndices[lCount + 2] : -1;
                    lFaceVertex.m_ColorIndex  = lData.m_Colors.Count > 0 ? lIndices[lCount + 2] : -1;
                    lFace.AddVertex(lFaceVertex);

                    lGroup.AddFace(lFace);
                }

                lData.m_Groups.Add(lGroup);
            }

            return(lData);
        }
Beispiel #7
0
    private void CreateGridElement(GameObject elementGrid, string elementName, string path)
    {
        //Create button gameobject
        GameObject go = new GameObject(elementName);

        go.transform.SetParent(elementGrid.transform, false);

        //Add interaction component
        BDDElementHandler bddElementHandler = go.AddComponent <BDDElementHandler>();

        bddElementHandler.SetPath(path);
        bddElementHandler.SetElementPositioningPanel(ElementPositioningPanel);

        //Add image
        Image img = go.AddComponent <Image>() as Image;

        img.type = Image.Type.Sliced;
        //Create temporary instance to get thumb image
        GameObject thumbTransform = new GameObject(elementName);
        //Material
        MeshRenderer mr = thumbTransform.AddComponent <MeshRenderer>();

        mr.material = Resources.Load <Material>("Materials/BenchMat");
        //Mesh
        MeshFilter mf       = thumbTransform.AddComponent <MeshFilter>();
        FileStream lStream  = new FileStream(path, FileMode.Open);
        OBJData    lOBJData = OBJLoader.LoadOBJ(lStream);

        lStream.Close();
        mf.mesh.LoadOBJ(lOBJData);
        mf.mesh.RecalculateNormals();

        //Generate thumb
        Texture2D image = RuntimePreviewGenerator.GenerateModelPreview(thumbTransform.transform, 128, 128, false);

        img.sprite = Sprite.Create(image, new Rect(0, 0, image.width, image.height), new Vector2(0.5f, 0.5f));
        //Destroy temporary instance
        Destroy(thumbTransform);

        //Text under button
        GameObject txGO = new GameObject();
        Text       tx   = txGO.AddComponent <Text>() as Text;

        tx.text      = elementName;
        tx.color     = Color.black;
        tx.alignment = TextAnchor.UpperCenter;
        tx.font      = Resources.GetBuiltinResource(typeof(Font), "Arial.ttf") as Font;
        tx.fontStyle = FontStyle.Bold;
        tx.fontSize  = 14;
        //tx.resizeTextForBestFit = true;
        //tx.resizeTextMaxSize = 25;
        txGO.transform.position = new Vector3(0, -63, 0);
        txGO.transform.GetComponent <RectTransform>().SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, 25);
        txGO.transform.SetParent(go.transform, false);
    }
Beispiel #8
0
 public void Init(OBJData data, int i)
 {
     DoRender      = true;
     this.data     = data;
     lines         = new List <RenderLine>();
     navigationBox = new RenderBoundingBox();
     navigationBox.Init(new BoundingBox(new Vector3(-0.1f), new Vector3(0.1f)));
     navigationBox.SetColour(System.Drawing.Color.Green);
     navigationBox.SetTransform(Matrix.Translation(data.vertices[i].Position));
     vertex = data.vertices[i];
 }
Beispiel #9
0
    //------------------------------------------------------------------------------------------------------------
    public static OBJData LoadOBJ(Stream lStream)
    {
        m_OBJData = new OBJData();

        m_CurrentMaterial = null;
        m_CurrentGroup    = null;

        StreamReader lLineStreamReader = new StreamReader(lStream);

        Action <string> lAction      = null;
        string          lCurrentLine = null;

        string[] lFields  = null;
        string   lKeyword = null;
        string   lData    = null;


        while (!lLineStreamReader.EndOfStream)
        {
            lCurrentLine = lLineStreamReader.ReadLine();

            if (StringExt.IsNullOrWhiteSpace(lCurrentLine) ||
                lCurrentLine[0] == '#')
            {
                continue;
            }

            lFields = lCurrentLine.Trim().Split(null, 2);
            if (lFields.Length < 2)
            {
                continue;
            }

            lKeyword = lFields[0].Trim();
            lData    = lFields[1].Trim();

            lAction = null;
            m_ParseOBJActionDictionary.TryGetValue(lKeyword.ToLowerInvariant(), out lAction);

            if (lAction != null)
            {
                lAction(lData);
            }
        }

        Debug.Log("reached");
        var lOBJData = m_OBJData;

        m_OBJData = null;

        return(lOBJData);
    }
Beispiel #10
0
        //------------------------------------------------------------------------------------------------------------
        public static void LoadOBJ(this Mesh lMesh, OBJData lData)
        {
            List <Vector3> lVertices = new List <Vector3>();
            List <Vector3> lNormals  = new List <Vector3>();
            List <Vector2> lUVs      = new List <Vector2>();

            List <int>[] lIndices = new List <int> [lData.m_Groups.Count];
            OBJGroup     lGroup   = null;

            lMesh.subMeshCount = lData.m_Groups.Count;


            for (int lGCount = 0; lGCount < lData.m_Groups.Count; ++lGCount)
            {
                lGroup            = lData.m_Groups[lGCount];
                lIndices[lGCount] = new List <int>();

                for (int lFCount = 0; lFCount < lGroup.Faces.Count; ++lFCount)
                {
                    for (int lVCount = 0; lVCount < lGroup.Faces[lFCount].Count; ++lVCount)
                    {
                        if (lGroup.Faces[lFCount][lVCount].m_VertexIndex < lData.m_Vertices.Count)
                        {
                            lIndices[lGCount].Add(lVertices.Count);

                            lVertices.Add(lData.m_Vertices[lGroup.Faces[lFCount][lVCount].m_VertexIndex]);
                            if (lGroup.Faces[lFCount][lVCount].m_UVIndex >= 0)
                            {
                                lUVs.Add(lData.m_UVs[lGroup.Faces[lFCount][lVCount].m_UVIndex]);
                            }
                            if (lGroup.Faces[lFCount][lVCount].m_NormalIndex >= 0)
                            {
                                lNormals.Add(lData.m_Normals[lGroup.Faces[lFCount][lVCount].m_NormalIndex]);
                            }
                        }
                    }
                }
            }

            lMesh.vertices = lVertices.ToArray();
            lMesh.uv       = lUVs.ToArray();
            lMesh.normals  = lNormals.ToArray();
            lMesh.RecalculateTangents();

            for (int lGCount = 0; lGCount < lData.m_Groups.Count; ++lGCount)
            {
                lMesh.SetTriangles(lIndices[lGCount].ToArray(), lGCount);
            }
        }
Beispiel #11
0
    //------------------------------------------------------------------------------------------------------------
    public static OBJData LoadOBJ(Stream lStream)
    {
        m_OBJData = new OBJData();

        m_CurrentMaterial = null;
        m_CurrentGroup = null;

        StreamReader lLineStreamReader = new StreamReader(lStream);

        Action<string> lAction = null;
        string lCurrentLine = null;
        string[] lFields = null;
        string lKeyword = null;
        string lData = null;

        while (!lLineStreamReader.EndOfStream)
        {
            lCurrentLine = lLineStreamReader.ReadLine();

            if (StringExt.IsNullOrWhiteSpace(lCurrentLine) 
                || lCurrentLine[0] == '#')
            {
                continue;
            }

            lFields = lCurrentLine.Trim().Split(null, 2);
            if (lFields.Length < 2)
            {
                continue;
            }

            lKeyword = lFields[0].Trim();
            lData = lFields[1].Trim();

            lAction = null;
            m_ParseOBJActionDictionary.TryGetValue(lKeyword.ToLowerInvariant(), out lAction);

            if (lAction != null)
            {
                lAction(lData);
            }
        }

        var lOBJData = m_OBJData;
        m_OBJData = null;

        return lOBJData; 
    }
Beispiel #12
0
        public static object LoadUncached(string path, Type type)
        {
            AssetMetadata metadata;

            TryGetMapped(path, out metadata, true);

            if (metadata == null)
            {
                goto NoMetadata;
            }

            if ((type == Types.Texture || type == Types.Texture2D) &&
                metadata.AssetType == Types.Texture2D)
            {
                Texture2D tex = new Texture2D(2, 2);
                tex.name = path;
                tex.LoadImage(metadata.Data);
                return(tex);
            }

            if (metadata.AssetType == Types.Meshes &&
                metadata.AssetFormat == "obj")
            {
                OBJData data = OBJParser.ParseOBJ(path);

                if (type == Types.OBJData)
                {
                    return(data);
                }
                else if (type == Types.OBJObject)
                {
                    return(data.Objects.Count == 0 ? null : data.Objects[0]);
                }

                else if (type == Types.Meshes)
                {
                    return(data.ToMeshes());
                }
                else
                {
                    return(data.Objects.Count == 0 ? null : data.Objects[0].ToMesh());
                }
            }

NoMetadata:
            return(null);
        }
Beispiel #13
0
    private GameObject ObjIOLoad(byte[] fileData)
    {
        /////////// Load the .obj using OBJIO
        GameObject targetObject = new GameObject();

        System.IO.Stream lStream  = new System.IO.MemoryStream(fileData);
        OBJData          lOBJData = OBJLoader.LoadOBJ(lStream);
        MeshFilter       filter   = targetObject.AddComponent <MeshFilter>();

        filter.mesh.LoadOBJ(lOBJData);
        lStream.Close();
        lStream  = null;
        lOBJData = null;
        targetObject.AddComponent <MeshRenderer> ().material = brainMaterial;
        filter.mesh.RecalculateNormals();
        return(targetObject);
        ///////////
    }
Beispiel #14
0
    /**<summary> Initialise world, get obstacles and build navmesh </summary>*/
    public async void InitArea(Parser.Area area)
    {
        finished    = false;
        currentArea = area;
        collected   = 0;
        ClearWorld();

        Tuple <bool, byte[]> result = await ServerAPI.Obstacles(area.areaID);

        if (result.Item1)
        {
            Stream  s       = new MemoryStream(result.Item2);
            OBJData objData = OBJLoader.LoadOBJ(s);
            s.Dispose();
            if (objData == null || objData.m_Vertices.Count <= 0)
            {
                if (App.config.debug)
                {
                    Debug.Log("Couldn't load obstacle obj");
                }
                return;
            }
            Mesh mesh = new Mesh();
            mesh.LoadOBJ(objData);

            worldObject.GetComponent <MeshFilter>().mesh         = mesh;
            worldObject.GetComponent <MeshCollider>().sharedMesh = mesh;

            worldObject.GetComponent <NavMeshSurface>().BuildNavMesh();
        }
        else
        {
            if (App.config.debug)
            {
                Debug.Log("No obstacle obj");
            }
        }

        SelectGame();
    }
Beispiel #15
0
        public void Init(OBJData data)
        {
            DoRender  = true;
            this.data = data;

            string VertexBatchID = string.Format("NavObjData_{0}", RefManager.GetNewRefID());

            PathVertexBatch = new PrimitiveBatch(PrimitiveType.Box, VertexBatchID);
            foreach (OBJData.VertexStruct Vertex in data.vertices)
            {
                RenderBoundingBox navigationBox = new RenderBoundingBox();
                navigationBox.Init(new BoundingBox(new Vector3(-0.1f), new Vector3(0.1f)));
                navigationBox.SetColour(System.Drawing.Color.Green);
                navigationBox.SetTransform(Matrix4x4.CreateTranslation(Vertex.Position));

                int PathHandle = RefManager.GetNewRefID();
                PathVertexBatch.AddObject(PathHandle, navigationBox);
                BoundingBoxes.Add(navigationBox);
            }

            OwnGraphics.OurPrimitiveManager.AddPrimitiveBatch(PathVertexBatch);
            OwnGraphics.OurPrimitiveManager.AddPrimitiveBatch(PointConnectionsBatch);
        }
Beispiel #16
0
    //------------------------------------------------------------------------------------------------------------
    public static void ExportOBJ(OBJData lData, Stream lStream)
    {
        StreamWriter lLineStreamWriter = new StreamWriter(lStream);

        lLineStreamWriter.WriteLine(string.Format("# File exported by Unity3D version {0}", Application.unityVersion));

        for (int lCount = 0; lCount < lData.m_Vertices.Count; ++lCount)
        {
            lLineStreamWriter.WriteLine(string.Format("v {0} {1} {2}",
                lData.m_Vertices[lCount].x.ToString("n8"),
                lData.m_Vertices[lCount].y.ToString("n8"),
                lData.m_Vertices[lCount].z.ToString("n8")));
        }

        for (int lCount = 0; lCount < lData.m_UVs.Count; ++lCount)
        {
            lLineStreamWriter.WriteLine(string.Format("vt {0} {1}",
                lData.m_UVs[lCount].x.ToString("n5"),
                lData.m_UVs[lCount].y.ToString("n5")));
        }

        for (int lCount = 0; lCount < lData.m_UV2s.Count; ++lCount)
        {
            lLineStreamWriter.WriteLine(string.Format("vt2 {0} {1}",
                lData.m_UVs[lCount].x.ToString("n5"),
                lData.m_UVs[lCount].y.ToString("n5")));
        }

        for (int lCount = 0; lCount < lData.m_Normals.Count; ++lCount)
        {
            lLineStreamWriter.WriteLine(string.Format("vn {0} {1} {2}",
                lData.m_Normals[lCount].x.ToString("n8"),
                lData.m_Normals[lCount].y.ToString("n8"),
                lData.m_Normals[lCount].z.ToString("n8")));
        }

        for (int lCount = 0; lCount < lData.m_Colors.Count; ++lCount)
        {
            lLineStreamWriter.WriteLine(string.Format("vc {0} {1} {2} {3}",
                lData.m_Colors[lCount].r.ToString("n8"),
                lData.m_Colors[lCount].g.ToString("n8"),
                lData.m_Colors[lCount].b.ToString("n8"),
                lData.m_Colors[lCount].a.ToString("n8")));
        }

        for (int lGroup = 0; lGroup < lData.m_Groups.Count; ++lGroup)
        {
            lLineStreamWriter.WriteLine(string.Format("g {0}", lData.m_Groups[lGroup].m_Name));

            for (int lFace = 0; lFace < lData.m_Groups[lGroup].Faces.Count; ++lFace)
            {
                lLineStreamWriter.WriteLine(string.Format("f {0} {1} {2}",
                    lData.m_Groups[lGroup].Faces[lFace].ToString(0),
                    lData.m_Groups[lGroup].Faces[lFace].ToString(1),
                    lData.m_Groups[lGroup].Faces[lFace].ToString(2)));
            }
        }

        lLineStreamWriter.Flush();
    }
        //------------------------------------------------------------------------------------------------------------
        public static void LoadOBJ(this Mesh lMesh, OBJData lData)
        {
            List <Vector3> lVertices = new List <Vector3>();
            List <Vector3> lNormals  = new List <Vector3>();
            List <Vector2> lUVs      = new List <Vector2>();

            List <int>[] lIndices = new List <int> [lData.m_Groups.Count];
            Dictionary <OBJFaceVertex, int> lVertexIndexRemap = new Dictionary <OBJFaceVertex, int>();
            bool lHasNormals = lData.m_Normals.Count > 0;
            bool lHasUVs     = lData.m_UVs.Count > 0;

            lMesh.subMeshCount = lData.m_Groups.Count;
            for (int lGCount = 0; lGCount < lData.m_Groups.Count; ++lGCount)
            {
                OBJGroup lGroup = lData.m_Groups[lGCount];
                lIndices[lGCount] = new List <int>();

                for (int lFCount = 0; lFCount < lGroup.Faces.Count; ++lFCount)
                {
                    OBJFace lFace = lGroup.Faces[lFCount];

                    // Unity3d doesn't support non-triangle faces
                    // so we do simple fan triangulation
                    for (int lVCount = 1; lVCount < lFace.Count - 1; ++lVCount)
                    {
                        foreach (int i in new int[] { 0, lVCount, lVCount + 1 })
                        {
                            OBJFaceVertex lFaceVertex  = lFace[i];
                            int           lVertexIndex = -1;

                            if (!lVertexIndexRemap.TryGetValue(lFaceVertex, out lVertexIndex))
                            {
                                lVertexIndexRemap[lFaceVertex] = lVertices.Count;
                                lVertexIndex = lVertices.Count;

                                lVertices.Add(lData.m_Vertices[lFaceVertex.m_VertexIndex]);
                                if (lHasUVs)
                                {
                                    lUVs.Add(lData.m_UVs[lFaceVertex.m_UVIndex]);
                                }
                                if (lHasNormals)
                                {
                                    lNormals.Add(lData.m_Normals[lFaceVertex.m_NormalIndex]);
                                }
                            }

                            lIndices[lGCount].Add(lVertexIndex);
                        }
                    }
                }
            }

            lMesh.triangles = new int[] { };
            lMesh.vertices  = lVertices.ToArray();
            lMesh.uv        = lUVs.ToArray();
            lMesh.normals   = lNormals.ToArray();
            if (!lHasNormals)
            {
                lMesh.RecalculateNormals();
            }

            lMesh.RecalculateTangents();

            for (int lGCount = 0; lGCount < lData.m_Groups.Count; ++lGCount)
            {
                lMesh.SetTriangles(lIndices[lGCount].ToArray(), lGCount);
            }
        }
Beispiel #18
0
        //------------------------------------------------------------------------------------------------------------
        public static void LoadOBJ(this Mesh lMesh, OBJData lData)
        {
            List<Vector3> lVertices = new List<Vector3>();
            List<Vector3> lNormals = new List<Vector3>();
            List<Vector2> lUVs = new List<Vector2>();
            List<int>[] lIndices = new List<int>[lData.m_Groups.Count];
            Dictionary<OBJFaceVertex, int> lVertexIndexRemap = new Dictionary<OBJFaceVertex, int>();
            bool lHasNormals = lData.m_Normals.Count > 0;
            bool lHasUVs = lData.m_UVs.Count > 0;

            lMesh.subMeshCount = lData.m_Groups.Count;
            for (int lGCount = 0; lGCount < lData.m_Groups.Count; ++lGCount)
            {
                OBJGroup lGroup = lData.m_Groups[lGCount];
                lIndices[lGCount] = new List<int>();

                for (int lFCount = 0; lFCount < lGroup.Faces.Count; ++lFCount)
                {
                    OBJFace lFace = lGroup.Faces[lFCount];

                    // Unity3d doesn't support non-triangle faces
                    // so we do simple fan triangulation
                    for (int lVCount = 1; lVCount < lFace.Count - 1; ++lVCount)
                    {
                        foreach (int i in new int[]{0, lVCount, lVCount + 1})
                        {
                            OBJFaceVertex lFaceVertex = lFace[i];
                            int lVertexIndex = -1;

                            if (!lVertexIndexRemap.TryGetValue(lFaceVertex, out lVertexIndex)) {
                                lVertexIndexRemap[lFaceVertex] = lVertices.Count;
                                lVertexIndex = lVertices.Count;

                                lVertices.Add(lData.m_Vertices[lFaceVertex.m_VertexIndex]);
                                if (lHasUVs)
                                {
                                    lUVs.Add(lData.m_UVs[lFaceVertex.m_UVIndex]);
                                }
                                if (lHasNormals)
                                {
                                    lNormals.Add(lData.m_Normals[lFaceVertex.m_NormalIndex]);
                                }
                            }

                            lIndices[lGCount].Add(lVertexIndex);
                        }
                    }
                }
            }

            lMesh.triangles = new int[]{ };
            lMesh.vertices = lVertices.ToArray();
            lMesh.uv = lUVs.ToArray();
            lMesh.normals = lNormals.ToArray();
            if (!lHasNormals)
            {
                lMesh.RecalculateNormals();
            }

            lMesh.RecalculateTangents();

            for (int lGCount = 0; lGCount < lData.m_Groups.Count; ++lGCount)
            {
                lMesh.SetTriangles(lIndices[lGCount].ToArray(), lGCount);
            }
        }
Beispiel #19
0
        //------------------------------------------------------------------------------------------------------------
        public static OBJData EncodeOBJ(this Mesh lMesh)
        {
            OBJData lData = new OBJData
            {
                m_Vertices = new List<Vector3>(lMesh.vertices),
                m_UVs = new List<Vector2>(lMesh.uv),
                m_Normals = new List<Vector3>(lMesh.normals),
                m_UV2s = new List<Vector2>(lMesh.uv1),
                m_Colors = new List<Color>(lMesh.colors)
            };

            int[] lIndices = null;
            OBJGroup lGroup = null;
            OBJFace lFace = null;
            OBJFaceVertex lFaceVertex = null;

            for (int lMCount = 0; lMCount < lMesh.subMeshCount; ++lMCount)
            {
                lIndices = lMesh.GetTriangles(lMCount);
                lGroup = new OBJGroup(lMesh.name + "_" + lMCount.ToString());

                for (int lCount = 0; lCount < lIndices.Length; lCount += 3)
                {
                    lFace = new OBJFace();

                    lFaceVertex = new OBJFaceVertex();
                    lFaceVertex.m_VertexIndex = lData.m_Vertices.Count > 0 ? lIndices[lCount] : -1;
                    lFaceVertex.m_UVIndex = lData.m_UVs.Count > 0 ? lIndices[lCount] : -1;
                    lFaceVertex.m_NormalIndex = lData.m_Normals.Count > 0 ? lIndices[lCount] : -1;
                    lFaceVertex.m_UV2Index = lData.m_UV2s.Count > 0 ? lIndices[lCount] : -1;
                    lFaceVertex.m_ColorIndex = lData.m_Colors.Count > 0 ? lIndices[lCount] : -1;
                    lFace.AddVertex(lFaceVertex);

                    lFaceVertex = new OBJFaceVertex();
                    lFaceVertex.m_VertexIndex = lData.m_Vertices.Count > 0 ? lIndices[lCount + 1] : -1;
                    lFaceVertex.m_UVIndex = lData.m_UVs.Count > 0 ? lIndices[lCount + 1] : -1;
                    lFaceVertex.m_NormalIndex = lData.m_Normals.Count > 0 ? lIndices[lCount + 1] : -1;
                    lFaceVertex.m_UV2Index = lData.m_UV2s.Count > 0 ? lIndices[lCount + 1] : -1;
                    lFaceVertex.m_ColorIndex = lData.m_Colors.Count > 0 ? lIndices[lCount + 1] : -1;
                    lFace.AddVertex(lFaceVertex);

                    lFaceVertex = new OBJFaceVertex();
                    lFaceVertex.m_VertexIndex = lData.m_Vertices.Count > 0 ? lIndices[lCount + 2] : -1;
                    lFaceVertex.m_UVIndex = lData.m_UVs.Count > 0 ? lIndices[lCount + 2] : -1;
                    lFaceVertex.m_NormalIndex = lData.m_Normals.Count > 0 ? lIndices[lCount + 2] : -1;
                    lFaceVertex.m_UV2Index = lData.m_UV2s.Count > 0 ? lIndices[lCount + 2] : -1;
                    lFaceVertex.m_ColorIndex = lData.m_Colors.Count > 0 ? lIndices[lCount + 2] : -1;
                    lFace.AddVertex(lFaceVertex);

                    lGroup.AddFace(lFace);
                }

                lData.m_Groups.Add(lGroup);
            }

            return lData;
        }
Beispiel #20
0
    private void InstantiateGo(Vector3 position)
    {
        //Instantiate the a game object from the name of the UI element clicked
        GameObject GO = new GameObject(this.name + "-" + DateTime.Now.ToString("hhmmssffff"));

        //Add material
        MeshRenderer mr = GO.AddComponent <MeshRenderer>();

        mr.material = Resources.Load <Material>("Materials/BenchMat");
        //Load mesh from file
        MeshFilter mf       = GO.AddComponent <MeshFilter>();
        FileStream lStream  = new FileStream(path, FileMode.Open);
        OBJData    lOBJData = OBJLoader.LoadOBJ(lStream);

        lStream.Close();
        mf.mesh.LoadOBJ(lOBJData);
        mf.mesh.RecalculateNormals();

        //Box collider
        BoxCollider collider = GO.AddComponent <BoxCollider>();

        //Create container for rotation
        GameObject container = new GameObject("Container-" + GO.name);

        container.transform.position = collider.bounds.center;
        container.transform.SetParent(GameObject.Find("AdditionalElements").transform, true);
        //Add interactions script
        SelectableElementController selectableElement = container.AddComponent <SelectableElementController>();
        //Check if is alterable
        GameObject alterableElements = GameObject.Find("Alterable");

        if (this.transform.IsChildOf(alterableElements.transform))
        {
            selectableElement.SetAlterable(true);
        }
        else
        {
            selectableElement.SetAlterable(false);
        }

        //Set parent
        GO.transform.SetParent(container.transform, false);
        GO.transform.localPosition = Vector3.zero;
        GO.tag = "AdditionalElements";

        //Normalize scale to MIN_MAX_SIZE Unity units...
        if (collider.bounds.size.x > MIN_MAX_SIZE)
        {
            while (collider.bounds.size.magnitude >= MIN_MAX_SIZE)
            {
                container.transform.localScale -= new Vector3(0.01f, 0.01f, 0.01f);
            }
        }
        else
        {
            while (collider.bounds.size.magnitude <= MIN_MAX_SIZE)
            {
                container.transform.localScale += new Vector3(0.01f, 0.01f, 0.01f);
            }
        }

        container.transform.position = position;

        //Stick to floor
        Bounds  bounds       = collider.bounds;
        float   colliderMinY = bounds.min.y;
        Vector3 newPosition  = container.transform.position;

        if (colliderMinY < 0)
        {
            newPosition += new Vector3(0, -colliderMinY, 0);
        }
        else
        {
            newPosition -= new Vector3(0, colliderMinY, 0);
        }
        container.transform.position = newPosition;

        //Update UI
        HistoryPanelManager.instance.Refresh();
    }