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?
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.
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)
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.
@Ashkan, just wanted to thank you for this great answer - still the best after two years!
@Fattie You are welcome
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.
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Answers and Comments
2 People are following this question.
Black graphical errors appearing on textures
Is the final look of a model,in-game work done in a modeling software or inside unity?
Render Single Camera to Two Displays
Grass rendering problem
Painting on Textures