冯乐乐的《Unity Shader入门精要》附带项目,其中部分shader已经修改测试
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

100 lines
2.9 KiB

Shader "Unlit/TestRefractShader"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_BumpMap ("Normal Map", 2D) = "bump" {}
_GlassColor ("Glass Color" ,COLOR) = (1,1,1,1)
_ScaleTest("Scale Vertex" ,Range(0,1)) = 1
_TexTint("Tex Tint",Range(0.0,3.0)) = 0.0
}
SubShader
{
Tags { "RenderType"="Opaque" "Queue"="Transparent" }
LOD 100
GrabPass { "_RefractionTex2" }
Cull Off
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// make fog work
//#pragma multi_compile_fog
#include "UnityCG.cginc"
sampler2D _RefractionTex2;
float4 _RefractionTex2_ST;
sampler2D _BumpMap;
float4 _BumpMap_ST;
sampler2D _MainTex;
float4 _MainTex_ST;
float4 _GlassColor;
float _TexTint;
float _ScaleTest;
struct a2v
{
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 tangent : TANGENT;
float2 uv : TEXCOORD0;
};
struct v2f
{
float4 vertex : SV_POSITION;
float4 scrPos : TEXCOORD0;
float2 uv : TEXCOORD1;
half3 wNormal : TEXCOORD2;
half3 wTangent : TEXCOORD3;
half3 wBitangent : TEXCOORD4;
float3 worldPos : TEXCOORD5;
};
v2f vert (a2v v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
//o.uv = TRANSFORM_TEX(v.uv, _MainTex);
//o.vertex = float4(o.vertex.x+(1+sin(o.vertex.x+_ScaleTest)),o.vertex.y,o.vertex.z,o.vertex.w);
o.scrPos = ComputeGrabScreenPos(o.vertex);
o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
o.uv = v.uv;
o.wTangent = UnityObjectToWorldDir(v.tangent.xyz);
o.wNormal = UnityObjectToWorldNormal(v.normal);
// compute bitangent from cross product of normal and tangent
// 通过计算法线与切线的叉积,得到二次切线bitangent,叉积*切线方向
// half tangentSign = v.tangent.w * unity_WorldTransformParams.w;
// output the tangent space matrix
half tangentSign = v.tangent.w;
o.wBitangent = cross(o.wNormal, o.wTangent) * tangentSign;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 tex = tex2D(_MainTex, i.uv);
half3 tnormal = UnpackNormal(tex2D(_BumpMap, i.uv));
float3x3 TBNMatrix = float3x3(i.wTangent,i.wBitangent,i.wNormal);
half3 worldNormal = mul(tnormal,TBNMatrix);
half3 worldViewDir = UnityWorldSpaceViewDir(i.worldPos);
half3 worldRefra = refract(worldViewDir,worldNormal,_ScaleTest);
fixed4 texTint = _TexTint*fixed4(i.vertex.z,i.vertex.z,i.vertex.z,1);
// sample the texture
float2 offset = worldRefra.xy;
i.scrPos.xy = offset * i.scrPos.z + i.scrPos.xy;
//i.scrPos.xy = tex.xy * i.scrPos.z + i.scrPos.xy;
fixed4 refrCol = tex2D(_RefractionTex2, i.scrPos.xy/i.scrPos.w);
fixed4 col = tex2D(_RefractionTex2, i.uv);
return refrCol*_GlassColor+tex*texTint;
//return tex*_TexTint;
}
ENDCG
}
}
}