Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by akisrn · May 27, 2016 at 02:00 AM · cameratexturepanorama

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;
         
     }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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();
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image akisrn · May 27, 2016 at 04:48 AM 0
Share

Dude this totally did the trick! Thank you so much!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

59 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges