So I am trying to learn compute shaders and I started creating some simple ones that came to mind, one of them being using bresenham's algorithm to draw a line between two points, however I m stuck to how you face these problems. Do you segment the problem depending on the part of the line? or check if each pixel is under the line?
For what purpose do you want to use Bresenham's algorithm ? To make a custom rasterizer, or just to draw a single line in render texture ?
To start learn compute shaders you can study following example. Texture coordinates are in range [0-1]. Line is drawn from first point (0.3,0.1) to second point (0.8,0.5). Attach script to quad and press Play. Code (CSharp): using UnityEngine; public class Line : MonoBehaviour { public ComputeShader CS; void Start() { RenderTexture RT = new RenderTexture(1024, 1024, 0); RT.enableRandomWrite = true; RT.Create(); CS.SetTexture(0, "surface", RT); CS.Dispatch(0, RT.width / 8, RT.height / 8, 1); GetComponent<Renderer>().material.mainTexture = RT; } } Code (CSharp): #pragma kernel CSMain RWTexture2D<float4> surface; float Line( float2 p, float2 a, float2 b ) { float2 pa = p-a, ba = b-a; float h = saturate( dot(pa,ba)/dot(ba,ba) ); float2 d = pa - ba * h; return dot(d,d); } [numthreads(8,8,1)] void CSMain (uint2 id : SV_DispatchThreadID) { float2 uv = float2 ((float)id.x/1024, (float)id.y/1024); float k = Line(uv,float2(0.3,0.1),float2(0.8,0.5)); float thickness = 0.00001; surface[id.xy] = lerp( float4(1,1,1,1), float4(0,0,0,1), smoothstep(0.0, thickness, k) ); }
Thanks a lot, I am trying to linedraw a single line (or bezier eventually) in render texture, and I wasn't sure from where to start. I have a draw texture shader but it is using too many drawcalls and I wanted to reduce that by passing em to a compute shader to batch calculate the shape.
Is it possible to draw a histogram with compute shader? I mean draw a bunch of line and fill color like CPU Draw API.