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)

相关推荐
tealcwu9 小时前
【Unity基础】为什么游戏需要物理按键映射?从 Unity Input Manager 到跨引擎输入设计
java·游戏·unity
tealcwu1 天前
【Unity高级】Unity URP:构建同时支持 2D 与 3D 场景的项目完全指南
3d·unity·游戏引擎
淡海水1 天前
10-01-互操作-C#与C++边界的内存管理
unity·c#·游戏引擎·.netcore·gc
小小数媒成员1 天前
UGUI原理(一)
开发语言·游戏·unity·游戏引擎
暴走约伯1 天前
【VFX工具链源码拆解 04】拯救丢失的绑定:Timeline 一键克隆与层级相对路径重映射算法
unity·c#·游戏引擎·硬件工程
小小数媒成员1 天前
Unity UGUI布局组件(LayoutGroup、ContentSizeFitter、LayoutElement)详解
unity·游戏引擎
tealcwu1 天前
【Unity基础】Unity输入与Time.deltaTime的使用原则
unity·游戏引擎
郝学胜-神的一滴2 天前
AI 编程智能体 22:解析器赋能与实战落地全指南
人工智能·程序人生·游戏·unity·游戏引擎
BuHuaX2 天前
Lua 与 ToLua:运行机制及热更新原理
开发语言·unity·junit·c#·lua
peter67683 天前
unity2d游戏开发经验总结
unity