The two triangles in the image below are part of the same mesh. What I would like, is for the transparency of the triangle in front to "overwrite" the triangle in the back in order to see the sphere behind. Essentially, where the two triangles clip will be completely transparent, but only when the triangle in front is transparent: Code (CSharp): Shader "Custom/TransparencyTest" { Properties { _Color ("Color", Color) = (1,1,1,1) } SubShader { Tags { "Queue" = "Transparent" "RenderType" = "Transparent" } LOD 200 ZWrite Off CGPROGRAM // Physically based Standard lighting model, and enable shadows on all light types #pragma surface surf Standard fullforwardshadows alpha:fade // Use shader model 3.0 target, to get nicer looking lighting #pragma target 3.0 sampler2D _MainTex; struct Input { float2 uv_MainTex; float4 color : COLOR; }; 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 surf (Input IN, inout SurfaceOutputStandard o) { // Albedo comes from a texture tinted by color fixed4 c = _Color; o.Albedo = c.rgb; o.Alpha = IN.color.r; } ENDCG } FallBack "Diffuse" } Code (CSharp): public class transparencyTest : MonoBehaviour { public float trans_1 = 0.5f; public float trans_2 = 1; // Use this for initialization void Start () { Vector3[] verts = new Vector3[] { new Vector3(0,0,0), new Vector3(0.5f, 1, 0), new Vector3(1, 0, 0), new Vector3(0,0, 1), new Vector3(0.5f, 1, 1), new Vector3(1, 0, 1), }; int[] tris = new int[] { 0, 1, 2, 3, 4, 5 }; Color[] data = new Color[] { new Color(trans_1,0, 0), new Color(trans_1,0, 0), new Color(trans_1,0, 0), new Color(trans_2,0, 0), new Color(trans_2,0, 0), new Color(trans_2,0, 0), }; //uv = new Vector2[0]; MeshFilter filter = GetComponent<MeshFilter>(); Mesh mesh = new Mesh(); mesh.vertices = verts; mesh.colors = data; mesh.triangles = tris; filter.sharedMesh = mesh; } // Update is called once per frame void Update () { Color[] data = new Color[] { new Color(trans_1,0, 0), new Color(trans_1,0, 0), new Color(trans_1,0, 0), new Color(trans_2,0, 0), new Color(trans_2,0, 0), new Color(trans_2,0, 0), }; MeshFilter filter = GetComponent<MeshFilter>(); Mesh mesh = filter.sharedMesh; mesh.colors = data; filter.sharedMesh = mesh; } }
If nothing else needs to render “behind” the triangles, you could use a z depth pre pass. Add this before the CGPROGRAM line: Code (CSharp): Pass { ColorMask 0 ZWrite On }
I should have elaborated on that more. By "behind" I mean if nothing else that is in the transparent queue and could potentially get rendered after the triangles when using the default back to front transparency sorting, like a particle effect that has a large bounds, needs to render behind the triangle... Try it and see if it works well for you. The alternative would require you use stencils and two separate meshes, along with forcing the closer triangle to render first via the shader/material queue or the renderer's sorting order. Or if the front shape really is always going to be a triangle you could do in-shader raytracing/raycasting and clip the visible triangle based on a virtual triangle.
What I'm trying to do is hide cave walls. You see, the first triangle represents the backside of another triangle, flipped the opposite way. The second triangle represents the front side of a cave wall. I want the "backside" triangles to hide any front side triangles behind it by making them transparent.
Code (CSharp): Shader "Custom/TransparencyTest" { Properties { _Color ("Color", Color) = (1,1,1,1) } SubShader { Tags { "Queue" = "Transparent" "RenderType" = "Transparent" } LOD 200 Pass { ColorMask 0 ZWrite On Cull Front } CGPROGRAM // Physically based Standard lighting model, and enable shadows on all light types #pragma surface surf Standard fullforwardshadows alpha:fade // Use shader model 3.0 target, to get nicer looking lighting #pragma target 3.0 sampler2D _MainTex; struct Input { float2 uv_MainTex; float4 color : COLOR; }; 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 surf (Input IN, inout SurfaceOutputStandard o) { // Albedo comes from a texture tinted by color fixed4 c = _Color; o.Albedo = c.rgb; o.Alpha = IN.color.r; } ENDCG } FallBack "Diffuse" }
I mean, I copied the shader you posted, slapped it on an inverted cone mesh I had from another project, done?
Ah, now It's working. So it makes any triangle behind a back side of a triangle transparent. brilliant! Thanks.