- Home /
Need help loading multiple images into RawImage
HI. I want to load multiple images side by side into my Raw Image so it would look like a panorama. So far it's only loading the last picture. I need help with offsetting the image before putting it into the texture so the first ones wouldnt be overwritten. Thanks in advance!
while (_CountLoad <= _PhotoCount) {
string url = _SavePath + _CountLoad.ToString() + ".png";
byte [] imageprev = File.ReadAllBytes(url);
_InitialPreview = new Texture2D(960, 720, TextureFormat.ARGB32, false);
Rect sourceRect = new Rect(0, 0, _InitialPreview.width, _InitialPreview.height);
_InitialPreview.LoadImage(imageprev);
Color[] preview = _InitialPreview.GetPixels(0, 0, _InitialPreview.width, _InitialPreview.height);
_DestTex = new Texture2D(_InitialPreview.width, _InitialPreview.height);
_DestTex.SetPixels(preview);
_DestTex.Apply();
_CountLoad++;
}
ImagePreview.texture = _DestTex;
}
Answer by AlucardJay · May 27, 2016 at 04:23 AM
Currently you are overwriting _DestTex with the last loaded texture each time, that's why it seems you are only getting the last image. You need to load each texture, then combine them before applying to your final texture.
This is quite a complex process. A colour array of pixels is 1D, not 2D. So to merge several images, you have to read the x from each texture first, add it to the 1D array, then increment y.
more simply:
first row (y=0)
read each x from image 1, add to list,
read each x from image 2, add to list,
now repeat for next row (y++)
This script accomplishes that. I have added comments throughout to hopefully explain what is happening where in the process. (also assumes each texture is the same width and height)
// store the width and height of loaded texture
// for use in calculating merged texture dimensions
// assuming each texture has the same width and height !!
int width = 0;
int height = 0;
// list for storing each loaded texture colours (don't forget using System.Collections.Generic;)
List< Color[] > allTexturePixels = new List< Color[] >();
// load each texture
while ( _CountLoad <= _PhotoCount )
{
string url = _SavePath + _CountLoad.ToString() + ".png";
byte[] imageprev = File.ReadAllBytes(url);
// generate texture from bytes
_InitialPreview = new Texture2D( 2, 2 ); // this gets overwritten by LoadImage anyway
_InitialPreview.LoadImage( imageprev );
// create colour array from texture pixels
Color[] preview = _InitialPreview.GetPixels( 0, 0, _InitialPreview.width, _InitialPreview.height );
// store colour array in list
allTexturePixels.Add( preview );
// store width and height
width = _InitialPreview.width;
height = _InitialPreview.height;
// increment index
_CountLoad ++;
}
// merge textures
// texture pixel array is 1D
// reads from the bottom-left
// horizontally, then vertically (x,y)
// so, for each texture
// read the x row, then increment y, repeat
// create a list to store merged pixels
List< Color > mergedPixels = new List< Color >();
// increment y last
for ( int y = 0; y < height; y++ )
{
// for each texture
for ( int i = 0; i < _PhotoCount; i++ )
{
// read x, add to list. do x for each texture, then increment y
for ( int x = 0; x < width; x++ )
{
// index of current mergedPixels colour
int c = x + ( width * y );
mergedPixels.Add( allTexturePixels[ i ][ c ] );
}
}
}
// generate merged texture
_DestTex = new Texture2D( width * _PhotoCount, height, TextureFormat.ARGB32, false );
_DestTex.SetPixels( mergedPixels.ToArray() );
_DestTex.Apply();
Your answer
Follow this Question
Related Questions
Path Trigger Animation 1 Answer
Overlaying an image over a certain camera? 1 Answer
Clarification with Texture2D.ReadPixels. 1 Answer
Texture Size doesn't match collider size 0 Answers
Render texture on slim plane 1 Answer