コード例 #1
0
ファイル: SA3TextureCombine.cs プロジェクト: sacu/CatchFish
    void unpackMat2RectMap(SA2TextureCombineResults resultAtlasesAndRects)
    {
        List <Material> ms = new List <Material>();
        List <Rect>     rs = new List <Rect>();

        for (int i = 0; i < resultAtlasesAndRects.combinedMaterialInfo.Length; i++)
        {
            SAAtlasesAndRects           newMesh = resultAtlasesAndRects.combinedMaterialInfo[i];
            Dictionary <Material, Rect> map     = newMesh.mat2rect_map;
            foreach (Material m in map.Keys)
            {
                ms.Add(m);
                rs.Add(map[m]);
            }
        }
        resultAtlasesAndRects.materials     = ms.ToArray();
        resultAtlasesAndRects.prefabUVRects = rs.ToArray();
    }
コード例 #2
0
ファイル: SA3TextureCombine.cs プロジェクト: sacu/CatchFish
 /// <summary>
 /// Creates the atlases.
 /// </summary>
 /// <returns>
 /// The atlases.
 /// </returns>
 /// <param name='progressInfo'>
 /// Progress info is a delegate function that displays a progress dialog. Can be null
 /// </param>
 /// <param name='saveAtlasesAsAssets'>
 /// if true atlases are saved as assets in the project folder. Othersise they are instances in memory
 /// </param>
 /// <param name='editorMethods'>
 /// Texture format tracker. Contains editor functionality such as save assets. Can be null.
 /// </param>
 public SAAtlasesAndRects[] CreateAtlases(ProgressUpdateDelegate progressInfo, bool saveAtlasesAsAssets = false, SA2EditorMethodsInterface editorMethods = null)
 {
     SAAtlasesAndRects[] mAndAs = null;
     try
     {
         mAndAs = _CreateAtlases(progressInfo, saveAtlasesAsAssets, editorMethods);
     }
     catch (Exception e)
     {
         Debug.LogError(e);
     }
     finally
     {
         if (saveAtlasesAsAssets)
         { //Atlases were saved to project so we don't need these ones
             if (mAndAs != null)
             {
                 for (int j = 0; j < mAndAs.Length; j++)
                 {
                     SAAtlasesAndRects mAndA = mAndAs[j];
                     if (mAndA != null && mAndA.atlases != null)
                     {
                         for (int i = 0; i < mAndA.atlases.Length; i++)
                         {
                             if (mAndA.atlases[i] != null)
                             {
                                 if (editorMethods != null)
                                 {
                                     editorMethods.Destroy(mAndA.atlases[i]);
                                 }
                                 else
                                 {
                                     SAUtility.Destroy(mAndA.atlases[i]);
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     return(mAndAs);
 }
コード例 #3
0
ファイル: SA3TextureCombine.cs プロジェクト: sacu/CatchFish
    SAAtlasesAndRects[] _CreateAtlases(ProgressUpdateDelegate progressInfo, bool saveAtlasesAsAssets = false, SA2EditorMethodsInterface editorMethods = null)
    {
        //validation
        if (saveAtlasesAsAssets && editorMethods == null)
        {
            Debug.LogError("Error in CreateAtlases If saveAtlasesAsAssets = true then editorMethods 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, SAObjsToCombineTypes.dontCare, editorMethods))
        {
            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 = SAUtility.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);
                    }
                    else 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 = SAUtility.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;
        //        }
        //    }
        //}

        SA3TextureCombiner combiner = new SA3TextureCombiner();

        combiner.LOG_LEVEL             = LOG_LEVEL;
        combiner.atlasPadding          = _atlasPadding;
        combiner.customShaderPropNames = _customShaderPropNames;
        combiner.fixOutOfBoundsUVs     = _fixOutOfBoundsUVs;
        combiner.maxTilingBakeSize     = _maxTilingBakeSize;
        combiner.packingAlgorithm      = _packingAlgorithm;
        combiner.combineTexturePackerForcePowerOfTwo = _combineTexturePackerForcePowerOfTwo;
        combiner.resizePowerOfTwoTextures            = _resizePowerOfTwoTextures;
        combiner.saveAtlasesAsAssets = saveAtlasesAsAssets;

        // 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, combiner.customShaderPropNames);
        }

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

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

        //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, editorMethods))
            {
                return(null);
            }
        }

        //Save the results
        textureBakeResults.combinedMaterialInfo = resultAtlasesAndRects;
        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
        SA3MeshCombineCommon[] mb = GetComponentsInChildren <SA3MeshCombineCommon>();
        for (int i = 0; i < mb.Length; i++)
        {
            mb[i].textureBakeResults = textureBakeResults;
        }

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