Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Feedback How limit edge length in compute shader(Resovled)

Discussion in 'Shaders' started by flyer19, Jul 18, 2019.

  1. flyer19

    flyer19

    Joined:
    Aug 26, 2016
    Posts:
    121
    Code (CSharp):
    1. // Each #kernel tells which function to compile; you can have many kernels
    2. #pragma kernel CSMain
    3. #pragma kernel LimitLen
    4. #include "UnityCG.cginc"
    5. struct VtoInt3
    6. {
    7.     int x;
    8.     int y;
    9.     int z;
    10. };
    11. // Create a RenderTexture with enableRandomWrite flag and set it
    12. // with cs.SetTexture
    13. RWStructuredBuffer<float3> _PositionBuffer;
    14. RWStructuredBuffer<VtoInt3> _PositionBufferInt;
    15.  
    16. [numthreads(8,1,1)]
    17. void CSMain (uint3 id : SV_DispatchThreadID)
    18. {
    19.     // TODO: insert actual code here!
    20.     float3 pos = _PositionBuffer[id.x] ;
    21.     int factor = 100000;
    22.     if (id.x>1)
    23.     {
    24.         //pos += sin(_Time.x);
    25.         pos +=0.1;//can limited
    26.     }
    27.  
    28.     InterlockedAdd(_PositionBufferInt[id.x].x, (int)(pos.x * factor));
    29.     InterlockedAdd(_PositionBufferInt[id.x].y, (int)(pos.y * factor));
    30.     InterlockedAdd(_PositionBufferInt[id.x].z, (int)(pos.z * factor));
    31.  
    32. }
    33.  
    34. [numthreads(8, 1, 1)]
    35. void LimitLen(uint3 id : SV_DispatchThreadID)
    36. {
    37.     float factor = 100000;
    38.     VtoInt3 intPos = _PositionBufferInt[id.x];
    39.  
    40.     float3 pos = float3(intPos.x / factor, intPos.y / factor, intPos.z / factor);
    41.     _PositionBuffer[id.x] = pos;
    42.  
    43.  
    44.  
    45.     // TODO: insert actual code here!
    46.     if (id.x >1)
    47.     {
    48.         VtoInt3 intPosUp = _PositionBufferInt[id.x-2];
    49.  
    50.         float3 posUp = float3(intPosUp.x / factor, intPosUp.y / factor, intPosUp.z / factor);
    51.         float3 dir = pos - posUp;
    52.         float limitlen = 0.05;
    53.         if (length(dir) > limitlen)
    54.         {
    55.             _PositionBuffer[id.x] = posUp+normalize(dir)*limitlen;
    56.  
    57.         }
    58.     }
    59.  
    60.  
    61.     _PositionBufferInt[id.x].x = 0;
    62.     _PositionBufferInt[id.x].y = 0;
    63.     _PositionBufferInt[id.x].z = 0;
    64. }
    How can make the length limited in compute shader?this not work,it seems only limted the first thread. limt.png
     

    Attached Files:

  2. flyer19

    flyer19

    Joined:
    Aug 26, 2016
    Posts:
    121
    any help?
     
  3. flyer19

    flyer19

    Joined:
    Aug 26, 2016
    Posts:
    121