当前位置:网站首页>Unity uses a map to control the transparency of the material's main map
Unity uses a map to control the transparency of the material's main map
2022-07-19 14:38:00 【humilezr】
There's a need recently : The transparency of the material's main map is controlled by the color value of a map .
Make a note first : The map used to control the transparency of the main map , Let's call it “Alpha Mapping ”; The main map is called “ Main map ”.
First , What we need to solve is to Alpha The color value of the map is converted to transparency ; After many tests , Finally, the following methods are adopted :1) Compare pixels r、g、b;2) hypothesis r Maximum , be float val = r* The weight +g+b;3)alpha = val/( The weight +2)
Go straight to the code :
Shader "Custom/MainTexAlphaCtrl"
{
Properties
{
_MainTex("Texture", 2D) = "white" {}// Main map texture
_AlphaTex("AlphaTexture",2D) = "white"{}//Alpha texture
_BaseColor("Base Color",Color) = (1, 1, 1, 1)
_Weight("Weight",float) = 0//Alpha Weight when calculating
[Toggle(_Reverse)] _Reverse("Reverse", float) = 0// Whether to reverse , Used for transparency calculation of the main map
}
SubShader
{
Tags { "RenderType" = "Transparent"}
LOD 100
Cull Off
// If you use Alpha blend , Need to turn off deep buffer writing
ZWrite Off
// Add mixed command
Blend SrcAlpha OneMinusSrcAlpha
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// make fog work
#pragma multi_compile_fog
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
sampler2D _AlphaTex;
float4 _MainTex_ST;
float4 _BaseColor;
float _Weight;
float _Reverse;
// principle :
// 1. precondition :1) Single channel rendering ,Unity Built in parameters unity_StereoEyeIndex As a sign of rendering left and right eyes ,
// unity_StereoEyeIndex = 0: Render the left eye ;=1: Render the right eye .
// 2) Texture contains left and right binocular pictures , If the texture is monocular , This method is not applicable to .
// 2. When rendering the left eye , Textured 0~0.5f( contain 0.5f) Range ; When rendering the right eye , Textured 0.5f( It doesn't contain 0.5f)~1 Range .
v2f vert(appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
return o;
}
// Convert color values to transparency
float color2Alpha(fixed3 col)
{
float a = 0;
float val = 0;
// Compare r、g、b Three values , Get the maximum , If r Maximum , be val = 4*r+g+b;
if (col.r > col.g)
{
if (col.r > col.b)
{
val = _Weight * col.r + col.g + col.b;
}
else
{
val = _Weight * col.b + col.g + col.r;
}
}
else
{
if (col.g > col.b)
{
val = _Weight * col.g + col.r + col.b;
}
else
{
val = _Weight * col.b + col.g + col.r;
}
}
// then val Divide ( The weight +2), Get the final alpha value
return val / (_Weight+2);
//return (col.r + col.g + col.b)/3;
}
fixed4 frag(v2f i) : SV_Target
{
// sample the texture
fixed4 main_col = tex2D(_MainTex, i.uv) * _BaseColor;
fixed3 alpha_col = tex2D(_AlphaTex, i.uv).rgb;
if (_Reverse == 0)
{
return fixed4(main_col.rgb, main_col.a * color2Alpha(alpha_col));
}
else
{
return fixed4(main_col.rgb, main_col.a * (1 - color2Alpha(alpha_col)));
}
}
ENDCG
}
}
}
边栏推荐
- 單片機軟件定時器V2.0
- Robotics at google:laura Graesser | i-sim2real: strengthen the learning robot strategy in the close human-computer interaction cycle
- Notes with a face value of 10exp (303)
- Unveil the mystery of service grid istio service mesh
- Overview report of Chinese AI medical imaging industry in 2022
- OSPF appendix anti ring Reissue
- Codeforces Round #808 (Div. 1)(A~C)
- Logu: p4516 [jsoi2018] stealth action (tree DP, tree grouping knapsack statistical scheme number)
- topy库的安装(拓扑优化软件)
- SQL相关的时间日期类型
猜你喜欢

Installation of Topy Library (topology optimization software)

Redis源码与设计剖析 -- 1.简单动态字符串

Problème de la valeur maximale de la fenêtre coulissante

Colliding Mice碰撞老鼠工程分析

CF 807 E. Mark and Professor Koro(权值线段树)

Zhikanghu property elderly care service plan

数据库的增删改查

Si446 usage record (III): match function

96. Different binary search trees

Homework on the first day of summer rhcsa training
随机推荐
数据库的增删改查
Data consistency between redis and MySQL
[Luogu p3220] and not (construction) (digit DP) (inference)
Qchartview overwrites the previous control when it is added in qgridlayout
Homework for the third day of summer rhcsa training
ospf-LSA
How to avoid global index in pychart? How to cancel the indexing of a folder?
揭开服务网格~Istio Service Mesh神秘的面纱
topy库的安装(拓扑优化软件)
Cmake learning notes
树与二分图【思维】
Redis source code and design analysis -- 3 Dictionaries
After 2000, he was hired as a special associate researcher of Nanjing University. He went to primary school at the age of 4 and was admitted to Nanjing University at the age of 14!
Colliding Mice碰撞老鼠工程分析
2022年中国AI医学影像行业概览报告
keystore文件里PrivateKeyEntry错误配置导致TLS连接失败
Optimal Biking Strategy【DP + 二分】
Alibaba微服务组件Nacos注册中心
Codeforces Round #808 (Div. 1)(A~C)
Redis源码与设计剖析 -- 3.字典