Exemple #1
0
        public IEnumerator CreateAtlasesCoroutine()
        {
            this.OnCombinedTexturesCoroutineAtlasesAndRects = null;
            MBValidationLevel vl = MBValidationLevel.quick;

            if (!DoCombinedValidate(this, MB_ObjsToCombineTypes.dontCare, vl))
            {
                yield break;
            }
            if (_doMultiMaterial && !_ValidateResultMaterials())
            {
                yield break;
            }
            else if (!_doMultiMaterial)
            {
                if (_resultMaterial == null)
                {
                    Debug.LogError("Combined Material is null please create and assign a result material.");
                    yield break;
                }
                Shader targShader = _resultMaterial.shader;
                for (int i = 0; i < objsToMesh.Count; i++)
                {
                    Material[] ms = MeshBakerUtility.GetGOMaterials(objsToMesh[i]);
                    for (int j = 0; j < ms.Length; j++)
                    {
                        Material m = ms[j];
                        if (m != null && m.shader != targShader)
                        {
                            Debug.LogWarning("Game object " + objsToMesh[i] + " does not use shader " + targShader + " it may not have the required textures. If not small solid color textures will be generated.");
                        }
                    }
                }
            }

            for (int i = 0; i < objsToMesh.Count; i++)
            {
                Material[] ms = MeshBakerUtility.GetGOMaterials(objsToMesh[i]);
                for (int j = 0; j < ms.Length; j++)
                {
                    Material m = ms[j];
                    if (m == null)
                    {
                        Debug.LogError("Game object " + objsToMesh[i] + " has a null material. Can't build atlases");
                        yield break;
                    }
                }
            }

            TextureCombiner combiner = new TextureCombiner();

            combiner.LOG_LEVEL             = LOG_LEVEL;
            combiner.atlasPadding          = _atlasPadding;
            combiner.maxAtlasSize          = _maxAtlasSize;
            combiner.customShaderPropNames = _customShaderProperties;
            combiner.fixOutOfBoundsUVs     = _fixOutOfBoundsUVs;
            combiner.maxTilingBakeSize     = _maxTilingBakeSize;

            //initialize structure to store results
            int numResults = 1;

            if (_doMultiMaterial)
            {
                numResults = resultMaterials.Length;
            }
            OnCombinedTexturesCoroutineAtlasesAndRects = new AtlasesAndRects[numResults];
            for (int i = 0; i < OnCombinedTexturesCoroutineAtlasesAndRects.Length; i++)
            {
                OnCombinedTexturesCoroutineAtlasesAndRects[i] = new AtlasesAndRects();
            }

            //Do the material combining.
            for (int i = 0; i < OnCombinedTexturesCoroutineAtlasesAndRects.Length; i++)
            {
                Material        resMatToPass = null;
                List <Material> sourceMats   = null;
                if (_doMultiMaterial)
                {
                    sourceMats   = resultMaterials[i].sourceMaterials;
                    resMatToPass = resultMaterials[i].combinedMaterial;
                }
                else
                {
                    resMatToPass = _resultMaterial;
                }
                Debug.Log(string.Format("Creating atlases for result material {0} using shader {1}", resMatToPass, resMatToPass.shader));
                TextureCombiner.CombineTexturesIntoAtlasesResult result2 = new TextureCombiner.CombineTexturesIntoAtlasesResult();
                yield return(combiner.CombineTexturesIntoAtlases(OnCombinedTexturesCoroutineAtlasesAndRects[i],
                                                                 resMatToPass, objsToMesh, sourceMats, result2));

                if (!result2.success)
                {
                    yield break;
                }
            }

            //Save the results
            textureBakeResults.doMultiMaterial   = _doMultiMaterial;
            textureBakeResults.resultMaterial    = _resultMaterial;
            textureBakeResults.resultMaterials   = resultMaterials;
            textureBakeResults.fixOutOfBoundsUVs = combiner.fixOutOfBoundsUVs;
            unpackMat2RectMap(textureBakeResults);

            //set the texture bake resultAtlasesAndRects on the Mesh Baker component if it exists
            MeshBakerCommon[] mb = bakerCommons.ToArray();
            for (int i = 0; i < mb.Length; i++)
            {
                mb[i].textureBakeResults = textureBakeResults;
                mb[i].TexBaker           = this;
            }

            if (LOG_LEVEL >= MBLogLevel.info)
            {
                Debug.Log("Created Atlases");
            }

            if (onBuiltAtlasesSuccess != null)
            {
                onBuiltAtlasesSuccess(this);
            }
        }
Exemple #2
0
        public static bool DoCombinedValidate(MeshBakerRoot mom, MB_ObjsToCombineTypes objToCombineType, MBValidationLevel validationLevel)
        {
            if (mom.textureBakeResults == null)
            {
                Debug.LogError("Need to set Material Bake Result on " + mom);
                return(false);
            }
            if (mom is MeshBakerCommon)
            {
                MeshBakerCommon momMB = (MeshBakerCommon)mom;
                TextureBaker    tb    = momMB.GetTextureBaker();
                if (tb != null && tb.textureBakeResults != mom.textureBakeResults)
                {
                    Debug.LogWarning("Material Bake Result on this component is not the same as the Material Bake Result on the MB3_TextureBaker.");
                }
            }

            Dictionary <int, MeshBakerUtility.MeshAnalysisResult> meshAnalysisResultCache = null;

            if (validationLevel == MBValidationLevel.robust)
            {
                meshAnalysisResultCache = new Dictionary <int, MeshBakerUtility.MeshAnalysisResult>();
            }
            List <GameObject> objsToMesh = mom.GetObjectsToCombine();

            for (int i = 0; i < objsToMesh.Count; i++)
            {
                GameObject go = objsToMesh[i];
                if (go == null)
                {
                    Debug.LogError("The list of objects to combine contains a null at position." + i + " Select and use [shift] delete to remove");
                    return(false);
                }
                for (int j = i + 1; j < objsToMesh.Count; j++)
                {
                    if (objsToMesh[i] == objsToMesh[j])
                    {
                        Debug.LogError("The list of objects to combine contains duplicates at " + i + " and " + j);
                        return(false);
                    }
                }
                if (MeshBakerUtility.GetGOMaterials(go) == null)
                {
                    Debug.LogError("Object " + go + " in the list of objects to be combined does not have a material");
                    return(false);
                }
                Mesh m = MeshBakerUtility.GetMesh(go);
                if (m == null)
                {
                    Debug.LogError("Object " + go + " in the list of objects to be combined does not have a mesh");
                    return(false);
                }
                if (m != null)              //This check can be very expensive and it only warns so only do this if we are in the editor.
                {
                    if (!Application.isEditor &&
                        Application.isPlaying &&
                        mom.textureBakeResults.doMultiMaterial &&
                        validationLevel >= MBValidationLevel.robust)
                    {
                        MeshBakerUtility.MeshAnalysisResult mar;
                        if (!meshAnalysisResultCache.TryGetValue(m.GetInstanceID(), out mar))
                        {
                            MeshBakerUtility.doSubmeshesShareVertsOrTris(m, ref mar);
                            meshAnalysisResultCache.Add(m.GetInstanceID(), mar);
                        }
                        if (mar.hasOverlappingSubmeshVerts)
                        {
                            Debug.LogWarning("Object " + objsToMesh[i] + " in the list of objects to combine has overlapping submeshes (submeshes share vertices). If the UVs associated with the shared vertices are important then this bake may not work. If you are using multiple materials then this object can only be combined with objects that use the exact same set of textures (each atlas contains one texture). There may be other undesirable side affects as well. Mesh Master, available in the asset store can fix overlapping submeshes.");
                        }
                    }
                }
            }


            List <GameObject> objs = objsToMesh;

            if (mom is MeshBaker)
            {
                objs = mom.GetObjectsToCombine();
                //if (((MB3_MeshBaker)mom).useObjsToMeshFromTexBaker && tb != null) objs = tb.GetObjectsToCombine();
                if (objs == null || objs.Count == 0)
                {
                    Debug.LogError("No meshes to combine. Please assign some meshes to combine.");
                    return(false);
                }
            }

            return(true);
        }