Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 PsyDev · Dec 09, 2015 at 09:18 PM · texturematerialimage effectsimage effect

How to set a texture in an image effect material?

I'm trying to set the texture via code on a material used in a post-process image effect. I'm calling SetTexture() on a reference to the material, but it's not having any effect.

In this example, myMaterial has a shader that has a texture property. Just for this test, the shader simply overwrites the output with whatever color is in the texture.

 [ExecuteInEditMode]
 public class MyImageEffect : MonoBehaviour
 {
     public Material myMat;
     public Texture myTexture;
 
     void Start()
     {
         myMat.SetTexture(0, myTexture);
     }
 
     void OnRenderImage(RenderTexture source, RenderTexture destination)
     {
         Graphics.Blit(source, destination, myMat);
     }
 }

This all works fine if I drag a texture into the material's texture property before running. However, if I try to assign the texture at runtime, it does not work.

For non-image-effect materials, you can change the texture on a material by grabbing the material from an object's renderer:

     void Start () 
     {
         thisRenderer = GetComponent<MeshRenderer>();
         thisRenderer.sharedMaterial.SetTexture(0, myTex);
     }

In the case of a material used in a image effect, there is no renderer to grab the material off, so I' not sure if I'm accessing it the correct way.
Any help would be appreciated.

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

Answer by helarts · Dec 09, 2015 at 11:46 PM

However, if I try to assign the texture at runtime, it does not work.

Maybe because you set the texture only once at Start(), try to do it before the Blit like so:

     void OnRenderImage(RenderTexture source, RenderTexture destination)
     {
              myMat.SetTexture(0, myTexture);
              Graphics.Blit(source, destination, myMat);
     }

Also are you sure your texture ID is 0 ? I would use the property string directly if I have troubles, guessing the sampler2D is called "_MainTex" in the shader:

 myMat.SetTexture("_MainTex", myTexture);

If you really want to use an int (which is faster for repetitive calls) check Shader.PropertyToID, it should be much safer than hardcoded ID.

Edit: in Edit mode you might have to manually refresh the game view to see changes, this redraws all views:

 UnityEditorInternal.InternalEditorUtility.RepaintAllViews()
Comment
Add comment · Show 4 · 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 PsyDev · Dec 10, 2015 at 03:14 PM 0
Share

Thanks for the suggestions @helarts , that really helped clear things up for me. I mistakenly thought that texture IDs were in order of declaration in the shader starting from zero. I see now that is not the case. So setting the texture by name does work. And retrieving the actual id with Shader.PropertyToID then using that id also does work.

You only need to set the texture once in the Start(), not every frame.

I appreciate you taking the time to reply.

avatar image helarts PsyDev · Dec 10, 2015 at 04:14 PM 0
Share

Sure, like you figured out shader property IDs are global, not per shader. You are right, setting the texture once is enough if you don't want to change it at runtime (I though it was something you wanted to do). I am glad you got it working the way you want.

avatar image PsyDev helarts · Dec 10, 2015 at 05:30 PM 0
Share

In my case, I am updating the texture every frame, but I have only set it once in the Start(). It is a RenderTexture that a camera draws into, then I use it in an image effect. The image is updating correctly each frame.

Show more comments

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

36 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

Related Questions

Image loaded with LoadImageIntoTexture gets inverted 0 Answers

Material/Texture on the Inside of a Sphere 5 Answers

Cutout Material is not showing texture transparency? 0 Answers

Blending between arbitrary numbers of textures on the same material 0 Answers

Repeating Material texture 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