unity源码解析Texture2D

游戏中图形的表现大多来自于纹理,纹理使用最多的是Texture2D,Texture2D继承自Texture,Texture里面大多是对纹理的一些参数的设置,那些设置对于2d3d纹理是通用的,本篇就来看看texture2D具体是如何工作的。

using System;
using System.Runtime.CompilerServices;
using UnityEngine.Internal;

namespace UnityEngine
{
    public sealed class Texture2D : Texture
    {

获取纹理的级数,这个级数首先是开启了mipmap,读取一张图片时,计算机会生成多张纹理,根据观察者与物体的远近选择不同的纹理。
        public extern int mipmapCount
        {
            [WrapperlessIcall]
            [MethodImpl(MethodImplOptions.InternalCall)]
            get;
        }
获取纹理格式,比如RGBA32什么的
        public extern TextureFormat format
        {
            [WrapperlessIcall]
            [MethodImpl(MethodImplOptions.InternalCall)]
            get;
        }
这是一个静态方法,获取一个纯白色的纹理,测试了一下,该纹理的尺寸为(0.3,0.3)
        public static extern Texture2D whiteTexture
        {
            [WrapperlessIcall]
            [MethodImpl(MethodImplOptions.InternalCall)]
            get;
        }
也是个静态方法,获取一个纯黑色纹理,注意的是透明度也是0
        public static extern Texture2D blackTexture
        {
            [WrapperlessIcall]
            [MethodImpl(MethodImplOptions.InternalCall)]
            get;
        }
创建一个自定义尺寸的纹理,当然其他都是默认值
        public Texture2D(int width, int height)
        {
            Texture2D.Internal_Create(this, width, height, TextureFormat.ARGB32, true, false, IntPtr.Zero);
        }
通过限制尺寸,像素格式,是否多级别纹理来创建一个纹理
        public Texture2D(int width, int height, TextureFormat format, bool mipmap)
        {
            Texture2D.Internal_Create(this, width, height, format, mipmap, false, IntPtr.Zero);
        }
通过限制尺寸,像素格式,是否多级别纹理,是否使用线性滤波方式来创建一个纹理
        public Texture2D(int width, int height, TextureFormat format, bool mipmap, bool linear)
        {
            Texture2D.Internal_Create(this, width, height, format, mipmap, linear, IntPtr.Zero);
        }
与上面的创建方式不一样的地方是,传入了一个本地的纹理,利用本地纹理创建一个新的纹理,而这个nativeTex可以通过Texture中GetNativeTextureID获得
        internal Texture2D(int width, int height, TextureFormat format, bool mipmap, bool linear, IntPtr nativeTex)
        {
            Texture2D.Internal_Create(this, width, height, format, mipmap, linear, nativeTex);
        }

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        private static extern void Internal_Create([Writable] Texture2D mono, int width, int height, TextureFormat format, bool mipmap, bool linear, IntPtr nativeTex);

        public static Texture2D CreateExternalTexture(int width, int height, TextureFormat format, bool mipmap, bool linear, IntPtr nativeTex)
        {
            return new Texture2D(width, height, format, mipmap, linear, nativeTex);
        }
利用纹理id来更新当前纹理
        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        public extern void UpdateExternalTexture(IntPtr nativeTex);
设置纹理中指定坐标的颜色值,注意之后必须调用apply才起作用
        public void SetPixel(int x, int y, Color color)
        {
            Texture2D.INTERNAL_CALL_SetPixel(this, x, y, ref color);
        }

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        private static extern void INTERNAL_CALL_SetPixel(Texture2D self, int x, int y, ref Color color);
获取像素值
        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        public extern Color GetPixel(int x, int y);
通过uv坐标值获取像素
        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        public extern Color GetPixelBilinear(float u, float v);

        [ExcludeFromDocs]
        public void SetPixels(Color[] colors)
        {
            int miplevel = 0;
            this.SetPixels(colors, miplevel);
        }

        public void SetPixels(Color[] colors, [DefaultValue("0")] int miplevel)
        {
            int num = this.width >> miplevel;
            if (num < 1)
            {
                num = 1;
            }
            int num2 = this.height >> miplevel;
            if (num2 < 1)
            {
                num2 = 1;
            }
            this.SetPixels(0, 0, num, num2, colors, miplevel);
        }

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        public extern void SetPixels(int x, int y, int blockWidth, int blockHeight, Color[] colors, [DefaultValue("0")] int miplevel);

        [ExcludeFromDocs]
        public void SetPixels(int x, int y, int blockWidth, int blockHeight, Color[] colors)
        {
            int miplevel = 0;
            this.SetPixels(x, y, blockWidth, blockHeight, colors, miplevel);
        }

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        public extern void SetPixels32(Color32[] colors, [DefaultValue("0")] int miplevel);

        [ExcludeFromDocs]
        public void SetPixels32(Color32[] colors)
        {
            int miplevel = 0;
            this.SetPixels32(colors, miplevel);
        }
通过读取文件数据来加载纹理数据
        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        public extern bool LoadImage(byte[] data);
通过读取文件数据来加载纹理数据
        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        public extern void LoadRawTextureData(byte[] data);

        [ExcludeFromDocs]
        public Color[] GetPixels()
        {
            int miplevel = 0;
            return this.GetPixels(miplevel);
        }

        public Color[] GetPixels([DefaultValue("0")] int miplevel)
        {
            int num = this.width >> miplevel;
            if (num < 1)
            {
                num = 1;
            }
            int num2 = this.height >> miplevel;
            if (num2 < 1)
            {
                num2 = 1;
            }
            return this.GetPixels(0, 0, num, num2, miplevel);
        }

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        public extern Color[] GetPixels(int x, int y, int blockWidth, int blockHeight, [DefaultValue("0")] int miplevel);

        [ExcludeFromDocs]
        public Color[] GetPixels(int x, int y, int blockWidth, int blockHeight)
        {
            int miplevel = 0;
            return this.GetPixels(x, y, blockWidth, blockHeight, miplevel);
        }

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        public extern Color32[] GetPixels32([DefaultValue("0")] int miplevel);

        [ExcludeFromDocs]
        public Color32[] GetPixels32()
        {
            int miplevel = 0;
            return this.GetPixels32(miplevel);
        }

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        public extern void Apply([DefaultValue("true")] bool updateMipmaps, [DefaultValue("false")] bool makeNoLongerReadable);

        [ExcludeFromDocs]
        public void Apply(bool updateMipmaps)
        {
            bool makeNoLongerReadable = false;
            this.Apply(updateMipmaps, makeNoLongerReadable);
        }

        [ExcludeFromDocs]
        public void Apply()
        {
            bool makeNoLongerReadable = false;
            bool updateMipmaps = true;
            this.Apply(updateMipmaps, makeNoLongerReadable);
        }
重新设定纹理参数
        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        public extern bool Resize(int width, int height, TextureFormat format, bool hasMipMap);

        public bool Resize(int width, int height)
        {
            return this.Internal_ResizeWH(width, height);
        }

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        private extern bool Internal_ResizeWH(int width, int height);
决定纹理的质量
        public void Compress(bool highQuality)
        {
            Texture2D.INTERNAL_CALL_Compress(this, highQuality);
        }

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        private static extern void INTERNAL_CALL_Compress(Texture2D self, bool highQuality);
传进来一个纹理集合,把多个纹理打包为当前纹理
        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        public extern Rect[] PackTextures(Texture2D[] textures, int padding, [DefaultValue("2048")] int maximumAtlasSize, [DefaultValue("false")] bool makeNoLongerReadable);
传进来一个纹理集合,把多个纹理打包为当前纹理
        [ExcludeFromDocs]
        public Rect[] PackTextures(Texture2D[] textures, int padding, int maximumAtlasSize)
        {
            bool makeNoLongerReadable = false;
            return this.PackTextures(textures, padding, maximumAtlasSize, makeNoLongerReadable);
        }
传进来一个纹理集合,把多个纹理打包为当前纹理
        [ExcludeFromDocs]
        public Rect[] PackTextures(Texture2D[] textures, int padding)
        {
            bool makeNoLongerReadable = false;
            int maximumAtlasSize = 2048;
            return this.PackTextures(textures, padding, maximumAtlasSize, makeNoLongerReadable);
        }

        public void ReadPixels(Rect source, int destX, int destY, [DefaultValue("true")] bool recalculateMipMaps)
        {
            Texture2D.INTERNAL_CALL_ReadPixels(this, ref source, destX, destY, recalculateMipMaps);
        }

        [ExcludeFromDocs]
        public void ReadPixels(Rect source, int destX, int destY)
        {
            bool recalculateMipMaps = true;
            Texture2D.INTERNAL_CALL_ReadPixels(this, ref source, destX, destY, recalculateMipMaps);
        }

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        private static extern void INTERNAL_CALL_ReadPixels(Texture2D self, ref Rect source, int destX, int destY, bool recalculateMipMaps);

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        public extern byte[] EncodeToPNG();

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        public extern byte[] EncodeToJPG(int quality);

        public byte[] EncodeToJPG()
        {
            return this.EncodeToJPG(75);
        }
    }
}

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梁工123

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值