当前位置:网站首页>Realization of reflection and refraction effect in unity shader cube texture
Realization of reflection and refraction effect in unity shader cube texture
2022-07-28 17:06:00 【Morita Rinko】
Reflection :
If you don't consider the reflection effect , The effect of the mirror surface in the scene should be to sample the cube texture with the normal of the surface to get the color of the point .
So when we calculate the reflection , Just find the reflection direction , Then sample the cube texture in the direction of reflection .
effect :
Code :
Shader "Custom/Chapter10-Reflaction"
{
Properties
{
_Color("Color Tint",Color)=(1,1,1,1)
// Reflection color
_RefractColor("Reflection Color",Color)=(1,1,1,1)
// Degree of reflection
_RefractAmount("Reflect Amount",Range(0,1))=1
// Proportion of media
_RefractRatio("Refraction Ratio",Range(0.1,1))=0.5
// Cubic texture
_Cubemap("Reflection Cubemap",Cube)="_Skybox"{
}
}
SubShader
{
Tags {
"RenderType"="Opaque" "Queue"="Geometry"}
Pass{
Tags{
"LightMode"="ForwardBase"}
CGPROGRAM
#pragma multi_compile_fwdbase
#pragma vertex vert
#pragma fragment frag
#include "Lighting.cginc"
#include "AutoLight.cginc"
fixed4 _Color;
fixed4 _RefractColor;
float _RefractAmount;
fixed _RefractRatio;
samplerCUBE _Cubemap;
struct a2v{
float4 vertex:POSITION;
float3 normal:NORMAL;
};
struct v2f{
float4 pos:SV_POSITION;
float3 worldPos:TEXCOORD0;
fixed3 worldNormal:TEXCOORD1;
fixed3 worldViewDir:TEXCOORD2;
// Direction of reflected light
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).xyz;
o.worldViewDir=UnityWorldSpaceViewDir(o.worldPos);
// Direction of reflection
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 worldLightDir = normalize(UnityWorldSpaceLightDir(i.worldPos));
fixed3 worldViewDir = normalize(i.worldViewDir);
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
fixed3 diffuse = _LightColor0.rgb*_Color.rgb*max(0,dot(worldNormal,worldLightDir));
fixed3 Refraction=texCUBE(_Cubemap,i.worldRefr).rgb*_RefractColor.rgb;
UNITY_LIGHT_ATTENUATION(atten,i,i.worldPos);
fixed3 color=ambient +lerp(diffuse,Refraction,_RefractAmount)*atten;
return fixed4(color,1.0);
}
ENDCG
}
}
FallBack "Reflective/VertexLit"
}
refraction
Empathy , Find the direction of refraction , Sample the cube texture .
Realization effect :
Code :
Shader "Custom/Chapter10-Reflaction"
{
Properties
{
_Color("Color Tint",Color)=(1,1,1,1)
// Refraction color
_RefractColor("Reflection Color",Color)=(1,1,1,1)
// Degree of refraction
_RefractAmount("Reflect Amount",Range(0,1))=1
// Proportion of media
_RefractRatio("Refraction Ratio",Range(0.1,1))=0.5
// Cubic texture
_Cubemap("Reflection Cubemap",Cube)="_Skybox"{
}
}
SubShader
{
Tags {
"RenderType"="Opaque" "Queue"="Geometry"}
Pass{
Tags{
"LightMode"="ForwardBase"}
CGPROGRAM
#pragma multi_compile_fwdbase
#pragma vertex vert
#pragma fragment frag
#include "Lighting.cginc"
#include "AutoLight.cginc"
fixed4 _Color;
fixed4 _RefractColor;
float _RefractAmount;
fixed _RefractRatio;
samplerCUBE _Cubemap;
struct a2v{
float4 vertex:POSITION;
float3 normal:NORMAL;
};
struct v2f{
float4 pos:SV_POSITION;
float3 worldPos:TEXCOORD0;
fixed3 worldNormal:TEXCOORD1;
fixed3 worldViewDir:TEXCOORD2;
// Refract the direction of light
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).xyz;
o.worldViewDir=UnityWorldSpaceViewDir(o.worldPos);
// The direction of refraction
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 worldLightDir = normalize(UnityWorldSpaceLightDir(i.worldPos));
fixed3 worldViewDir = normalize(i.worldViewDir);
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
fixed3 diffuse = _LightColor0.rgb*_Color.rgb*max(0,dot(worldNormal,worldLightDir));
fixed3 Refraction=texCUBE(_Cubemap,i.worldRefr).rgb*_RefractColor.rgb;
UNITY_LIGHT_ATTENUATION(atten,i,i.worldPos);
fixed3 color=ambient +lerp(diffuse,Refraction,_RefractAmount)*atten;
return fixed4(color,1.0);
}
ENDCG
}
}
FallBack "Reflective/VertexLit"
}
Fresnel reflex
The degree of reflection is controlled by Fresnel reflection according to the direction of viewing angle . For example, in real life , We looked at the water near the lake , You can see stones, fish and so on under the water , But when we walk far away and look at the water beside the lake, we can only see the water , And I can't see the underwater scene , This is the Fresnel effect .
Fresnel approximate equation :
Schlick Fresnel approximate equation :
f Is the reflection coefficient , Control the intensity of Fresnel reflection Empricial Fresnel approximate equation :

effect :
Code :
Shader "Custom/Chapter10-Fresnel"
{
Properties
{
_Color("Color Tint",Color)=(1,1,1,1)
//F0
_FresnelScale("Fresnel Scale",Range(0,1))=0.5
// Cubic texture
_Cubemap("Reflection Cubemap",Cube)="_Skybox"{
}
}
SubShader
{
Tags {
"RenderType"="Opaque" "Queue"="Geometry"}
Pass{
Tags{
"LightMode"="ForwardBase"}
CGPROGRAM
#pragma multi_compile_fwdbase
#pragma vertex vert
#pragma fragment frag
#include "Lighting.cginc"
#include "AutoLight.cginc"
fixed4 _Color;
float _FresnelScale;
samplerCUBE _Cubemap;
struct a2v{
float4 vertex:POSITION;
float3 normal:NORMAL;
};
struct v2f{
float4 pos:SV_POSITION;
float3 worldPos:TEXCOORD0;
fixed3 worldNormal:TEXCOORD1;
fixed3 worldViewDir:TEXCOORD2;
// Direction of reflected light
fixed3 worldRefl: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).xyz;
o.worldViewDir=UnityWorldSpaceViewDir(o.worldPos);
// Direction of reflection
o.worldRefl = reflect(-o.worldViewDir,o.worldNormal);
TRANSFER_SHADOW(o);
return o;
}
fixed4 frag(v2f i):SV_Target{
fixed3 worldNormal = normalize(i.worldNormal);
fixed3 worldLightDir = normalize(UnityWorldSpaceLightDir(i.worldPos));
fixed3 worldViewDir = normalize(i.worldViewDir);
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
fixed3 diffuse = _LightColor0.rgb*_Color.rgb*max(0,dot(worldNormal,worldLightDir));
// The Fresnel approximation is obtained by calculation
fixed3 Reflection =texCUBE(_Cubemap,i.worldRefl).rgb;
fixed fresnel =_FresnelScale+(1-_FresnelScale)*pow(1-dot(worldViewDir,worldNormal),5);
UNITY_LIGHT_ATTENUATION(atten,i,i.worldPos);
// Use Fresnel approximation to difference reflection color and diffuse reflection color
fixed3 color =ambient +lerp(diffuse,Reflection,saturate(fresnel))*atten;
return fixed4(color,1.0);
}
ENDCG
}
}
FallBack "Reflective/VertexLit"
}
边栏推荐
- How should I understand craft
- [deep learning]: day 5 of pytorch introduction to project practice: realize softmax regression from 0 to 1 (including source code)
- How to use fail2ban to protect WordPress login page
- parseJson
- 【深度学习】:《PyTorch入门到项目实战》第八天:权重衰退(含源码)
- 在AD中添加差分对及连线
- TCP handshake, waving, time wait connection reset and other records
- 如何使用Fail2Ban保护WordPress登录页面
- Re11:读论文 EPM Legal Judgment Prediction via Event Extraction with Constraints
- [learn slam from scratch] publish the coordinate system transformation relationship to topic TF
猜你喜欢

负整数及浮点数的二进制表示

技术分享 | 误删表以及表中数据,该如何恢复?
![[deep learning]: day 9 of pytorch introduction to project practice: dropout implementation (including source code)](/img/19/18d6e94a1e0fa4a75b66cf8cd99595.png)
[deep learning]: day 9 of pytorch introduction to project practice: dropout implementation (including source code)

Re11: read EPM legal judgment prediction via event extraction with constraints

Probability theory and mathematical statistics Chapter 1

Outline and principle of structured design -- modularization

Re10: are we really making much progress? Revisiting, benchmarking, and refining heterogeneous gr

Games101 section 13 ray tracing notes

综合设计一个OPPE主页--页面的售后服务
![[deep learning]: model evaluation and selection on the seventh day of pytorch introduction to project practice (Part 1): under fitting and over fitting (including source code)](/img/19/18d6e94a1e0fa4a75b66cf8cd99595.png)
[deep learning]: model evaluation and selection on the seventh day of pytorch introduction to project practice (Part 1): under fitting and over fitting (including source code)
随机推荐
kubenertes 1.16集群部署问题总结
[deep learning]: day 5 of pytorch introduction to project practice: realize softmax regression from 0 to 1 (including source code)
MD5 encryption verification
Oracle table partition
A total of 13billion flash and 400million MCU were shipped! In depth analysis of the three product lines of Zhaoyi innovation
Mysql与Oracle的13点区别
综合设计一个OPPE主页--页面的售后服务
Microsoft: edge browser has built-in disk cache compression technology, which can save space and not reduce system performance
Comprehensively design an oppe homepage -- page service part
Read excel xlsx format file in unity
在AD中添加差分对及连线
Ugui learning notes (IV) ugui event system overview and Usage Summary
Ugui learning notes (VI) get the information of the clicked UI
Summary of kubenertes 1.16 cluster deployment problems
Detailed steps for setting up SUSE storage6 environment – win10 + VMware Workstation
我该如何理解工艺
Ruoyi集成flyway后启动报错的解决方法
Jsonarray traversal
leetcode647. 回文子串
Is smart park the trend of future development?