当前位置:网站首页>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"
}效果实现:

边栏推荐
- 【软件工程之美 - 专栏笔记】25 | 有哪些方法可以提高开发效率?
- Ml8 self study notes LDA principle formula derivation
- Unity-默认渲染管线-刻晴卡渲shader
- 关于【链式前向星】的自学理解
- Eight sorts ------------- heap sort
- Ml8 self study notes
- Ml9 self study notes
- 顺序表和链表
- 简洁代码实现pdf转word文档
- FPGA based: moving target detection (schematic + source code + hardware selection, available)
猜你喜欢

JUC并发知识点

Traditional model predictive control trajectory tracking - wavy trajectory (function package has been updated)

shell工具finalShell

LeetCode #977.有序数组的平方

UE5 纹理系统讲解及常见问题设置及解决方案

Ml4 self study notes

scanBasePackages扫包范围配置

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

【软件工程之美 - 专栏笔记】25 | 有哪些方法可以提高开发效率?

Leetcode 35. search insertion location
随机推荐
Leetcode 977. Square of ordered array
八大排序-----------------堆排序
Computer factory interview questions
clickhouse 导入CSV失败 不报错但是无数据
官方教程 Redshift 03 各种GI的参数和常规使用说明
Redshift还原SP效果 - SP贴图导出设置及贴图导入配置
单链表面试题
官方教程 Redshift 08 Light
Leetcode 19. delete the penultimate node of the linked list
抽象封装继承多态
JUC集合类不安全
scanBasePackages扫包范围配置
计算机网络面试题
leetcode---技巧
UE4/UE5 C盘变大处理
Single chain surface test questions
Sqlyog installation and configuration tutorial
Redshift 2.6.41 for maya2018 水印去除
Eight sorts ------------- heap sort
mavan中的plugin位置