CopyTexture() public static method

Copy texture contents.

public static CopyTexture ( Texture src, Texture dst ) : void
src Texture Source texture.
dst Texture Destination texture.
return void
示例#1
0
    private void HandleTextureArry(List <Texture2D> texs)
    {
        if (texs == null || texs.Count == 0)
        {
            enabled = false;
            return;
        }

        if (SystemInfo.copyTextureSupport == CopyTextureSupport.None ||
            !SystemInfo.supports2DArrayTextures)
        {
            enabled = false;
            return;
        }
        TexArr = new Texture2DArray(texs[0].width, texs[0].width, texs.Count, TextureFormat.RGBA32, false, false);

        for (int i = 0; i < texs.Count; i++)
        {
            //Debug.Log(" index is" + i);
            try
            {
                Graphics.CopyTexture(texs[i], 0, 0, TexArr, i, 0);
            }
            catch (Exception e)
            {
                Debug.Log("index is" + i);
                throw e;
            }
        }

        TexArr.wrapMode   = TextureWrapMode.Clamp;
        TexArr.filterMode = FilterMode.Bilinear;

        Debug.Log("HandleTextureArry End ===============>>>>>>>>>>>   TexArr Length is " + TexArr.depth);
    }
        private static void SaveSplitted(string suffix, Texture2D tex, int xSrc, int ySrc, int width, int height,
                                         int xOffset, int yOffset, int x, int y)
        {
            Texture2D target    = new Texture2D(width, height, TextureFormat.RGBA32, false);
            var       widthSrc  = width - xOffset;
            var       heightSrc = height - yOffset;

            UnityGraphics.CopyTexture(tex, 0, 0, xSrc, ySrc, widthSrc, heightSrc, target, 0, 0, x, y);
            for (int k = 0; k < height && xOffset > 0; ++k)
            {
                var c = target.GetPixel(widthSrc - 1, k);
                for (int kx = widthSrc; kx < width; ++kx)
                {
                    target.SetPixel(kx, k, c);
                }
            }
            for (int k = 0; k < width && yOffset > 0; ++k)
            {
                var c = target.GetPixel(k, heightSrc - 1);
                for (int ky = heightSrc; ky < height; ++ky)
                {
                    target.SetPixel(k, ky, c);
                }
            }
            target.Apply();
            string projectPath = Application.dataPath.Replace("/Assets", "/");
            string path        = AssetDatabase.GetAssetPath(tex);

            path = $"{path.Substring(0, path.LastIndexOf('.'))}_{suffix}.png";
            File.WriteAllBytes(projectPath + path, target.EncodeToPNG());
            AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
        }
示例#3
0
    private void HandleTextureArry(List <Texture2D> texs)
    {
        if (texs == null || texs.Count == 0)
        {
            enabled = false;
            return;
        }

        if (SystemInfo.copyTextureSupport == CopyTextureSupport.None ||
            !SystemInfo.supports2DArrayTextures)
        {
            enabled = false;
            return;
        }
        TexArr = new Texture2DArray(texs[0].width, texs[0].width, texs.Count, TextureFormat.DXT5, false, false);

        for (int i = 0; i < texs.Count; i++)
        {
            Graphics.CopyTexture(texs[i], 0, 0, TexArr, i, 0);
        }

        TexArr.wrapMode   = TextureWrapMode.Clamp;
        TexArr.filterMode = FilterMode.Bilinear;
    }
示例#4
0
        public void CopyActiveRenderTextureToTexture(string textureName, int textureIndex, RectInt sourceRect, Vector2Int dest, bool allowDelayedCPUSync)
        {
            if (String.IsNullOrEmpty(textureName))
            {
                throw new ArgumentNullException("textureName");
            }

            var source = RenderTexture.active;

            if (source == null)
            {
                throw new InvalidOperationException("Active RenderTexture is null.");
            }

            int textureWidth  = 0;
            int textureHeight = 0;

            if (textureName == SurfaceMaskTextureName)
            {
                if (textureIndex != 0)
                {
                    throw new ArgumentOutOfRangeException("textureIndex");
                }
                else if (source == surfaceMaskTexture)
                {
                    throw new ArgumentException("source", "Active RenderTexture cannot be surfaceMaskTexture.");
                }
                textureWidth = textureHeight = surfaceMaskResolution;
            }
            else if (textureName == AlphamapTextureName)
            {
                if (textureIndex < 0 || textureIndex >= alphamapTextureCount)
                {
                    throw new ArgumentOutOfRangeException("textureIndex");
                }
                textureWidth = textureHeight = alphamapResolution;
            }
            else
            {
                // TODO: Support generic terrain textures.
                throw new ArgumentException($"Unrecognized terrain texture name: \"{textureName}\"");
            }

            if (sourceRect.x < 0 || sourceRect.y < 0 || sourceRect.xMax > source.width || sourceRect.yMax > source.height)
            {
                throw new ArgumentOutOfRangeException("sourceRect");
            }
            else if (dest.x < 0 || dest.x + sourceRect.width > textureWidth)
            {
                throw new ArgumentOutOfRangeException("dest.x");
            }
            else if (dest.y < 0 || dest.y + sourceRect.height > textureHeight)
            {
                throw new ArgumentOutOfRangeException("dest.y");
            }

            if (textureName == SurfaceMaskTextureName)
            {
                Internal_CopyActiveRenderTextureToSurfaceMask(sourceRect, dest.x, dest.y, allowDelayedCPUSync);
                return;
            }

            var dstTexture = GetAlphamapTexture(textureIndex);

            // Delay synching back (using ReadPixels) if CopyTexture can be used.
            // TODO: Checking the format compatibility is difficult as it varies by platforms. For instance copying between ARGB32 RT and RGBA32 Tex seems to be fine on all tested platforms...
            allowDelayedCPUSync = allowDelayedCPUSync && SupportsCopyTextureBetweenRTAndTexture;
            if (allowDelayedCPUSync)
            {
                if (dstTexture.mipmapCount > 1)
                {
                    // Composes mip0 in a RT with full mipchain.
                    var tmp = RenderTexture.GetTemporary(new RenderTextureDescriptor(dstTexture.width, dstTexture.height, source.format)
                    {
                        sRGB             = false,
                        useMipMap        = true,
                        autoGenerateMips = false
                    });
                    if (!tmp.IsCreated())
                    {
                        tmp.Create();
                    }

                    Graphics.CopyTexture(dstTexture, 0, 0, tmp, 0, 0);
                    Graphics.CopyTexture(source, 0, 0, sourceRect.x, sourceRect.y, sourceRect.width, sourceRect.height, tmp, 0, 0, dest.x, dest.y);

                    // Generate the mips on the GPU
                    tmp.GenerateMips();

                    // Copy the full mipchain back to the alphamap texture
                    Graphics.CopyTexture(tmp, dstTexture);

                    RenderTexture.ReleaseTemporary(tmp);
                }
                else
                {
                    Graphics.CopyTexture(source, 0, 0, sourceRect.x, sourceRect.y, sourceRect.width, sourceRect.height, dstTexture, 0, 0, dest.x, dest.y);
                }

                // TODO: Support generic terrain textures.
                Internal_MarkAlphamapDirtyRegion(textureIndex, dest.x, dest.y, sourceRect.width, sourceRect.height);
            }
            else
            {
                if (SystemInfo.graphicsDeviceType == GraphicsDeviceType.Metal || !SystemInfo.graphicsUVStartsAtTop)
                {
                    dstTexture.ReadPixels(new Rect(sourceRect.x, sourceRect.y, sourceRect.width, sourceRect.height), dest.x, dest.y);
                }
                else
                {
                    dstTexture.ReadPixels(new Rect(sourceRect.x, source.height - sourceRect.yMax, sourceRect.width, sourceRect.height), dest.x, dest.y);
                }
                dstTexture.Apply(true);

                // TODO: Check if the texture is previously marked dirty?
                // TODO: Support generic terrain textures.
                Internal_ClearAlphamapDirtyRegion(textureIndex);
            }

            Experimental.TerrainAPI.TerrainCallbacks.InvokeTextureChangedCallback(this, textureName, new RectInt(dest.x, dest.y, sourceRect.width, sourceRect.height), !allowDelayedCPUSync);
        }