Create a grid with the same normal as raycast normal using quaternion

I am trying to create a grid plane which will have the normal from a raycast, and it Should be constrained inside a box. I have made the grid plan, but i have problems with adjusting the grid to fit inside the box. I can create the grid on a specific point, but if i try to offset the grid then i will encounter som weird rotations or scaling.


If i can at least control the offsets on the grid then i can move it to somewhat fit inside a box bounds.
I am using ECS but if you have a solution in standard Monobehaviour, then that might work for helping me understand how to solve this


My result


What i want to achieve


My current attempt in code


    private void GridCreation(GameObject originGO, GameObject testObject1, GameObject testObject2, float testValue1, float testValue2, float angle1, float angle2, float offsetX, float offsetY)
    {
        float3 direction1 = new float3(testObject1.transform.position.x, testObject1.transform.position.y, testObject1.transform.position.z);
        float3 origin = new float3(originGO.transform.position.x, originGO.transform.position.y, originGO.transform.position.z);

        Debug.DrawLine(origin, direction1,Color.black);

        float degreesToRad = math.PI / 180f;

        float quadAngleRad1 = degreesToRad * angle2;
        float quadAngleRad2 = degreesToRad * (270 + angle2);

        float3 newOriginDir = direction1 * testValue1;

        quaternion rotation1 = quaternion.AxisAngle(math.normalize(newOriginDir), quadAngleRad1);
        quaternion rotation2 = quaternion.AxisAngle(math.normalize(newOriginDir), quadAngleRad2);

        float3 offestFloat3X = math.cross(newOriginDir, math.forward(rotation1) * offsetX);
        float3 offestFloat3Y = math.cross(newOriginDir, math.forward(rotation2) * offsetY);

        //First Quadrant test
        float3 firstCourner1 = newOriginDir + (offestFloat3X + offestFloat3Y);
        float3 firstCourner2 = firstCourner1 + (math.cross(firstCourner1, math.forward(rotation1)) * testValue2);
        float3 firstCourner3 = firstCourner2 + (math.cross(firstCourner1, math.forward(rotation2)) * testValue2);
        float3 firstCourner4 = firstCourner1 + (math.cross(firstCourner1, math.forward(rotation2)) * testValue2);


        List<float3> positionList = new List<float3>();
        List<float3> positionListY = new List<float3>();


        positionList.Add(firstCourner1);
        positionList.Add(firstCourner2);
        positionList.Add(firstCourner3);
        positionList.Add(firstCourner4);

        positionListY.Add(firstCourner1);
        positionListY.Add(firstCourner2);
        positionListY.Add(firstCourner3);
        positionListY.Add(firstCourner4);


        bool firstXPos = true;

        int xLenght = 5;
        int yLenght = 5;



        //Create several quads in a row
        for (int yI = 0; yI < yLenght; yI++)
        {
            firstXPos = true;
            if (firstXPos)
            {

                float3 rowCourner1 = positionListY[1];
                float3 rowCourner2 = positionListY[1] + (math.cross(firstCourner1, math.forward(rotation1)) * testValue2);
                float3 rowCourner3 = rowCourner2 + (math.cross(firstCourner1, math.forward(rotation2)) * testValue2);
                float3 rowCourner4 = positionListY[1] + (math.cross(firstCourner1, math.forward(rotation2)) * testValue2);
                QuadDebugLines(rowCourner1, rowCourner2, rowCourner3, rowCourner4);

                positionList.Clear();
                positionList.Add(rowCourner1);
                positionList.Add(rowCourner2);
                positionList.Add(rowCourner3);
                positionList.Add(rowCourner4);

                firstXPos = false;

            }


            for (int xI = 0; xI < xLenght; xI++)
            {

                float3 newQuadCourner1;
                float3 newQuadCourner2;
                float3 newQuadCourner3;
                float3 newQuadCourner4;

                if (xI == 0)
                {
                    newQuadCourner1 = positionList[0];
                    newQuadCourner2 = positionList[0] + (math.cross(firstCourner1, math.forward(rotation1)) * testValue2);
                    newQuadCourner3 = newQuadCourner2 + (math.cross(firstCourner1, math.forward(rotation2)) * testValue2);
                    newQuadCourner4 = positionList[0] + (math.cross(firstCourner1, math.forward(rotation2)) * testValue2);

                }
                else
                {
                    newQuadCourner1 = positionList[3];
                    newQuadCourner2 = positionList[3] + (math.cross(firstCourner1, math.forward(rotation1)) * testValue2);
                    newQuadCourner3 = newQuadCourner2 + (math.cross(firstCourner1, math.forward(rotation2)) * testValue2);
                    newQuadCourner4 = positionList[3] + (math.cross(firstCourner1, math.forward(rotation2)) * testValue2);
                    
                }

                QuadDebugLines(newQuadCourner1, newQuadCourner2, newQuadCourner3, newQuadCourner4);

                positionList.Clear();
                positionList.Add(newQuadCourner1);
                positionList.Add(newQuadCourner2);
                positionList.Add(newQuadCourner3);
                positionList.Add(newQuadCourner4);

                if (xI == 0)
                {
                    positionListY.Clear();
                    positionListY.Add(newQuadCourner1);
                    positionListY.Add(newQuadCourner2);
                    positionListY.Add(newQuadCourner3);
                    positionListY.Add(newQuadCourner4);

                }


            }



        }

        //float3 testThirdCourner3 = (newOriginDir + (math.cross(newOriginDir, math.forward(rotation3)) * testValue2)) + (math.normalize(math.forward(rotation2)));

        //Test position
        float3 newDirection = newOriginDir + (math.cross(newOriginDir, math.forward(rotation1)) * testValue2);

        //originGO.transform.position = new Vector3(newOriginDir.x, newOriginDir.y, newOriginDir.z);
        testObject2.transform.position = new Vector3(newDirection.x, newDirection.y, newDirection.z);

        
    }

Sort of solved it, for anyone that is interested in this problem here is the code.


 private void GridCreation(float angle1, float offsetX, float offsetY, int gridCount, float quadSize, List<float3x4> cubeList)
    {
        float3 targetPos = new float3(testObject1.transform.position.x, testObject1.transform.position.y, testObject1.transform.position.z);
        float3 origin = new float3(originGO.transform.position.x, originGO.transform.position.y, originGO.transform.position.z);

        float3 cubeCenter = (cubeList[0].c0 + cubeList[0].c1 + cubeList[0].c2 + cubeList[0].c3 + cubeList[1].c0 + cubeList[1].c1 + cubeList[1].c2 + cubeList[0].c3) / 8;

        float distanceXToCenter = origin.x + cubeCenter.x;
        float distanceZToCenter = origin.z + cubeCenter.z;

        offsetX = distanceXToCenter;
        offsetY = distanceZToCenter;


        float3 offsetPosition = origin;

        float degreesToRad = math.PI / 180f;

        float quadAngleRad1 = degreesToRad * angle1;
        float quadAngleRad2 = degreesToRad * (270 + angle1);

        float3 newOriginDir = math.normalize(targetPos);
        quaternion rotation1 = quaternion.AxisAngle(math.normalize(newOriginDir), quadAngleRad1);
        quaternion rotation2 = quaternion.AxisAngle(math.normalize(newOriginDir), quadAngleRad2);

        offsetPosition = offsetPosition - newOriginDir;

        //First Quadrant test
        float3 firstCourner1 = newOriginDir;
        float3 firstCourner2 = (firstCourner1 - (math.cross(firstCourner1, math.forward(rotation1)) * offsetX));
        float3 firstCourner3 = (firstCourner2 - (math.cross(firstCourner1, math.forward(rotation2)) * offsetY));
        float3 firstCourner4 = (firstCourner1 - (math.cross(firstCourner1, math.forward(rotation2)) * offsetY));

        QuadDebugLines(firstCourner1, firstCourner2, firstCourner3, firstCourner4);


        CustomTools.CustomGeometryTools customGeometryTools = new CustomTools.CustomGeometryTools();
        List<float3> positionList = new List<float3>();
        List<float3> positionListY = new List<float3>();
        List<float3x4> totalGridList = new List<float3x4>();
        List<float3x4> finalGridList = new List<float3x4>();

        positionList.Add(firstCourner1);
        positionList.Add(firstCourner2);
        positionList.Add(firstCourner3);
        positionList.Add(firstCourner4);

        positionListY.Add(firstCourner1);
        positionListY.Add(firstCourner3);
        positionListY.Add(firstCourner2);
        positionListY.Add(firstCourner4);


        bool firstXPos = true;


        //Create several quads in a row
        for (int yI = 0; yI < gridCount; yI++)
        {
            firstXPos = true;
            if (firstXPos)
            {

                float3 rowCourner1 = positionListY[1];
                float3 rowCourner2 = positionListY[1] + (math.cross(firstCourner1, math.forward(rotation1)) * quadSize);
                float3 rowCourner3 = rowCourner2 + (math.cross(firstCourner1, math.forward(rotation2)) * quadSize);
                float3 rowCourner4 = positionListY[1] + (math.cross(firstCourner1, math.forward(rotation2)) * quadSize);

                

                totalGridList.Add(new float3x4( rowCourner1, rowCourner2, rowCourner3, rowCourner4));

                
                positionList.Clear();
                positionList.Add(rowCourner1);
                positionList.Add(rowCourner2);
                positionList.Add(rowCourner3);
                positionList.Add(rowCourner4);

                firstXPos = false;

            }


            for (int xI = 0; xI < gridCount; xI++)
            {

                float3 newQuadCourner1;
                float3 newQuadCourner2;
                float3 newQuadCourner3;
                float3 newQuadCourner4;

                if (xI == 0)
                {
                    newQuadCourner1 = positionList[0];
                    newQuadCourner2 = positionList[0] + (math.cross(firstCourner1, math.forward(rotation1)) * quadSize);
                    newQuadCourner3 = newQuadCourner2 + (math.cross(firstCourner1, math.forward(rotation2)) * quadSize);
                    newQuadCourner4 = positionList[0] + (math.cross(firstCourner1, math.forward(rotation2)) * quadSize);

                }
                else
                {
                    newQuadCourner1 = positionList[3];
                    newQuadCourner2 = positionList[3] + (math.cross(firstCourner1, math.forward(rotation1)) * quadSize);
                    newQuadCourner3 = newQuadCourner2 + (math.cross(firstCourner1, math.forward(rotation2)) * quadSize);
                    newQuadCourner4 = positionList[3] + (math.cross(firstCourner1, math.forward(rotation2)) * quadSize);
                    
                }


                totalGridList.Add(new float3x4(newQuadCourner1, newQuadCourner2, newQuadCourner3, newQuadCourner4));

                positionList.Clear();
                positionList.Add(newQuadCourner1);
                positionList.Add(newQuadCourner2);
                positionList.Add(newQuadCourner3);
                positionList.Add(newQuadCourner4);

                if (xI == 0)
                {
                    positionListY.Clear();
                    positionListY.Add(newQuadCourner1);
                    positionListY.Add(newQuadCourner2);
                    positionListY.Add(newQuadCourner3);
                    positionListY.Add(newQuadCourner4);

                }


            }



        }

        for (int i = 0; i < totalGridList.Count; i++)
        {
            totalGridList <em>= new float3x4(totalGridList<em>.c0 + offsetPosition, totalGridList<em>.c1 + offsetPosition, totalGridList_.c2 + offsetPosition, totalGridList*.c3 + offsetPosition);*_</em></em></em>

if (customGeometryTools.InsideBox3D(cubeList[1], cubeList[0], totalGridList*.c0) &&*
customGeometryTools.InsideBox3D(cubeList[1], cubeList[0], totalGridList*.c1) &&*
customGeometryTools.InsideBox3D(cubeList[1], cubeList[0], totalGridList*.c2) &&*
customGeometryTools.InsideBox3D(cubeList[1], cubeList[0], totalGridList*.c3))*
{
finalGridList.Add(totalGridList*);*
QuadDebugLines(totalGridList.c0, totalGridList.c1, totalGridList_.c2, totalGridList*.c3);
}*_

}

}