Search Unity

Unity4 point sprite texture coordinates

Discussion in 'Scripting' started by mouurusai, Nov 17, 2012.

  1. mouurusai

    mouurusai

    Joined:
    Dec 2, 2011
    Posts:
    350
    In unity 4 now possible to draw a mesh as multiple dots.
    If I create a mesh like this:

    Code (csharp):
    1. using UnityEngine;
    2.    
    3.     public class NewBehaviourScript : MonoBehaviour
    4.     {
    5.         public int number = 100;
    6.    
    7.         [ContextMenu("crate mesh")]
    8.         void CrateMesh()
    9.         {
    10.             if (number<=0)
    11.             {
    12.                 return;
    13.             }
    14.    
    15.             MeshFilter filter = GetComponent<MeshFilter>();
    16.    
    17.             Vector3[] vertexes = new Vector3[number];
    18.             //Vector2[] texCoods = new Vector2[number];
    19.             int[] indexes;
    20.             Mesh mesh = new Mesh();
    21.    
    22.             //for (int i = 0; i < texCoods.Length; i++)//it's do nothing
    23.             //    texCoods[i] = Vector2.one*0.5f;
    24.    
    25.             int vInd = 0;
    26.    
    27.             for (int x = 0; x < (int)Mathf.Sqrt(vertexes.Length); x++)
    28.             {
    29.                 for (int y = 0; y < (int)Mathf.Sqrt(vertexes.Length); y++)
    30.                 {
    31.                     vertexes[vInd] = new Vector3(x,0,y);
    32.                     ++vInd;
    33.                 }
    34.             }
    35.    
    36.    
    37.             indexes = new int[vertexes.Length];
    38.    
    39.             for (int i = 0; i < vertexes.Length; i++)
    40.                 indexes[i] = i;
    41.    
    42.    
    43.             filter.mesh.Clear();
    44.    
    45.             mesh.vertices = vertexes;
    46.             //mesh.uv = texCoods;
    47.             mesh.SetIndices(indexes, MeshTopology.Points, 0);
    48.    
    49.             filter.mesh = mesh;
    50.         }
    51.     }

    And then render it with shader, which is used semantics PSIZE to size:

    Code (csharp):
    1. Shader "Point sprite"
    2.     {
    3.    
    4.     Properties
    5.     {
    6.         _MainTex ("Texture Image", 2D) = "white" {}
    7.         _PointSize("PointSize", Float) = 10
    8.     }
    9.    
    10.     SubShader
    11.         {
    12.             Pass
    13.             {
    14.                 CGPROGRAM
    15.                 #pragma vertex vert
    16.                 #pragma fragment frag
    17.                 #include "UnityCG.cginc"
    18.    
    19.                 struct v2f
    20.                 {
    21.                     float4 pos : SV_POSITION;
    22.                     half size:PSIZE;
    23.                     half2 texcoord : TEXCOORD0;
    24.                 };
    25.    
    26.                 half _PointSize;
    27.                 sampler2D _MainTex;
    28.    
    29.                 v2f vert (appdata_base v)
    30.                 {
    31.                     v2f o;
    32.                     o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
    33.                     o.size = _PointSize;
    34.                     o.texcoord = v.texcoord;
    35.                     return o;
    36.                 }
    37.    
    38.    
    39.                 half4 frag (v2f i) : COLOR
    40.                 {
    41.                     half4 c = tex2D(_MainTex,i.texcoord);
    42.                     return c;
    43.                 }
    44.    
    45.                 ENDCG
    46.    
    47.             }
    48.         }
    49.     }
    I get the point sprites, but they do not have the texture coordinates.


    What am I doing wrong, how to assign texture coordinates for the mesh, or calculate them in the shader?

    English is not my native language, sorry for the mistakes.
    Thank you!
     
    Last edited: Nov 17, 2012
  2. imaginaryhuman

    imaginaryhuman

    Joined:
    Mar 21, 2010
    Posts:
    5,834
    Just guessing, is it required that you call mesh.Triangles= in order to set the indices? I'm not familiar with the SetIndices thing or how it's different, but a mesh creation usually ends with setting triangles.
     
  3. mouurusai

    mouurusai

    Joined:
    Dec 2, 2011
    Posts:
    350
    Each point in the indices is the square on the screen. The scripting reference said the following:<<SetTriangles and ::triangles always make the mesh be composed of triangles faces. Using SetIndices you can create a mesh that's made out of lines or points, see MeshTopology enum. >>
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Not in Unity 4; you can do quads/points/lines.

    --Eric
     
  5. mouurusai

    mouurusai

    Joined:
    Dec 2, 2011
    Posts:
    350
  6. Blubberfisch

    Blubberfisch

    Joined:
    Mar 31, 2013
    Posts:
    5
    Ok, I have the same problem. I'm rendering points and would like to use texture coordinates automatically generated by the hardware after the vertex shader. In OpenGL/GLSL one would simply use the build-in fragment shader input gl_PointCoord. What is the Unity/Cg equivalent of that?

    Edit: I have created a new thread in the shader forum. I think it's more likely to get an answer there.
     
    Last edited: Mar 31, 2013
  7. rsodre

    rsodre

    Joined:
    May 9, 2012
    Posts:
    229
    I can't get PSIZE to work on my mac, am I missing something?
    Shader compiles, no warning, all points size 1.
     
    Last edited: May 24, 2013
  8. Jason Steel

    Jason Steel

    Joined:
    Dec 23, 2012
    Posts:
    5
    Did you ever specify UV coordinates for the vertices? I don't see it in your code...

    There should be another array of Vector2[] assigned to mesh.uv, and filled with appropriate texture coordinates.

    I'd definitely like to see if this works.
     
  9. stefann

    stefann

    Joined:
    Jan 4, 2011
    Posts:
    17
    @rsodre: did you figure out the problem? i'm also on a mac and no matter what i set for PSIZE, the sprites are the same size!
     
  10. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    I already reported this bug, unity know about it. This is usually some form of driver issue: with point sprites you cannot guarantee they are able to be bigger than a single pixel on a given driver and gpu.

    You should use your own quad based system in situations like that. There's no guarantee it will be fixed in a way that will work as expected on all platforms.
     
  11. stefann

    stefann

    Joined:
    Jan 4, 2011
    Posts:
    17
    Hi hippocoder,
    thanks for the answer. What do you mean with quad based system? Are you suggesting to make a mesh built out of quads?
    I'm quite new to shaders, so if you could get into a bit more detail, I would highly appreciate it!
    Thanks