Unity Shader 之 不锈钢(各向异性材质)
Unity Shader 之 不锈钢(各向异性材质)
常见的实现方案
- 思路:
先求出半角向量halfVector
再求法线方向与光照方向的点积NdotL
半角向量与各向异性方向点积HdotA
求出各向异性:
sin((半角向量与各向异性方向点积+偏移值)的弧度值 * 180)
再根据各向异性求出反光强度spec
最后与光照颜色等进行整合 主要代码:
inline fixed4 LightingAnisotropic (SurfaceAnisoOutput s, fixed3 lightDir, half3 viewDir, fixed atten) { fixed3 halfVector = normalize(normalize(lightDir) + normalize(viewDir));//normalize()函数把向量转化成单位向量 float NdotL = saturate(dot(s.Normal, lightDir)); fixed HdotA = dot(normalize(s.Normal + s.AnisoDirection), halfVector); float aniso = max(0, sin(radians((HdotA + _AnisoOffset) * 180f)));//radians()函数将角度值转换为弧度值 float spec = saturate(pow(aniso, s.Gloss * 128) * s.Specular);//saturate(x)函数 如果x小于0返回 0;如果x大于1返回1;否则返回x;把x限制在0-1 fixed4 c; c.rgb = ((s.Albedo * _LightColor0.rgb * NdotL) + (_LightColor0.rgb * _SpecularColor.rgb * spec)) * (atten * 2); c.a = 1.0; return c; }
效果图:
参考链接:http://blog.csdn.net/wolf96/article/details/41843973
本文的实现方案
- 公式:公式采用的是Ward 92年在 Measuring and modeling anisotropic reflection中提出的。
各项异性材质是BRDF的一部分, 有关BRDF的论文很多, 这里推荐一篇Physically-Based Shading at Disney by Brent Burley, Walt Disney Animation Studios [Revised Aug 31, 2012. Corrected normalization factor in Equation 4.], 在这篇论文中还讲到了关于各项异性的几篇文章包括Walter在2005年在 Notes on the Ward BRDF中提出了对Ward各项异性材质公式的一些优化算法, 以及Pacanowski在2012年在Oliver Salazar Celis中提出的关于各项异性材质的新的公式。 有兴趣的童鞋可以试试。 本文中用到的贴图
Shader中的主要代码
float _Diffuse, _Specular;
float _Gloss;
float _AnisoX, _AnisoY, _AnisoAngle, _AnisoIntensity;
sampler2D _AnisoTexture; float4 _AnisoTexture_ST;struct v2f
{float4 pos : SV_POSITION;float3 normal : NORMAL;float4 tangent : TANGENT;float4 worldPos : TEXCOORD0;float2 texcoord : TEXCOORD1;LIGHTING_COORDS(3, 4)
};void vert(in appdata_tan v, out v2f output)
{output.pos = UnityObjectToClipPos(v.vertex);output.normal = v.normal;output.tangent = v.tangent;output.worldPos = mul(unity_ObjectToWorld, v.vertex);output.worldPos /= output.worldPos.w;output.texcoord = v.texcoord;TRANSFER_VERTEX_TO_FRAGMENT(output);
}void frag(in v2f input, out float4 c : COLOR)
{c.rgb = 0;float3 lightColor = _LightColor0.rgb;float3 vertPos = input.worldPos.xyz;float3 lightPos = _WorldSpaceLightPos0.xyz;float3 normalDir = normalize(UnityObjectToWorldNormal(input.normal));float3 tangentDir = normalize(UnityObjectToWorldNormal(input.tangent));float3 bitangentDir = normalize(cross(tangentDir, normalDir));float3 anisoDir = normalize(UnpackNormal(tex2D(_AnisoTexture, input.texcoord)));float3 lightDir = lightPos - vertPos;if (_WorldSpaceLightPos0.w<0.001) lightDir = lightPos;float3 b = bitangentDir;float3 t = tangentDir;float3 n = normalDir;//if (anisoDir.z<0.99){float Pi = 3.141592654;float3 anisoParam = tex2D(_AnisoTexture, TRANSFORM_TEX(input.texcoord, _AnisoTexture));_AnisoX = anisoParam.r;_AnisoY = anisoParam.g;_AnisoAngle = anisoParam.b * 180;//t = normalize(anisoDir.x * tangentDir + anisoDir.y * bitangentDir);t = t * cos(_AnisoAngle*Pi/180) + b * sin(_AnisoAngle*Pi/180);n = normalDir;b = normalize(cross(t,n));}float3 l = normalize(lightDir);float3 v = normalize(_WorldSpaceCameraPos.xyz - vertPos); float3 h = normalize(l+v); float3 dotLN = saturate(dot(l,n));float3 dotHN = saturate(dot(h,n));float3 dotHT = (dot(h,t));float3 dotHB = (dot(h,b));float3 dotVN = max(0.01,dot(v,n));// attenuatefloat attenuation = LIGHT_ATTENUATION(input);// diffusefloat3 diffuse = dotLN * lightColor * _Diffuse;// specularfloat3 specular = pow(dotHN, _Gloss*256) * _Specular * lightColor;specular = _Specular * lightColor * sqrt(dotLN/dotVN) * exp(-2 * (dotHT*dotHT/_AnisoX/_AnisoX + dotHB*dotHB/_AnisoY/_AnisoY)/(1+dotHN));// reflect float3 rv = reflect(-v, n);rv -= t * dot(rv,t);float3 refl = 0;for (int i=0; i<0; i++) {float3 drv = rv + t*i*0.02;refl += UNITY_SAMPLE_TEXCUBE_LOD(unity_SpecCube0, drv, 0);drv = rv -t*i*0.02;refl += UNITY_SAMPLE_TEXCUBE_LOD(unity_SpecCube0, drv, 0);}refl /= 10;//refl = UNITY_SAMPLE_TEXCUBE_LOD(unity_SpecCube0, reflect(-v, n), 0);//refl = UNITY_SAMPLE_TEXCUBE_LOD(unity_SpecCube0, rv, 0);c.rgb = attenuation * (diffuse + specular);#ifdef UNITY_PASS_FORWARDBASEc.rgb += refl*0.2;#endif//c.rgb = attenuation;//c.rgb = sqrt(dotLN/dotVN)/10;//c.rgb = 0;//c.rgb = specular;//c.rgb = 0;c.a = 0;
}
- 效果图:
有需要完整工程的童鞋可以留言给我。
Unity Shader 之 不锈钢(各向异性材质)相关推荐
- Unity Shader 案例之 镜面材质制作
打开镜像世界 我们在Unity里打开镜像的世界呢,原理很简单,我们再加一个Mirror Camera就可以了,其位置是我们当前Camera相对于镜面的一个镜像的映射(Transform). 镜像矩阵 ...
- 学习Shader Unity Shader 基础
1.如何充分利用 Unity Shader 来为我们的游戏增光添彩? 材质和 Unity Shader: 在Unity中,我们需要配合使用材质(Material)和 Unity Shader 才能达到 ...
- Unity Shader入门精要第3 章 Unity Shader 基础
Unity系列文章目录 文章目录 Unity系列文章目录 前言 一.Unity Shader 概述 二.使用步骤 1.3.1.2 Unity 中的材质 2.Unity 中的Shader 3.Unity ...
- Unity Shader Graph 使用安装步骤缺失材质球,以及场景原本物体材质球丢失问题解决
Unity Shader Graph 使用安装步骤,以及原本物体材质球丢失问题 我是用的版本是2019.3.9版本的,仅供参考. 第1步:Window-PackageManager-点击All Pac ...
- 【Unity Shader】Unity Chan的卡通材质
写在前面 时隔两个月我终于来更新博客了,之前一直在学东西,做一些项目,感觉没什么可以分享的就一直没写.本来之前打算写云彩渲染或是Compute Shader的,觉得时间比较长所以打算先写个简单的. 今 ...
- Unity URP管线的PBR材质及Tessallation Shader(Height Map高度贴图)
在使用URP管线的过程中发现默认的URP管线的shader是没有提供height map参数设置的,经过查找才知道URP管线中height map相关的功能需要自己写shader开启Tessallat ...
- Unity Shader - Shader常用属性标签(材质属性面板标签)
记录分享一下一些Shader常用的属性标签(材质属性面板标签) 如图: 代码: Shader "lcl/ShaderPropertyDrawer/ShaderEnum" {// 材 ...
- unity Shader 入门精要 EX
unity Shader 入门精要: 1.shader概念 2.shader分类(顶点Shader.像素Shader) 3.Shader编程语言 4.Unity Shader 4.1概述 4.2分类( ...
- Unity shader入门精要(学习总结)
(学习总结) 一,基础概念 什么是shader? Unity Shader定义了渲染所需的各种代码(如顶点着色器和片元着色器).属性(如使用哪些纹理等)和指令(渲染和标签设置等),而材质则允许我们调节 ...
最新文章
- iis6.0怎么搭php,IIS6.0平台下PHP最佳配置方法
- prototype.js ajax.request,prototype.js的Ajax对象分享
- python里遍历筛选xml文件_python xml.etree.ElementTree遍历xml所有节点实例详解
- Unity 2018.3.1 SyncVar没有同步服务器变量
- 单步调试学习WordPress PHP文件的加载顺序
- kl散度度量分布_数据挖掘比赛技巧——确定数据同分布
- 爬虫3 requests基础之 乱码编码问题
- Stimulsoft Reports ASP.NET HTML5报表设计器
- 什么时候不该使用es6箭头函数
- c语言机试编程下载,C语言机试编程题库
- stm32的人体红外传感器的初步使用
- 4399小游戏 十滴水 求解器(输出路径的bfs)
- 水下航行器简介及水下面临的挑战
- 系统重构数据同步利器之Canal实战篇
- 中国芯片自给率激增,芯片库存高企的美国阻止中国发展先进工艺
- 5g发展趋势和未来前景,未来科技发展五大方向
- 新H5中用canvas画一个数字钟表
- ArchLinux 的 pacman 命令详解
- 03 # 网页元素的属性
- Oracle数据库导入导出(以用户的形式)