Cromon

Ellipse.hlsl

Oct 12th, 2012
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.33 KB | None | 0 0
  1.  
  2. matrix orthoProjMatrix;
  3. float4 elementSize;
  4. float4 strokeWidth;
  5. float4 ellipseParams;
  6.  
  7. struct VertexShaderInput
  8. {
  9.     float3 position : POSITION0;
  10.     float4 color : COLOR0;
  11.     float2 texCoord : TEXCOORD0;
  12. };
  13.  
  14. struct PixelShaderInput
  15. {
  16.     float4 position : POSITION0;
  17.     float2 texCoord : TEXCOORD0;
  18.     float4 color : TEXCOORD1;
  19. };
  20.  
  21. void VertexMain(in VertexShaderInput vsInput, out PixelShaderInput output)
  22. {
  23.     output = (PixelShaderInput)0;
  24.  
  25.     output.position = mul(float4(vsInput.position, 1), orthoProjMatrix);
  26.     output.texCoord = vsInput.texCoord;
  27.     output.color = vsInput.color;
  28. }
  29.  
  30. float getEllipseRadius(float x, float y)
  31. {
  32.     float phi = atan2(y, x);
  33.     float a2 = ellipseParams.x * ellipseParams.x;
  34.     float b2 = ellipseParams.y * ellipseParams.y;
  35.     float ab = ellipseParams.x * ellipseParams.y;
  36.     float sin2 = sin(phi) * sin(phi);
  37.     float cos2 = cos(phi) * cos(phi);
  38.  
  39.     return (ab) / sqrt(a2 * sin2 + b2 * cos2);
  40. }
  41.  
  42. void PixelMain(in PixelShaderInput input, out float4 color : COLOR0)
  43. {
  44.     float elemX = (-elementSize.x / 2.0f) + elementSize.x * input.texCoord.x;
  45.     float elemY = (-elementSize.y / 2.0f) + elementSize.y * input.texCoord.y;
  46.  
  47.     float radius = getEllipseRadius(elemX, elemY);
  48.     float radiusCoord = sqrt(elemX * elemX + elemY * elemY);
  49.     if(radiusCoord <= radius)
  50.         color = input.color;
  51.     else
  52.         color = float4(0, 0, 0, 0);
  53. }
Advertisement
Add Comment
Please, Sign In to add comment