Search Unity

Shadermaterial not showing - only on other gameobjects

Discussion in 'AR/VR (XR) Discussion' started by peter_the_coder, Jun 13, 2018.

  1. peter_the_coder

    peter_the_coder

    Joined:
    Nov 28, 2017
    Posts:
    11
    I tried the VR Sample Scene with the LWR - pipline.
    I have added a script where I add a mesh, but it is only showing on the other materials but not on the "ground" or in the "air"

    As we are all new to the LWR.. I hope someone can help me here

    ONLY the color behind objects is showing !

    this is it.. how it looks...

    upload_2018-6-13_0-59-4.png

    you can see, the navmesh border I added via script.. bu it tis only displayed when there are other gameobjects in front.

    what do I have to change to make it show up ?

    this is the shader
    upload_2018-6-13_1-4-20.png

    this is the code of the script which is from github user: FlaFla2
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. /// \brief A generic component that renders a border using the given polylines.  The borders are double sided and are oriented
    5. ///        upwards (ie normals are parallel to the XZ plane)
    6. [AddComponentMenu("Vive Teleporter/Border Renderer")]
    7. [ExecuteInEditMode]
    8. public class BorderRenderer : MonoBehaviour {
    9.     private Mesh[] CachedMeshes;
    10.     /// Material used to render the border mesh.  Note: UVs are set up so that v=0->bottom and v=1->top of border
    11.     [Tooltip("Material used to render the border mesh.  UV's are set up so that v=0->bottom and v=1->top.  u is stretched along each edge.")]
    12.     public Material BorderMaterial;
    13.  
    14.     [System.NonSerialized]
    15.     public Matrix4x4 Transpose = Matrix4x4.identity;
    16.  
    17.     [SerializeField] [Range(0,1)]
    18.     [Tooltip("Alpha (transparency) of the border mesh.")]
    19.     public float BorderAlpha = 1.0f;
    20.     private float LastBorderAlpha = 1.0f;
    21.  
    22.     [Tooltip("Layer to render the mesh at.")]
    23.     private int AlphaShaderID = -1;
    24.  
    25.     /// Polylines that will be drawn.
    26.     public BorderPointSet[] Points {
    27.         get
    28.         {
    29.             return _Points;
    30.         }
    31.         set
    32.         {
    33.             _Points = value;
    34.             RegenerateMesh();
    35.         }
    36.     }
    37.     private BorderPointSet[] _Points;
    38.  
    39.     public float BorderHeight
    40.     {
    41.         get
    42.         {
    43.             return _BorderHeight;
    44.         }
    45.         set
    46.         {
    47.             _BorderHeight = value;
    48.             RegenerateMesh();
    49.         }
    50.     }
    51.     [SerializeField]
    52.     [Tooltip("Height of the border mesh, in meters.")]
    53.     private float _BorderHeight = 0.2f;
    54.  
    55.     void Update()
    56.     {
    57.         if (CachedMeshes == null || BorderMaterial == null)
    58.             return;
    59.  
    60.         if (LastBorderAlpha != BorderAlpha && BorderMaterial != null)
    61.         {
    62.             BorderMaterial.SetFloat("_Alpha", BorderAlpha);
    63.             LastBorderAlpha = BorderAlpha;
    64.         }
    65.  
    66.         foreach (Mesh m in CachedMeshes)
    67.             Graphics.DrawMesh(m, Transpose, BorderMaterial, gameObject.layer, null, 0, null, false, false);
    68.     }
    69.  
    70.     void OnValidate()
    71.     {
    72.         RegenerateMesh();
    73.  
    74.         if (AlphaShaderID == -1)
    75.             AlphaShaderID = Shader.PropertyToID("_Alpha");
    76.         if(BorderMaterial != null)
    77.             BorderMaterial.SetFloat(AlphaShaderID, BorderAlpha);
    78.     }
    79.  
    80.     public void RegenerateMesh()
    81.     {
    82.         if (Points == null)
    83.         {
    84.             CachedMeshes = new Mesh[0];
    85.             return;
    86.         }
    87.         CachedMeshes = new Mesh[Points.Length];
    88.         for (int x = 0; x < CachedMeshes.Length; x++)
    89.         {
    90.             if (Points[x] == null || Points[x].Points == null)
    91.                 CachedMeshes[x] = new Mesh();
    92.             else
    93.                 CachedMeshes[x] = GenerateMeshForPoints(Points[x].Points);
    94.         }
    95.     }
    96.  
    97.     private Mesh GenerateMeshForPoints(Vector3[] Points)
    98.     {
    99.         if (Points.Length <= 1)
    100.             return new Mesh();
    101.  
    102.         Vector3[] verts = new Vector3[Points.Length * 2];
    103.         Vector2[] uv = new Vector2[Points.Length * 2];
    104.         for(int x=0;x<Points.Length;x++)
    105.         {
    106.             verts[2 * x] = Points[x];
    107.             verts[2 * x + 1] = Points[x] + Vector3.up * BorderHeight;
    108.  
    109.             uv[2 * x] = new Vector2(x % 2, 0);
    110.             uv[2 * x + 1] = new Vector2(x % 2, 1);
    111.         }
    112.  
    113.         int[] indices = new int[2 * 3 * (verts.Length - 2)];
    114.         for(int x=0;x<verts.Length/2-1;x++)
    115.         {
    116.             int p1 = 2*x;
    117.             int p2 = 2*x + 1;
    118.             int p3 = 2*x + 2;
    119.             int p4 = 2*x + 3;
    120.  
    121.             indices[12 * x] = p1;
    122.             indices[12 * x + 1] = p2;
    123.             indices[12 * x + 2] = p3;
    124.             indices[12 * x + 3] = p3;
    125.             indices[12 * x + 4] = p2;
    126.             indices[12 * x + 5] = p4;
    127.  
    128.             indices[12 * x + 6] = p3;
    129.             indices[12 * x + 7] = p2;
    130.             indices[12 * x + 8] = p1;
    131.             indices[12 * x + 9] = p4;
    132.             indices[12 * x + 10] = p2;
    133.             indices[12 * x + 11] = p3;
    134.         }
    135.  
    136.         Mesh m = new Mesh();
    137.         m.vertices = verts;
    138.         m.uv = uv;
    139.         m.triangles = indices;
    140.         m.RecalculateBounds();
    141.         m.RecalculateNormals();
    142.         return m;
    143.     }
    144. }
     
    Last edited: Jun 13, 2018