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 Opde · Jul 17, 2014 at 12:38 PM · texturespritematerialmeshrenderer

How to render a sprite to a quad?

I select a sprite to render on a quad. I use the following code:

 public Sprite       m_sprite;

 void Awake()
 {
     m_renderer  = GetComponent< MeshRenderer >();
     m_mesh      = GetComponent< MeshFilter >().mesh;
     m_material  = m_renderer.material;

     Rect spriteRect = m_sprite.textureRect;

     spriteRect.x /= m_sprite.texture.width;
     spriteRect.width /= m_sprite.texture.width;
     spriteRect.y /= m_sprite.texture.height;
     spriteRect.height /= m_sprite.texture.height;

     // Log.Info("spriteRect {0} num UVs {1}", spriteRect, m_mesh.uv.Length);

     // m_mesh.uv[0] = new Vector2(spriteRect.x + 0.0f,             spriteRect.y + spriteRect.height);
     // m_mesh.uv[1] = new Vector2(spriteRect.x + spriteRect.width, spriteRect.y + spriteRect.height);
     // m_mesh.uv[2] = new Vector2(spriteRect.x + 0.0f,             spriteRect.y + 0.0f);
     // m_mesh.uv[3] = new Vector2(spriteRect.x + spriteRect.width, spriteRect.y + 0.0f);

     m_mesh.uv[0] = new Vector2(0.0f, 1.0f);
     m_mesh.uv[1] = new Vector2(1.0f, 1.0f);
     m_mesh.uv[2] = new Vector2(0.0f, 0.0f);
     m_mesh.uv[3] = new Vector2(1.0f, 0.0f);

     m_material.mainTexture = m_sprite.texture;
 }

 MeshRenderer        m_renderer;
 Mesh                m_mesh;
 Material            m_material;


Update 1: as stevethorne pointed out I need the UVs but the texture is scramble even with the default UVs in the code above. Why is that does the shader or material need certain UVs?

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 stevethorne · Jul 17, 2014 at 01:12 PM 1
Share

You'll want to get the $$anonymous$$esh of the quad and set the uvs so that it only renders the portion of the texture that you want.

avatar image Opde · Jul 18, 2014 at 11:23 AM 0
Share

stevethorne: Thank you for your comment. I updated the question.

avatar image diegzumillo · Feb 13, 2017 at 04:05 AM 0
Share

Did you get this to work?

1 Reply

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

Answer by robertbu · Jul 18, 2014 at 01:53 PM

I believe that reading from mesh.uv gives you a copy of the array, not a reference to the array, so you need to do it this way:

 Vector2[] uvs = m_mesh.uv;
 uvs[0] = new Vector2(0.0f, 1.0f);
 uvs[1] = new Vector2(1.0f, 1.0f);
 uvs[2] = new Vector2(0.0f, 0.0f);
 uvs[3] = new Vector2(1.0f, 0.0f);
 m_mesh.uv = uvs;

Why is that does the shader or material need certain UVs?

The uvs tell the shader what part of the texture to render for each triangle.

At first glance, it appears that your commented out calculations for uvs should work.

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 Opde · Jul 21, 2014 at 11:43 AM 0
Share

Good hint. Even though a lot more was wrong like the shader, this answer helped me.

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

Created Material Shows Dark Sprite 1 Answer

How to scale specific part of texture when game object scales? 0 Answers

Create sprite renderer using custom material 2 Answers

Overlay texture on sprite 0 Answers

Material/texture striped in some places on mesh 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