how to draw polygon by point in Unity Unlit shader HLSL
- User
how to draw polygon by point in Unity Unlit shader HLSL
- Assistant
To draw a polygon using its points in Unity Unlit shader using HLSL, you can follow these steps:
-
Create a new Unlit Shader in your Unity project by right-clicking in your project window, then selecting "Create > Shader > Unlit Shader".
-
Rename the new shader file, for example, "PolygonShader". Open the shader file in your preferred code editor.
-
Remove the existing "pragma" lines and replace them with the following lines to include HLSL support:
#pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" #include "HLSLSupport.cginc"- Add a
float2[]array inside theUnityPerDrawblock to store the polygon points. Change the array size based on the number of points in your polygon. For example, for a pentagon:
struct UnityPerDraw { float4 _MainTex_ST; float4x4 _WorldToLocal; float2 _PolygonPoints[5]; // For a pentagon };- Declare a structure for the vertex shader's output:
struct VertexOutput { float2 uv : TEXCOORD0; float4 vertex : SV_POSITION; };- Create a vertex shader to pass the texture coordinates (UV) from the input to the output:
VertexOutput vert(appdata_full v) { VertexOutput o; o.vertex = UnityObjectToClipPos(v.vertex); o.uv = v.texcoord.xy; return o; }- Create a function to calculate the signed angle between two vectors:
float SignedAngle(float2 a, float2 b) { float angle = atan2(a.x * b.y - a.y * b.x, dot(a, b)); return angle; }- Create the main fragment shader function to check if a point is inside the polygon:
fixed4 frag(VertexOutput input) : COLOR { float2 uv = input.uv * _MainTex_ST.xy + _MainTex_ST.zw; // Convert the UV from [0,1] to [-1,1] range uv = uv * 2 - 1; // Initialize a variable to count the vertex angles float angleSum = 0; for (int i = 0; i < 5; i++) { // 5 is the number of polygon points int j = (i + 1) % 5; float2 v0 = (_PolygonPoints[i] - uv) / length(_PolygonPoints[i] - uv); float2 v1 = (_PolygonPoints[j] - uv) / length(_PolygonPoints[j] - uv); angleSum += SignedAngle(v0, v1); } // Check if angle sum is close to (2 * PI) to determine if the point is inside the polygon bool inside = abs(angleSum - 6.2831852) <= 0.001; // If inside, return the desired color, otherwise return a transparent color return inside ? fixed4(1, 1, 1, 1) : fixed4(0, 0, 0, 0); }-
Finally, replace the number
5in theforloop with the number of vertices in your polygon. -
Save the shader, create a new "Material" in your Unity project, and assign your new shader to the Material. Then apply the Material to a Quad or any other object.
-
To assign the polygon points, you need to use a script. Create a new script for this purpose, and assign the
float2[]array of polygon points to the_PolygonPointsshader property.
Here's an example of how to set the points of a simple pentagon in C# script:
using UnityEngine; public class PolygonShaderUpdater : MonoBehaviour { public Material PolygonMaterial; void Start() { // Set the center of the polygon to the origin (0, 0) Vector2[] points = { new Vector2(0, 1), new Vector2(-0.9510565, 0.309016994), new Vector2(-0.5877853, -0.809017), new Vector2(0.5877853, -0.809017), new Vector2(0.9510565, 0.309016994) }; // Add the converted points to the shader properties PolygonMaterial.SetVectorArray("_PolygonPoints", points); } }With these shaders and scripts, you should be able to visualize a polygon in Unity using HLSL and a custom Unlit Shader.
-