当前位置:网站首页>Unity中简单的cubecap+fresnel shader的实现
Unity中简单的cubecap+fresnel shader的实现
2022-07-29 05:24:00 【TingQiaoQiao】
Shader "Unlit/cubecap"
{
//cubecap
Properties{
_NormalMap("法线贴图", 2D) = "bump" {}
_Cubemap("环境贴图", Cube) = "_Skybox" {}
_MidMapLevel("贴图模糊度", Range(0, 7)) = 1.227742
_fresnelpow("菲涅尔强度", Range(0, 1)) = 1
_env("env", Range(0, 5)) = 1.93491
}
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 samplerCUBE _Cubemap;
uniform sampler2D _NormalMap;
uniform float _MidMapLevel;
uniform float _fresnelpow;
uniform float _env;
//输入结构
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;
};
//顶点shader
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 vDirWS = normalize(_WorldSpaceCameraPos.xyz - i.posWS.xyz);
float3 vrDirWS = reflect(-vDirWS, nDirWS);
//中间量准备
float ndotv = dot(nDirWS,vDirWS);
//光照模型
float3 cubemap = texCUBElod(_Cubemap,float4(vrDirWS, _MidMapLevel) );
float fresnel = pow(1.0 - ndotv, _fresnelpow);
float3 envSpcLighting = cubemap * fresnel * _env;
//返回值
return fixed4(envSpcLighting,1);
}
ENDCG
}
}
FallBack "Diffuse"
}效果实现:

边栏推荐
- LeetCode #344.反转字符串
- 无符号右移
- MySQL interview questions
- LeetCode #557.反转字符串中的单词 III
- Sqlyog installation and configuration tutorial
- 动态加载数据
- Leetcode 26. delete duplicates in the ordered array
- 【软件工程之美 - 专栏笔记】13 | 白天开会,加班写代码的节奏怎么破?
- 传统模型预测控制轨迹跟踪——波浪形轨迹(功能包已经更新)
- leetcode刷题笔记 452. Minimum Number of Arrows to Burst Balloons (Medium) 452.用最少数量的箭引爆气球(中等)
猜你喜欢

UE5 光影基础 阴影全解析 锯齿阴影解决方案 Lumen

Leetcode 1. sum of two numbers
![[beauty of software engineering - column notes] 16 | how to write project documents?](/img/52/70d66230679abae6ce26d3477a22f6.png)
[beauty of software engineering - column notes] 16 | how to write project documents?

LeetCode #344.反转字符串

Simple code to realize PDF to word document

Leetcode 7. integer inversion

官方教程 Redshift 05 system参数详细解释

【软件工程之美 - 专栏笔记】20 | 如何应对让人头疼的需求变更问题?

LeetCode #26.删除有序数组中的重复项

封装——super关键字
随机推荐
leetcode刷题笔记 605. Can Place Flowers (Easy) 605.种花问题
关于时间复杂度的个人看法
Leetcode notes 452. minimum number of arrows to burst balloons (medium) 452. detonate balloons with the minimum number of arrows (medium)
数论:px+py 不能表示的最大数为pq-p-q的证明
LeetCode #876.链表的中间结点
赛博朋克版特效shader
#6898 变幻的矩阵 题解
网络爬虫
Maya ACES工作流程配置(Arnold 及 RedShift 贴图配置规范-还原出SP-Aces流程下贴图正确的效果) PS还原Aces流程下渲染的图
【软件工程之美 - 专栏笔记】17 | 需求分析到底要分析什么?怎么分析?
抽象类以及接口
爬取表情包
太原市公交路线爬取
LeetCode #1.两数之和
唯美girls
动态加载数据
【软件工程之美 - 专栏笔记】19 | 作为程序员,你应该有产品意识
Leetcode 14. longest public prefix
leetcode刷题笔记 763.划分字母区间(中等)
计算机网络面试题