Hi,
I want to have an object I select be in full opacity, while all other objects are 50% faded.
If I use a material that its Rendering Mode is fade, and apply it to all other objects, except for the one I selected, then I get transparent objects in which you can see the transparent objects that are behind them. I don’t want that. I want the transparent object to only show the background behind it, not any other transparent object.
From what I see this has to do with the rendering order of transparent objects. if you render the objects that are closest to the camera last, you will get this effect. But what ever I do to change the rendering order just will not work. What should I do to change the rendering order of the transparent objects?
I’m not looking for a way to do it manually, give every object a number. I need to make Unity show it in the order I want to, the closest to the camera should be last.
How can this be done?
I found 2 shaders that are showing only the front faces of an object, which is good for me as I don’t want to see the inner structure of an object when it is transparent, so it solved me one problem, which is how to hid back faces while in transparent mode, but I can still se the objects behind the transparent object and I only want to see the background behind it. Can anyone help me with that?
here are the 2 shaders:
Shader "Transparent/Diffuse ZWrite" {
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
_MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
}
SubShader {
Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
LOD 200
// extra pass that renders to depth buffer only
Pass {
ZWrite On
ColorMask 0
}
// paste in forward rendering passes from Transparent/Diffuse
UsePass "Transparent/Diffuse/FORWARD"
}
Fallback "Transparent/VertexLit"
}
here is the other one:
Shader "Unlit/BlendMode4_29"
{
Properties
{
_Color ("Color",Color) = (1,1,1,1)
_MainTex ("MainTex",2D) = "white"{}
_AlphaScale ("AlphaScale",Range(0,1)) = 0.5
}
SubShader
{
//渲染队列为透明度测试队列,忽视投影器对该物体的影响开启,渲染类型为TransparentCutout,可理解为子队列
Tags { "Queue"="Transparent""IgnoreProjector"="True""RenderType"="Transparent" }
/* */ Pass {
ZWrite On
ColorMask 0
}
Pass
{
Tags { "LightMode"="ForwardBase"}
ZWrite Off
//混合操作方式 各因子
BlendOp add
Blend SrcAlpha OneMinusSrcAlpha
//常见混合方式
//柔和相加
//Blend OneMinusDstColor One
//相乘
//Blend DstColor Zero
//两倍相乘
//Blend DstColor SrcColor
//变暗
//BlendOp Min
//变亮
//BlendOp Max
//Blend One One
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Lighting.cginc"
fixed4 _Color;
sampler2D _MainTex;
fixed _AlphaScale;
float4 _MainTex_ST;
struct a2v{
float4 vertex:POSITION;
float3 normal:NORMAL;
float4 texcoord:TEXCOORD0;
};
struct v2f{
float4 pos:SV_POSITION;
float3 worldNormal:TEXCOORD1;
float3 worldPos:TEXCOORD2;
float2 uv:TEXCOORD3; //纹理采样用的坐标
};
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.uv = v.texcoord.xy*_MainTex_ST.xy+_MainTex_ST.zw;
o.uv = TRANSFORM_TEX(v.texcoord,_MainTex);
return o;
}
fixed4 frag(v2f i):SV_Target{
fixed3 worldNormal = normalize(i.worldNormal);
fixed3 worldLightDir = normalize(UnityWorldSpaceLightDir(i.worldPos));
fixed4 TexColor = tex2D(_MainTex,i.uv);
fixed3 albedo = TexColor.rgb*_Color.rgb;
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz*albedo;
fixed3 diffuse = _LightColor0.rgb*saturate(dot(worldNormal,worldLightDir));
return fixed4(diffuse + ambient,TexColor.a*_AlphaScale);
}
ENDCG
}
}
FallBack "Transparent/Cutout/VertexLit" //透明渲染物体用这个
}