当前位置:网站首页>Unity3D study notes 10 - texture array
Unity3D study notes 10 - texture array
2022-08-01 01:10:00 【charlee44】
1. 概述
个人认为,纹理数组是一个非常有用的图形特性.纹理本质上是一个二维的图形数据;通过纹理数组,给图形数据再加上了一个维度.这无疑会带来一个巨大的性能提升:一次性传输大量的数据总是比分批次传输数据要快.
2. 详论
2.1. 实现
创建一个GameObject对象,并且加入Mesh Filter组件和Mesh Renderer组件.Mesh Filter我们可以设置Mesh为Quad,同时在Mesh Filter上挂一个我们新建的材质:
在这个GameObject对象上挂接一个我们创建的C#脚本:
using Unity.Collections;
using UnityEngine;
[ExecuteInEditMode]
public class Note10Main : MonoBehaviour
{
public Texture2D texture1;
public Texture2D texture2;
[Range(0.0f, 1.0f)]
public float weight;
Material material;
// Start is called before the first frame update
void Start()
{
MeshRenderer mr = GetComponent<MeshRenderer>();
material = mr.sharedMaterial;
Texture2DArray texture2DArray = CreateTexture2DArray();
material.mainTexture = texture2DArray;
material.SetFloat("_Weight", weight);
}
Texture2DArray CreateTexture2DArray()
{
Texture2DArray texture2DArray = new Texture2DArray(texture1.width, texture1.height, 2,
texture1.format, false);
NativeArray<byte> pixelData1 = texture1.GetPixelData<byte>(0);
NativeArray<byte> pixelData2 = texture2.GetPixelData<byte>(0);
texture2DArray.SetPixelData(pixelData1, 0, 0, 0);
texture2DArray.SetPixelData(pixelData2, 0, 1, 0);
texture2DArray.Apply(false, false);
return texture2DArray;
}
// Update is called once per frame
void Update()
{
material.SetFloat("_Weight", weight);
}
}
这段C#脚本的意思是,通过传入两个Texture2d,生成一个texture2DArray;并且,将这个texture2DArray传入到材质中.需要注意的是纹理数组中的每个纹理的参数如宽、高等参数都需要一致,否则不能组成纹理数组.
材质使用我们自定义的Shader:
Shader "Custom/TextureArrayShader"
{
Properties
{
_MainTex ("Texture", 2DArray) = "" {
}
_Weight ("Weight", float) = 0.0
}
SubShader
{
Tags {
"RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
UNITY_DECLARE_TEX2DARRAY(_MainTex);
float _Weight;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 col0 = UNITY_SAMPLE_TEX2DARRAY(_MainTex, float3(i.uv, 0));
fixed4 col1 = UNITY_SAMPLE_TEX2DARRAY(_MainTex, float3(i.uv, 1));
return lerp(col0, col1, _Weight);
}
ENDCG
}
}
}
这里实现的效果是,将纹理数组中的两个纹理根据权重进行混合.权重值也是在C#脚本中传入到Shader中的.在编辑器中将权重调整到中间一点的位置(例如0.5):
Shader代码也很好理解,关键在于纹理数组相关的宏,其实是对hlsl或者glsl的封装:
#define UNITY_DECLARE_TEX2DARRAY(tex) Texture2DArray tex; SamplerState sampler##tex
#define UNITY_SAMPLE_TEX2DARRAY(tex,coord) tex.Sample (sampler##tex,coord)
#define UNITY_DECLARE_TEX2DARRAY(tex) sampler2DArray tex
#define UNITY_SAMPLE_TEX2DARRAY(tex,coord) tex2DArray (tex,coord)
2.2. 注意
- 关于纹理数组的创建,也可以使用Graphics.CopyTexture()这个接口.这个接口是纯走GPU端的,效率应该回更高.
- 纹理数组这个特性在低端显卡上可能不支持,但是不一定就会非常耗费性能.可以考虑通过纹理数组的方式来合并渲染的批次.
- 纹理数组个数的限制并不是纹理单元个数.实际上一个纹理数组只会绑定到一个纹理单元上,而在本人GTX 1660 Ti的显卡上,纹理数组个数的限制是4096个.
3. 参考
边栏推荐
- ROS2 series of knowledge (4): understand the concept of [service]
- js 实现复制功能
- leetcode:1648. 销售价值减少的颜色球【二分找边界】
- 【密码学/密码分析】基于TMTO的密码分析方法
- Google "Cloud Developer Quick Checklist"; Tsinghua 3D Human Body Dataset; SenseTime "Universal Vision Framework" open class; Web3 Minimalist Getting Started Guide; Free Books for Efficient Deep Learni
- Compose原理-视图和数据双向绑定的原理
- Key Points Estimation and Point Instance
- Kyoto University:Masaki Waga | 黑箱环境中强化学习的动态屏蔽
- Automated machine learning pycaret: PyCaret Basic Auto Classification LightGBM
- Kyoto University: Masaki Waga | Dynamic Masking for Reinforcement Learning in Black Box Environments
猜你喜欢
一体化步进电机在无人机自动机场的应用
cmake入门学习笔记
声称AI存在意识,谷歌工程师遭解雇:违反保密协议
Google "Cloud Developer Quick Checklist"; Tsinghua 3D Human Body Dataset; SenseTime "Universal Vision Framework" open class; Web3 Minimalist Getting Started Guide; Free Books for Efficient Deep Learni
Academicians of the two academies speak bluntly: Don't be superstitious about academicians
MYSQL索引解析
MYSQL主从复制
Likou Binary Tree
LeetCode每日一练 —— 环形链表问题(面试四连问)
Carefully organize 16 MySQL usage specifications to reduce problems by 80% and recommend sharing with the team
随机推荐
WAASAP WebClient UI页面标签的决定逻辑介绍
VPGNet
二叉树遍历非递归程序 -- 使用栈模拟系统栈
Rasa 3.x 学习系列- Rasa - Issues 4898 学习笔记
500 miles
mySql data view
Carefully summarize thirteen suggestions to help you create more suitable MySQL indexes
WebApi hits an Attribute to handle exceptions uniformly
RTL8762DK UART (two)
Matlab/ArcGIS processing GPM global monthly precipitation data
Item 36: Specify std::launch::async if asynchronicity is essential.
MVCC总结
【Cryptography/Cryptanalysis】Cryptanalysis method based on TMTO
Introduction to machine learning how to?
MYSQL关键字Explain解析
Google engineer fired for claiming AI awareness: breach of nondisclosure agreement
/usr/sbin/vmware-authdlauncher: error while loading shared libraries: libssl.so.1.0.2*解决办法
从零造键盘的键盘超级喜欢,IT人最爱
Key Points Estimation and Point Instance
Key Points Estimation and Point Instance