Greetings,
I’m currently facing a problem regarding updating values of several arrays inside a list.
The basic setup is the following: a List containing 2 arrays each comprised of 16 float numbers.
Earlier I had a problem problem regarding the update of the each array: whenever I update one, it also instantly affects ALL other arrays present in the list, something that I didn’t want to happen.
Now the problem is somewhat similar except the update earlier mentioned doesn’t happen instantly, i.e.:
- The matrix present in matrixList[0] is updated with calculated values;
- matrixList[1] remains unchanged;
- Swap to matrixList[1] to update her with new calculated values. It copies the values from matrixList[0] instead of the new calculated values.
The code is as follows (for a matter of simplicity I omitted most of the code, however I can post more if needed):
enter code herepublic class Homography : MonoBehaviour {
public float[] matrix;
public const int arraySize = 16;
public List<float[]> matrixList;
void Start () {
InitMatrixList();
}
void InitMatrixList(){
int numPlanes = manager.numExistPlanes;
matrixList = new List<float[]>();
for(int i = 0; i <= numPlanes; i++){
matrixList.Add(new float[16]);
}
}
void LateUpdate () {
planeMatrixNum = manager.activePlaneNum;
matrix = matrixList[planeMatrixNum-1];
FindHomography(ref source, ref destination, ref matrix);
meshMaterial.SetVector("matrixRow_1", new Vector4(matrix[0], matrix[4], matrix[8], matrix[12]));
meshMaterial.SetVector("matrixRow_2", new Vector4(matrix[1], matrix[5], matrix[9], matrix[13]));
meshMaterial.SetVector("matrixRow_3", new Vector4(matrix[2], matrix[6], matrix[10], matrix[14]));
meshMaterial.SetVector("matrixRow_4", new Vector4(matrix[3], matrix[7], matrix[11], matrix[15]));
}
}
About the desired process flow of this application:
- GameObject 1 is selected in the scene and has its position changed;
- matrixList[0] is selected and updated with new values based on the GO1 position changes;
- GameObject 2 is selected in the scene and has its position changed;
- matrixList[1] is selected and updated with new values based on the GO2 position changes;
This script (Homography.cs) is attached one gameobject only. However the changes done to each of the Matrix present in the matrixList affect other gameobjects. Through debugging I notice he does save the matrix values for GameObject 1, however the same does not happen for GameObject 2, he just copies the values obtained from the matrix of GameObject 1 and applies them to his own matrix.
Also I don’t really know if it’s relevant since I have very limited experience but these matrix values (matrixRow_X) are used within a shader (not written by me).
EDIT: Used an old version of the code by mistake. Also added more info.