コード例 #1
0
ファイル: TextureBaker.cs プロジェクト: lwx1010/2D_XProject
        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();
        }
コード例 #2
0
ファイル: TextureBaker.cs プロジェクト: lwx1010/2D_XProject
        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);
            }
        }