GetRawTextureData()
returns a linear (one dimensional) array of all the pixels in a texture.
AudioClips contain interleaved (left and right) data, in a one dimensional array, such that indices represent the x (or time) and each value is either the left and right signal's volume at that x/time. Even numbers = left and odd = right.
Is there a simple way to map this into the NativeArray that comes back from GetRawTextureData()
What I've tried, only focusing on showing the left signal, but is producing odd vertical results:
void FillTextureOne (Texture2D wav_tex, float[] firstWave ) {
var texdata = wav_tex.GetRawTextureData<Color32>( );
for ( int i = 0; i < firstWave.Length ; i+=2 )
{
var positionnew = i/2 * 256 + ( firstWave[i] + 1 ) * 128 ;
texdata[( int )positionnew ] = black;
}
wav_tex.Apply( );
}
texture is 256 x 256 in size. Array of AudioClip data has a length of 512, wherein stored values ranging from -1 to 1, and are being multiplied by 128 after adding one, in a failed attempt to range them between 0 and 256 vertically.
If I understand the solution you are confused about, here is a short explanation as to why it works. To properly map a 2D array into a 1D array, you would need to take the width
and height
of the 2D array, then take their product to give you the proper size of your 1D array. The next step is to make sure your 2D array is in row order or column order
which would decide how you access your information. Once that is figured out, there are two equations that will allow you to access a 1D array in the format of a 2D array using its width and height rather than a singular index. For simplicity, I will use your indexers x
and y
again along with width
and height
.
For Row Order Arrays
yourOneDimensionalArray[width * x + y]
For Column Order Arrays
yourOneDimensionalArray[height * y + x]
Think of a 1D array as a potential 2D array if you break it into equal sections. Depending on the order of your matrix, it will either define the row or column length based on either how large the break is or how many portions you can break apart.
A more visual explanation:
1 2 3 4 5 6 7 8 9 is a 1D array of size 1x9
1 2 3
4 5 6 is a 2D array row order of size 3x3
7 8 9
1 4 7
2 5 8 is a 2D array column order of size 3x3
3 6 9
If we want to access the number 6 in these arrays, we would do as follows:
1DArray[5]
2DRowArray[1][2]
2DColumnArray[2][1]
From the previous equations, we can now take the indexes we have to see how they relate.
2DRowArray is row 1, column 2 or x = 1, y = 2 and our width is 3, so we take 1 * 3 + 2, which is 3 + 2 = 5 or our 1D index
Similarly, the 2DColumnArray is row 2, column 1 or x = 2, y = 1 and our height is 3, so we take 1 * 3 + 2, which is 3 + 2 = 5 or our 1D index.
I could expand this visual solution to a 256x256
grid, but I think the 3x3
should get the idea across just as well. Let me know if you still have questions.