RSS< Twitter< etc

Krakatoa MY: Finding Nearest Particle Using Magma

Applicable to Krakatoa MY 2.3.0 and higher - Last Edited on 2013/11/21 @ 3:30 PM PST


In the following tutorials we will look at some of the particle data sampling capabilities of the Magma channel editor introduced in Krakatoa MY v2.3.0.

Currently, Magma provides several operators for reading data from another particle system - NearestParticle finds the particle that is closest to the sampling point, ParticleQuery can access any data from that particle, ParticleSumCount collects data from the N nearest particles, and ParticleSumRadius samples all particles within a spherical volume around the sampling point.

Using NearestParticle

First, let's test the simplest particle sampling operator - NearestParticle.

Creating The Test Scene

We will need a simple test scene containing two PRTVolume point clouds, both made from the same 10x10x10 pCube. 

  • In a new Maya scene, create a pCube with size 10x10x10 and place it at 0.0,5.0,5.0 (although you can place it anywhere and can use any size you like)
  • With the pCube selected, click the VOL icon on the Krakatoa shelf to create, align and connect a PRT Volume object to the pCube.
  • Enter 2.0 for Render Spacing and uncheck Use Viewport Spacing to use the same large spacing and low particle count in both the renderer and the viewport.
  • Check Enable Random Jitter to produce a randomized placement of the particles.

RESULT: A low resolution point cloud will be created inside the pCube. Note that since the spacing is so large (1/5th of the cube's size), some particles might be placed up to 2.0 units outside of the actual mesh due to level set sampling imprecisions. 

Now we need another PRT Volume to sample the first one: 

  • Select the pCube again and click the VOL icon once more to create a second PRT Volume object.
  • Change the Render Spacing to 0.200 and uncheck Use Viewport Spacing.
  • Check the Enable Random Jitter option. 

RESULT: We have a denser point cloud where each particle will sample the nearest particle from the lower resolution cloud. 

Querying The Nearest Particle

Next. we will add a Magma node to the second PRT Volume and will query the position of the nearest particle from the first PRT Volume. 

  • Click the MOD icon on the Krakatoa shelf to open the Krakatoa Modifier Editor.
  • Select the second PRT Volume (PRTVolumeTransform_pCube2|PRTVolume_pCube1) and click the +Add Magma button.
  • Click the EDIT Modifier button at the bottom of the Editor dialog - a Magma Editor window will open.
  • Right-click anywhere in the editor's grid and from the node creation menu select OUTPUT>Color.
  • Click and drag from the first and only input socket of the Output node and release over the empty area of the Magma Editor.
  • From the node creation menu, select Object>NearestParticle - the two nodes will be connected and the NearestParticle operator will be outlined in red because of an error (it has no valid inputs yet).
  • Press Ctrl+R to switch the Reorder mode from Manual to Branch (alternatively, select from the list in the bottom right corner of the Magma Editor). This will reorder the flow automatically as we keep editing it.

NOTE: In this tutorial, the Magma Editor was switched to the "Dark Monopoly" Color Theme via the "Colors" menu item in the Editor's menu bar. Your colors might look different depending on your preferred settings...

  • Click and drag from the first input socket of the NearestParticle node and release over an empty area of the editor. 
  • From the context menu, select New InputParticles Node.

  • In the newly created InputParticles node's Attributes panel, select PRTVolumeTransform_pCube1|PRTVolume_pCubr1 from the list. The name will appear in the Particle Name: text field and the first PRT Volume will now be used as the source system to sample.

  • Click and drag from the second input socket of the NearestParticle node and release over an empty area of the editor.
  • From the context menu, select New InputChannel Position->ToWorld. This will create two new nodes - an InputChannel Position, and a Transform>ToWorld operator. 

NOTE: The particle positions in the Position channel are in local object space, but all sampling operators work in World Space (WS). This is indicated by the Lookup Point (WS) name of the second socket of the NearestParticle node. This is why we must convert the local positions to world values before using them for sampling. Note that the ToWorld operator is automatically set to "Point" mode which applies all transforms including Translation, Scaling and Rotation.


RESULT: We now have a functioning flow where each particle from the high-resolution PRT Volume is finding the position of the nearest particle from the low-resolution PRT Volume and passing its Position to the Color channel of the high-resolution cloud.


Since most of the XYZ values produce RGB values greater than 1.0, we could scale down the color value by 0.1 in order to produce more obvious "spots": 

  • Select the NearestParticle node and hit the * key on the numeric keypad to insert a Multiply operator into the first output socket connection.
  • In the Right Value - Default Value panel in the Magma Attribute Editor, enter 0.1 for the multiplier value.

RESULT: Now we can see that each particle of the second PRT Volume has located the nearest particle from the first PRT Volume and has the same color as all other particles with the same closest neighbor. 


Using NearestParticle With ParticleQuery

Most of the time though, we will be interested in other particles channels than the Position channel. Let's see how we can expand the NearestParticle operator with the addition of a ParticleQuery operator which can sample arbitrarily named existing channels. 

  • Click and drag a new connection from the ParticleIndex socket of the NearestParticle operator.
  • Release over an empty area of the Editor and select Create And Connect ParticleQuery from the context menu.
  • (Alternatively, select the NearestParticle operator and click the Add ParticleQuery button in the Magma Attribute Editor).
  • Note that the resulting new node is automatically connected to the existing InputParticles and NearestParticle nodes, but does not have any output sockets yet - we have to enter a comma-separated list of the channel names we would like to sample. 

  • In the Channels text field in the Magma Attribute Editor, enter Color as the channel to sample - a new Color output socket will appear on the node.
  • Connect the Color output socket of the ParticleQuery to the Color input socket of the Output node. 

RESULT: An error will be displayed in the Maya Script Editor because the low-resolution PRT Volume object doesn't have an explicit Color channel yet - we must define one. 

  • In the Krakatoa Modifier Editor, select the object PRTVolumeTransform_pCube1|PRTVolume_pCube1 from the top list - this will also activate it in the Maya Attribute Editor.
  • Click the +Apply Texture button to connect a new Apply Texture Modifier node.
  • In the Maya Attribute Editor, leave all settings at their defaults (Channel Name should be Color, UVW Name should be Position) and click the mapping icon to the right of the Texture attribute.
  • Select the Ramp map from the list of Maya texture maps.
  • In the Ramp map, select the middle gradient color and change it from green to yellow.
  • In the Magma Editor, press the UPDATE button in the upper right corner to refresh the particles in the viewport. 

RESULT: The high-resolution PRT Volume with the Magma modifier will now see random colors in the low-resolution particles (because the particles are using the radomly jittered Position channel to look up the texture map color)



Creating A Cellular Look Using NearestParticle And Distance Gradient

Let's go a step back and reload the first scene we saved - the one with Color By NearestParticle Position. Instead of coloring all particles of a given neighbor using the same color, let's create a distance-based gradient. 

  • Reload the first saved scene.
  • Select the NearestParticle operator and press the - (minus) key on the numeric keypad to insert a new Subtract operator.
  • Since the Position output of the NearestParticle operator is in world space, we must subtract the ToWorld node's output to have the two values in the same space - connect the World Space output socket of ToWorld to the Right Value socket of the Subtract. 

RESULT: While this is not the result we are after, it still looks quite cool:

  • Select the Subtract node and press ALT+V for Vector category, and then M for Magnitude.
  • (Alternatively, with the Subtract node selected, right click and select Vector>Magnitude from the context menu)
  • With the Magnitude node selected, oress ALT+F for Function and then B for Blend.
  • (Alternatively, use the right-click context menu to insert a Function>Blend)
  • Grab the connection going from the Magnitude to the Blend close to the input socket and drag to disconnect and drop over the third (Blend Amount) socket.
  • (Alternatively, press CTRL+W and then SHIFT+CTRL+W to swap the first and second and then second and third connections to move the first connection into the third socket). 

RESULT: Since the default values of the Blend operator are Black and White RGB colors, the result is a black color close to the nearest particle, and white at a distance more than 1.0 units. 

Normalizing The Gradient

The Magnitude value is currently not normalized. But we know that the Render Sampling of the first PRT Volume was 2.0 units, so the average distance between two particles is somewhere in that vicinity.

  • Select the Magnitude operator and press the / key on the numeric keypad to insert a Divide operator.
  • Enter 2.000 for the Divisor value.

RESULT: The gradient is now 2.0 units in radius. When the distance to the nearest particle is greater than 2.0, the color will still be white.

NOTE: The Divisor value could be connected to the PRT Volume's Render Spacing attribute using an expression to make it adapt automatically to any changes.

  • Select the Blend operator and enter 1.0 for the X component of the Value At 0 Default.
  • Enter 1.0 for X and Y of the Value At 1 Default. 

RESULT: This will produce a red color near the neighbor particle, and a yellow color at more than 2.0 units away from it, with an orange gradient in between.

Mapping A Ramp Texture As Custom Gradient

Instead of generating the Color channel directly, we could produce a TextureCoord (UV) channel that controls a Maya texture map like the Ramp map. 

  • Select the Output node and change the Channel Name from Color to TextureCoord by selecting from the list of typical channel names.
  • In the Krakatoa Modifier Editor, with the second PRT Volume object selected in the top list and the Magma modifier shown in the bottom list, click the +Apply Texture button to connect another Apply Texture Modifier node.
  • Click the Apply Texture entry on the bottom list to activate the modifier in the Maya Attribute Editor.
  • Select TextureCoord from the UVW Selection drop-down list.
  • Click the map icon to assign a new Ramp map. 

RESULT: Since the Blend operator is blending the second component (Y value equivalent to V in the mapping coordinates) between 0.0 and 1.0, and the Ramp map defaults to V Ramp, we get a gradient from red through green to blue on our particles.

But there is a small problem - there are particles at distance above 2.0 that should be blue, but show up as red due to the wrap around of the coordinates greater than 1.0. So we need to clamp the normalized value between 0.0 and 1.0 to avoid this. 

  • Select the Divide operator and change the Divisor from 2.0 to 2.5 to further increase the size of the color cells. 
  • Press ALT+F for Function category, and then C to insert a Clamp. operator behind the selected Divide operator.
  • The Clamp operator already defaults to clamping between 0.0 and 1.0, so no further changes are needed.

RESULT: We now have larger cells going from red at the nearest particle through green to blue without any unexpected jumps in the color at large distances.

Finding Other Particles Than The Nearest One

By default, the NearestParticle operator returns exactly what the name implies. But there is a Which Nearest parameter exposed in the Magma Attribute Editor that lets you change that. 

  • Select the NearestParticle operator 
  • In the Magma Attribute Editor, change the Which Nearest from 1 to 2 to use the second-nearest particle and not the nearest one.
  • Change again from 2 to 3 and so on. 

RESULT: The high-resolution PRT Volume particles will use particles that are further than the nearest one, producing different gradients.