unity的纹理与采样器,各种宏的⽤法:
UNITY_DECLARE_TEX2D,UNITY。。。
有时在Shader编写过程中,我们可能会⽤到⾮常多纹理,如果每个纹理都采⽤类似uniform sampler2D _Mask;的⽅式进⾏声明,编辑器在编译shader的时候就会报错:
Shader error in 'CloudShaow/MaskBlend': maximum ps_4_0 sampler register index (16) exceeded at line 69 (on d3d11)
北大青鸟好不好
报错显⽰sampler采样器的数量超过了限制数量。这时可以把成对耦合的纹理和采样器分离,许多图形API和GPU允许使⽤⽐纹理更少的采样器。例如Direct3D 11允许你在⼀个单独的shader中使⽤最多128个纹理,但采样器最多只有16个。
例如我们可以通过以下的⽅式,仅声明⼀个sampler,⽽定义三个Texture2D。
// 定义纹理
Texture2D _MainTex;
Texture2D _SecondTex;
Texture2D _ThirdTex;
// 定义采样器
SamplerState sampler_MainTex; // "sampler" + “_MainTex”
// ...
// 对纹理进⾏采样
half4 color = _MainTex.Sample(sampler_MainTex, uv);
color += _SecondTex.Sample(sampler_MainTex, uv);
color += _ThirdTex.Sample(sampler_MainTex, uv);
Unity提供了⼀些宏让这个过程更加⽅便(因为不同的平台可能做法不同),它们就是UNITY_DECLARE_TEX2D和UNITY_DECLARE_TEX2D_NOSAMPLER。
descriptions上⾯的代码如果使⽤宏定义来替换的话:
// ⽤宏替代纹理和采样器的定义
UNITY_DECLARE_TEX2D(_MainTex);
UNITY_DECLARE_TEX2D_NOSAMPLER(_SecondTex);
UNITY_DECLARE_TEX2D_NOSAMPLER(_ThirdTex);
// ...
// ⽤宏对纹理进⾏采样
half4 color = UNITY_SAMPLE_TEX2D(_MainTex, uv);
chill
color += UNITY_SAMPLE_TEX2D_SAMPLER(_SecondTex, _MainTex, uv);
拖延症基因找到了
color += UNITY_SAMPLE_TEX2D_SAMPLER(_ThirdTex, _MainTex, uv);
源码:
相关的宏定义在inc,⽀持纹理和采样器分离的:
// Macros to declare textures and samplers, possibly parately. For platforms
// that have parate samplers & textures (like DX11), and we'd want to conrve
// the samplers.
// - UNITY_DECLARE_TEX*_NOSAMPLER declares a texture, without a sampler.
// - UNITY_SAMPLE_TEX*_SAMPLER samples a texture, using sampler from another texture.
// That another texture must also be actually ud in the current shader, otherwi
/
boyfriend歌词
心理疾病的治疗方法/ the correct sampler will not be t.
// 定义纹理与对应的采样器鲜奶油打发
#define UNITY_DECLARE_TEX2D(tex) Texture2D tex; SamplerState sampler##tex
// 只定义纹理
persuade#define UNITY_DECLARE_TEX2D_NOSAMPLER(tex) Texture2D tex
// 普通的纹理采样
#define UNITY_SAMPLE_TEX2D(tex,coord) tex.Sample (sampler##tex,coord)
// 通过samplertex传⼊其他纹理的名称,可复⽤其他纹理的采样器进⾏采样
#define UNITY_SAMPLE_TEX2D_SAMPLER(tex,samplertex,coord) tex.Sample (sampler##samplertex,coord)
不⽀持纹理和采样器分离的:祛斑美白方法
// DX9 style HLSL syntax; same object for texture+sampler
孔雀东南飞搞笑剧本// 2D textures
#define UNITY_DECLARE_TEX2D(tex) sampler2D tex
#define UNITY_DECLARE_TEX2D_NOSAMPLER(tex) sampler2D tex
#define UNITY_SAMPLE_TEX2D(tex,coord) tex2D (tex,coord)
#define UNITY_SAMPLE_TEX2D_SAMPLER(tex,samplertex,coord) tex2D (tex,coord)