Unity4 point sprite texture coordinates

In unity 4 now possible to draw a mesh as multiple dots.
If I create a mesh like this:

using UnityEngine;
    
    public class NewBehaviourScript : MonoBehaviour
    {
        public int number = 100;
    
    	[ContextMenu("crate mesh")]
    	void CrateMesh()
    	{
    	    if (number<=0)
    	    {
    	        return;
    	    }
    
    	    MeshFilter filter = GetComponent<MeshFilter>();
    
            Vector3[] vertexes = new Vector3[number];
            //Vector2[] texCoods = new Vector2[number];
            int[] indexes;
            Mesh mesh = new Mesh();
    
            //for (int i = 0; i < texCoods.Length; i++)//it's do nothing
            //    texCoods[i] = Vector2.one*0.5f;
    
    	    int vInd = 0;
    
            for (int x = 0; x < (int)Mathf.Sqrt(vertexes.Length); x++)
    	    {
                for (int y = 0; y < (int)Mathf.Sqrt(vertexes.Length); y++)
    	        {
                    vertexes[vInd] = new Vector3(x,0,y);
    	            ++vInd;
    	        }
    	    }
    
    
    	    indexes = new int[vertexes.Length];
    
    	    for (int i = 0; i < vertexes.Length; i++)
    	        indexes[i] = i;
    
    
    	    filter.mesh.Clear();
    
    	    mesh.vertices = vertexes;
    	    //mesh.uv = texCoods;
            mesh.SetIndices(indexes, MeshTopology.Points, 0);
    
    	    filter.mesh = mesh;
    	}
    }

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

Shader "Point sprite"
    {
    
    Properties
    {
    	_MainTex ("Texture Image", 2D) = "white" {}
    	_PointSize("PointSize", Float) = 10
    }
    
    SubShader
    	{
    		Pass
    		{
    			CGPROGRAM
    			#pragma vertex vert
    			#pragma fragment frag
    			#include "UnityCG.cginc"
    
    			struct v2f
    			{
    				float4 pos : SV_POSITION;
    				half size:PSIZE;
    				half2 texcoord : TEXCOORD0;
    			};
    
    			half _PointSize;
    			sampler2D _MainTex;
    
    			v2f vert (appdata_base v)
    			{
    				v2f o;
    				o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
    				o.size = _PointSize;
    				o.texcoord = v.texcoord;
    				return o;
    			}
    
    
    			half4 frag (v2f i) : COLOR
    			{
    				half4 c = tex2D(_MainTex,i.texcoord);
    				return c;
    			}
    
    			ENDCG
    
    		}
    	}
    }

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!

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.

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. >>

Not in Unity 4; you can do quads/points/lines.

–Eric

up

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.

I can’t get PSIZE to work on my mac, am I missing something?
Shader compiles, no warning, all points size 1.

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.

@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!

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.

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