Shader入门笔记2

反射 (reflection)

float3 refleDir=reflection(-viewDir,worldNormal)

float3 refleColor=texCUBE(_Cube,refleDir)

折射(refraction)

float3 refraDir=refraction(-viewDir,worldNormal,_RefractionRate)

float3 refraColor=texCUBE(_Cube,refraDir)

菲涅尔反射(随着观察角度变化而变化的反射)

公式:float fresnel=_FresnelScale+(1-_FresnelScale)*Pow(1-dot(viewDir,worldNormal),_FresnelPower)

变体:float fresnel=max(0,min(1,refraction+_FresnelScale*pow(1-dot(viewDir,worldNormal),_FresnelPower))) 用于风格化渲染

float3 finColor=lerp(ambientColor,reflection,fresnel)

渲染纹理

1.镜面效果

renderTextrue + camera + 主纹理(rendertextrue)uv.x取反

2.玻璃效果

需要半透

tag("Queue"="Transparent" RenderType="TrnasParent")

ZWrite=Off

Blend ScrAlpha OneMinuxScrAlpha

_Distortion("Distortion",Range(0,100))=10

使用GrabPass("_RefractionTex") 抓取屏幕图片做折射 名字固定的

sampler2D _RefractionTex

float3 _RefractionTex_TexelSize

v2f vert(appdate i)

{

o.pos=UnityObjectToClipPos(i.vertex)

o.scrPos=ComputeGrabScreenPos(o.pos)

}

需要法线纹理,以纹理来做折射偏移

float2 offset=normal.xy*_RefractionTex_TexelSize.xy*_Distortion

scrPos.xy+=offset

float3 refractionColor = tex2D(_RefractionTex,scrPos.xy/scrPos.w)

lerp(diff,refractionColor,_Refraction)

纹理动画

_FrameTex("FrameTex",2d)="white"

_PlaySpeed("PlaySpeed",float)=1

循环纹理动画

frac(float2(xSpeed,ySpeed)*_Time,y)

frac取数据小数部分 = x-floor(x)

相关推荐
点心的游戏开发世界9 小时前
Unity C# 脚本学习笔记:命名空间与 using
学习·unity·c#
点心的游戏开发世界11 小时前
Unity C# 脚本学习笔记:泛型
学习·unity·c#
微三云生态系统架构师-彭丹2 天前
任务卷轴积分系统架构设计:从任务状态机到合规风控的完整实现
unity·系统架构·游戏引擎
XR技术研习社2 天前
从 PICO 文档看未来短期内的 Unity 版本选择
unity·游戏引擎·xr·vr
玖玥拾2 天前
Unity 热更新(一)
unity·游戏引擎
软件黑马王子2 天前
1.non-MonoBehaviour 单例模式泛型基类
unity·前端框架·c#
KhalilRuan3 天前
Unity性能优化
unity·性能优化·游戏引擎
ellis19703 天前
u3d IMGUI[二] 编辑器扩展
unity
五仁烧饼3 天前
Unity性能优化系列内存篇 - 移动端内存优化
unity·性能优化·游戏引擎