Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by TGPTIAlves · Oct 15, 2014 at 12:14 PM · crop

Cropping Texture2D using a Rect

Hello,

This is getting me crazy! I have texture2D (which is an image from a camera) and somewhere on my code I do some processing and I come up with a Rect, which should be used to crop te image and select only a portion of it.

To show the entire image I'm using the following (placed on OnGUI function):

 Texture2D texture = k.getTexture2D(Mode.RGB);
 GUI.DrawTexture(new Rect(0, 0, texture.width, texture.height), texture);

The texture is showing the RGB image from the camera. As stated, I have a rect and I want to crop the texture i get from the camera. For that I'm doing as follow:

 Rect r = getUserRect(skeletonID);
 if (r.width != 0 && r.height != 0)
 {
      MonoBehaviour.print(r);
      Texture2D result = new Texture2D((int)r.width, (int)r.height);
      result.SetPixels(color.getTexture2D().GetPixels(Mathf.FloorToInt(r.x), Mathf.FloorToInt(r.y), Mathf.FloorToInt(r.width), Mathf.FloorToInt(r.height)));
      result.Apply();
 }

After this I show the cropped area(inside the OnGUI function):

 if (user != null)
 {
      GUI.DrawTexture(new Rect(0, 0, result.width, result.height), result);
 }


This is not working and I don't know why. The cropped area seems to stucked at y = 0, but if I draw onlye the Rect (instead on cropping) the cropped area is fully filled with the rect (to do this I do as follow):

 void DrawQuad(Rect position, Color color)
 {
      Texture2D texture = new Texture2D(1, 1);
      texture.SetPixel(0, 0, color);
      texture.Apply();
      GUI.skin.box.normal.background = texture;
      GUI.Box(position, GUIContent.none);
 }
 
 void OnGUI()
 {
      ...
      DrawQuad(r, Color.black);
      ...
 }


Any ideias? Thanks!

EDIT: Code overview

 void OnGUI()
 {
      Texture2D texture = k.getTexture2D(Mode.RGB);
      GUI.DrawTexture(new Rect(0, 0, texture.width, texture.height), texture);
 
      Rect r = getUserRect(skeletonID);
      if (r.width != 0 && r.height != 0)
      {
           MonoBehaviour.print(r);
           Texture2D result = new Texture2D((int)r.width, (int)r.height);
           result.SetPixels(texture.GetPixels(Mathf.FloorToInt(r.x), Mathf.FloorToInt(r.y), Mathf.FloorToInt(r.width), Mathf.FloorToInt(r.height)));
           result.Apply();
 
           GUI.DrawTexture(new Rect(0, 0, result.width, result.height), result);
 
      }
 
 }

Comment
Add comment · Show 3
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 CHPedersen · Oct 15, 2014 at 01:57 PM 0
Share

It's hard to figure out exactly what happens when we can't see the code in getUserRect. :)

But my idea is that this is some confusion between the orientation of texture coordinates and that of GUI coordinates. If getUserRect is an implementation of some lasso-function that allows the user to drag out a rect on screen, then you should beware that the returned coordinates will have their origin in the upper left corner, while GetPixels and SetPixels work from bottom and up. Perhaps this is at least part of the reason it doesn't work at expected?

avatar image TGPTIAlves · Oct 15, 2014 at 02:02 PM 0
Share

I substituted the getUserRect with new Rect(320, 240, 320, 240). Since Texture I get from the camera is 640x480, I think I should get the lower right corner of the image, but what I'm getting is the upper right corner of the image. What am I doing wrong?

avatar image TGPTIAlves · Oct 15, 2014 at 02:16 PM 0
Share

Replacing that line with these ones: return new Rect(0, 0, 320, 240); //expected lower left, but got upper left return new Rect(320, 240, 320, 240); //expected upper right, but got lower right return new Rect(0, 240, 320, 240); //expected upper left, but got lower left return new Rect(320, 0, 320, 240); //expected lower right, but got upper right

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Animation editor - trimming tail 3 Answers

Generate objects in a square? 0 Answers

How to not scale my Texture? 1 Answer

jibjab (insert your own face) implementation in Unity 0 Answers

crop an image from WWW 0 Answers


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