This is my first post here and I'm still learning about Blender (aren't we all though?) and I had a question about an effect I'd like to do.

I'd like to make it so that a character with solid-color glasses lenses can have their pupil render through the lens, while maintaining its appropriate location in 3D space (ie not textured onto the lens). A way I think this could work would be rendering the pupil over just the lens but I don't know if that's the most practical or achievable way to do this.

For my example I'll use this model of Velma from Scooby-Doo I made for practice: "hello"

The glasses, body, and eyes exist as separate objects for clarification (but that can be changed need be)

Instead of making the lens clear or not being able to see the actual eye or face behind the lens from the sides, I'd like to keep the cartoony look of this and would like to use a similar effect in the future. I hope I'm being clear enough with what I'm trying to achieve. Thank you for your help in advance!

EDIT: A rough approximation of the type of effect I'd like: I drew these on with an image editing software

  • $\begingroup$ Hi, maybe you could show a reference of how you'd like it to look... $\endgroup$ – m.ardito Aug 27 '18 at 9:17
  • $\begingroup$ Pardon the crudeness of this, I drew black dots to approximate where the pupils would be in 3D space, I'd like it to look like this but in motion I'd like it to accurately represent where the actual pupil is/replicate its exact shape through the lens: imgur.com/a/vYJTnNV $\endgroup$ – Tony P Aug 27 '18 at 15:42
  • 1
    $\begingroup$ Using Blender Render makes this easy, enable the Invert Z Depth option for the pupil material. $\endgroup$ – sambler Aug 29 '18 at 9:10

If you don't mind using OSL (that is, setting the render to Cycles/CPU, and checking the 'OSL' option) then you can look ahead down a ray, using a script.

This script returns the Pass Index of the next material encountered, continuing in the direction of the incoming ray, (so it won't, without improvement, take refractive/reflective deflection into account, and the lens is a single plane, without thickness.)

#include "stdosl.h"
shader NextIdx(  
  output float nextIdx  = 0.0    
  if (trace(P,-I,"mindist",0.00001)){
    getmessage("trace", "material:index", nextIdx);

.. and can be used in a tree something like this, to make the 'glass' of the lens:

enter image description here

.. where the eye material has an Pass Index of 10, and everything else has an index of 0...


enter image description here

so in this example, the 'glass' is transparent to everything has a material Pass Index greater than 5, and diffuse grey to everything else.

  • 1
    $\begingroup$ Your suggestion worked great, thank you! imgur.com/a/D9DOIJ8 $\endgroup$ – Tony P Aug 29 '18 at 22:56
  • $\begingroup$ I like the style $\endgroup$ – Robin Betts Aug 29 '18 at 23:05

Well I will admit this is somewhat hacky but it does work.

The trick is to make your glasses lenses transparent, and then every material behind the lenses--except the eye material--uses the "transparent depth" information from the "Light Path" node as a switching factor. If the light ray is passing through transparency (i.e., the glasses lenses), the material is a diffuse or (even better) emission white. Otherwise, it's whatever color it would normally be.

To make this work, I think you would need to add some sort of plane behind the head of your character that uses this switching factor, otherwise the glasses will be partly white, partly transparent whenever the background is visible through the lenses. Or you could add this same type of switching logic to your background, if that works for you.

Here's a setup scene:

enter image description here

The circle (glasses lens) is a simple transparent material with a diffuse black border around it.

The sphere (eye pupil) is a simple black diffuse material.

The flesh-colored cube (standing in for the head) has this material:

enter image description here

The large plane behind the cube has the following material. You'd probably want to parent this plane to the head.

enter image description here

Et voila! The specially factored materials behind the glasses lens (or any transparency) look white, while other materials (i.e., the pupil) stay the color they would normally be.

enter image description here

enter image description here

It would be nice to be able to make the glasses lens material transparent or opaque depending on what's behind it, but I'm not sure if that can be done. Blender's ray tracers start from the camera, so I'm not sure how to make the material "aware" of what's behind it from a node logic standpoint.


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.