Share

Spline Basics

In this topic, you learn how to create a MCG spline tool which generates lines based on the ray intersections between two meshes in the scene.

  1. Open the Max Creation Graph Editor: Scripting > Max Creation Graph Editor

  2. Add a Output: Spline node to the graph, either by pressing the X key, typing its name and pressing enter, or by dragging it from the Operator Depot under Outputs > Tools. This is the terminal node of the graph, which places the tool under the Create Tab > Shapes > Max Creation Graph button list.

  3. Our tool will need two meshes: one mesh from which the rays will be emitted, the "Start Surface", and one mesh onto which the rays will intersect, the "End Surface". Use two Tool Input: SceneNode operators (Inputs > Tools) to acquire these surfaces. Connect these to Exit If SceneNode Is Invalid (3ds Max > Scene Node) to terminate the graph's execution if these nodes have not yet been picked.

  4. The most straightforward way to perform ray intersections in MCG is to operate in world space. To that effect, we'll extract the node's mesh using the SceneNode Mesh in World Space compound (3ds Max > Scene Node).

    Our rays will be emitted from the centerpoint of each polygon on the mesh, so add a Mesh Polygon Transforms compound (Geometry > Mesh > Polygon) to obtain each polygon's orientation and position as an array of matrices.

  5. Use the Ray from Matrix Axis compound (Ray Tracing) to convert each polygon's transform into a ray. The axis value of 3 indicates that the ray will point along the Z axis (i.e. the average normal direction of the polygon). Set the flipAxis value to False to ensure the ray is emitted in the same direction as the normal.

  6. With the rays in hand, use the Ray Intersection with SceneNode operator (Ray Tracing) to calculate the ray intersections on the "End Surface". Ray Intersection with SceneNode requires that its input rays be defined in world space, and returns the intersection results in world space. Use the Filter and Sort Ray Intersections compound (Ray Tracing) to discard rays which did not successfully intersect.

    The result of Filter and Sort Ray Intersections is an array of pairs.

    1. The first item in each pair is a ray which describes the intersection point and normal on the surface.

    2. The second item in each pair is the originally emitted ray.

  7. The next step is to convert every pair of rays into an individual spline. To achieve this, we need to use the For Each operator (Array > Iteration). The For Each operator returns a new array by applying the one-argument foreachFn to each item in the array.

    The most convenient way to start a function is to create a group (Right-Click in the background > Create Group Node > Double-Click on the title bar to edit) and to add a Pass Through to represent one item in the array - in this case: a Pass Through Pair (Core > Pass Through). The input of the Pass Through will be the function's input.

    Note: Groups are completely optional for proper function creation, but are a convenient way to identify functions in your graphs.
  8. To decompose the contents of a pair, use Pair Item 1 and Pair Item 2. In the graph below, we continue to use groups to label values in our graph. The Ray Position operator (Ray Tracing) extracts the position of each ray.

    Note: Use the Ray Direction operator (Ray Tracing) to obtain the ray's direction. We won't be needing it in this case.
  9. Use an Array 2 operator (Array > Generation) to combine the points into an array of two items.

    Connect this array into a Spline from Points operator (Geometry > Spline) to define a linear spline containing just those two points. Set its closed input to False to avoid creating another segment which links the spline's end point back to its starting point.

    Once these connections are made, connect the Fn output from Spline from Points into the foreachFn input. The square Fn connectors are referred to as "Function Connectors". These convert the connected nodes into a function. The unconnected node inputs within a function act as that function's inputs. In our case, the function only has one input, namely the pair of rays: Pair[Ray,Ray]. See here for more information on functions.

    Note: You may supply an array of n points into Spline from Points to define a continuous spline which traverses these points. The resulting spline is composed of linear segments (as opposed to Bezier segments).
  10. Combine all the splines in the array into a single spline with Attach All Splines (Geometry > Spline).

    One final detail - the rays which formed this spline were defined in world space, so we must transform this spline into the tool's local space before it can be properly output. To complete this step, invert the Spline: Local to World Matrix with a Matrix Inverse. This creates a world to local matrix, which can be connected into the Transform Spline to transform the spline from world coordinates into local (tool) coordinates.

  11. Press CTRL+S (or File > Save As...) to save the graph.

    • Set the Tool Name to "Ray Splines" in the Graph Properties dialog, and press Save As.

    • Save your graph to the default Tools location (your user profile /Autodesk/3ds Max 2018/Max Creation Graph/Tools folder).

  12. Press CTRL+E (or Build > Evaluate) to evaluate the tool and register it into 3ds Max. The Message Log should indicate that the tool was successfully evaluated.

  13. Create two geometry objects in your scene, then go to Create Tab > Shapes > Max Creation Graph and click on Ray Splines. Click on the grid to create the spline, then pick the Start Surface and End Surface. Ensure some of the polygons from the start surface point towards the end surface, and you should begin seeing Ray Splines appear.

    Note: By default, spline tools appear under the Max Creation Graph drop down, however you may change this by modifying the Category in the tool's Graph Properties dialog (Edit > Edit Graph Properties...). Setting a tool's category to My Tools will place it under My Tools in the Shapes drop-down list.

Was this information helpful?