当前位置:网站首页>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"
}
边栏推荐
- Re11: read EPM legal judgment prediction via event extraction with constraints
- Read excel xlsx format file in unity
- 综合设计一个OPPE主页--页面服务部分
- Outline and principle of structured design -- modularization
- SUSE CEPH rapid deployment – storage6
- Probability theory and mathematical statistics Chapter 1
- How should I understand craft
- Given positive integers n and m, both between 1 and 10 ^ 9, n < = m, find out how many numbers have even digits between them (including N and m)
- Epoll horizontal departure, which edge triggers
- Quickly master kotlin set functions
猜你喜欢

Ugui learning notes (II) Scrollview related

Do you really understand CMS garbage collector?
![[deep learning]: day 5 of pytorch introduction to project practice: realize softmax regression from 0 to 1 (including source code)](/img/19/18d6e94a1e0fa4a75b66cf8cd99595.png)
[deep learning]: day 5 of pytorch introduction to project practice: realize softmax regression from 0 to 1 (including source code)

Easypoi multi sheet export by template

Games101 section 13 ray tracing notes

Re11:读论文 EPM Legal Judgment Prediction via Event Extraction with Constraints

Re13: read the paper gender and racial stereotype detection in legal opinion word embeddings

MySQL installation tutorial

College students participated in six Star Education PHP training and found jobs with salaries far higher than those of their peers

Binary representation of negative integers and floating point numbers
随机推荐
Re14:读论文 ILLSI Interpretable Low-Resource Legal Decision Making
在AD中添加差分对及连线
Using MVC in the UI of unity
SUSE Ceph 增加节点、减少节点、 删除OSD磁盘等操作 – Storage6
Create a self-organizing / safe / controllable Lora network! Semtech responded for the first time to the impact of the "new regulations of the Ministry of industry and information technology"
Ugui learning notes (V) togglegroup makes multiple choice radio boxes
Semtech推出物联网地理定位解决方案LoRa Edge,首款芯片LR1110现已上市
[deep learning]: day 8 of pytorch introduction to project practice: weight decline (including source code)
【深度学习】:《PyTorch入门到项目实战》第九天:Dropout实现(含源码)
【从零开始学习SLAM】将坐标系变换关系发布到 topic tf
海康威视回应'美国禁令'影响:目前所使用的元器件都有备选
SUSE CEPH add nodes, reduce nodes, delete OSD disks and other operations – storage6
How to set ticdc synchronization data to only synchronize the specified library?
[deep learning]: day 5 of pytorch introduction to project practice: realize softmax regression from 0 to 1 (including source code)
Interesting kotlin 0x08:what am I
Call DLL file without source code
给定正整数N、M,均介于1~10 ^ 9之间,N <= M,找出两者之间(含N、M)的位数为偶数的数有多少个
Ugui learning notes (VI) get the information of the clicked UI
Jsonarray traversal
Oracle system composition