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 klolololol · May 08, 2015 at 10:17 AM · texture2dgradientsetpixelssetpixel

Setpixels creates gradient texture instead of raw colored pixels

Hi!

I'm using Texture2D's setPixels() method to generate a texture. I want to turn my data like:

 000000002222211
 112222200011221
 ...

to an image like this

alt text

However my result looks like this (I've added a black frame to make it more visible):

alt text

I think it must be some kind of shader thing I have to change, it looks like when you say in openGL (it's just a pseudo something):

 var topLeft  = vec2(0, 0)
 var topRight = vec2(0, 1)
 var botLeft  = vec2(1, 0)
 var botRight = vec2(1, 1)
 
 var p = Primitive.Quad(topLeft, topRight, botLeft, botRight)
 
 p.setColors(Color.brown, Color.bown Color.white, Color.white)


Here's my Unity Code for generating the texture:

 using UnityEngine;
 using System.Collections;
 
 public class MiniMap : MonoBehaviour {
     public static int WHITE         = 0;
     public static int BROWN         = 1;
     public static int DEEP_BROWN    = 2;
 
     public SpriteRenderer myRenderer;
     public Map map;
 
     Texture2D texture;
     Sprite sp;
 
     Color c_WHITE       = Color.white;
     Color c_BROWN       = new Color(204, 139, 0);
     Color c_DEEP_BROWN  = new Color(148, 101, 0);
 
     void Start() {
         texture = new Texture2D(30, 15);
     }
 
     void Update() {
         int[][] minimapInfo = map.getMinimapInfo(0);
         Color[] pixels = new Color[30*15];
 
         Color color = Color.red;
         for(int i = 0; i < 30; ++i) {
             for(int j = 0; j < 15; ++j) {
                 if(WHITE == minimapInfo[i][j]) {
                     pixels[i*15 +j] = c_WHITE;
                 }
                 else if(BROWN == minimapInfo[i][j]) {
                     pixels[i*15 + j] = c_BROWN;
                 }
                 else if(DEEP_BROWN == minimapInfo[i][j]) {
                     pixels[i*15 + j] = c_DEEP_BROWN;
                 }
 
             }
         }
 
         texture.SetPixels(pixels, 0);
 
         // Apply all SetPixel calls
         texture.Apply();
         sp = Sprite.Create(texture, new Rect(0, 0, 0.5f, 1f), new Vector2(0.0f, 0.0f), 0.1f);
         myRenderer.sprite = sp;
     }
 }
 

nevtelen.png (4.5 kB)
gradient.png (1.3 kB)
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
1
Best Answer

Answer by Bunny83 · May 08, 2015 at 11:06 AM

I almost never used the 2d stuff in Unity (at least the Sprite stuff). However the only thing i can think of which could cause this is that the second "Rect" parameter of Sprite.Create doesn't represent UV coordinates but pixel coordinates. So if this is true that means you only use 1 pixel in y and the next pixel is bleeding into the texture from above. Try using:

 Sprite.Create(texture, new Rect(0, 0, 15f, 15f), new Vector2(0.0f, 0.0f), 0.1f);




Comment
Add comment · Show 2 · 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 klolololol · May 08, 2015 at 11:48 AM 0
Share

thanks for the quick response mate, that was the problem, the reference doesn't say much about that property btw.

avatar image Bunny83 · May 08, 2015 at 03:49 PM 0
Share

@klolololol: Yeah, i know the documentation for some classes / methods is just like a stub. However due to the fact that sprites use a lot per-pixel values (like the borders) and you got that result i just assumed it might be the problem ^^.

You might want to file a bug report in Unity (Help->Report a bug). You can choose "documentation" as category.

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

20 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

Related Questions

Fastest rendering of 2D world with lots of changes to screen every frame? 3 Answers

Drawing a solid circle onto texture 2 Answers

For different texture sizes, which is faster? SetPixel or SetPixels? 2 Answers

Best Method for Setting Mass Numbers of Pixels (SetPixel, SetPixels) 2 Answers

SetPixels32 called with invalid number if pixels in the array 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