Modelling Lenses, Viewfinders and Anamorphics with Grasshopper, and simulating them with LuxCore

If you’re already a Grasshopper and Blender user then this is perfect for you, if not, It might be a little technical but it could be the thing that pushes you to start learning how to use them!

How did this come to exist?

I wrote two grasshopper definitions, based on this script and video from the blender forums. When I first saw it, I was really excited, it was right up my alley, unfortunately by the time I got there in January of 2022, the drobox file was broken, so I had to rewrite it entirely from the glimpses of the C# code we get in the video - mind you when this started I had absolutely no C# experience, I just really wanted to have this definition, so I brute forced my way through it.

There wase one specific line of code, which is clipped in the video and so you can’t see its entirety. Annoyingly it’s also the line of code that does the actual refraction, it stumped me for a bit until I realised it was just the vector from of Snell’s Law .

 

Video at 1:32

 
 
{\displaystyle {\vec _{\mathrm =r{\vec +\left(rc-{\sqrt \right){\vec

I also made the raytracer more robust, because of the way you calculate normals in grasshopper is dependent on the curve direction, the raytracer as written would give you the wrong result if your curve was flipped the wrong way (something that is difficult to see, since there’s no visual indicator, other than your rays ending up being wrong). This was because the code ommited to check the normal n’s direction

      double dotProduct = Vector3d.Multiply(n, i);
      int sign = Math.Sign(dotProduct);
      if (sign != 1){
        n = -n;
      }

I also made it possible to input different indices of refraction for the different elements, be sure to input either one, or as many as you have lenses, if you have fewer, it will loop back to the first once you’re at the end of your list.

Optimisation

In the video he uses the Galapagos plugin to do some optimisation on the lens, this works well when you have a lot of variables that can all affect the system in a way to complex to know all of the impacts. For some of my purposes, such as building an anamorphic adapter, I’m often really only changing one variable, and so that approach is a bit overkill, so I used the Anemone Plugin to make some loops instead and go through the my focusing range while changing the offset between the two lenses. Doing it this way gave me a way to find out the adjustment needed to make to an anamorphic to line up the two different focal lengths, for a given focusing distance, without resorting to using an equation. To be honest I was hoping to be able to reverse engineer and equation through this process, but I haven’t managed yet.

 

Adapter Optimisation Setup

 

I rewrote the other parts of the definition in C# as well, so that I could make it more streamlined, I also integrated the focal length calculator from Edmund Optics as well, to make it easier to calculate front focal lengths and so forth. I then wrote a component which takes the viewfinder equation and does that calculation for you as well.

 
 

What is the point of this? Well, I decided to make these definitions simply out of curiosity, and the possibility of being able to validate the results using LuxCore . Luxcore is a physically based renderer, so it can be a bit slow, because it doesn’t pull any performance tricks like most renderers will do, but in return it will give you precise results, so it seems to be used quite a bit in research environments. You can export your setup manually to blender, or by using something like MeshSync, if you want to be able to make tweaks and see the results quickly.

 
 
 
 

Look at that field curvature! Looking more closely at the definition we could have predicted this

 
 

Early Test with a Cooke Triplet

 
 

LuxCore Rendered Image

 

You can download the definitions here :

Basic Viewfinder Setup - Human plugin needed (only used to give a nicer preview)

Adapter Setup - Anemone and Human

if you make some improvements, please share them with me! Maybe this can be turned into a free full optics Plugin at some point, that would be really nice.

 
Oscar OwesonComment