Notifications
Article
How to bake local vertex position into vertex colours using Blender
Published 6 months ago
161
0
A simple introduction to baking custom data in Blender
This is a technique we've used to simplify our shader calculations. The idea is to bake the vertex position into vertex colours, so rather than having to do any calculations on the fly we can just use the vertex colour directly. We mainly use this to create a parameterized gradient from the bottom to the top of foliage so in order to simplify things I'm just going to show how to bake a single coordinate. This same technique could be applied to all x, y and z coordinates however if necessary.

Getting started

We're going to start with the default Blender start-up file. Take the starting cube a subdivide it a few times so we have some vertices to play with. It's also a good idea to set the origin point to the base of the object because the local vertex position is relative to the origin, so anything below the origin will be negative and this'll make your remapping function more complicated. Next up we'll do a quick UV unwrap using Smart UV Project. To do this go into edit mode, press "u" and select Smart UV Project. It's probably a good idea to increase the island margin to 0.1 or so. Finally, make sure you set the renderer to Cycles for the next step.

Setup your Cycles graph

We're going to use the Cycles render node graph in order to setup our desired look. With Cycles set as the renderer, go to the Material tab of the Properties pane, and click Use Nodes. Now replace your Timeline pane with a Node editor since we won't be using it. Now with the Node editor open we're going to create our Node graph. Create a ColorRamp node and plug that into the Diffuse BSDF color input. This will allow us to map a value (the vertex position) to a color in a gradient across the object. Next, create a Gradient Texture node and plug the Fac output into the Fac input on the ColorRamp. This generates a procedural intensity gradient and maps it across generated coordinates, which isn't quite what we're looking for. So the next step is to feed it our local vertex position instead of using generated coordinates. We can fix this by creating a Texture Coordinate node and plugging the Object output into the Gradient Texture Vector input. However the problem with this is we have no control over the gradient's size, start position, orientation, etc. So to fix that we're going to add a Mapping node inbetween the Texture Coordinate and Gradient Texture Nodes. Now you can translate, rotate and scale your values in order to get the Gradient you're looking for. I recommend playing with the Gradient Texture type and ColorRamp as well in order to achieve your desired goal. In my example I'm using black and white to signify the bottom of the object to the top of the object, but if you wanted to bake multiple coordinates at once you would use one channel (R, G, or B) per coordinate instead. In the end this is what my graph looks like:

Bake to a texture

The next step is to bake our gradient to a texture. Open the UV/Image editor and create a new image. Add an Image Texture Node to the Node editor and assign your newly created texture to it. This is so the Bake operation knows which texture to bake to. Go to the Render tab of the Properties pane and open up the Bake section. Switch your Bake Type to Diffuse and turn off the Direct and Indirect lighting options (only Color should be enabled). Now hit Bake and you should see your new texture appear in the UV/Image editor. At this point I like to save this image to disk because we're going to switch renderers and in my experience it's not uncommon for Blender to get confused and lose your texture if it's only sitting in memory.

Switch to Blender Render and bake to Vertex Color

After saving our texture to disk we're going to switch to Blender Render. Next, create a new material in case we need to go back to Cycles later to re-bake. In the UV/Image editor, open your saved texture from the previous step (I'm actually not certain if this step is necessary). If you switch your 3D View to textured mode you should now see your baked texture applied to your object. Next go to the Texture tab of the Properties pane and add a new texture slot for the current material. Set this to an Image and open your saved texture from the previous Bake step. Go to the Data tab of the Properties pane and add a color data channel under Vertex Colors. Finally, go to the Render tab of the Properties pane and expand the Bake section. Set the Bake Mode to Textures and check the box Bake to Vertex Color and click Bake. Now if you go to Vertex Paint mode in the 3D View you'll see your gradient is now baked into your vertex color data.
So with that we're done. Hopefully this helped some of you save some time and frustration. Please let me know if anything needs further clarification and thanks for reading!
Bronson Zgeb
Game Developer - Programmer
2
Comments