Advertisement
raphael76280

Untitled

Jun 23rd, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.23 KB | None | 0 0
  1. // Each #kernel tells which function to compile; you can have many kernels
  2. #pragma kernel CSMain
  3. #pragma kernel CSStraight
  4. #pragma kernel Blur
  5.  
  6. // Create a RenderTexture with enableRandomWrite flag and set it
  7. // with cs.SetTexture
  8. RWTexture2D<float4> Result;
  9.  
  10. float2 size;
  11. float radius;
  12. float pixelPerUnit;
  13.  
  14.  
  15.  
  16. int maxAngles;
  17. RWStructuredBuffer<float> startAngles;
  18. RWStructuredBuffer<float> magAngles;
  19.  
  20. [numthreads(32, 32,1)]
  21. void CSMain (uint3 id : SV_DispatchThreadID)
  22. {
  23.     // TODO: insert actual code here!
  24.  
  25.     // /!\ pixelPerUnit est le nombre de pixelPerUnit
  26.  
  27.     //Vecteur du pixel
  28.     float2 dir = float2(size[0] / 2 - id.x, size[1] / 2 - id.y);
  29.     //Distance du pixel au centre
  30.     float mag = sqrt(dir[0] * dir[0] + dir[1] * dir[1]);
  31.     //Normal du vecteur (pour le calcul d'angle)
  32.     float2 dirNorm = float2(dir[0] / mag, dir[1] / mag);
  33.  
  34.     //Couleur de base pour le dégradé
  35.     float color = 1-mag/(pixelPerUnit*radius);
  36.     //Si la distance est supérieure au rayon (coin) c'est noir
  37.     if (mag > (radius * pixelPerUnit))
  38.     {
  39.         color = 0;
  40.     }
  41.     else
  42.     {
  43.         color = 0;
  44.         //Sinon on calcul l'angle
  45.         float a = atan2(dirNorm[0], dirNorm[1]);
  46.         a = a + 3.14;
  47.  
  48.  
  49.         //On compare l'angle au angles enregistré
  50.         for (int i = 0; i < maxAngles; i = i + 1) {
  51.             if (abs(a) < startAngles[i+1] && abs(a) > startAngles[i] || abs(a) >= 6.26 && startAngles[i] >= 6.26 || abs(a) <= 0.03 && startAngles[i] <= 0.03)
  52.             {
  53.                 color = 1-mag / (pixelPerUnit * radius);
  54.                 //On récupére la magnitude de l'angle enregistré et on vérifie si notre pixel est plus loin
  55.                 if (mag > (radius * pixelPerUnit * magAngles[i])) {
  56.                     color =  0;
  57.  
  58.                 }
  59.             }
  60.         }
  61.     }
  62.  
  63.     Result[id.xy] = float4(1, 1, 1, color);
  64. }
  65.  
  66. RWTexture2D<float4> BlurredTexture;
  67. RWTexture2D<float4> BlurredResult;
  68.  
  69.  
  70. [numthreads(32, 32, 1)]
  71. void Blur(uint3 id : SV_DispatchThreadID)
  72. {
  73.     BlurredResult = Result;
  74.  
  75.     float2 center = size * pixelPerUnit / 2;
  76.     float2 dir = id.xy - center;
  77.  
  78.     float4 zero = float4(0, 0, 0, 0);
  79.  
  80.     int count = 0;
  81.  
  82.     float4 up;
  83.     float4 down;
  84.     float4 right;
  85.     float4 left;
  86.  
  87.     if (dir.y > 0) {
  88.         up = Result[float2(id.x, id.y + 1)];
  89.         count = count + 1;
  90.     }
  91.     else {
  92.         up = zero;
  93.     }
  94.  
  95.     if (dir.y < 0) {
  96.         down = Result[float2(id.x, id.y - 1)];
  97.         count = count + 1;
  98.     }
  99.     else {
  100.         down = zero;
  101.     }
  102.  
  103.     if (dir.x < 0) {
  104.         left = Result[float2(id.x - 1, id.y)];
  105.         count = count + 1;
  106.     }
  107.     else {
  108.         left = zero;
  109.     }
  110.  
  111.     if (dir.x > 0) {
  112.         right = Result[float2(id.x + 1, id.y)];
  113.         count = count + 1;
  114.     }
  115.     else {
  116.         right = zero;
  117.     }
  118.  
  119.     up = Result[float2(id.x, id.y + 1)];
  120.  
  121.    
  122.  
  123.      down = Result[float2(id.x, id.y -1)];
  124.  
  125.  
  126.      left = Result[float2(id.x -1, id.y)];
  127.  
  128.  
  129.      right = Result[float2(id.x + 1, id.y)];
  130.  
  131.     float sum = (up.w + down.w + left.w + right.w) / 4;
  132.     //sum = up.w / 2;
  133.     //BlurredResult[id.xy] = Result[id.xy];
  134.     BlurredResult[id.xy] = float4(1, 1, 1, sum);
  135.  
  136.  
  137.     Result = BlurredResult;
  138.  
  139.    
  140. }
  141.  
  142.  
  143.  
  144. float width;
  145. float realWidth;
  146. float height;
  147. int precision;
  148. RWStructuredBuffer<float> points;
  149.  
  150. float rP(float x) {
  151.     return x * (realWidth*pixelPerUnit) / width;
  152. }
  153.  
  154. float realPos(float x) {
  155.     float offset = (width - (realWidth*pixelPerUnit)) / 2;
  156.     return rP(x) - rP(offset);
  157. }
  158.  
  159. [numthreads(32, 32, 1)]
  160. void CSStraight(uint3 id : SV_DispatchThreadID)
  161. {
  162.     float color = 1;
  163.             float magnitude = height-(height-id.y);
  164.  
  165.             float offset = (width - (realWidth*pixelPerUnit))/2;
  166.             float offsetBis = realWidth * pixelPerUnit + offset;
  167.  
  168.             float realX = realPos(id.x)-realPos(offset);
  169.         /*  if (id.x < offset || id.x > realWidth*pixelPerUnit + offset)
  170.                 realX = 0;*/
  171.             if (realX < 0 || realX > realPos(realWidth*pixelPerUnit+offset))
  172.                 realX = 0;
  173.  
  174.             //int o =  round(id.x / (float)pixelPerUnit);
  175.             //int o = round((realX / (float)pixelPerUnit)*realWidth-(offset/(realWidth*5000)));
  176.             int o = round(realX/realPos(offsetBis)*precision);
  177.  
  178.             if (magnitude < points[clamp(o, 0, precision -1)]*height)
  179.             {
  180.                 color = 1;
  181.             }
  182.             else
  183.             {
  184.                 color = 0;
  185.             }
  186.  
  187.            
  188.  
  189.             if (id.x <= offset || id.x >= width- (offset)) {
  190.                 color = 0;
  191.             }
  192.  
  193.             if (id.y <= 1 || id.y >= height-2) {
  194.                 color = 0;
  195.             }
  196.  
  197.             //color = realX;
  198.            
  199.             //color = magnitude;
  200.             //color = 0.5;
  201.     Result[id.xy] = float4(color, color, color, color);
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement