dot product

Dot Product to Cull Points and Establish a Gradient

Here’s another super-simple trick you can perform with the dot product of two vectors. If you create two nulls and get the vector between them, you can compare that with the vector from any point to the first null using a dot product. Any value less than zero is past the plane of the first control normal to the second. That doesn’t read well, but the result is straightforward, you are using the controls to act like a normal and cull points.

Furthermore, the distance between those two nulls acts as a handy scale factor to color points in a gradient between the two. Here for instance we’ve “cut” a sphere in half…

The structure is very simple as you can see in the network view. The wrangle is also very simple. Thanks to Tokeru for inspiration and the basic structure of this wrangle.

vector p1, p2, v1, v2;
 
p1 = point(1,'P',0); //control null 1
p2 = point(2,'P',0); //control null 2

v1 = normalize(p2-p1); 
//vector between control nulls

v2 = @P-p1;
//vector from current eval point to control 1
 
f@angle = dot(v2, v1);
//The dot product of the two, indicates 'perpendicularity' 
 
if (@angle <= 0){
removepoint(0,@ptnum);}
//Cull out any points past p1

float dist = distance(p1,p2);
@Cd = dot(v2,v1)/dist;
//The distance between the two can also act as a gradient

This works, but there’s a far simpler approach to take using the matrix transforms mentioned a few posts earlier. Transform the points to the origin of a control object’s local space (you saved that wrangle as an asset, right?) and then you can use very simple logic – in this case let’s delete any point greater than zero in z.

if ( @P.z > 0) {
removepoint(0,@ptnum);
}

Translate the points back to their original world position (in that other wrangle you saved as an asset, right?) and bam, you have a super easy tool to cull points according to a control object. Or make a gradient, or whatever else you want.

This is super handy for tons of things.

Dot Product and Edges in Vex

Getting the dot product of two vectors is infinitely useful, and easy in Vex. In this example I get a vector pointing from scattered particles to the camera and then get their dot product with the surface normal at that point. This gives you a very good idea of whether the surface is facing or perpendicular to the camera at any given point.

In this case, I simply cull any point which isn’t close to a value of zero, indicating the surface is perpendicular to the camera. If instead I culled particles that were less than zero I would be removing all back-facing (occluded) particles, which is perhaps more useful but less visually interesting. Since I am left with only particles near the edges of the geometry it’s easy to connect them with lines and get an “edge detection” kind of look.

Here’s the Vex wrangle I used. Note an earlier polyframe node gave me point normals on the scattered points to work with.

float threshold = chf("Threshold");
//User input threshold

vector camPos = point(1, "P", 0); 
//The position of the imported camera

vector toCam = normalize(camPos - @P); 
//A vector from each point to the camera)

//@N = toCam; 
//Use to visualize toCam as normals

f@angle = dot(toCam, @N);
//The dot product of the normal and toCam vectors. 

if (@angle > threshold || @angle < -1*threshold){
removepoint(0,@ptnum);
}

I’ve used this edge culling in production a few times, to create anime style speedlines and particle effects like rain strikes and halos. Culling particles behind geometry is of course also handy, though raycast solutions are much cleaner for that kind of thing. Next, we’ll use a simple dot product calculation to cull points and create a gradient effect.

Cheers.