当前位置:网站首页>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
}
}
}
边栏推荐
- Compréhension initiale de la fonction - partie 2
- 详解C语言动态内存管理
- Robotics at Google:Laura Graesser | i-Sim2Real:在紧密的人机交互循环中强化学习机器人策略
- 函數初認識-下
- 敏捷的第一步:把 “迭代” 变为 “冲刺” 开始!
- How to avoid global index in pychart? How to cancel the indexing of a folder?
- Deep understanding of transaction isolation levels
- Win10 Microsoft Store打不开(开启TLS 1.2)
- 洛谷:P3092 [USACO13NOV]No Change G(状压+二分,独特的状态定义,不写会后悔一辈子的题)
- Pyside2 drawing embedded in Matplotlib
猜你喜欢

Win10 Microsoft Store打不开(开启TLS 1.2)

matplotlib绘制多折线图(解决matplotlib中文无法显示问题)

Notes with a face value of 10exp (303)

Alibaba microservice component Nacos registry

Keil环境下STM32定位hardfault位置方法和遇到的情况

What are the ways to realize load balancing?

Optimal Biking Strategy【DP + 二分】

智康护物业养老服务方案

歐奈爾的RPS曲線的編制方法(陶博士原創)

Take a look at try{}catch{}
随机推荐
C. Watto and Mechanism(哈希 | 字典树 + dfs (树上dfs))
C - pointer
Logu: p4516 [jsoi2018] stealth action (tree DP, tree grouping knapsack statistical scheme number)
Huawei wireless devices are configured with static load balancing
273. Grading - acwing question bank [DP]
ClassNotFoundException:com.tongweb.geronimo.osgi.locator.ProviderLocator
A review of classical must see for Nonconvex Optimization Problems "from symmetry to geometry", University of Rochester, et al
CMAKE学习笔记
CF 807 E. mark and Professor Koro (weight segment tree)
Comprehensive analysis of C language multimedia open source framework GStreamer
OSPF appendix anti ring Reissue
Homework for the third day of summer rhcsa training
Rotation formula of coordinate simulation matrix
陶博士月线反转6.0
Luo Gu: p3092 [usaco13nov]no change G
论文阅读 TEMPORAL GRAPH NETWORKS FOR DEEP LEARNING ON DYNAMIC GRAPHS
Excellent jar package startup shell script collection
函數初認識-下
Overview report of Chinese AI medical imaging industry in 2022
BiShe - online reservation and registration system based on SSM