Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Compute shader always has wrong position or scale

Discussion in 'Shaders' started by ATMLVE, Oct 19, 2023.

  1. ATMLVE

    ATMLVE

    Joined:
    Jun 11, 2023
    Posts:
    66
    I'm using youtube tutorials like a good indie dev to get a compute shader to generate grass. A major difference from the tutorial is I already have my list of world vertices easily because I'm generating my terrain at runtime, but the tutorial assumes a predefined mesh which needs its vertices converted to world coordinates. I already have my vertices in world coordinates which should make this simpler.

    However running the below compute shader, if I do matrix multiplication like (rotation X scale&position), everything scales and rotates properly but the positions are all totally wrong. If I multiply (scale&position X rotation), then everything is positioned exactly where it should be but the scale is applied after the rotation, so the meshes are all distorted shapes. I've tried transposing things with no luck.

    I've already adjusted the original code a bit. Right now it's not doing things the way it should, like it still is using the number of triangles when that won't be relevant for my purposes, but at the core of it I just want it to output the mesh orientated and position correctly.

    Code (CSharp):
    1. #pragma kernel CalculateBladePositions
    2.  
    3. StructuredBuffer<float3> _TerrainPositions;
    4.  
    5. RWStructuredBuffer<float4x4> _TransformMatrices;
    6. uniform int _TerrainTriangleCount;
    7. uniform float _Scale;
    8. uniform float _MinBladeHeight;
    9. uniform float _MaxBladeHeight;
    10. uniform float _MinOffset;
    11. uniform float _MaxOffset;
    12. uniform float4x4 _TerrainObjectToWorld;
    13. //uniform float3 _LookDirection;  // New uniform for the direction the object should look in
    14.  
    15.  
    16. #define TWO_PI 6.28318530718f
    17.  
    18. // Function that takes a 2-element seed and returns a random value
    19. // between the min and max bounds.
    20. float randomRange(float2 seed, float min, float max)
    21. {
    22.     float randnum = frac(sin(dot(seed, float2(12.9898, 78.233)))*43758.5453);
    23.     return lerp(min, max, randnum);
    24. }
    25.  
    26. // Function to rotate around the y-axis by a specified angle.
    27. float4x4 rotationMatrix(float3 anglesDeg)
    28. {
    29.     float sX, cX, sY, cY, sZ, cZ;
    30.     sincos(anglesDeg.x, sX, cX);
    31.     sincos(anglesDeg.y, sY, cY);
    32.     sincos(anglesDeg.z, sZ, cZ);
    33.  
    34.     float4x4 rotationX = float4x4(1,   0,   0,   0,
    35.                                   0,   cX,  sX, 0,
    36.                                   0,   -sX,  cX,  0,
    37.                                   0,   0,   0,   1);
    38.  
    39.     float4x4 rotationY = float4x4(cY,  0,   -sY,  0,
    40.                                   0,   1,   0,   0,
    41.                                   sY, 0,   cY,  0,
    42.                                   0,   0,   0,   1);
    43.  
    44.     float4x4 rotationZ = float4x4(cY,  sY, 0,   0,
    45.                                   -sY,  cY,  0,   0,
    46.                                   0,   0,   1,   0,
    47.                                   0,   0,   0,   1);
    48.  
    49.     return rotationX * rotationY * rotationZ;
    50. }
    51.  
    52. // This kernel calculates transformation matrices for each grass blade
    53. // to place them in different positions on the terrain mesh.
    54. [numthreads(64, 1, 1)]
    55. void CalculateBladePositions(uint3 id : SV_DispatchThreadID)
    56. {
    57.     // Avoid running 'overflow' tasks when the number of tasks
    58.     // wasn't divisible by the number of threads.
    59.     if (id.x > _TerrainTriangleCount)
    60.     {
    61.         return;
    62.     }
    63.  
    64.     float2 randomSeed1 = float2(id.x, id.y);
    65.     float2 randomSeed2 = float2(id.y, id.x);
    66.  
    67.     float scaleY = _Scale * _MaxBladeHeight;
    68.  
    69.     float4x4 grassTransformMatrix = float4x4
    70.     (
    71.         _Scale,    0,        0,        _TerrainPositions[id.x].x,
    72.         0,        scaleY,    0,        _TerrainPositions[id.x].y,
    73.         0,        0,        _Scale,    _TerrainPositions[id.x].z,
    74.         0,        0,        0,        1
    75.     );
    76.  
    77.     float4x4 randomRotationMatrix = rotationMatrix(float3(randomRange(randomSeed1, 0.0f, TWO_PI), randomRange(randomSeed1, 0.0f, TWO_PI), randomRange(randomSeed1, 0.0f, TWO_PI)));
    78.  
    79.     _TransformMatrices[id.x] = mul(randomRotationMatrix, grassTransformMatrix);
    80.     //_TransformMatrices[id.x] = mul(grassTransformMatrix, randomRotationMatrix);
    81. }
    82.  
    Below is from the actual .shader file. The matrices from above are assigned to objectToWorld becase that's what the original code was doing. What confuses me is why this all seems to include the position twice, and why that works anyway as long as I do the matrix calculation a certain way. I've meshed with this a bunch and basically nothing but exactly this code will output anything at all (and it's funny because I can see the number of triangles in the scene jump, but I don't actually see them anywhere)

    Code (CSharp):
    1.             v2f vert(appdata v)
    2.             {
    3.                 v2f o;
    4.  
    5.                 float4 positionOS = float4(_Positions[v.vertexID], 1.0f);
    6.                 float4x4 objectToWorld = _TransformMatrices[v.instanceID];
    7.  
    8.                 o.positionWS = mul(objectToWorld, positionOS);
    9.                 o.positionCS = mul(UNITY_MATRIX_VP, o.positionWS);
    10.                 o.uv = _UVs[v.vertexID];
    11.  
    12.                 return o;
    13.             }
     
  2. ATMLVE

    ATMLVE

    Joined:
    Jun 11, 2023
    Posts:
    66
    I kind of have the issue narrowed down. The problem is my rotations seem to apply to both each individual instanced mesh as well as that mesh's position around world origin as a whole.

    So if I have a quad, and I give it a rotation of 90 degrees around the Y axis and a position off the world origin, it rotates 90 degrees around it's own Y axis but then also rotates 90 degrees around the world Y-axis.

    I cannot figure out why it's doing this, I have tried injecting and removing and skipping so many matrices in this process. I either get nothing out or I get them all doubling up on some calculation.

    I did find the documentation on Graphics.RenderPrimitivesIndexed to be somewhat comparable to the example I was already using, but it's matrix is very simple whereas mine comes from a comptue shader with an issue I don't understand.

    Compute shader simplified section:
    Code (CSharp):
    1. float4x4 rotationMatrix(float3 angles)
    2. {
    3.     float sX, cX, sY, cY, sZ, cZ;
    4.     sincos(angles.x, sX, cX);
    5.     sincos(angles.y, sY, cY);
    6.     sincos(angles.z, sZ, cZ);
    7.  
    8.     float4x4 rotationX = float4x4(1,   0,   0,   0,
    9.                                   0,   cX,  sX, 0,
    10.                                   0,   -sX,  cX,  0,
    11.                                   0,   0,   0,   1);
    12.  
    13.     float4x4 rotationY = float4x4(cY,  0,   -sY,  0,
    14.                                   0,   1,   0,   0,
    15.                                   sY, 0,   cY,  0,
    16.                                   0,   0,   0,   1);
    17.  
    18.     float4x4 rotationZ = float4x4(cZ,  sZ, 0,   0,
    19.                                   -sZ,  cZ,  0,   0,
    20.                                   0,   0,   1,   0,
    21.                                   0,   0,   0,   1);
    22.     return rotationX; // * rotationY * rotationZ;
    23. }
    24.  
    25. [numthreads(64, 1, 1)]
    26. void CalculateBladePositions(uint3 id : SV_DispatchThreadID)
    27. {
    28.     if (id.x > _IndexCount)
    29.     {
    30.         return;
    31.     }
    32.  
    33.     float2 randomSeed1 = float2(id.x, id.y);
    34.     float2 randomSeed2 = float2(id.y, id.x);
    35.     float2 randomSeed3 = float2(id.x, id.x);
    36.  
    37.     float scaleY = _Scale * randomRange(randomSeed1, _MinBladeHeight, _MaxBladeHeight);
    38.  
    39.     float4x4 grassTransformMatrix = float4x4
    40.     (
    41.         _Scale*10,    0,        0,        _TerrainPositions[id.x].x,
    42.         0,        scaleY*10,    0,        _TerrainPositions[id.x].y,
    43.         0,        0,        _Scale*10,    _TerrainPositions[id.x].z,
    44.         0,        0,        0,        1
    45.     );
    46.  
    47.     float4x4 randomRotationMatrix = rotationMatrix(float3(randomRange(randomSeed1, 0.0f, TWO_PI), randomRange(randomSeed2, 0.0f, TWO_PI), randomRange(randomSeed3, 0.0f, TWO_PI)));
    48.  
    49.     _TransformMatrices[id.x] = mul(randomRotationMatrix, grassTransformMatrix);
    50. }
    Shader:
    Code (CSharp):
    1.             v2f vert(appdata v)
    2.             {
    3.                 v2f o;
    4.  
    5.                 float4 positionOS = float4(_Positions[v.vertexID], 1.0f);
    6.                 float4x4 objectToWorld = _TransformMatrices[v.instanceID];
    7.  
    8.                 o.positionWS = mul(objectToWorld, positionOS);
    9.                 o.positionCS = mul(UNITY_MATRIX_VP, o.positionWS);
    10.                 o.uv = _UVs[v.vertexID];
    11.  
    12.                 return o;
    13.             }