Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Visibility with LineRenderer and planes

Discussion in 'Shaders' started by SimRuJ, Sep 20, 2018.

  1. SimRuJ

    SimRuJ

    Joined:
    Apr 7, 2016
    Posts:
    247
    I'm creating planes and LineRenderers at runtime and just can't get the transparency to work right with everything.

    My code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using UnityEngine;
    5.  
    6. public class TransparencyTester : MonoBehaviour {
    7.  
    8.    void Start () {
    9.         MakeLineRenderer();
    10.         MakePlane();
    11.    }
    12.  
    13.    private void MakeLineRenderer() {
    14.         GameObject o = new GameObject();
    15.         LineRenderer line = o.AddComponent<LineRenderer>().GetComponent<LineRenderer>();
    16.         Shader shader = Shader.Find("Hidden/Internal-Colored");
    17.         Material mat = new Material(shader) { color = Color.red };
    18.         line.material = mat;
    19.         line.startWidth = 0.3f;
    20.         line.endWidth = 0.3f;
    21.         Vector3[] v = new Vector3[] { new Vector3(-12,10,0),new Vector3(-10,10,0),new Vector3(-10,-5,0),new Vector3(10,-5,0),new Vector3(10,10,0),new Vector3(12,10,0) };
    22.         line.positionCount = v.Length;
    23.         line.SetPositions(v);
    24.     }
    25.  
    26.     private void MakePlane() {
    27.         GameObject o1 = new GameObject();
    28.         Mesh m1 = new Mesh();
    29.         Material mat1 = new Material(Shader.Find("Hidden/Internal-Colored")) { color = new Color(1,1,0,0.3f) };
    30.         if(o1.GetComponent<MeshFilter>()==null) { o1.AddComponent<MeshFilter>(); }
    31.         if(o1.GetComponent<MeshRenderer>()==null) { o1.AddComponent<MeshRenderer>(); }
    32.         o1.GetComponent<MeshFilter>().mesh = m1;
    33.         o1.GetComponent<MeshRenderer>().material = mat1;
    34.         Vector3[] v1 = new Vector3[] { new Vector3(-15,5,-15),new Vector3(-15,5,15),new Vector3(15,5,15), new Vector3(15,5,15),new Vector3(15,5,-15),new Vector3(-15,5,-15) };
    35.         m1.vertices = v1;
    36.         m1.triangles = Enumerable.Range(0,v1.Length).ToArray();
    37.  
    38.         GameObject o2 = new GameObject();
    39.         Mesh m2 = new Mesh();
    40.         Material mat2 = new Material(Shader.Find("Hidden/Internal-Colored")) { color = new Color(0,0,1,0.5f) };
    41.         if(o2.GetComponent<MeshFilter>()==null) { o2.AddComponent<MeshFilter>(); }
    42.         if(o2.GetComponent<MeshRenderer>()==null) { o2.AddComponent<MeshRenderer>(); }
    43.         o2.GetComponent<MeshFilter>().mesh = m2;
    44.         o2.GetComponent<MeshRenderer>().material = mat2;
    45.         Vector3[] v = new Vector3[] { new Vector3(-20,0,-20),new Vector3(-20,0,20),new Vector3(20,0,20),new Vector3(20,0,20),new Vector3(20,0,-20),new Vector3(-20,0,-20) };
    46.         m2.vertices = v;
    47.         m2.triangles = Enumerable.Range(0,v.Length).ToArray();
    48.     }
    49. }
    My scene from the side ("Standard" shader): click
    The transparency doesn't work at all and from below only the LineRenderer is visible.

    "Hidden/Internal-Colored" shader:
    From above: click
    From below: click
    Both planes use very similar vertices in the same order but how is it possible that the yellow plane is only transparent if you look from above (but not from below) and with the blue one it's the other way around?

    "Transparent/VertexLit" shader:

    From above: click
    From below: Only the LineRenderer is visible
    If fully opaque: click

    If both planes are transparent, I want to see the LineRenderer through both of them (like a mix between "Internal-Colored - From above" and "VertexLit - From above" but from both sides) but if at least one of the planes is completely opaque, I want the visibilty to adjust accordingly.
    Are there any shaders that do that or can I trick my way around it somehow (without doubling up on the planes)?
     
  2. SimRuJ

    SimRuJ

    Joined:
    Apr 7, 2016
    Posts:
    247
    I managed to fix parts of it by making the LineRenderer use the "Unlit/Color" shader (not standard because of the Fresnel effect and this one just seemed more light-weight anyway). I'm still dealing with problems with the visability of the planes but I'm going to open a new thread for that with the according infos.