Approximate a texture by vertex colors

If you feel like you're up to the challenge of building your own Ages in Blender or 3ds Max, this is the place for you!

Approximate a texture by vertex colors

Postby Christian Walther » Sun Jul 06, 2008 9:32 am

I've come up with something today that some of you might find useful. I have no idea whether such a thing already exists somewhere, maybe even built-in in Blender, but writing this was a fun exercise so I didn't bother finding out.

texture2vertexcol.zip
(2.47 KiB) Downloaded 323 times

Contained in the attached ZIP file is a Blender Python script that can generate vertex colors that approximate a texture image. This can be useful for example for making vertex-color-based shading, by baking shadows or ambient occlusion to a texture, then running this script on it.

Installation
  • The numpy package is required for the number crunching. You can find out if you already have that by running python -c "import numpy" on the command line - if nothing happens, you have it, if you get an ImportError, you don't. In the latter case, get it from your package manager or from http://numpy.scipy.org/. I happened to already have it here on Mac OS X, I don't remember whether I had installed it myself or whether it came with the OS.
  • Place the unzipped script file in your Blender scripts folder. On Mac OS X and other Unixes that's ~/.blender/scripts; I'm not going to risk a guess where it is on Windows since that seems to be a complicated matter.
  • If Blender is running, open a Scripts window and choose Update Menus from the Scripts menu to make the new script show up in the menus.

Usage
Select a UV-mapped mesh object, and open the image you want to approximate in the UV/image editor. Then choose Texture to Vertex Colors from the Mesh > Scripts (or Scripts > Mesh) menu. The script takes a few seconds to run (probably a few minutes for more complex meshes). When it's done, the result has been stored into the selected vertex color layer of the object, or a new one if there wasn't any before. (I have not quite figured out yet what you need to do to make Blender display new vertex colors and/or textures in the textured 3D view. I usually achieve it by randomly fiddling around a bit.) To get satisfactory results, several iterations of tweaking and subdividing the mesh and the UV mapping may be needed. Remember that for PyPRP to use the vertex colors, you need to add a second layer named Alpha (filled with white if you don't use it).

Have fun, and let me know what you think!
Christian Walther
 
Posts: 443
Joined: Sun Jun 08, 2008 3:10 am
Location: Switzerland

Re: Approximate a texture by vertex colors

Postby Kato » Sun Jul 06, 2008 9:49 am

Oooh, neat. I was wondering a long time ago if something like this (though I think I was wanting to go in the other direction) existed. This could prove useful! :)

I'll try it out soon :)

-Kato
Image
(explorer card designed and created by me)
User avatar
Kato
 
Posts: 315
Joined: Fri Sep 28, 2007 8:02 pm
Location: South USA

Re: Approximate a texture by vertex colors

Postby Nadnerb » Sun Jul 06, 2008 10:33 am

Just as a note, since Kato mentioned it, if you want to go in the other direction, (ie, vertex colors to uvmapped texture) there are a couple ways to go about it. One is to make a non repeating uvmap on the mesh, clear the material and set it to VCol paint and then bake that material to the texture.

Another way is to use a simple gradient UV texture (such as the default "blend" type texture in the textures panel) and use a script (like this one that I wrote) that replaces the u vales with the lightness values from the vertex colors for each vertex. Cyan uses this method to make stencils that look like they were painted by vertex colors.
Image
Live KI: 34914 MOULa KI: 23247 Gehn KI: 11588 Available Ages: TunnelDemo3, BoxAge, Odema
Nadnerb
 
Posts: 1057
Joined: Fri Sep 28, 2007 8:01 pm
Location: US (Eastern Time)

Re: Approximate a texture by vertex colors

Postby Christian Walther » Mon Jul 14, 2008 1:23 am

Here is version 1.1: texture2vertexcol.py. It now takes into account sharp edges, i.e. allows discontinuous vertex colors along them.

And a companion: vertexcoltexturediff.py. It takes the vertex colors and texture image and computes their difference into a new image. Helpful to determine which parts of the mesh need tweaking.

Finally, a screenshot:
image Show Spoiler

At the bottom left, ambient occlusion baked to a texture. On the model at the top, vertex colors generated from that texture (and no other texturing or shading). At the bottom right, the difference between the vertex colors and the original texture: middle gray means no difference, lighter means vertex color is lighter than texture, darker means vertex color is darker than texture. Differences close to zero are exaggerated to get a clearly visible boundary between darker and lighter areas.
Christian Walther
 
Posts: 443
Joined: Sun Jun 08, 2008 3:10 am
Location: Switzerland


Return to Building

Who is online

Users browsing this forum: No registered users and 1 guest

cron