shader 半透明怎么把要过滤的颜色变成透明

3991人阅读
Unity之Shader(11)
过滤掉不带Alpha通道贴图的黑色背景颜色,屏蔽掉光照影响(改变光源的颜色模型贴图颜色不受影响)
Shader "Custom/NewShader2" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
_TransVal ("Transparency Value", Range(0,10)) = 1.0
SubShader {
Tags { "RenderType"="Opaque" "Queue"="Transparent"}
Lighting Off
One OneMinusSrcAlpha
#pragma surface surf Lambert
sampler2D _MainTex
float _TransVal
struct Input {
float2 uv_MainTex
void surf (Input IN, inout SurfaceOutput o) {
half4 c = tex2D (_MainTex, IN.uv_MainTex)
o.Albedo = c.rgb
o.Emission = tex2D (_MainTex, IN.uv_MainTex)
o.Alpha = (c.r + c.g + c.b) *_TransVal
下面对代码的注释比较详细
Shader "Custom/NewShader2" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
_TransVal ("Transparency Value", Range(0,10)) = 1.0
SubShader {
Tags { "RenderType"="Opaque" "Queue"="Transparent"}
Lighting Off
One OneMinusSrcAlpha
#pragma surface surf Lambert
sampler2D _MainT
float _TransV
struct Input {
float2 uv_MainT
void surf (Input IN, inout SurfaceOutput o) {
half4 c = tex2D (_MainTex, IN.uv_MainTex);
o.Albedo = c.
o.Emission = tex2D (_MainTex, IN.uv_MainTex);
o.Alpha = (c.r + c.g + c.b) *_TransV
方法二、代码和方法一基本相同不做过多解释
Shader "Custom/NewShader1" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
_TransVal ("Transparency Value", Range(0,10)) = 1.0
SubShader {
Tags { "RenderType"="Opaque" "Queue"="Transparent"}
Lighting Off
//在此我自定义了 Lambe 不使用系统自带的光照方法
//另外 在最后加了 alpha
#pragma surface surf Lambe alpha
sampler2D _MainTex
float _TransVal
struct Input {
float2 uv_MainTex
void surf (Input IN, inout SurfaceOutput o) {
half4 c = tex2D (_MainTex, IN.uv_MainTex)
o.Albedo = c.rgb
o.Emission = tex2D (_MainTex, IN.uv_MainTex)
o.Alpha = (c.r + c.g + c.b) *_TransVal
inline float4 LightingLambe(SurfaceOutput s, fixed3 lightDir, fixed atten)
return (0, 0, 0, 0)
FallBack "Diffuse"
在材质中选择该shader
然后选择贴图
贴图选择如下
在世界中的显示如下
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:376430次
积分:5791
积分:5791
排名:第4737名
原创:218篇
评论:57条
(1)(1)(3)(5)(13)(4)(1)(3)(7)(13)(1)(9)(11)(11)(7)(130)
(window.slotbydup = window.slotbydup || []).push({
id: '4740881',
container: s,
size: '200,200',
display: 'inlay-fix'unity透明shader实现的亦难问题? - 知乎47被浏览2417分享邀请回答0添加评论分享收藏感谢收起/question/51615956/answer/?utm_source=wechat_session&utm_medium=social&from=singlemessage总结一句就是尽量避免使用alpha blending,特别是交叉的物体。一般用取巧的方案实现。你要排序完全正确不是不可以,可是性能消耗高了,可是效果却不明显。0添加评论分享收藏感谢收起查看更多回答

我要回帖

更多关于 unity3d 半透明shader 的文章

 

随机推荐