• 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 /
  • Help Room /
avatar image
0
Question by 334499p · Mar 26, 2016 at 09:45 PM · spritetexture2dwebcamtexturegetpixelssetpixel

WebCamTexture to circular sprite

Hi, I'm creating an application for iOS and android and need to capture a picture to be used as a projectile. The user is displayed a video feed (working) and when they press the video feed a picture will be taken and cut into a circle shape by first converting the picture into a texture2d. When I test it on my phone the video feed works correctly but when I capture the picture and try to make it a circle I receive an output of a gray square.

     void finalizeSpritePicture(){
         if (camTexture != null) {
             IntPtr pointer = camTexture.GetNativeTexturePtr ();
             Texture2D camTexture2D = new Texture2D (256, 256, TextureFormat.ARGB32, false, true);
             camTexture2D.UpdateExternalTexture (pointer);
             camTexture2D = CalculateTexture (camTexture2D);
             profileSprite = Sprite.Create (camTexture2D, new Rect (0, 0, 256, 256), new Vector2 (128, 128));
             profileImage.sprite = profileSprite;
             camTexture.Stop ();
         } else {
             Debug.Log ("Failed to capture picture: No Camera Feed");
         }
     }
 
 
     Texture2D CalculateTexture(Texture2D sourceTexture){
         Color[] c = sourceTexture.GetPixels (0, 0, sourceTexture.width, sourceTexture.height);
         Texture2D b = new Texture2D (256, 256);
         for (int i = 0; i < (256 * 256); i++) {
             int y = Mathf.FloorToInt (((float)i) / ((float)256));
             int x = Mathf.FloorToInt (((float)i - ((float)(y * 256))));
             if (256 * 256 >= (x - 128) * (x - 128) + (y - 128) * (y - 128)) {
                 b.SetPixel (x, y, c [i]);
             } else {
                 b.SetPixel (x, y, Color.clear);
             }
         }
         b.Apply ();
         return b;
 
     }

Please provide help as to why the webcam picture cannot be cut into a circle and turned into a sprite. Thank you!

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

Answer by Andrea_Marchetti · May 27, 2017 at 09:39 AM

use a layer mask!

Comment
Add comment · 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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

69 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

When I run getPixel32 () in WebcamTexture, only black pixels are returned. 1 Answer

Switch Texture Type To Sprite. 0 Answers

texture.setpixel is making a blend of colors instead of solid colors (help) (examples provided) 0 Answers

Is it possible to create a texture that hides a specific texture? 0 Answers

resizing texturerect on update of animated 2D sprite 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges