Texture2DArray 功能测试

在使用GPU Instancing时,如果要给每个物体设置不同的贴图,直觉的想法是使用 MaterialPropertyBlock对象设置一个Texture对象数组,类似 GPU Instancing测试 这篇里写给每个物体设置不同颜色的方式(MaterialPropertyBlock.SetVectorArray),如果有一个MaterialPropertyBlock.SetTextureArray之类的方法就完美了,但是实际上并没有,所以需要用其他方式来实现,这时候应该让 Texture2DArray 来发挥作用。

效果:每次点击按钮传递一个不同的Index到Shader,用来从Texture2DArray中读取不同的图片。
使用了四张图

每次点击传递不同的Index到Shader

Texture2DArray 类

看名字可以大概猜到这个类的对象可以包含多个Texture2D对象,类似Texture2D对象的数组。用Texture2DArray对象来实现给每个物体一个不同的贴图的思路是:

  1. 在C#中定义一个Texture2D数组
  2. 再定义一个Texture2DArray对象,用Texture2D数组内容来初始化Texture2DArray对象
  3. 把Texture2DArray对象作为Texture传递给Shader
  4. 通过一个索引值(_Index)来控制Shader中读取Texture2DArray对象里的哪一张图

思路清晰以后就直接上代码:

using UnityEngine;
using UnityEngine.Rendering;

public class Tex2DArrayTest : MonoBehaviour
{
    public MeshRenderer render;
    public Texture2D[] textures;
    public ECopyTexMethpd copyTexMethod;                // 把Texrure2D信息拷贝到Texture2DArray对象中使用的方式 //

    public enum ECopyTexMethpd
    {
        CopyTexture = 0,                                 // 使用 Graphics.CopyTexture 方法 //
        SetPexels = 1,                                      // 使用 Texture2DArray.SetPixels 方法 //
    }

    private Material m_mat;

    void Start()
    {
        if (textures == null || textures.Length == 0)
        {
            enabled = false;
            return;
        }

        if (SystemInfo.copyTextureSupport == CopyTextureSupport.None ||
            !SystemInfo.supports2DArrayTextures)
        {
            enabled = false;
            return;
        }

        Texture2DArray texArr = new Texture2DArray(textures[0].width, textures[0].width, textures.Length, textures[0].format, false, false);

        // 结论 //
        // Graphics.CopyTexture耗时(单位:Tick): 5914, 8092, 6807, 5706, 5993, 5865, 6104, 5780 //
        // Texture2DArray.SetPixels耗时(单位:Tick): 253608, 255041, 225135, 256947, 260036, 295523, 250641, 266044 //
        // Graphics.CopyTexture 明显快于 Texture2DArray.SetPixels 方法 //
        // Texture2DArray.SetPixels 方法的耗时大约是 Graphics.CopyTexture 的50倍左右 //
        // Texture2DArray.SetPixels 耗时的原因是需要把像素数据从cpu传到gpu, 原文: Call Apply to actually upload the changed pixels to the graphics card //
        // 而Graphics.CopyTexture只在gpu端进行操作, 原文: operates on GPU-side data exclusively //

        //using (Timer timer = new Timer(Timer.ETimerLogType.Tick))
        //{
            if (copyTexMethod == ECopyTexMethpd.CopyTexture)
            {
                for (int i = 0; i < textures.Length; i++)
                {
                    // 以下两行都可以 //
                    //Graphics.CopyTexture(textures[i], 0, texArr, i);
                    Graphics.CopyTexture(textures[i], 0, 0, texArr, i, 0);
                }
            }
            else if (copyTexMethod == ECopyTexMethpd.SetPexels)
            {
                for (int i = 0; i < textures.Length; i++)
                {
                    // 以下两行都可以 //
                    //texArr.SetPixels(textures[i].GetPixels(), i);
                    texArr.SetPixels(textures[i].GetPixels(), i, 0);
                }

                texArr.Apply();
            }
        //}

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

        m_mat = render.material;

        m_mat.SetTexture("_TexArr", texArr);
        m_mat.SetFloat("_Index", Random.Range(0, textures.Length));

        //AssetDatabase.CreateAsset(texArr, "Assets/RogueX/Prefab/texArray.asset");
    }

    void OnGUI()
    {
        if (GUI.Button(new Rect(0, 0, 200, 100), "Change Texture"))
        {
            m_mat.SetFloat("_Index", Random.Range(0, textures.Length));
        }
    }
}

Shader部分:

Shader "MJ/Texture2DArray"
{
	Properties
	{
		_TexArr ("Texture Array", 2DArray) = "" {}
		_Index("Texture Array Index", Range(0,4)) = 0
	}

	SubShader
	{
		Tags { "Queue"="Geometry" "RenderType"="Opaque" }
		LOD 100

		Pass
		{
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#include "UnityCG.cginc"

			// 会提示警告: Unrecognized #pragma directive: require at line 24
			// #pragma require 2darray

			UNITY_DECLARE_TEX2DARRAY(_TexArr);
			int _Index;

			struct appdata
			{
				float4 vertex : POSITION;
				float3 uv : TEXCOORD0;
			};

			struct v2f
			{
				float4 pos : SV_POSITION;
				float3 uv : TEXCOORD0;
			};

			v2f vert (appdata v)
			{
				v2f o;

				o.pos = UnityObjectToClipPos(v.vertex);
				o.uv = v.uv;
				return o;
			}

			fixed4 frag (v2f i) : SV_Target
			{
				return UNITY_SAMPLE_TEX2DARRAY(_TexArr, float3(i.uv.xy, _Index));
			}
			ENDCG
		}
	}

	Fallback Off
}

需要注意的地方有:

  1. Texture2DArray使用的图片需要保证大小相同,格式一致,并且开启了 read/write enabled 选项
  2. Graphics.CopyTexture和Texture2DArray.SetPixels,这两种方法都可以把图像信息传给Texture2DArray对象的每一个子Texture2D。整体上Graphics.CopyTexture 方法要比 Texture2DArray.SetPixels 快的多,大概是40到50倍的样子,原因大概是 Graphics.CopyTexture 只在GPU端进行操作,而Texture2DArray.SetPixels在CPU端操作,操作结束后需要调用Apply方法把图片数据传给GPU,所以比较耗时,Texture2DArray.Apply文档 中也建议如果不需要在CPU上读取像素信息的话建议使用更快的 Graphics.CopyTexture 方法。
参考链接:
https://www.reddit.com/r/Unity3D/comments/6uueox/gpu_instancing_texture2darray/
https://forum.unity.com/threads/instance-of-texture.500408/
https://docs.unity3d.com/Manual/SL-TextureArrays.html
https://docs.unity3d.com/ScriptReference/Texture2DArray.Apply.html
https://docs.unity3d.com/ScriptReference/Graphics.CopyTexture.html
https://blog.csdn.net/aa20274270/article/details/64923942
https://www.cnblogs.com/hont/p/7258615.html
  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值