Пример #1
0
        /*-----------------------------------------------------------------------------------------
        *
        *	Computational and optimization stuff :
        *
        *  -----------------------------------------------------------------------------------------*/

        /// <summary>
        /// Defragmentates mesh subsets with same materials
        /// </summary>
        public void DefragmentSubsets(Scene scene, bool takeFromTriangleMtrlIndices)
        {
            //	if there are not shading groups,
            //	take them from per triangle material indices
            if (!Subsets.Any() || takeFromTriangleMtrlIndices)
            {
                for (int i = 0; i < Triangles.Count; i++)
                {
                    MeshSubset sg = new MeshSubset();
                    sg.MaterialIndex  = Triangles[i].MaterialIndex;
                    sg.StartPrimitive = i;
                    sg.PrimitiveCount = 1;

                    Subsets.Add(sg);
                    //Console.Write( "*{0}", Triangles[i].MaterialIndex );
                }
            }


            if (Subsets.Count == 1)
            {
                return;
            }


            List <List <MeshTriangle> > perMtrlTris = new List <List <MeshTriangle> >();

            foreach (var mtrl in scene.Materials)
            {
                perMtrlTris.Add(new List <MeshTriangle>());
            }

            foreach (var sg in Subsets)
            {
                for (int i = sg.StartPrimitive; i < sg.StartPrimitive + sg.PrimitiveCount; i++)
                {
                    perMtrlTris[sg.MaterialIndex].Add(Triangles[i]);
                }
            }

            Subsets.Clear();
            Triangles.Clear();

            for (int i = 0; i < perMtrlTris.Count; i++)
            {
                var sg = new MeshSubset();
                sg.MaterialIndex  = i;
                sg.StartPrimitive = Triangles.Count;
                sg.PrimitiveCount = perMtrlTris[i].Count;

                if (sg.PrimitiveCount == 0)
                {
                    continue;
                }

                Triangles.AddRange(perMtrlTris[i]);
                Subsets.Add(sg);
            }
        }