3

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

Huw Walters
  • 133
  • 5

1 Answers1

3

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

Jackdaw
  • 4,099
  • 11
  • 23
  • 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). – Huw Walters Jun 11 '19 at 07:30
  • 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. – Huw Walters Jun 11 '19 at 07:32
  • 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. – Jackdaw Jun 11 '19 at 16:28
  • 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. – Jackdaw Jun 11 '19 at 16:49
  • That's great, thanks. Only one minor point: even though I've enabled "transparent" in the film properties (checkered background shows in the render layers compositing node), and enabled "use alpha" in the viewer nodes, I'm still getting a solid black full alpha background in the viewer nodes. Even connecting the alpha output of the render layers node to the alpha input of the viewer nodes made no difference. Any ideas? – Huw Walters Jun 11 '19 at 22:07
  • 2
    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. – Huw Walters Jun 11 '19 at 22:29