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?
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.
answered Nov 18 '09 at 10:09 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)
answered Nov 18 '09 at 08:28 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.
answered Nov 18 '09 at 09:41 AM