Basically brand new to shaders. Need to create a coloured, semi-transparent shader, that shows all sides of an object (no backface culling), however despite being transparent, objects will be opaque to any objects behind them, and mask objects further back. The object to be transparent to itself, but opaque to other objects in the scene (the OPPOSITE of what’s shown in the pic below, that is transparent globally, but opaque locally).
Shader “Custom/Self Transparent”
{
Properties
{
_Color (“Color”, Color) = (1,1,1,1)
_MainTex (“Albedo (RGB)”, 2D) = “white” {}
_Glossiness (“Smoothness”, Range(0,1)) = 0.5
_Metallic (“Metallic”, Range(0,1)) = 0.0
}
CGINCLUDE
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
sampler2D _MainTex;
struct Input
{
float2 uv_MainTex;
};
half _Glossiness;
half _Metallic;
fixed4 _Color;
// Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
// See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
// #pragma instancing_options assumeuniformscaling
UNITY_INSTANCING_BUFFER_START(Props)
// put more per-instance properties here
UNITY_INSTANCING_BUFFER_END(Props)
void vert (inout appdata_full v)
{
float3 op = mul (unity_WorldToObject, float4 (_WorldSpaceCameraPos.xyz, 1)).xyz;
float3 viewDir = v.vertex.xyz - op;
v.normal.xyz *= dot (viewDir, v.normal.xyz) > 0.0 ? -1.0 : 1.0;
}
void surf (Input IN, inout SurfaceOutputStandard o)
{
// Albedo comes from a texture tinted by color
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
// Metallic and smoothness come from slider variables
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
o.Alpha = c.a;
}
ENDCG
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 200
ZWrite On
Cull Front
//Optional to stop Z-fighting
//Offset -1,-10
CGPROGRAM
#pragma surface surf Standard vertex:vert noshadow
ENDCG
ZWrite Off
Cull Back
CGPROGRAM
#pragma surface surf Standard alpha:fade noshadow
ENDCG
}
FallBack "Diffuse"
}
The basic idea here is to split rendering into two sections; internal and external rendering. We render the inside first as opaque with depth-writing on to make sure nothing is drawn behind it. We also flip the normals so that they face the right direction. Then, we draw the front faces with depth-write off and alpha blending such that you can see the inside of the object like a normal semi-transparent shader. There is also a line commented out that can help fix z-fighting if your object’s internal faces are too close to those of another object.