- Home /
Possible shader interference when updating matrix
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.
Answer by whydoidoit · Feb 28, 2014 at 12:17 PM
You are putting the same matrix array into each slot - you mean to do this I think:
void InitMatrixList(){
int numPlanes = manager.numExistPlanes;
matrixList = new List<float[]>();
for(int i = 0; i <= numPlanes; i++){
matrixList.Add(new float[16]);
}
}
But I'm not sure how any of that would refer to your matrix variable (the one you were repeatedly adding). No chance the shader is doing this - wish it could :)
$$anonymous$$y bad! I wrongly copied the code from my earlier post, so it wasn't updated with that change. The problem I'm currently facing happens while using that exact same code you posted.
You are only doing this once right? this script is only on one object - because otherwise you'd need it in some pre rendering function not LateUpdate or all of the variables will be overwritten.
You are passing matrix to FindHomography as a ref parameter which indicates that this function might actually create and return a new matrix. If it does so then you are not storing that matrix and ought to add:
matrixList[plane$$anonymous$$atrixNum-1] = matrix;
Through debugging he is storing and receiving the proper matrix values. Problem only occurs when I move to another matrix index. Also, decided to add quite a few more information to the original post.
Your answer
Follow this Question
Related Questions
Inventory System wont work 2 Answers
C# Enemy list for player to attack 2D 1 Answer
How to select an Game Object from an Item List 0 Answers
Problem with List not filling using add method in a foreach loop 1 Answer
How to modify array values? 1 Answer