Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
4
Question by flexrails · Nov 18, 2009 at 08:04 AM · graphicstexturesrendertexturecharactercustomizationmultimaterial

modify textures at runtime?

how do i modify/paint a texture at runtime. e. g. i'd like to change color of texture parts like hair, skin. or i'd like to change the color of a sphere, based on user input. how would i do that?

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

3 Replies

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

Answer by Kuba · Nov 18, 2009 at 10:09 AM

Judging from your use case, I don't think you actually want to change the texture itself. If you want to change the colour of an object that uses a particular texture, just use a shader that uses a Main Colour and a Base (RGB) texture, e.g. a Diffuse shader. If you want to change the colour of only the parts of your object, in your 3D modelling app you can assign different materials to those parts (vertex groups). Unity will pick up the fact that the object uses different materials, and then you can change the colour only on the parts you want.

In any case, here's what you have to do if you really need to change the texture itself:

The fast way to do it is to use the GPU to modify the colours. You will need a helper camera that will render to a RenderTexture of the size of your texture.

You don't need to render any objects yourself, just use the image effects technique (like in e.g. EdgeDetectEffect in Pro Standard Assets) - Graphics.Blit(). That will render the full screen quad for you with a specified material. Shader from that material will get the source texture (so the one that you want to modify) as *MainTex property, and the destination texture will be the active render texture.

For this helper camera set a RenderTexture that you have created in your code as a target render texture, disable the camera (so that it doesn't render automatically). Then from your script on the main camera in OnPreRender() method call helperCamera.Render(). Last step - instead of using your original texture on the object that you want to be modified - use the render texture to which the helper camera has rendered.

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

Answer by Ashkan_gc · Nov 18, 2009 at 08:28 AM

for texture modification you can use Texture2d class's functions like setpixels and getpixels

you should do all of your setpixels and then call apply function.

you can copy an image on to another by getting the source image pixels and copying them to destination by setpixels. for some cases you can use the color property of the material to change the colors.

if you want to change certain parts of the texture based on ray tracing or collision you can use raycasthitinfo argument of raycast functions to see what UV position of the object is hit by ray and then copy something there (e.g blod)

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 duck ♦♦ · Nov 18, 2009 at 09:47 AM 3
Share

An important change introduced in Unity 2.6 means that if you want to be able to use SetPixels, GetPixels, or any other Texture2D function which accesses the texture data, you need to set the texture to be "Is Readable" in the inspector pane for that texture.

Textures are set as NOT readable by default, because it consumes less memory.

avatar image Fattie · Oct 14, 2011 at 07:08 AM 0
Share

@Ashkan, just wanted to thank you for this great answer - still the best after two years!

avatar image Ashkan_gc · Oct 14, 2011 at 07:18 AM 0
Share

@Fattie You are welcome

avatar image hoseinharami · Feb 04, 2017 at 11:51 AM 0
Share

Thank You Very $$anonymous$$uch ! I just needed to apply my changes ! thanks ;)

avatar image
1

Answer by duck · Nov 18, 2009 at 09:41 AM

Another way to achieve this would be to use a separate material for each part (although they can share the same texture if you're using a texture atlas), and then you can simply modify the color property to tint the material.

See Material.SetColor() for more information.

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.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to Render to Fullscreen Texture pixel perfect 2 Answers

dynamic hole in layer / texture / camera 0 Answers

Modify Gbuffer Layout / Add RenderTarget 2 Answers

HDRP Texture issue with LitTesselation. Textures appear Blurry 1 Answer

Graphics.ExecuteComputeBuffer only works the first time 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