当前位置:网站首页>Unity中简单的matcap+fresnel shader的实现
Unity中简单的matcap+fresnel shader的实现
2022-07-29 05:24:00 【TingQiaoQiao】
Shader "Unlit/matcap"{
//matcap
Properties{
_NormalMap("法线贴图", 2D) = "bump" {}
_Matcap("matcap", 2D) = "white" {}
_FresnelPow("菲涅尔强度",Range(0,5)) = 1
_EnvSpeInt("环境反射强度",Range(0,5)) = 1
}
SubShader{
Tags {
"RenderType" = "Opaque"
}
Pass {
Name "FORWARD"
Tags {
"LightMode" = "ForwardBase"
}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#pragma multi_compile_fwdbase_fullshadows
#pragma target 3.0
//输入参数
uniform sampler2D _NormalMap;
uniform sampler2D _Matcap;
uniform float _FresnelPow;
uniform float _EnvSpeInt;
//输入结构
struct VertexInput {
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 tangent : TANGENT;
float2 uv0 : TEXCOORD0;
};
//输出结构
struct VertexOutput {
float4 pos : SV_POSITION;
float2 uv0 : TEXCOORD0;
float3 nDirWS : TEXCOORD1;
float3 tDirWS : TEXCOORD2;
float3 bDirWS : TEXCOORD3;
float3 posWS : TEXCOORD4;
};
VertexOutput vert(VertexInput v) {
VertexOutput o = (VertexOutput)0;
o.uv0 = v.uv0;
o.nDirWS = UnityObjectToWorldNormal(v.normal);
o.tDirWS = normalize(mul(unity_ObjectToWorld, float4(v.tangent.xyz, 0.0)).xyz);
o.bDirWS = normalize(cross(o.nDirWS, o.tDirWS) * v.tangent.w);
o.pos = UnityObjectToClipPos(v.vertex);
o.posWS = mul(unity_ObjectToWorld,v.vertex);
return o;
}
//像素shader
float4 frag(VertexOutput i) : COLOR{
//向量准备
float3 nDirTS = UnpackNormal(tex2D(_NormalMap, i.uv0)).rgb;
float3x3 tbn = float3x3(i.tDirWS, i.bDirWS, i.nDirWS);
float3 nDirWS = normalize(mul(nDirTS, tbn));
float3 nDirVS = mul(UNITY_MATRIX_V,float4(nDirWS,0.0));
float3 vDirWS = normalize(_WorldSpaceCameraPos.xyz - i.posWS.xyz);
//中间量准备
float2 mapUV = nDirVS.rg * 0.5 + 0.5;
float ndotv = dot(nDirWS,vDirWS);
//光照模型
float3 matcap = tex2D(_Matcap, mapUV);
float fresnel = pow(1.0-ndotv, _FresnelPow);
float3 envSpcLighting = matcap * fresnel * _EnvSpeInt;
//返回值
return fixed4(envSpcLighting,1);
}
ENDCG
}
}
FallBack "Diffuse"
}实现效果:

边栏推荐
- 【软件工程之美 - 专栏笔记】16 | 怎样才能写好项目文档?
- 太原市公交路线爬取
- 【软件工程之美 - 专栏笔记】14 | 项目管理工具:一切管理问题,都应思考能否通过工具解决
- [beauty of software engineering - column notes] 20 | how to deal with the headache of requirement change?
- 动态加载数据
- 抽象封装继承多态
- Joiner.on和stream().map联合使用技巧
- Rowkey设计
- [beauty of software engineering - column notes] 19 | as a programmer, you should have product awareness
- 【软件工程之美 - 专栏笔记】13 | 白天开会,加班写代码的节奏怎么破?
猜你喜欢
随机推荐
leetcode刷题笔记 605. Can Place Flowers (Easy) 605.种花问题
[beauty of software engineering - column notes] 14 | project management tools: all management problems should be considered whether they can be solved by tools
滑动窗口 Leetcode 76.最小覆盖子串(困难) 76.76. MinimumWindow Substring (Hard)
【软件工程之美 - 专栏笔记】17 | 需求分析到底要分析什么?怎么分析?
#7110 数字走向2 题解
Abstract classes and interfaces
官方教程 Redshift 02 4中GI引擎概述及总结
Leetcode 283. move zero
计算机大厂面试题
简洁代码实现pdf转word文档
FPGA based: multi-target motion detection (hand-in-hand teaching ①)
八大排序-----------快速排序
shell工具finalShell
循环链表和双向链表
LeetCode #7.整数反转
Redshift还原SP效果 - SP贴图导出设置及贴图导入配置
官方教程 Redshift 09 Camera
Mathematical modeling experience
[beauty of software engineering - column notes] 20 | how to deal with the headache of requirement change?
Eight sorts --------- quick sort








