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
0
Question by Mizipzor · May 12, 2014 at 02:57 PM · materialtexture2dmeshrenderer

How to apply a generated texture to a mesh?

I'm trying to render a randomly generated texture.

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(MeshFilter))]
 [RequireComponent(typeof(MeshRenderer))]
 public class RandomTexture : MonoBehaviour {
 
     void Start () {
         BuildQuad();
         BuildTexture();
     }
 
     void BuildTexture() {
     
         Texture2D texture = new Texture2D(16, 16, TextureFormat.RGBA32, false);
 
         // fill texture with random color
         for (int x = 0; x < texture.width; x++) {
             for (int y = 0; y < texture.height; y++) {
                 texture.SetPixel(x, y, Random.Range(0, 2) == 0 ? Color.red : Color.blue);
             }
         }
         texture.Apply();
 
         // create a material holding the texture
         MeshRenderer meshRenderer = GetComponent<MeshRenderer>();
         Material material = new Material(Shader.Find("Sprites/Default"));
         material.color = Color.white;
         material.SetTexture("texture", texture);
 
         // apply material to mesh
         meshRenderer.sharedMaterial = material;
         renderer.material.mainTexture = texture;
     }
 
     void BuildQuad() {
         Mesh mesh = new Mesh();
         // [...]
         // create vertices, triangles, normals and uv for a single quad
         
         MeshFilter meshFilter = GetComponent<MeshFilter>();
         meshFilter.mesh = mesh;
     }
 }

At the end of the BuildTexture method there are two statements that I don't understand.

  • If I remove meshRenderer.sharedMaterial = material the quad is not rendered at all (or is invisible).

  • If I remove renderer.material.mainTexture = texture the quad is rendered completely white.

  • If I keep both lines the quad is rendered as expected.

What is the difference between the two statements? To me it looks like one or the other should be enough. Or is there a better way to do this that I'm missing?

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
2
Best Answer

Answer by supernat · May 12, 2014 at 03:20 PM

There is maybe a confusion due to the name of your local variable. As you know, all MonoBehaviour scripts are derived from the Component class, and that class contains properties to let you access standard components on the game object (like renderer, material, gameObject, collider, etc). Since you named your local variable "Material material", it might seem confusing to you, because you are changing the material and expect it to be applied to the current game object. Really you are just creating an object "material" locally that is not applied to anything yet. That is why you have to say "meshRenderer.sharedMaterial = material". In fact, although I don't have the full details, I suspect you can just say "renderer.sharedMaterial = material" instead.

So, another common question is why use renderer.sharedMaterial versus renderer.material, and that is dependent on your need. sharedMaterial is the material that is shared between all game objects that use this material, so to update the texture on all game objects, you would say "renderer.sharedMaterial.mainTexture" but to update only this object "renderer.material.mainTexture".

Your second question is probably due to the SetTexture() call. Unless you've renamed your texture in your shader, you probably meant SetTexture("_MainTex", texture); See this: http://docs.unity3d.com/Documentation/ScriptReference/Material.SetTexture.html Basically your SetTexture call is doing nothing, because it can't find "texture". That's why you have to set it via renderer.material.mainTexture (which actually is the preferred way for the main texture I think).

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 Mizipzor · May 20, 2014 at 01:11 PM 0
Share

I think it was indeed the SetTexture call that got me confused, correcting that it's much easier to understand what the other assignments do. [Edit] Sadly, I cannot upvote your answer.

avatar image supernat · May 20, 2014 at 03:58 PM 0
Share

That's okay, no upvotes needed, just glad I can provide an answer, hopefully it will help someone down the line too (so thanks for marking it answered).

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

21 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

Related Questions

Assigning script-generated textures 0 Answers

Configure standard shader/material with Texture2Darray: how? 1 Answer

Apply Generated Texture to Mesh 1 Answer

How to render a sprite to a quad? 1 Answer

Changing gameobject material via mouse click not working 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