Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.

First time visiting? Here are some places to start:
  1. Looking for a certain topic? Check out the categories filter or use Search (upper right).
  2. Need support? Ask a question to our Community Support category.
  3. Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
  4. Be respectful, on topic and if you see a problem, Flag it.

If you would like to contact our Community Manager personally, feel free to send a private message or an email.

Ray Tracer - Refraction in adjoining parts?

I am attempting to model refraction at the interface of two differing materials using the Ray Tracer feature script.  I am finding that the feature script does not account for adjoining materials.  Rather, the entrance and exit of the traced ray from any part acts as if the interface were between the part and air (the "medium" as defined in the Ray Tracer setup).

Reference document link: https://cad.onshape.com/documents/480c079de04a57b2b4e460da/w/97683ca07ef3a54828fe65ac/e/d93e36d310047ae8b78930b7?configuration=List_4kFJp9OvaVhgL5=Default;List_Azxmm8NwScCLJA=No_Gap&renderMode=0&uiState=64fb832334c4d04337f0f976

The document link above is set up to demonstrate this issue.  Two parts are modeled with different refractive indices.  A ray originates near the bottom of Part 1, aimed upwards towards Part 2.  At the interface of Part 1 and Part 2, the desired behavior is that the refraction angle would be calculated as a function of the indices of refraction of Part 1 and Part 2.  However, that appears to not be the case.

Construction lines are created in the "Ray definition" sketch that show the incidence angle (in Part 1) and the expected refraction angle (in Part 2).

A configuration drop-down is set up to adjust the gap size between the two parts.  Below are listed the three conditions of that configuration table, and the resulting output.
  1. Gap: In this condition, a small gap is created between Parts 1 and 2.  The ray refracts across the [Part 1]:[Air] interface, then again at the [Air]:[Part 2] interface.  The final ray angle inside of part 2 is at the correct angle, but is offset from the expected ray position as a function of the size of the gap between the two parts.
  2. No Gap: In this condition, there is no gap between Parts 1 and 2.  Something goes really wrong here.  The ray is reflected internally, rather than refracting into Part 2.  This is the issue/error.
  3. Negative Gap (Overlap): In this condition, Part 2 is modeled to overlap Part 1.  Similar to Condition #1 in this list, there are again two refraction steps resulting in a ray paralell to the expected ray angle, but offset from the expected ray position.
One workaround option is to set the refractive index of the surrounding medium (typically air) to match the refractive index of Part 2.  Then I get the correct refraction at the exit of Part 1 where it interfaces with Part 2, but I lose the ability to model the interaction of any parts with air.

This is my first exposure to FeatureScript, so while I read through the code, I'm not picking up all the details to solve the problem on my own just yet.  Is anyone out there familiar with this script? Or perhaps someone might be willing to dig into the issue?  

I'm tagging @maximilian_schommer , though I'm not sure if he is still involved or active with OnShape development.


  • Evan_ReeseEvan_Reese Member Posts: 1,981 PRO
    Interesting catch, and good timing for me. I've been actually looking at this feature because I've been working on a Multi Ray Tracer feature to add some automations to this one that will generate whole sets of rays at once, instead of needing a direction point per ray. I started out writing my own Ray Tracer function to help me understand the math better because it sounded fun, but then I ended up hitting some challenges and opting for just generating the inputs for the Ray Tracer feature and using it internally. I think you're right though; there may be some cases that don't behave as expected, in which case I may get back to doing as scratch write of a ray tracer.

    Hopefully @maximilian_schommer chimes in though, since I'd rather just add onto his feature rather than re-invent it.
    Evan Reese / Principal and Industrial Designer with Ovyl
    Website: ovyl.io
  • Evan_ReeseEvan_Reese Member Posts: 1,981 PRO
    Another benefit of a clean re-write I'm imagining is the ability to specify "roughness" for each material too, which could scatter rays randomly when they hit something. I imagine it would be on a 0-to-1 scale with 0 being no scattering (i.e. "polished to mirror finish" like the existing feature), 0.5 scattering slightly, but still in the general direction (i.e. "satin" or "semi-gloss") and 1 being totally scattered (i.e. "matte"). Would something like that be helpful for what you're working on?
    Evan Reese / Principal and Industrial Designer with Ovyl
    Website: ovyl.io
  • John_ChristiansenJohn_Christiansen Member Posts: 5
    @Evan_Reese thanks for the response.  The added features in the image you shared would indeed be helpful.  I am currently simulating multiple rays from a light source using either linear patterns or circular patterns of the Ray Tracer feature.  It works, but it mysteriously slows down the model - a lot.

    Regarding the surface roughness feature: I would have use for that feature.  I am working on a design to evenly illuminate an object at close range.  LED's being concentrated light sources, this requires a lot of physical prototyping.  Some method of modeling scatter could help with that.

    My dream feature right now would be internal scattering (reflections) in a not-entirely-clear material.  That could get complicated quickly, though I can imagine that with clever implementation it might just be a reimplementation of the surface reflection applied with some proportional random frequency inside the material.

    In the meantime, I am trying (well, a co-worker is trying) to simulate what we need to know using the beta rendering platform in OnShape.  Time will tell if we have enough control over the position and nature of the light source to make this useful.

  • Evan_ReeseEvan_Reese Member Posts: 1,981 PRO
    I've been doing the feature patterns of Ray Tracer too. Did you know you can choose more than 1 direction point though?

    I expect that whatever I make with featurescript will just be a convenience tool, but that a real optical package will be important for validation of anything complex. I doubt that I'll get into anything like internal scattering. Even surface scattering is a stretch for me.

    I've gotten back into the idea of a clean re-write. I'll keep you posted if I get that done.
    Evan Reese / Principal and Industrial Designer with Ovyl
    Website: ovyl.io
  • John_ChristiansenJohn_Christiansen Member Posts: 5
    Yes, I have seen I can choose more than 1 direction point.  Using the feature pattern seems like a cleaner workflow - just preference, I guess.

    Good luck on the re-write.  Thanks again.

Sign In or Register to comment.