Ejemplo n.º 1
0
            //Rect[] uvRectInAtlas;

            public Material2AtlasRectangleMapper(TextureBakeResults res)
            {
                tbr = res;
                matsAndSrcUVRect = res.materialsAndUVRects;

                //backward compatibility. this may be an old TextureBakeResult which has no materialsAndUVRects if so then build it now
                if (matsAndSrcUVRect == null || matsAndSrcUVRect.Length == 0)
                {
                    matsAndSrcUVRect = new MaterialAndUVRect[res.materials.Length];
                    for (int i = 0; i < res.materials.Length; i++)
                    {
                        matsAndSrcUVRect[i] = new MaterialAndUVRect(res.materials[i], res.prefabUVRects[i], new Rect(0f, 0f, 1f, 1f), new Rect(0f, 0f, 1f, 1f), new Rect(0f, 0f, 1f, 1f), "");
                    }
                    res.materialsAndUVRects = matsAndSrcUVRect;
                }

                //count the number of times a material appears in the atlas. used for fast lookup
                numTimesMatAppearsInAtlas = new int[matsAndSrcUVRect.Length];
                for (int i = 0; i < matsAndSrcUVRect.Length; i++)
                {
                    if (numTimesMatAppearsInAtlas[i] > 1)
                    {
                        continue;
                    }
                    int count = 1;
                    for (int j = i + 1; j < matsAndSrcUVRect.Length; j++)
                    {
                        if (matsAndSrcUVRect[i].material == matsAndSrcUVRect[j].material)
                        {
                            count++;
                        }
                    }
                    numTimesMatAppearsInAtlas[i] = count;
                    if (count > 1)
                    {
                        //allMatsAreUnique = false;
                        for (int j = i + 1; j < matsAndSrcUVRect.Length; j++)
                        {
                            if (matsAndSrcUVRect[i].material == matsAndSrcUVRect[j].material)
                            {
                                numTimesMatAppearsInAtlas[j] = count;
                            }
                        }
                    }
                }
            }
Ejemplo n.º 2
0
        void unpackMat2RectMap(TextureBakeResults tbr)
        {
            List <Material>          ms  = new List <Material>();
            List <MaterialAndUVRect> mss = new List <MaterialAndUVRect>();
            List <Rect> rs = new List <Rect>();

            for (int i = 0; i < OnCombinedTexturesCoroutineAtlasesAndRects.Length; i++)
            {
                AtlasesAndRects          newMesh = this.OnCombinedTexturesCoroutineAtlasesAndRects[i];
                List <MaterialAndUVRect> map     = newMesh.mat2rect_map;
                for (int j = 0; j < map.Count; j++)
                {
                    mss.Add(map[j]);
                    ms.Add(map[j].material);
                    rs.Add(map[j].atlasRect);
                }
            }
            tbr.materialsAndUVRects = mss.ToArray();
            tbr.materials           = ms.ToArray();
            //tbr.prefabUVRects = rs.ToArray();
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Creates for materials on renderer.
        /// </summary>
        /// <returns>Generates an MB2_TextureBakeResult that can be used if all objects to be combined use the same material.
        /// Returns a MB2_TextureBakeResults that will map all materials used by renderer r to
        /// the rectangle 0,0..1,1 in the atlas.</returns>
        /// <param name="r">The red component.</param>
        public static TextureBakeResults CreateForMaterialsOnRenderer(GameObject[] gos)
        {
            HashSet <Material> fullMaterialList = new HashSet <Material>();

            for (int i = 0; i < gos.Length; i++)
            {
                if (gos[i] == null)
                {
                    Debug.LogError(string.Format("Game object {0} in list of objects to add was null", i));
                    return(null);
                }
                Material[] oMats = MeshBakerUtility.GetGOMaterials(gos[i]);
                if (oMats == null)
                {
                    Debug.LogError(string.Format("Game object {0} in list of objects to add no renderer", i));
                    return(null);
                }
                for (int j = 0; j < oMats.Length; j++)
                {
                    fullMaterialList.Add(oMats[j]);
                }
            }
            Material[] rms = new Material[fullMaterialList.Count];
            fullMaterialList.CopyTo(rms);

            TextureBakeResults tbr = new TextureBakeResults();
            //Material[] ms = rms;
            //MB_MaterialAndUVRect[] mss = new MB_MaterialAndUVRect[rms.Length];
            List <MaterialAndUVRect> mss = new List <MaterialAndUVRect>();

            Material[] ms;
            for (int i = 0; i < rms.Length; i++)
            {
                if (rms[i] != null)
                {
                    MaterialAndUVRect matAndUVRect = new MaterialAndUVRect(rms[i], new Rect(0f, 0f, 1f, 1f),
                                                                           new Rect(0f, 0f, 1f, 1f),
                                                                           new Rect(0f, 0f, 1f, 1f),
                                                                           new Rect(0f, 0f, 1f, 1f), "");
                    if (!mss.Contains(matAndUVRect))
                    {
                        mss.Add(matAndUVRect);
                    }
                }
            }
            if (rms.Length > 1)
            {
                tbr.prefabUVRects   = new Rect[mss.Count];
                tbr.materials       = ms = new Material[mss.Count];
                tbr.resultMaterials = new MultiMaterial[mss.Count];
                for (int i = 0; i < mss.Count; i++)
                {
                    ms[i] = mss[i].material;
                    tbr.prefabUVRects[i]   = new Rect(0f, 0f, 1f, 1f);
                    tbr.resultMaterials[i] = new MultiMaterial();
                    List <Material> sourceMats = new List <Material>();
                    sourceMats.Add(ms[i]);
                    tbr.resultMaterials[i].sourceMaterials  = sourceMats;
                    tbr.resultMaterials[i].combinedMaterial = ms[i];
                }
                tbr.doMultiMaterial = true;
            }
            else
            {
                tbr.doMultiMaterial = false;
                tbr.prefabUVRects   = new Rect[] { new Rect(0f, 0f, 1f, 1f) };
                tbr.materials       = ms = new Material[] { mss[0].material };
                tbr.resultMaterial  = mss[0].material;
                tbr.resultMaterials = new MultiMaterial[] { new MultiMaterial() };
                List <Material> sourceMats = new List <Material>();
                sourceMats.Add(ms[0]);
                tbr.resultMaterials[0].sourceMaterials  = sourceMats;
                tbr.resultMaterials[0].combinedMaterial = mss[0].material;
            }
            tbr.materialsAndUVRects = mss.ToArray();
            tbr.fixOutOfBoundsUVs   = false;
            return(tbr);
        }