3
$\begingroup$

I'm using the Blender Cycles engine to create graphics for a game; specifically, I want to display a grid of coloured ellipsoid counters for the player to move around. To avoid creating a separate image for each colour, I want to store a single greyscale image in the resources, and apply a colour tint at runtime:

greyscale output

However, with this approach, the specular highlights would also be tinted; so I would like to store the highlights as a separate image with an alpha channel, and overlay it on top of the tinted image.

I've tried Googling for ways to do this in Blender, and cannot find anything. The best solution I can think of is to set the glossy BSDF colour to red (1.0, 0.0, 0.0) and the diffuse BSDF colour to green (0.0, 1.0, 0.0):

glossy and diffuse BDSFs red and green channels

I would then need to create a mask from the red channel, and create base and overlay greyscale images from the green and red channels respectively, either as a manual step in Gimp, or (better) store a single image in the resources and do the channel operations in code:

greyscale from green channel greyscale from red channel

While I realise that I'm probably subverting the properties of light for the sake of coding simplicity, I am concerned that I may not be getting the most believable results. Given that the rendering engine knows more about the light paths than I do from the final image, is there a way to do this in Blender?

Thanks!

UPDATE

For posterity, these are the material nodes I ended up with:

material nodes

With a further change to adjust the alpha channel on the highlight image, these are the compositing nodes I created:

compositing nodes

Finally, this is the end result, after tinting and overlaying in the game source code:

enter image description here

$\endgroup$
  • $\begingroup$ You need to research about "render layer passes" which can be exported separately. $\endgroup$ – Leander Jun 10 at 18:43
  • $\begingroup$ Hi Leander, thanks for your comment. I checked the Cycles render passes page, but it doesn't have any instructions on how to go about exporting separate images. $\endgroup$ – Huw Walters Jun 11 at 7:49
  • $\begingroup$ If you meant the Blender Render passes page, which has better instructions, that's no good; I wanted to use the Cycles engine. $\endgroup$ – Huw Walters Jun 11 at 7:51
  • $\begingroup$ Look at the second image of the answer you have, you can see the out-sockets of the renderlayer node named diff / gloss, to output them separately, use a file output node. $\endgroup$ – Leander Jun 11 at 8:01
  • $\begingroup$ Thanks. The Blender docs appear to be more reference than tutorial; but with Jackdaw's answer and your further suggestions, I've (mostly) got it working. $\endgroup$ – Huw Walters Jun 11 at 22:12
3
$\begingroup$

You should add in the specular with color, not mix shaders. In that way you will get a white highlight.

Here I'm using color ramps to make a mask for transparency and removing some of the gray on the specular before adding it to a solid color.

enter image description here

You can also render out Diffuse and Glossy and use the Compositor to choose color.

enter image description here

You need to add light passes for gloss direct and diffuse direct enter image description here

The new passes can be then be exported from the Compositor via Image editor (set the image to be "Viewer Node") and then saved from there Image->Save As...

enter image description here

$\endgroup$
  • $\begingroup$ Thanks Jackdaw; however, I want to apply the diffuse colour in the Java code for the game I'm writing, not in Blender (this way, I only have to store one set of images, and can generate colour at runtime, from the game board dimensions). $\endgroup$ – Huw Walters Jun 11 at 7:30
  • $\begingroup$ You said "You can also render out Diffuse and Glossy and use the Compositor to choose color". Assuming I can save separate images to disc, that's exactly what I'm trying to do; could you give me some pointers on how to go about it? (if that was obvious from your screenshots, they do not appear to have uploaded successfully!) Thanks. $\endgroup$ – Huw Walters Jun 11 at 7:32
  • $\begingroup$ I have updated the answer with details about how to export the images. You will need to do the composition in your game but the math should be the same. $\endgroup$ – Jackdaw Jun 11 at 16:28
  • $\begingroup$ Also... you can set the rendering to be transparent (Scene->Film->Transparency) and then add the alpha to one or both of the images via the Viewer node. $\endgroup$ – Jackdaw Jun 11 at 16:49
  • 2
    $\begingroup$ Worked it out, thanks to the (curiously unvoted) answer to this question. I routed the gloss and alpha outputs from the "render layers" compositing node to a "set alpha" node, and routed its output to a "viewer" node. $\endgroup$ – Huw Walters Jun 11 at 22:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.