Search Unity

Very basic question

Discussion in 'Shaders' started by MJonee, Jan 5, 2020.

  1. MJonee

    MJonee

    Joined:
    Oct 19, 2013
    Posts:
    20
    Hi, I'm having trouble understanding a very simple shader(I modified the standard Unlit shader):

    v2f vert (appdata v)
    {
    v2f o;
    v.vertex += v.normal * 0.5;
    o.vertex = UnityObjectToClipPos(v.vertex);
    o.normal = v.normal;
    return o;
    }

    fixed4 frag (v2f i) : SV_Target
    {
    return i.normal;
    }

    If I apply it to a simple cube, this is what I get:


    My question is: if the cube has 8 vertices and I'm only moving the vertices, why do I get 6 faces with 4 vertices each?
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    Because a cube doesn’t have 8 vertices, it has 24. At least that cube has 24 vertices.

    The reason is the way meshes work for real time rendering is the vertices hold one set of unique data. One position, one normal, and one UV coordinate (per UV set). The mesh is then made of triangles that reference 3 of those vertices. If the data can be shared, then multiple triangles can share the same vertices. If you need unique data, you need another vertex. In the case of a cube like this, each side has unique normals (and UVs). So each side needs 4 vertices with the appropriate normals. 4 vertices for 6 sides means 24 total vertices.

    “But it’s only 8 vertices in my 3D modeling program.”
    Modeling tools are able to store multiple sets of data “per vertex”, or even per face. They have much more complex representations of the mesh data. But all of those tools will end up having to break the mesh down to the simpler representation for the real time preview. It might even be doing it for non real time rendering.
     
  3. MJonee

    MJonee

    Joined:
    Oct 19, 2013
    Posts:
    20
    That makes a lot of sense. Thanks!