Search Unity

Point Sprite

Discussion in 'Shaders' started by josericardojr, May 14, 2020.

  1. josericardojr

    josericardojr

    Joined:
    Dec 9, 2016
    Posts:
    6
    Hi everyone,

    Actually I'm doing some sort of particle simulation using DirectCompute. In order to use the same buffer for both rendering and processing, I'm using the command Graphics.DrawProcedural(...).

    However, I'm facing a problem. I have a HLSL code that can render a sphere from a point. For this to work, the Texture Coordinate must be generated automatically (setting D3DRS_POINTSPRITEENABLE = true). I'm lost where I issue this command using Unity. I've tried to submit a texture coordinate for each vertex but they are not interpolated in the pixel shader. Here is my code:

    Code (CSharp):
    1. Shader "Custom/Point"
    2. {
    3.     Properties
    4.     {
    5.         _PointSize("Point Size", Float) = 5.0
    6.         _Radius("Radius", Float) = 5.0
    7.     }
    8.     SubShader {
    9.  
    10.         Pass {
    11.             Tags { "RenderType"="Opaque" }
    12.             LOD 200
    13.  
    14.  
    15.             CGPROGRAM
    16.             #pragma vertex vert
    17.             #pragma fragment frag
    18.             #include "UnityCG.cginc"
    19.  
    20.  
    21.             uniform float _PointSize = 5.0;
    22.             uniform float _Radius = 5.0;
    23.  
    24.  
    25.             StructuredBuffer<float4> positions;
    26.             StructuredBuffer<float4> colors;
    27.             StructuredBuffer<float2> texCoord;
    28.            
    29.  
    30.             struct VertexOutput {
    31.                 float4 pos : SV_POSITION;
    32.                 float4 col : COLOR;
    33.                 float size : PSIZE;
    34.                 float2 texCoord : TEXCOORD0;
    35.             };
    36.  
    37.  
    38.  
    39.             VertexOutput vert(uint id : SV_VertexID, uint inst : SV_InstanceID)
    40.             {
    41.                 VertexOutput o;
    42.                 float3 p = positions[id];
    43.                 float dist = length(posEye);
    44.                 o.size =_Radius * (_PointSize / dist);
    45.                 o.pos = UnityObjectToClipPos(p);
    46.                
    47.                 o.col = colors[id];
    48.                 o.texCoord = texCoord[id];
    49.                
    50.  
    51.                 return o;
    52.             }
    53.  
    54.             float4 frag(VertexOutput o) : SV_Target
    55.             {
    56.                 return float4(o.texCoord.xy, 0, 1);
    57.             }
    58.             ENDCG
    59.         }
    60.     }
    61.     FallBack "Diffuse"
    62. }
    63.  
    I'm just output the texcoord as a color but they are all black.
    Please, do you have any ideia how to perform it?

    Thanks.