Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Point Sprite automatic texture coords

Discussion in 'Shaders' started by Blubberfisch, Mar 31, 2013.

  1. Blubberfisch

    Blubberfisch

    Joined:
    Mar 31, 2013
    Posts:
    5
    I'm rendering points (not triangles or quads) 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?

    (Note: There is already a discussion in the Scripting forum about this topic, but I think it is more likely to get an answer in the Shaders forum ;) )

    Point rendering seems to be badly documented, so for anyone interested:
    Set your Mesh to MeshTopology.Points mode using the SetIndices method. Then use a shader like this which writes the point size:
    Code (csharp):
    1. Shader "Custom/Starfield" {
    2.     Properties {
    3.     }
    4.     SubShader {
    5.         Tags { "RenderType"="Opaque" }
    6.         LOD 200
    7.         Pass {
    8.             CGPROGRAM
    9.                 #pragma exclude_renderers flash
    10.                 #pragma vertex vert
    11.                 #pragma fragment frag
    12.                
    13.                 struct appdata {
    14.                     float4 pos : POSITION;
    15.                 };
    16.                
    17.                 struct v2f {
    18.                     float4 pos : SV_POSITION;
    19.                     float size : PSIZE;
    20.                 };
    21.    
    22.                 v2f vert(appdata v)
    23.                 {
    24.                     v2f o;
    25.                     o.pos = mul(UNITY_MATRIX_MVP, v.pos);
    26.                     o.size = 10.0;
    27.                     return o;
    28.                 }
    29.    
    30.                 half4 frag(v2f i) : COLOR
    31.                 {
    32.                     return half4(1,0,0,1);
    33.                 }
    34.             ENDCG
    35.         }
    36.     }
    37. }
     
    Last edited: Mar 31, 2013
  2. Alex_May

    Alex_May

    Joined:
    Dec 29, 2013
    Posts:
    198
    I've been looking into this outside of Unity. In OpenGL and Cg, I'm doing this:

    Code (csharp):
    1.  
    2.             glEnable(GL_POINT_SPRITE_ARB);
    3.             glEnable(GL_PROGRAM_POINT_SIZE_EXT);
    4.             CgGL.SetStateMatrixParameter(mParticleModelViewProj, cgGLModelviewProjectionMatrix, cgGLMatrixIdentity);
    5.             mParticleShader.Activate();
    6.             cgGLSetTextureParameter(mParticleTexture, TextureId);
    7.             cgGLEnableTextureParameter(mParticleTexture);
    8.             glTexEnvi(GL_POINT_SPRITE, GL_COORD_REPLACE, GL_TRUE);
    9.             glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
    10.             mParticleFragShader.Activate();
    11.             mParticles.Render(true);
    12.  
    (I have shader classes etc that handle some things but the gl and cggl calls are what's important here)

    vertex shader
    Code (csharp):
    1. struct output{
    2.     float4 position : POSITION;
    3.     float4 color : COLOR;
    4.     float  size      : PSIZE;
    5. };
    6. struct input {
    7.     float4 position: POSITION;
    8.     float4 color : COLOR;
    9. };
    10.  
    11.  
    12. output main(input IN, uniform float4x4 ModelViewProj)
    13. {
    14.    
    15.     output OUT;
    16.  
    17.     OUT.position = mul(ModelViewProj, IN.position);
    18.     OUT.size = 500.0f / OUT.position.w;
    19.     OUT.color = IN.color;
    20.  
    21.     return OUT;
    22. }
    fragment shader
    Code (csharp):
    1. struct output
    2. {
    3.     float4 color : COLOR;
    4. };
    5.  
    6. struct input
    7. {
    8.     float4 color : COLOR;
    9.     float2 texCoord : TEXCOORD0;
    10. };
    11.  
    12. output main(input IN, uniform sampler2D tex)
    13. {
    14.  
    15.   output OUT;
    16.  
    17.   OUT.color = IN.color * tex2D(tex, IN.texCoord.xy);
    18.  
    19.   return OUT;
    20.  
    21. }
    Hope that helps.
     
  3. Blubberfisch

    Blubberfisch

    Joined:
    Mar 31, 2013
    Posts:
    5
    Thanks for your effort, but as I would like to do this inside Unity it's not helping :|