Exemple #1
0
        internal static Texture2D _copyTexturesIntoAtlas(Texture2D[] texToPack, int padding, Rect[] rs, int w, int h, TextureCombineHandler combiner)
        {
            Texture2D ta = new Texture2D(w, h, TextureFormat.ARGB32, true);

            MeshBakerUtility.setSolidColor(ta, Color.clear);
            for (int i = 0; i < rs.Length; i++)
            {
                Rect      r      = rs[i];
                Texture2D t      = texToPack[i];
                Texture2D tmpTex = null;
                int       x      = Mathf.RoundToInt(r.x * w);
                int       y      = Mathf.RoundToInt(r.y * h);
                int       ww     = Mathf.RoundToInt(r.width * w);
                int       hh     = Mathf.RoundToInt(r.height * h);
                if (t.width != ww && t.height != hh)
                {
                    tmpTex = t = MeshBakerUtility.resampleTexture(t, ww, hh);
                }
                ta.SetPixels(x, y, ww, hh, t.GetPixels());
                if (tmpTex != null)
                {
                    MeshBakerUtility.Destroy(tmpTex);
                }
            }
            ta.Apply();
            return(ta);
        }
Exemple #2
0
 internal void _destroyAllTemporaryTextures()
 {
     Debug.Log("Destroying " + _temporaryTextures.Count + " temporary textures");
     for (int i = 0; i < _temporaryTextures.Count; i++)
     {
         MeshBakerUtility.Destroy(_temporaryTextures[i].texture);
     }
     _temporaryTextures.Clear();
 }
Exemple #3
0
        /// <summary>
        /// Creates the atlases.创建图集
        /// saveAtlasesAsAssets 创建图集并保存至项目资源目录,或者内存中
        /// editorMethods 贴图格式编辑器方法
        /// </summary>
        public AtlasesAndRects[] CreateAtlases(ProgressUpdateDelegate progressInfo,
                                               bool saveAtlasesAsAssets             = false,
                                               EditorMethodsInterface editorMethods = null)
        {
            AtlasesAndRects[] resultAtlasesAndRects = null;
            try
            {
                _coroutineResult = new CreateAtlasesCoroutineResult();

                TextureCombineHandler.RunCorutineWithoutPause(
                    CreateAtlasesCoroutine(progressInfo, _coroutineResult, saveAtlasesAsAssets, editorMethods, 1000f), 0);
                if (_coroutineResult.success && textureBakeResults != null)
                {
                    resultAtlasesAndRects = this.OnCombinedTexturesCoroutineAtlasesAndRects;
                }
            }
            catch (Exception e)
            {
                Debug.LogError(e);
            }
            finally
            {
                if (saveAtlasesAsAssets)
                { //Atlases were saved to project so we don't need these ones
                    if (resultAtlasesAndRects != null)
                    {
                        for (int j = 0; j < resultAtlasesAndRects.Length; j++)
                        {
                            AtlasesAndRects mAndA = resultAtlasesAndRects[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
                                        {
                                            MeshBakerUtility.Destroy(mAndA.atlases[i]);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return(resultAtlasesAndRects);
        }
 Vector3[] _getMeshNormals(Mesh m)
 {
     Vector3[] ns = m.normals;
     if (ns.Length == 0)
     {
         //MB2_Log.LogDebug("Mesh " + m + " has no normals. Generating");
         Debug.LogWarning("Mesh " + m + " didn't have normals. Generating normals.");
         Mesh tempMesh = (Mesh)GameObject.Instantiate(m);
         tempMesh.RecalculateNormals();
         ns = tempMesh.normals;
         MeshBakerUtility.Destroy(tempMesh);
     }
     return(ns);
 }
Exemple #5
0
        internal void _destroyTemporaryTextures(string propertyName)
        {
            int numDestroyed = 0;

            for (int i = _temporaryTextures.Count - 1; i >= 0; i--)
            {
                if (_temporaryTextures[i].property.Equals(propertyName))
                {
                    numDestroyed++;
                    MeshBakerUtility.Destroy(_temporaryTextures[i].texture);
                    _temporaryTextures.RemoveAt(i);
                }
            }
            Debug.Log("Destroying " + numDestroyed + " temporary textures " + propertyName + " num remaining " + _temporaryTextures.Count);
        }
Exemple #6
0
        /// <summary>
        /// Джх╬ Atlas
        /// </summary>
        /// <returns></returns>
        public Texture2D DoRenderAtlas(GameObject gameObject,
                                       int width,
                                       int height,
                                       int padding,
                                       Rect[] rss,
                                       List <MaterialPropTexturesSet> textureSetss,
                                       int indexOfTexSetToRenders,
                                       ShaderTextureProperty texPropertyname,
                                       TextureCombinerNonTextureProperties resultMaterialTextureBlender,
                                       bool isNormalMap,
                                       bool fixOutOfBoundsUVs,
                                       bool considerNonTextureProperties,
                                       TextureCombineHandler texCombiner)
        {
            textureSets           = textureSetss;
            indexOfTexSetToRender = indexOfTexSetToRenders;
            _texPropertyName      = texPropertyname;
            _padding                      = padding;
            _isNormalMap                  = isNormalMap;
            _fixOutOfBoundsUVs            = fixOutOfBoundsUVs;
            _resultMaterialTextureBlender = resultMaterialTextureBlender;
            rs = rss;
            Shader s;

            if (_isNormalMap)
            {
                s = Shader.Find("MeshBaker/NormalMapShader");
            }
            else
            {
                s = Shader.Find("MeshBaker/AlbedoShader");
            }
            if (s == null)
            {
                Debug.LogError("Could not find shader for RenderTexture. Try reimporting mesh baker");
                return(null);
            }
            mat = new Material(s);
            _destinationTexture            = new RenderTexture(width, height, 24, RenderTextureFormat.ARGB32);
            _destinationTexture.filterMode = FilterMode.Point;

            myCamera = gameObject.GetComponent <Camera>();
            myCamera.orthographic     = true;
            myCamera.orthographicSize = height >> 1;
            myCamera.aspect           = ((float)width) / height;
            myCamera.targetTexture    = _destinationTexture;
            myCamera.clearFlags       = CameraClearFlags.Color;

            Transform camTransform = myCamera.GetComponent <Transform>();

            camTransform.localPosition = new Vector3(width / 2.0f, height / 2f, 3);
            camTransform.localRotation = Quaternion.Euler(0, 180, 180);

            _doRenderAtlas = true;
            Debug.Log(string.Format("Begin Camera.Render destTex w={0} h={1} camPos={2} camSize={3} camAspect={4}",
                                    width, height, camTransform.localPosition, myCamera.orthographicSize, myCamera.aspect.ToString("f5")));
            //This triggers the OnRenderObject callback
            myCamera.Render();
            _doRenderAtlas = false;

            MeshBakerUtility.Destroy(mat);
            MeshBakerUtility.Destroy(_destinationTexture);

            Debug.Log("Finished Camera.Render ");

            Texture2D tempTex = targTex;

            targTex = null;
            return(tempTex);
        }
        public IEnumerator CreateAtlases(ProgressUpdateDelegate progressInfo,
                                         TexturePipelineData data,
                                         TextureCombineHandler combiner,
                                         AtlasPackingResult packedAtlasRects,
                                         Texture2D[] atlases,
                                         EditorMethodsInterface textureEditorMethods)
        {
            Debug.Assert(!data.IsOnlyOneTextureInAtlasReuseTextures());
            Rect[] uvRects = packedAtlasRects.rects;

            int atlasSizeX = packedAtlasRects.atlasX;
            int atlasSizeY = packedAtlasRects.atlasY;

            Debug.Log("Generated atlas will be " + atlasSizeX + "x" + atlasSizeY);

            //create a game object
            GameObject renderAtlasesGO = null;

            try
            {
                renderAtlasesGO = new GameObject("MBrenderAtlasesGO");
                AtlasPackerRenderTexture atlasRenderTexture = renderAtlasesGO.AddComponent <AtlasPackerRenderTexture>();
                renderAtlasesGO.AddComponent <Camera>();
                if (data._considerNonTextureProperties)
                {
                    Debug.LogWarning("Blend Non-Texture Properties has limited functionality when used with Mesh Baker Texture Packer Fast.");
                }

                for (int propIdx = 0; propIdx < data.numAtlases; propIdx++)
                {
                    Texture2D atlas = null;
                    if (!TextureCombinerPipeline._ShouldWeCreateAtlasForThisProperty(propIdx, data._considerNonTextureProperties, data.allTexturesAreNullAndSameColor))
                    {
                        atlas = null;
                        Debug.Log("Not creating atlas for " + data.texPropertyNames[propIdx].name + " because textures are null and default value parameters are the same.");
                    }
                    else
                    {
                        GC.Collect();

                        TextureCombinerPackerBase.CreateTemporaryTexturesForAtlas(data.distinctMaterialTextures, combiner, propIdx, data);

                        if (progressInfo != null)
                        {
                            progressInfo("Creating Atlas '" + data.texPropertyNames[propIdx].name + "'", .01f);
                        }
                        // ===========
                        // configure it
                        Debug.Log("About to render " + data.texPropertyNames[propIdx].name + " isNormal=" + data.texPropertyNames[propIdx].isNormalMap);
                        atlasRenderTexture.width                        = atlasSizeX;
                        atlasRenderTexture.height                       = atlasSizeY;
                        atlasRenderTexture.padding                      = data._atlasPadding;
                        atlasRenderTexture.rects                        = uvRects;
                        atlasRenderTexture.textureSets                  = data.distinctMaterialTextures;
                        atlasRenderTexture.indexOfTexSetToRender        = propIdx;
                        atlasRenderTexture.texPropertyName              = data.texPropertyNames[propIdx];
                        atlasRenderTexture.isNormalMap                  = data.texPropertyNames[propIdx].isNormalMap;
                        atlasRenderTexture.fixOutOfBoundsUVs            = data._fixOutOfBoundsUVs;
                        atlasRenderTexture.considerNonTextureProperties = data._considerNonTextureProperties;
                        atlasRenderTexture.resultMaterialTextureBlender = data.nonTexturePropertyBlender;
                        // call render on it
                        atlas = atlasRenderTexture.OnRenderAtlas(combiner);

                        // destroy it
                        // =============
                        Debug.Log("Saving atlas " + data.texPropertyNames[propIdx].name + " w=" + atlas.width + " h=" + atlas.height + " id=" + atlas.GetInstanceID());
                    }
                    atlases[propIdx] = atlas;
                    if (progressInfo != null)
                    {
                        progressInfo("Saving atlas: '" + data.texPropertyNames[propIdx].name + "'", .04f);
                    }
                    if (data._saveAtlasesAsAssets && textureEditorMethods != null)
                    {
                        textureEditorMethods.SaveAtlasToAssetDatabase(atlases[propIdx], data.texPropertyNames[propIdx], propIdx, data.resultMaterial);
                    }
                    else
                    {
                        data.resultMaterial.SetTexture(data.texPropertyNames[propIdx].name, atlases[propIdx]);
                    }
                    data.resultMaterial.SetTextureOffset(data.texPropertyNames[propIdx].name, Vector2.zero);
                    data.resultMaterial.SetTextureScale(data.texPropertyNames[propIdx].name, Vector2.one);
                    combiner._destroyTemporaryTextures(data.texPropertyNames[propIdx].name); // need to save atlases before doing this
                }
            }
            catch (Exception ex)
            {
                //Debug.LogError(ex);
                Debug.LogException(ex);
            }
            finally
            {
                if (renderAtlasesGO != null)
                {
                    MeshBakerUtility.Destroy(renderAtlasesGO);
                }
            }
            yield break;
        }