MB_AtlasesAndRects[] _CreateAtlases(ProgressUpdateDelegate progressInfo, bool saveAtlasesAsAssets = false, MB2_EditorMethodsInterface textureFormatTracker = null)
    {
        //validation
        if (saveAtlasesAsAssets && textureFormatTracker == null)
        {
            Debug.LogError("Error in CreateAtlases If saveAtlasesAsAssets = true then textureFormatTracker cannot be null.");
            return(null);
        }
        if (saveAtlasesAsAssets && !Application.isEditor)
        {
            Debug.LogError("Error in CreateAtlases If saveAtlasesAsAssets = true it must be called from the Unity Editor.");
            return(null);
        }
        if (!doCombinedValidate(this, MB_ObjsToCombineTypes.dontCare, textureFormatTracker))
        {
            return(null);
        }
        if (doMultiMaterial && !_ValidateResultMaterials())
        {
            return(null);
        }
        else if (!doMultiMaterial)
        {
            if (resultMaterial == null)
            {
                Debug.LogError("Combined Material is null please create and assign a result material.");
                return(null);
            }
            Shader targShader = resultMaterial.shader;
            for (int i = 0; i < objsToMesh.Count; i++)
            {
                Material[] ms = MB_Utility.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 2x2 clear textures will be generated.");
                    }
                }
            }
        }

        for (int i = 0; i < objsToMesh.Count; i++)
        {
            Material[] ms = MB_Utility.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");
                    return(null);
                }
            }
        }

        MB_TextureCombiner combiner = new MB_TextureCombiner();

        // if editor analyse meshes and suggest treatment
        if (!Application.isPlaying)
        {
            Material[] rms;
            if (doMultiMaterial)
            {
                rms = new Material[resultMaterials.Length];
                for (int i = 0; i < rms.Length; i++)
                {
                    rms[i] = resultMaterials[i].combinedMaterial;
                }
            }
            else
            {
                rms    = new Material[1];
                rms[0] = resultMaterial;
            }
            combiner.SuggestTreatment(objsToMesh, rms, customShaderPropNames);
        }

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

        if (doMultiMaterial)
        {
            numResults = resultMaterials.Length;
        }
        MB_AtlasesAndRects[] resultAtlasesAndRects = new MB_AtlasesAndRects[numResults];
        for (int i = 0; i < resultAtlasesAndRects.Length; i++)
        {
            resultAtlasesAndRects[i] = new MB_AtlasesAndRects();
        }

        //Do the material combining.
        for (int i = 0; i < resultAtlasesAndRects.Length; i++)
        {
            Material        resMatToPass = null;
            List <Material> sourceMats   = null;
            if (doMultiMaterial)
            {
                sourceMats   = resultMaterials[i].sourceMaterials;
                resMatToPass = resultMaterials[i].combinedMaterial;
            }
            else
            {
                resMatToPass = resultMaterial;
            }
            Debug.Log("Creating atlases for result material " + resMatToPass);
            if (!combiner.combineTexturesIntoAtlases(progressInfo, resultAtlasesAndRects[i], resMatToPass, objsToMesh, sourceMats, atlasPadding, customShaderPropNames, resizePowerOfTwoTextures, fixOutOfBoundsUVs, maxTilingBakeSize, saveAtlasesAsAssets, texturePackingAlgorithm, textureFormatTracker))
            {
                return(null);
            }
        }

        //Save the results
        textureBakeResults.combinedMaterialInfo = resultAtlasesAndRects;
        textureBakeResults.doMultiMaterial      = doMultiMaterial;
        textureBakeResults.resultMaterial       = resultMaterial;
        textureBakeResults.resultMaterials      = resultMaterials;
        textureBakeResults.fixOutOfBoundsUVs    = fixOutOfBoundsUVs;
        unpackMat2RectMap(textureBakeResults);

        //originally did all the assign of atlases to the result materials here
        //don't touch result material assets until we are sure atlas creation worked.
        //unfortunatly Unity has a bug where it Destroys Texture2Ds without warning when memory gets low and generates MissingReferenceException
        //so if generating assets in editor then save and assign atlases as soon as each atlas is created
//			if (Application.isPlaying){
//				if (doMultiMaterial){
//					for (int j = 0; j < resultMaterials.Length; j++){
//						Material resMat = resultMaterials[j].combinedMaterial; //resultMaterials[j].combinedMaterial;
//						Texture2D[] atlases = resultAtlasesAndRects[j].atlases;
//						for(int i = 0; i < atlases.Length;i++){
//							resMat.SetTexture(resultAtlasesAndRects[j].texPropertyNames[i], atlases[i]);
//						}
//					}
//				} else {
//					Material resMat = resultMaterial; //resultMaterials[j].combinedMaterial;
//					Texture2D[] atlases = resultAtlasesAndRects[0].atlases;
//					for(int i = 0; i < atlases.Length;i++){
//						resMat.SetTexture(resultAtlasesAndRects[0].texPropertyNames[i], atlases[i]);
//					}
//				}
//			}

        //set the texture bake resultAtlasesAndRects on the Mesh Baker component if it exists
        MB2_MeshBakerCommon mb = GetComponent <MB2_MeshBakerCommon>();

        if (mb != null)
        {
            mb.textureBakeResults = textureBakeResults;
        }

        if (VERBOSE)
        {
            Debug.Log("Created Atlases");
        }
        return(resultAtlasesAndRects);
    }
Example #2
0
    public MB_AtlasesAndRects[] CreateAtlases(ProgressUpdateDelegate progressInfo, bool saveAtlasesAsAssets = false, MB_TextureCombiner.FileSaveFunction fileSaveFunction = null)
    {
        if (doMultiMaterial)
        {
            for (int i = 0; i < resultMaterials.Length; i++)
            {
                MB_MultiMaterial mm = resultMaterials[i];
                if (mm.combinedMaterial == null)
                {
                    Debug.LogError("Combined Material is null please create and assign a result material.");
                    return(null);
                }
                UnityEngine.Shader targShader = mm.combinedMaterial.shader;
                for (int j = 0; j < mm.sourceMaterials.Count; j++)
                {
                    if (mm.sourceMaterials[j] == null)
                    {
                        Debug.LogError("There are null entries in the list of Source Materials");
                        return(null);
                    }
                    if (targShader != mm.sourceMaterials[j].shader)
                    {
                        Debug.LogWarning("Source material " + mm.sourceMaterials[j] + " does not use shader " + targShader + " it may not have the required textures. If not empty textures will be generated.");
                    }
                }
            }
        }
        else
        {
            if (resultMaterial == null)
            {
                Debug.LogError("Combined Material is null please create and assign a result material.");
                return(null);
            }
            UnityEngine.Shader targShader = resultMaterial.shader;
            for (int i = 0; i < objsToMesh.Count; i++)
            {
                Material[] ms = MB_Utility.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");
                        return(null);
                    }
                    if (m.shader != targShader)
                    {
                        Debug.LogWarning("Game object " + objsToMesh[i] + " does not use shader " + targShader + " it may not have the required textures. If not empty textures will be generated.");
                    }
                }
            }
        }

        int numResults = 1;

        if (doMultiMaterial)
        {
            numResults = resultMaterials.Length;
        }
        MB_AtlasesAndRects[] results = new MB_AtlasesAndRects[numResults];
        for (int i = 0; i < results.Length; i++)
        {
            results[i] = new MB_AtlasesAndRects();
        }
        MB_TextureCombiner tc = new MB_TextureCombiner();

        Material        resMatToPass = null;
        List <Material> sourceMats   = null;

        for (int i = 0; i < results.Length; i++)
        {
            if (doMultiMaterial)
            {
                sourceMats   = resultMaterials[i].sourceMaterials;
                resMatToPass = resultMaterials[i].combinedMaterial;
            }
            else
            {
                resMatToPass = resultMaterial;
            }
            Debug.Log("Creating atlases for result material " + resMatToPass);
            if (!tc.combineTexturesIntoAtlases(progressInfo, results[i], resMatToPass, objsToMesh, sourceMats, atlasPadding, customShaderPropNames, resizePowerOfTwoTextures, fixOutOfBoundsUVs, maxTilingBakeSize, saveAtlasesAsAssets, texturePackingAlgorithm, fileSaveFunction))
            {
                return(null);
            }
        }

        if (results != null)
        {
            textureBakeResults.combinedMaterialInfo = results;
            textureBakeResults.doMultiMaterial      = doMultiMaterial;
            textureBakeResults.resultMaterial       = resultMaterial;
            textureBakeResults.resultMaterials      = resultMaterials;
            textureBakeResults.fixOutOfBoundsUVs    = fixOutOfBoundsUVs;
            unpackMat2RectMap(textureBakeResults);

            if (UnityEngine.Application.isPlaying)
            {
                if (doMultiMaterial)
                {
                    for (int j = 0; j < resultMaterials.Length; j++)
                    {
                        Material    resMat  = resultMaterials[j].combinedMaterial;                     //resultMaterials[j].combinedMaterial;
                        Texture2D[] atlases = results[j].atlases;
                        for (int i = 0; i < atlases.Length; i++)
                        {
                            resMat.SetTexture(results[j].texPropertyNames[i], atlases[i]);
                            //				_setMaterialTextureProperty(resMat, newMesh.texPropertyNames[i], relativePath);
                        }
                    }
                }
                else
                {
                    Material    resMat  = resultMaterial;                 //resultMaterials[j].combinedMaterial;
                    Texture2D[] atlases = results[0].atlases;
                    for (int i = 0; i < atlases.Length; i++)
                    {
                        resMat.SetTexture(results[0].texPropertyNames[i], atlases[i]);
                        //				_setMaterialTextureProperty(resMat, newMesh.texPropertyNames[i], relativePath);
                    }
                }
            }
        }

        if (VERBOSE)
        {
            Debug.Log("Created Atlases");
        }
        return(results);
    }
Example #3
0
    public MB_AtlasesAndRects[] CreateAtlases(ProgressUpdateDelegate progressInfo)
    {
        if (doMultiMaterial)
        {
            for (int i = 0; i < resultMaterials.Length; i++)
            {
                MB_MultiMaterial mm         = resultMaterials[i];
                Shader           targShader = mm.combinedMaterial.shader;
                for (int j = 0; j < mm.sourceMaterials.Count; j++)
                {
                    if (targShader != mm.sourceMaterials[j].shader)
                    {
                        Debug.LogWarning("Source material " + mm.sourceMaterials[j] + " does not use shader " + targShader + " it may not have the required textures. If not empty textures will be generated.");
                    }
                }
            }
        }
        else
        {
            Shader targShader = resultMaterial.shader;
            for (int i = 0; i < objsToMesh.Count; i++)
            {
                Material[] ms = MB_Utility.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");
                        return(null);
                    }
                    if (m.shader != targShader)
                    {
                        Debug.LogWarning("Game object " + objsToMesh[i] + " does not use shader " + targShader + " it may not have the required textures. If not empty textures will be generated.");
                    }
                }
            }
        }

        int numResults = 1;

        if (doMultiMaterial)
        {
            numResults = resultMaterials.Length;
        }
        MB_AtlasesAndRects[] results = new MB_AtlasesAndRects[numResults];
        for (int i = 0; i < results.Length; i++)
        {
            results[i] = new MB_AtlasesAndRects();
        }
        MB_TextureCombiner tc = new MB_TextureCombiner();

        Material[]      resMatsToPass = new Material[1];
        List <Material> sourceMats    = null;

        for (int i = 0; i < results.Length; i++)
        {
            if (doMultiMaterial)
            {
                sourceMats       = resultMaterials[i].sourceMaterials;
                resMatsToPass[0] = resultMaterials[i].combinedMaterial;
            }
            else
            {
                resMatsToPass[0] = resultMaterial;
            }
            Debug.Log("Creating atlases for result material " + resMatsToPass[0]);
            if (!tc.combineTexturesIntoAtlases(progressInfo, results[i], resMatsToPass, objsToMesh, sourceMats, atlasPadding, customShaderPropNames, resizePowerOfTwoTextures, fixOutOfBoundsUVs, 1024))
            {
                return(null);
            }
        }
        updateMaterialToRectMapping(results);
        return(results);
    }