I'm trying to draw a voronoi diagram using a shader. I do this by creating a collection of quads, each centred at a point in the diagram. The shader below uses the the depth buffer to turn each quad into a cone, which creates the voronoi effect. That much works but what I want to do next is to colour each voronoi cell by sampling the colour of a texture at that point in space. I tried doing this by setting gl_FrontColor in the vertex shader but the gl_Color variable in the fragment shader is interpolated between the four vertices of the quad which is not what I want. I want the whole cell to be flat shaded based on the colour of the centre point. Ideally, I'd be using a point sprite for this but I understand they aren't working correctly in current versions of Unity (or has this been fixed?) Any suggestions how to solve this problem? Code (csharp): Shader "Custom/Voronoi" { Properties { _MainTex ("RGBA Texture Image", 2D) = "white" {} } SubShader { Pass { Cull Off GLSLPROGRAM uniform sampler2D _MainTex; #ifdef VERTEX void main() { gl_TexCoord[0] = gl_MultiTexCoord0; gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; // sample the texture at the vertex location gl_FrontColor = texture2D(_MainTex, gl_Vertex.xy); } #endif #ifdef FRAGMENT void main() { // find the distance to the middle of the quad vec2 v = 2. * gl_TexCoord[0].xy - 1.; float dist = length(v); // make the quad circular by discarding // fragments more that 1 away from the centre if (dist > 1.) { discard; } // make a cone by setting the depth equal to the distance gl_FragDepth = dist; // set the colour -- this doesn't work how I want gl_FragColor = gl_Color; } #endif ENDGLSL } } }
My hacky solution: set the normals of the quad to point towards the centre point. Then do Code (csharp): #ifdef VERTEX void main() { gl_TexCoord[0] = gl_MultiTexCoord0; gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; // sample the texture at the centre of the quad vec2 st = gl_Vertex.xy + gl_Normal.xy; gl_FrontColor = texture2D(_MainTex, st); } #endif That way all four vertices end up with the same colour, so interpolation has no effect.