Search Unity

Question Warped GPU Instanced Objects

Discussion in 'General Graphics' started by Cornysam, Sep 13, 2022.

  1. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,463
    Hello,

    This may be the wrong section, so i apologize if so. Anyways, I am learning about GPU Instancing and finding some good benefits. I am experimenting with it a bit and trying to see if I can place an instanced object in the exact right position, rotation, and scale. I appear to be understanding the position and scale portions, but the rotation part is baffling.

    The issue: When i try to rotate the object using new Quaternion(x, y, z, w) or from a public field, it is distorting the object's scale (like it is stretching it) and it is moving its position dramatically. When i followed the tutorial it used Random.rotation and that appears to rotate them properly without distortion. But getting the exact rotation is not working.

    I am sure i am misunderstanding something pretty basic but after searching the forums and manuals, i believe i am not understanding the Matrix4x4 part. Not sure if that is where my issue is, but it is the only thing i am not grasping properly.

    Any explanation or help is appreciated.

    Here is my code:
    Code (CSharp):
    1.  
    2.     public int instances;
    3.  
    4.     public Mesh mesh;
    5.  
    6.     public Material[] mats;
    7.  
    8.     private List<List<Matrix4x4>> batches = new List<List<Matrix4x4>>();
    9.  
    10.     public Vector3 pos; public Quaternion rot; public Vector3 scale;
    11.  
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         int AddMatricies = 0;
    16.         batches.Add(new List<Matrix4x4>());
    17.  
    18.         for (int i = 0; i < instances; i++)
    19.         {
    20.             if (AddMatricies < 1000 && batches.Count != 0)
    21.             {
    22.                 //batches[batches.Count - 1].Add(Matrix4x4.TRS(new Vector3(Random.Range(0, 50), Random.Range(0, 50), Random.Range(0, 50)), Random.rotation, Vector3.one * 50)); //Works great
    23.                 batches[batches.Count - 1].Add(Matrix4x4.TRS(new Vector3(-35.5599f, .242317f, -36.99f), rot, Vector3.one * 103)); // Quaternion parameter does not work properly
    24.                 AddMatricies += 1;
    25.             }
    26.             else
    27.             {
    28.                 batches.Add(new List<Matrix4x4>());
    29.                 AddMatricies = 0;
    30.  
    31.             }
    32.         }
    33.     }
    34.  
    35.     // Update is called once per frame
    36.     void Update()
    37.     {
    38.         RenderBatches();
    39.     }
    40.  
    41.     private void RenderBatches()
    42.     {
    43.         foreach (var Batch in batches)
    44.         {
    45.             for (int i = 0; i < mesh.subMeshCount; i++)
    46.             {
    47.                 Graphics.DrawMeshInstanced(mesh, i, mats[i], Batch);
    48.             }
    49.         }
    50.     }
     
  2. georgerh

    georgerh

    Joined:
    Feb 28, 2020
    Posts:
    72
    How do you set the quaternion values? Only normalized quaternions correspond with rotations, so you can't just change the x,y,z,w values any way you want.
     
  3. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,463
    I've tried setting it via values from the Inspector since "rot" is public. I have also tried just using "new Quaternion(value, value, value, value)" and the same thing happens.