Skip to content

Commit

Permalink
Add refract node (#1698)
Browse files Browse the repository at this point in the history
Adding refract node to partner with the reflect node, with a similar named interface, and implementation as a nodegraph taken from the OSL spec.
  • Loading branch information
ld-kerley authored Feb 7, 2024
1 parent cff9a7c commit a81ed0c
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
11 changes: 11 additions & 0 deletions libraries/stdlib/stdlib_defs.mtlx
Original file line number Diff line number Diff line change
Expand Up @@ -2761,6 +2761,17 @@
<output name="out" type="vector3" />
</nodedef>

<!--
Node: <refract>
Compute the refraction vector given an incident vector, unit surface normal, and index of refraction.
-->
<nodedef name="ND_refract_vector3" node="refract" nodegroup="math" doc="Compute the refraction vector">
<input name="in" type="vector3" value="1.0, 0.0, 0.0" doc="Incident vector" />
<input name="normal" type="vector3" defaultgeomprop="Nworld" doc="Surface normal" />
<input name="ior" type="float" value="1.0" doc="Index of refraction" />
<output name="out" type="vector3" />
</nodedef>

<!-- ======================================================================== -->
<!-- Adjustment nodes -->
<!-- ======================================================================== -->
Expand Down
61 changes: 61 additions & 0 deletions libraries/stdlib/stdlib_ng.mtlx
Original file line number Diff line number Diff line change
Expand Up @@ -2728,6 +2728,67 @@
<output name="out" type="vector3" nodename="reflection_vector" />
</nodegraph>

<!--
Node: <refract>
Compute the refraction vector given an incident vector, unit surface normal, and index of refraction.
-->
<nodegraph name="NG_refract_vector3" nodedef="ND_refract_vector3">
<dotproduct name="IdotN" type="float">
<input name="in1" type="vector3" interfacename="in"/>
<input name="in2" type="vector3" interfacename="normal"/>
</dotproduct>
<multiply name="IdotNsq" type="float">
<input name="in1" type="float" nodename="IdotN"/>
<input name="in2" type="float" nodename="IdotN"/>
</multiply>
<multiply name="iorsq" type="float">
<input name="in1" type="float" interfacename="ior"/>
<input name="in2" type="float" interfacename="ior"/>
</multiply>
<subtract name="one_IdotNsq" type="float">
<input name="in1" type="float" value="1.0"/>
<input name="in2" type="float" nodename="IdotNsq"/>
</subtract>
<multiply name="iorsq_one_IdotNsq" type="float">
<input name="in1" type="float" nodename="iorsq"/>
<input name="in2" type="float" nodename="one_IdotNsq"/>
</multiply>
<subtract name="k" type="float">
<input name="in1" type="float" value="1.0"/>
<input name="in2" type="float" nodename="iorsq_one_IdotNsq"/>
</subtract>
<multiply name="I_scaled" type="vector3">
<input name="in1" type="vector3" interfacename="in"/>
<input name="in2" type="float" interfacename="ior"/>
</multiply>
<sqrt name="sqrt_k" type="float">
<input name="in" type="float" nodename="k"/>
</sqrt>
<multiply name="ior_NdotI" type="float">
<input name="in1" type="float" interfacename="ior"/>
<input name="in2" type="float" nodename="IdotN"/>
</multiply>
<add name="ior_NdotI_sqrt_k" type="float">
<input name="in1" type="float" nodename="ior_NdotI"/>
<input name="in2" type="float" nodename="sqrt_k"/>
</add>
<multiply name="N_scaled" type="vector3">
<input name="in1" type="vector3" interfacename="normal"/>
<input name="in2" type="float" nodename="ior_NdotI_sqrt_k"/>
</multiply>
<subtract name="refract_dir" type="vector3">
<input name="in1" type="vector3" nodename="I_scaled"/>
<input name="in2" type="vector3" nodename="N_scaled"/>
</subtract>
<ifgreater name="result" type="vector3">
<input name="value1" type="float" value="0.0"/>
<input name="value2" type="float" nodename="k"/>
<input name="in1" type="vector3" value="0.0, 0.0, 0.0"/>
<input name="in2" type="vector3" nodename="refract_dir"/>
</ifgreater>
<output name="out" type="vector3" nodename="result"/>
</nodegraph>

<!-- ======================================================================== -->
<!-- Adjustment nodes -->
<!-- ======================================================================== -->
Expand Down

0 comments on commit a81ed0c

Please sign in to comment.