话不多说,直接上代码,具体区别我后边会补充上,
暂时委屈各位,先锻炼下自己的“找不同”的能力,哈哈:
Built-CG写法:
Shader "Unity Shaders Book/Chapter 10/Refraction" {
Properties {
_Color("Color Tint", Color) = (1,1,1,1)
_RefractColor("Refraction Color", Color) = (1,1,1,1)
_RefractAmount("Refraction Amount", Range(0, 1)) = 0.5
_RefractRatio("Refraction Ration", Range(0, 1)) = 0.5
_Cubemap("Cube Map", Cube) = "_Skybox" {}
}
SubShader {
Tags {"Queue"="Geometry" "RenderType"="Opaque"}
Pass {
Tags {"LightMode"="ForwardBase"}
CGPROGRAM
#include "Lighting.cginc"
#include "AutoLight.cginc"
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fwdbase
fixed4 _Color;
fixed4 _RefractColor;
fixed _RefractAmount;
fixed _RefractRatio;
samplerCUBE _Cubemap;
struct a2v {
float4 vertex : POSITION;
float3 normal : NORMAL;
};
struct v2f {
float4 pos : SV_POSITION;
float3 worldPos : TEXCOORD0;
float3 worldNormal : TEXCOORD1;
fixed3 worldViewDir : TEXCOORD2;
fixed3 worldRefr : TEXCOORD3;
SHADOW_COORDS(4)
};
v2f vert(a2v v) {
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.worldNormal = UnityObjectToWorldNormal(v.normal);
o.worldPos = mul(unity_ObjectToWorld, v.vertex);
o.worldViewDir = UnityWorldSpaceViewDir(o.worldPos);
o.worldRefr = refract(-normalize(o.worldViewDir), normalize(o.worldNormal), _RefractRatio);
TRANSFER_SHADOW(o);
return o;
}
fixed4 frag(v2f i) : SV_Target {
fixed3 worldNormal = normalize(i.worldNormal);
fixed3 viewDir = normalize(i.worldViewDir);
fixed3 lightDir = normalize(UnityWorldSpaceLightDir(i.worldPos));
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
fixed3 refraction = texCUBE(_Cubemap, i.worldRefr).rgb * _RefractColor.rgb;
fixed3 diffuse = _LightColor0.rgb * _Color.rgb * saturate(dot(i.worldNormal, lightDir));
UNITY_LIGHT_ATTENUATION(atten, i, i.worldPos);
fixed3 color = ambient + lerp(diffuse, refraction, _RefractAmount) * atten;
return fixed4(color, 1);
}
ENDCG
}
}
FallBack "Reflective/VertexLit"
// FallBack "Refraction/vertexLit"
}
URP+HLSL写法:
Shader "Unity Shaders Book/Chapter 10/Refraction" {
Properties {
_Color("Color Tint", Color) = (1,1,1,1)
_RefractColor("Refraction Color", Color) = (1,1,1,1)
_RefractAmount("Refraction Amount", Range(0, 1)) = 0.5
_RefractRatio("Refraction Ration", Range(0, 1)) = 0.5
_Cubemap("Cube Map", Cube) = "_Skybox" {}
}
SubShader {
Tags {
"RenderPipeline"="UniversalPipeline"
"Queue"="Geometry" "RenderType"="Opaque"
}
Pass {
Tags {"LightMode"="UniversalForward"}
HLSLPROGRAM
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
#pragma vertex vert
#pragma fragment frag
// #pragma multi_compile_fwdbase
CBUFFER_START(UnityPerMaterial)
half4 _Color;
half4 _RefractColor;
half _RefractAmount;
half _RefractRatio;
CBUFFER_END
TEXTURECUBE(_Cubemap);
SAMPLER(sampler_Cubemap);
struct a2v {
float4 vertex : POSITION;
half3 normal : NORMAL;
};
struct v2f {
float4 pos : SV_POSITION;
half3 worldPos : TEXCOORD0;
half3 worldNormal : TEXCOORD1;
half3 worldViewDir : TEXCOORD2;
half3 worldRefr : TEXCOORD3;
// SHADOW_COORDS(4)
};
v2f vert(a2v v) {
v2f o;
o.pos = TransformObjectToHClip(v.vertex.xyz);
o.worldNormal = TransformObjectToWorldNormal(v.normal).xyz;
o.worldPos = mul(UNITY_MATRIX_M, v.vertex).xyz;
o.worldViewDir = _WorldSpaceCameraPos.xyz - o.worldPos;
o.worldRefr = refract(-normalize(o.worldViewDir), normalize(o.worldNormal), _RefractRatio); // 折射率
// TRANSFER_SHADOW(o);
return o;
}
half4 frag(v2f i) : SV_Target {
half3 worldNormal = normalize(i.worldNormal);
// half3 viewDir = normalize(i.worldViewDir);
// half3 lightDir = normalize(UnityWorldSpaceLightDir(i.worldPos));
float4 shadowCoord = TransformWorldToShadowCoord(i.worldPos);
Light mainLight = GetMainLight(shadowCoord);
half3 lightDir = normalize(mainLight.direction);
half3 ambient = half3(unity_SHAr.w, unity_SHAg.w, unity_SHAb.w);
half3 refraction = SAMPLE_TEXTURECUBE(_Cubemap, sampler_Cubemap, i.worldRefr).rgb * _RefractColor.rgb;
half3 diffuse = mainLight.color.rgb * _Color.rgb * saturate(dot(i.worldNormal, lightDir));
// UNITY_LIGHT_ATTENUATION(atten, i, i.worldPos);
half3 color = ambient + lerp(diffuse, refraction, _RefractAmount);
return half4(color, 1);
}
ENDHLSL
}
}
// FallBack "Reflective/VertexLit"
// 不知道URP用什么fallback代替
FallBack "Universal Render Pipeline/Simple Lit"
}