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?

more ▼

asked Nov 18, 2009 at 08:04 AM

flexrails gravatar image

766 74 65 88

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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.

more ▼

answered Nov 18, 2009 at 10:09 AM

Kuba gravatar image

721 2 10 12

(comments are locked)
10|3000 characters needed characters left

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)

more ▼

answered Nov 18, 2009 at 08:28 AM

Ashkan_gc gravatar image

10.1k 77 101 164

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

Oct 14, 2011 at 07:08 AM Fattie

@Fattie You are welcome

Oct 14, 2011 at 07:18 AM Ashkan_gc

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.

Nov 18, 2009 at 09:47 AM duck ♦♦
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Nov 18, 2009 at 09:41 AM

duck gravatar image

duck ♦♦
47.8k 131 233 472

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: Nov 18, 2009 at 08:04 AM

Seen: 15398 times

Last Updated: Sep 09 at 05:19 PM