Example #1
0
        public static void CreateMeshesFromCaronte(CaronteSharp.MeshComplex[] arrCaronteMesh, out UnityEngine.Mesh[] arrMesh, out Tuple2 <int, int>[] arrSubmeshRange, out int[] meshComplexIndex)
        {
            int numMeshes = arrCaronteMesh.Length;

            List <Mesh> listMesh = new List <Mesh>();
            List <Tuple2 <int, int> > listSubmeshRange = new List <Tuple2 <int, int> >();
            List <int> listMeshComplexIndex            = new List <int>();

            for (int i = 0; i < numMeshes; i++)
            {
                CaronteSharp.MeshComplex caronteMesh = arrCaronteMesh[i];

                Mesh[] arrAuxMesh;
                Tuple2 <int, int>[] arrAuxSubmeshRange;

                CRGeometryUtils.CreateMeshesFromComplex(caronteMesh, out arrAuxMesh, out arrAuxSubmeshRange);
                listMesh.AddRange(arrAuxMesh);
                listSubmeshRange.AddRange(arrAuxSubmeshRange);

                for (int j = 0; j < arrAuxMesh.Length; j++)
                {
                    listMeshComplexIndex.Add(i);
                }
            }

            arrMesh          = listMesh.ToArray();
            arrSubmeshRange  = listSubmeshRange.ToArray();
            meshComplexIndex = listMeshComplexIndex.ToArray();
        }
Example #2
0
        private static void CreateListMeshComplexFromUnity(List <GameObject> listGameObject, List <Mesh> listMesh, out List <MeshComplex> listMeshComplex, out List <Matrix4x4> listMeshComplexLocalToWorld)
        {
            listMeshComplex             = new List <CaronteSharp.MeshComplex>();
            listMeshComplexLocalToWorld = new List <Matrix4x4>();
            int nParentGameObjects = listGameObject.Count;

            for (int i = 0; i < nParentGameObjects; i++)
            {
                UnityEngine.Mesh un_mesh   = listMesh[i];
                GameObject       parent_go = listGameObject[i];

                CaronteSharp.MeshComplex car_mesh = new CaronteSharp.MeshComplex();
                car_mesh.Set(un_mesh);
                listMeshComplex.Add(car_mesh);

                Matrix4x4 m_Local_to_World = parent_go.transform.localToWorldMatrix;
                listMeshComplexLocalToWorld.Add(m_Local_to_World);
            }
        }
Example #3
0
        public static void TransformListMesh(List <GameObject> listParentGO, List <UnityEngine.Mesh> listMesh_un, Matrix4x4 m_BoundsWorld_to_BoundsLocal, out List <CaronteSharp.MeshComplex> listMesh_car)
        {
            listMesh_car = new List <CaronteSharp.MeshComplex>();
            int numParentGameObjects = listParentGO.Count;

            for (int i = 0; i < numParentGameObjects; ++i)
            {
                UnityEngine.Mesh un_mesh   = listMesh_un[i];
                GameObject       parent_go = listParentGO[i];

                Matrix4x4 m_Local_to_World  = parent_go.transform.localToWorldMatrix;
                Matrix4x4 m_Local_to_Bounds = m_BoundsWorld_to_BoundsLocal * m_Local_to_World;

                UnityEngine.Mesh transformedMesh;
                CRGeometryUtils.CreateMeshTransformed(un_mesh, m_Local_to_Bounds, out transformedMesh);

                CaronteSharp.MeshComplex car_mesh = new CaronteSharp.MeshComplex();
                car_mesh.Set(transformedMesh);

                UnityEngine.Object.DestroyImmediate(transformedMesh);
                listMesh_car.Add(car_mesh);
            }
        }
        //-----------------------------------------------------------------------------------

        static void LoadASE()
        {
            Material defaultmaterial = new Material(Shader.Find("Diffuse"));

            defaultmaterial.name = "default";
            CaronteSharp.MeshComplex[] arrMeshes;
            CaronteSharp.MatrixRTS[]   arrMatrixRTS;
            CaronteSharp.Tools.LoadASE(ASEpath, out arrMeshes, out arrMatrixRTS);

            int numMeshes = arrMeshes.Length;

            for (int i = 0; i < numMeshes; i++)
            {
                CaronteSharp.MeshComplex mesh      = arrMeshes[i];
                CaronteSharp.MatrixRTS   matrixRTS = arrMatrixRTS[i];

                GameObject       go         = new GameObject(/*mesh.name_*/);
                MeshFilter       meshfilter = go.AddComponent <MeshFilter>();
                UnityEngine.Mesh unityMesh  = new UnityEngine.Mesh();

                // unityMesh.name      = mesh.name_;
                unityMesh.vertices  = mesh.arrPosition_;
                unityMesh.normals   = mesh.arrNormal_;
                unityMesh.uv        = mesh.arrUV_;
                unityMesh.triangles = mesh.arrIndex_;

                unityMesh.RecalculateBounds();

                go.transform.position   = matrixRTS.translation_;
                go.transform.rotation   = matrixRTS.rotation_;
                go.transform.localScale = matrixRTS.scale_;

                meshfilter.sharedMesh = unityMesh;
                MeshRenderer meshRenderer = go.AddComponent <MeshRenderer>();
                meshRenderer.sharedMaterial = defaultmaterial;
            }
        }//
Example #5
0
        private static void CreateMeshesFromComplex(CaronteSharp.MeshComplex inputMesh, out UnityEngine.Mesh[] outputMeshes, out Tuple2 <int, int>[] outputSubmeshRange)
        {
            Vector3[] vertices = inputMesh.arrPosition_;
            Vector3[] normals  = inputMesh.arrNormal_;
            Vector4[] tangents = inputMesh.arrTan_;
            Vector2[] uv       = inputMesh.arrUV_;

            bool hasNormals  = (normals != null) && (normals.Length > 0);
            bool hasTangents = (tangents != null) && (tangents.Length > 0);
            bool hasUVs      = (uv != null) && (uv.Length > 0);

            int maxVertexesPerMesh = 65533;

            List <UnityEngine.Mesh>    listMeshes       = new List <UnityEngine.Mesh>();
            List <Tuple2 <int, int>  > listSubmeshRange = new List <Tuple2 <int, int> >();

            List <Vector3> listCurPos = new List <Vector3>();
            List <Vector3> listCurNor = new List <Vector3>();
            List <Vector4> listCurTan = new List <Vector4>();
            List <Vector2> listCurUVs = new List <Vector2>();

            List <List <int> > listListCurIndices = new List <List <int> >();

            Dictionary <int, int> dictOldIndexToNew = new Dictionary <int, int>();

            int indexMin = 0;
            int indexMax = 0;

            int submeshIdMin = 0;

            int[] subMeshesIdx = inputMesh.arrSubmeshIdx_;
            int   nSubmeshIdx  = subMeshesIdx.Length;

            for (int i = 0; i < nSubmeshIdx; i++)
            {
                indexMin = indexMax;
                indexMax = subMeshesIdx[i] * 3;

                List <int> listCurIndices = new List <int>();
                listListCurIndices.Add(listCurIndices);

                for (int j = indexMin; j < indexMax; j++)
                {
                    int oldIndex = inputMesh.arrIndex_[j];
                    if (!dictOldIndexToNew.ContainsKey(oldIndex))
                    {
                        dictOldIndexToNew.Add(oldIndex, listCurPos.Count);

                        listCurPos.Add(vertices[oldIndex]);
                        if (hasNormals)
                        {
                            listCurNor.Add(normals[oldIndex]);
                        }
                        if (hasTangents)
                        {
                            listCurTan.Add(tangents[oldIndex]);
                        }
                        if (hasUVs)
                        {
                            listCurUVs.Add(uv[oldIndex]);
                        }
                    }

                    int index = dictOldIndexToNew[oldIndex];
                    listCurIndices.Add(index);

                    bool isTriangleEnd      = ((j % 3) == 2);
                    bool isMaxVertexReached = (listCurPos.Count >= maxVertexesPerMesh);
                    bool isLastMeshTriangle = ((i == (nSubmeshIdx - 1)) && (j == (indexMax - 1)));

                    if ((isMaxVertexReached && isTriangleEnd) || isLastMeshTriangle)
                    {
                        Mesh mesh;
                        GenerateUnityMesh(listCurPos, listCurNor, listCurTan, listCurUVs, listListCurIndices, out mesh);

                        listMeshes.Add(mesh);
                        listSubmeshRange.Add(Tuple2.New(submeshIdMin, i));

                        submeshIdMin = i;

                        listListCurIndices.Add(listCurIndices);
                        dictOldIndexToNew.Clear();
                    }
                }
            }

            outputMeshes       = listMeshes.ToArray();
            outputSubmeshRange = listSubmeshRange.ToArray();
        }