当前位置:网站首页>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"
}
边栏推荐
- SUSE Ceph 增加节点、减少节点、 删除OSD磁盘等操作 – Storage6
- Some suggestions on Oracle SQL tuning
- 【深度学习】:《PyTorch入门到项目实战》第四天:从0到1实现logistic回归(附源码)
- MySQL 5.7 and sqlyogv12 installation and use cracking and common commands
- 做题笔记2(两数相加)
- leetcode70假设你正在爬楼梯。需要 n 阶你才能到达楼顶。每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢?
- 浏览器解码过程分析
- Quickly master kotlin set functions
- 【深度学习】:《PyTorch入门到项目实战》第六天:多层感知机(含代码)
- Technology sharing | MySQL shell customized deployment MySQL instance
猜你喜欢

HTAP是有代价的

Interesting kotlin 0x07:composition

PostgreSQL每周新闻—2022年7月20日

: No such file or directory

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

Easypoi multi sheet export by template

【深度学习】:《PyTorch入门到项目实战》:简洁代码实现线性神经网络(附代码)

Jsonarray traversal

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

【深度学习】:《PyTorch入门到项目实战》第二天:从零实现线性回归(含详细代码)
随机推荐
Some notes on how unity objects move
Ruoyi集成flyway后启动报错的解决方法
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)
SUSE CEPH rapid deployment – storage6
Probability theory and mathematical statistics Chapter 1
做题笔记4(第一个错误的版本,搜索插入位置)
Comprehensively design an oppe homepage -- page service part
Games101 section 13 ray tracing notes
Ugui learning notes (IV) ugui event system overview and Usage Summary
Implementation of transfer business
打造自组/安全/可控的LoRa网!Semtech首度回应“工信部新规”影响
A total of 13billion flash and 400million MCU were shipped! In depth analysis of the three product lines of Zhaoyi innovation
Record the processing process of CEPH two RBDS that cannot be deleted
[deep learning]: introduction to pytorch to project practice: simple code to realize linear neural network (with code)
Leetcode70 suppose you are climbing stairs. You need n steps to reach the roof. You can climb one or two steps at a time. How many different ways can you climb to the roof?
Re14:读论文 ILLSI Interpretable Low-Resource Legal Decision Making
Easypoi --- excel file export
Huawei mate 40 series exposure: large curvature hyperboloid screen, 5nm kylin 1020 processor! There will also be a version of Tianji 1000+
做题笔记2(两数相加)
Implementation of paging