Search Unity

Shader disregards UV coordinates

Discussion in 'Shaders' started by qorthos, Apr 13, 2014.

  1. qorthos

    qorthos

    Joined:
    Apr 13, 2014
    Posts:
    6
    Hello!

    I've been playing around with unity for the past week (it's amazing, I like this editor) and am very impressed. I've even created a custom map editor setting atlased tiles on a mesh. The simple fact that I can extend an EditorWindow as easily as I did is awesome.

    I've used a wizard to create a mesh and my map layers into a group of objects. The mesh itself looks okay, but the UV data gets lost somewhere between meshing and the surface shader. My first attempt to debug this was to simply output the UV's in the mesh shader:

    Code (csharp):
    1.  
    2. Shader "Mapping/MapShader"
    3. {
    4.     Properties
    5.     {
    6.         _Atlas ("Atlas Texture (RGB)", 2D) = "white" {}
    7.     }
    8.  
    9.     SubShader
    10.     {
    11.         Tags { "RenderType"="Opaque" } 
    12.         CGPROGRAM
    13.         #pragma surface surf NoLighting
    14.        
    15.         fixed4 LightingNoLighting(SurfaceOutput s, fixed3 lightDir, fixed atten)
    16.         {
    17.             fixed4 c;
    18.             c.rgb = s.Albedo;
    19.             c.a = s.Alpha;
    20.             return c;
    21.         }
    22.  
    23.  
    24.         sampler2D _Atlas;
    25.  
    26.         struct Input
    27.         {
    28.             float2 uv_MainTex;
    29.         };
    30.  
    31.         void surf (Input input, inout SurfaceOutput o)
    32.         {
    33.             //float4 tex = tex2D (_Atlas, input.uv_MainTex);           
    34.             //o.Albedo = tex.rgb;
    35.            
    36.             o.Albedo = float3(input.uv_MainTex.r, input.uv_MainTex.g, 0.0f);
    37.         }
    38.         ENDCG
    39.  
    40.     }
    41.  
    42.     Fallback "Diffuse"
    43. }
    44.  
    45.  
    The mesh stays completely black. Then I tried setting every UV to 1.0f.

    I'm creating my mesh like this:
    Code (csharp):
    1.  
    2. Mesh BuildMesh(MapLayer layer, Mesh mesh, float ht)
    3.     {
    4.         int vertexCount = MapData.Width * MapData.Height * 4;
    5.         int indexCount  = MapData.Width * MapData.Height * 6;
    6.  
    7.         // CREATE VERTICES
    8.         Vector3[]   vertices    = new Vector3[vertexCount];
    9.         Vector2[]   uvs         = new Vector2[vertexCount];
    10.         Color[]     colors      = new Color[vertexCount];
    11.         int[]       indices     = new int[indexCount];
    12.  
    13.         int         vCount  = 0;
    14.         int         iCount  = 0;
    15.         Vector3     start   = Vector3.zero;
    16.         Color       color   = new Color(255, 255, 255, 255);
    17.  
    18.  
    19.         for (int k = 0; k < MapData.Height; k++)
    20.         {
    21.             for (int j = 0; j < MapData.Width; j++)
    22.             {
    23.                 vertices[vCount + 0] = start + new Vector3(j + 0, k + 0);
    24.                 vertices[vCount + 1] = start + new Vector3(j + 1, k + 0);
    25.                 vertices[vCount + 2] = start + new Vector3(j + 1, k + 1);
    26.                 vertices[vCount + 3] = start + new Vector3(j + 0, k + 1);
    27.  
    28.                 uvs[vCount + 0] = new Vector2(1.0f, 1.0f);
    29.                 uvs[vCount + 1] = new Vector2(1.0f, 1.0f);
    30.                 uvs[vCount + 2] = new Vector2(1.0f, 1.0f);
    31.                 uvs[vCount + 3] = new Vector2(1.0f, 1.0f);
    32.  
    33.                 colors[vCount + 0] = color;
    34.                 colors[vCount + 1] = color;
    35.                 colors[vCount + 2] = color;
    36.                 colors[vCount + 3] = color;
    37.  
    38.                 indices[iCount + 0] = (short)(vCount + 0);
    39.                 indices[iCount + 1] = (short)(vCount + 2);
    40.                 indices[iCount + 2] = (short)(vCount + 1);
    41.  
    42.                 indices[iCount + 3] = (short)(vCount + 0);
    43.                 indices[iCount + 4] = (short)(vCount + 3);
    44.                 indices[iCount + 5] = (short)(vCount + 2);
    45.  
    46.                 vCount += 4;
    47.                 iCount += 6;
    48.             }
    49.         }
    50.  
    51.         mesh.Clear();
    52.         mesh.vertices = vertices;
    53.         mesh.triangles = indices;
    54.         mesh.uv = uvs;
    55.            
    56.         mesh.Optimize();
    57.         mesh.RecalculateNormals();
    58.  
    59.         Debug.Log(mesh.uv[0].ToString());
    60.         Debug.Log(mesh.uv[1].ToString());
    61.         Debug.Log(mesh.uv[2].ToString());
    62.         Debug.Log(mesh.uv[3].ToString());
    63.  
    64.         return mesh;
    65.     }
    66.  
    67.  
    Still black. Checking the debug log, my UVs are DEFINITELY being set to 1.0f.

    Any ideas?
     
  2. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    Your texture is called _Atlas.

    You're trying to get the UV coordinates for a texture called _MainTex...
     
  3. qorthos

    qorthos

    Joined:
    Apr 13, 2014
    Posts:
    6
    That did it! Thank you very much.