当前位置: 移动技术网 > IT编程>开发语言>c# > Unity Shader实现纹理遮罩效果

Unity Shader实现纹理遮罩效果

2020年05月13日  | 移动技术网IT编程  | 我要评论

纹理遮罩用处很多,简单来讲就是允许我们可以保护模型中的某些区域不受某些因素的影响。例如我们有时希望模型表面某些区域的反光强烈一些,而某些区域弱一些。为了得到更加细腻的结果,我们需要用一张遮罩纹理来控制该光照。还有一些情况就是某些模型需要多张纹理混合时,此时使用遮罩纹理可以控制如何混合这些纹理。

具体流程为:通过采样得到遮罩纹理的纹素值,然后使用其中某个或者几个通道的值来与某种表面属性进行相乘。当该通道的值为0时,此时该表面属性不受遮罩纹理的影响。

shader代码如下:

shader "custom/masktexture"
{
  properties
  {
    _color ("color", color) = (1,1,1,1)
    _maintex ("main tex", 2d) = "white" {}
 _bumpmap("normal map", 2d) = "white" {}
 _bumpscale("bump scale", float) = 1.0
 _specularmask("specular mask", 2d) = "white" {}
 _specularscale("specular scale", float) = 1.0
 _specular("specular", color) = (1,1,1,1)
 _gloss("gloss", range(8.0, 256)) = 20
 }
 
 subshader
 {
 pass
 {
  tags{"lightmode" = "forwardbase"}
 
  cgprogram
 
  #pragma vertex vert
  #pragma fragment frag
  #include "unitycg.cginc"
  #include "lighting.cginc"
 
  fixed4 _color;
  sampler2d _maintex;
  float4 _maintex_st;   //纹理变量名+_st为unity内置宏,存储了该纹理的缩放大小和偏移量,分别对应.xy和.zw属性
  sampler2d _bumpmap;
  float _bumpscale;    //控制凹凸程度的变量
  sampler2d _specularmask; //遮罩纹理
  float _specularscale;  //控制遮罩纹理的可见度
  fixed4 _specular;
  float _gloss;
 
  struct a2v {
  float4 vertex : position;
  float3 normal : normal;
  float4 tangent : tangent;
  float4 texcoord : texcoord0;
  };
 
  struct v2f {
  float4 pos : sv_position;
  float2 uv : texcoord0;
  float3 lightdir : texcoord1;
  float3 viewdir : texcoord2;
  };
 
  v2f vert(a2v v)
  {
  v2f o;
 
  o.pos = unityobjecttoclippos(v.vertex);
  //将_maintex纹理信息(缩放大小和偏移量以及坐标信息)存储到o.uv.xy中
  o.uv.xy = v.texcoord.xy * _maintex_st.xy + _maintex_st.zw;
 
  //unity内置宏,计算并得到从模型空间到切线空间的变换矩阵rotation
  tangent_space_rotation;
  //获取切线空间下的光照方向
  o.lightdir = mul(rotation, objspacelightdir(v.vertex)).xyz;
  //获取切线空间下的视角方向
  o.viewdir = mul(rotation, objspaceviewdir(v.vertex)).xyz;;
 
  return o;
  }
 
  fixed4 frag(v2f i) : sv_target
  {
  //将切线空间下的光照方向和视角方向单位化
  fixed3 tangentlightdir = normalize(i.lightdir);
  fixed3 tangentviewdir = normalize(i.viewdir);
 
  //获取切线空间下的法向量
  fixed3 tangentnormal = unpacknormal(tex2d(_bumpmap, i.uv));
  tangentnormal.xy *= _bumpscale;
  tangentnormal.z = sqrt(1.0 - saturate(dot(tangentnormal.xy, tangentnormal.xy)));
 
  //获取片元上的主纹理,并和变量_color相乘得到其混合结果
  fixed3 albedo = tex2d(_maintex, i.uv).rgb * _color.rgb;
  //获取环境光
  fixed3 ambient = unity_lightmodel_ambient.xyz * albedo;
  //漫反射计算
  fixed3 diffuse = _lightcolor0.rgb * albedo * max(0, dot(tangentnormal, tangentlightdir));
 
  //高光反射计算,其计算方式跟前文的计算一样,这里只能另外跟specularmask的遮罩纹理相乘得到其与遮罩纹理的混合结果
  fixed3 halfdir = normalize(tangentlightdir + tangentviewdir);
  fixed specularmask = tex2d(_specularmask, i.uv).r * _specularscale;
  fixed3 specular = _lightcolor0.rgb * _specular.rgb * pow(max(0, dot(tangentnormal, halfdir)), _gloss) * specularmask;
 
  return fixed4(ambient + diffuse + specular, 1.0);
  }
 
  endcg
 }
  }
  fallback "specular"
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网