UnityShader源码2017---学习笔记与自我拓展006

源自CubeBlend

首先映入眼帘的是


稍微解释一下[NoScaleOffset]吧,就是在material的面板上没有tilling和offset控制部分的GUI显示。

然后我看到了

half4 _TexA_HDR;
half4 _TexB_HDR;

这个应该是unity声明的吧,比如想_MainTex_ST这样的规则

我搜了一大群,找到了一点点信息。

说如此声明的变量,它是一个float4类型的,所包含的信息是如何在所使用的颜色空间下解码一张疑似HDR的图片。如果你想看DecodeHDR这个方法呢,请移步UnityCG.cginc里查阅。

反正是第一遍通读unity的shader源码,那就顺藤摸瓜式的学习吧。

// Decodes HDR textures
// handles dLDR, RGBM formats
inline half3 DecodeHDR ( half4 data, half4 decodeInstructions)
{
// Take into account texture alpha if decodeInstructions.w is true(the alpha value affects the RGB channels)
half alpha = decodeInstructions.w * (data.a - 1.0 ) + 1.0 ;

// If Linear mode is not supported we can skip exponent part
#if defined (UNITY_COLORSPACE_GAMMA)
return (decodeInstructions.x * alpha) * data.rgb;
#else
# if defined (UNITY_USE_NATIVE_HDR)
return decodeInstructions.x * data.rgb; // Multiplier for future HDRI relative to absolute conversion.
# else
return (decodeInstructions.x * pow (alpha, decodeInstructions.y)) * data.rgb;
# endif
#endif
}

目前阶段先看一下,后面单独研究一下cginc系列的库文件。


然后我们继续刚才源码的进度,有看到了

UNITY_DECLARE_TEXCUBE (_TexA);
UNITY_DECLARE_TEXCUBE (_TexB);


我们去HLSLSupport.cginc里查阅

#define UNITY_DECLARE_TEXCUBE (tex) TextureCube tex; SamplerState sampler ##tex


他的意思很明确,一个宏定义。那么我们把shader里替换成下面这样也是没有错误的

TextureCube _TexA;
SamplerState sampler_TexA;
TextureCube _TexB;
SamplerState sampler_TexB;

看着SamplerState好眼熟,似乎他认识我。。。Max里的材质。。

那么这么写:

TextureCube _TexB;
SamplerState sampler_TexB
{
    Filter = MIN_MAG_MIP_LINEAR;
    AddressU = Wrap;
AddressV = Wrap;
};

窝了。。。unity也不报错。。

然后我突发奇想。开始了一个测试。


是不是这样,我就可以在shader里改掉Texture的Wrap Model呢。。。

天真的我付出了天真的代价。完全没有效果。好神奇的unity,也没有报错。响当当的容错能力。


是不是被Override了?我无从得知了。


继续。。。

half3 texA = DecodeHDR ( UNITY_SAMPLE_TEXCUBE_LOD (_TexA, i.texcoord, _Level), _TexA_HDR);
half3 texB = DecodeHDR ( UNITY_SAMPLE_TEXCUBE_LOD (_TexB, i.texcoord, _Level), _TexB_HDR);

这个UNITY_SAMPLE_TEXCUBE_LOD,也是个宏

#define UNITY_SAMPLE_TEXCUBE_LOD (tex,coord,lod) tex. SampleLevel ( sampler ##tex,coord, lod)

等价于

     half3 texA = DecodeHDR (_TexA. SampleLevel (sampler_TexA,i.texcoord,_Level),_TexA_HDR);

最后就剩下这个了

half3 res = lerp (texA, texB, _value);

反正我不会解释lerp的,谁解释谁是小狗。。。。

lerp(a,b,x) = a*(1-x) + b*x;


汪汪汪。。。















猜你喜欢

转载自blog.csdn.net/u012871784/article/details/80496791