# Difference between revisions of "JoyOfVex9"

MattEstela (talk | contribs) |
MattEstela (talk | contribs) |
||

(2 intermediate revisions by the same user not shown) | |||

Line 1: | Line 1: | ||

− | '' | + | ''Dot and cross product, fake lighting, combing normals to a surface'' |

=== Day 9 === | === Day 9 === | ||

Line 6: | Line 6: | ||

Switch to a more complex model now, like tommy, make sure his textures are disabled so we're not distracted by that. | Switch to a more complex model now, like tommy, make sure his textures are disabled so we're not distracted by that. | ||

− | <source> | + | <source lang="javascript" > |

@Cd = @N.y; | @Cd = @N.y; | ||

</source> | </source> | ||

Line 12: | Line 12: | ||

That looks like he's lit from above. If you use @N.x or @N.z, he looks lit from the right or front. Can negate to make him look like he's lit from underneath/left/behind. | That looks like he's lit from above. If you use @N.x or @N.z, he looks lit from the right or front. Can negate to make him look like he's lit from underneath/left/behind. | ||

− | <source> | + | <source lang="javascript" > |

@Cd = -@N.y; | @Cd = -@N.y; | ||

</source> | </source> | ||

Line 20: | Line 20: | ||

Eg, compare the normals to a vector pointing along the y axis {0,1,0}: | Eg, compare the normals to a vector pointing along the y axis {0,1,0}: | ||

− | <source> | + | <source lang="javascript" > |

@Cd = dot(@N, {0,1,0}); | @Cd = dot(@N, {0,1,0}); | ||

</source> | </source> | ||

Line 26: | Line 26: | ||

Replace that hard coded vector with a chv slider, now we can light from any angle: | Replace that hard coded vector with a chv slider, now we can light from any angle: | ||

− | <source> | + | <source lang="javascript" > |

@Cd = dot(@N, chv('angle')); | @Cd = dot(@N, chv('angle')); | ||

</source> | </source> | ||

Line 34: | Line 34: | ||

An alternative way to think about this is if you had a point in space. You move that where you want, draw a line from the origin to the point, thats your light vector. You can do exactly this; make a single point with an add sop, feed it to the second input, retrieve its @P, and treat it as a vector to do the dot product test: | An alternative way to think about this is if you had a point in space. You move that where you want, draw a line from the origin to the point, thats your light vector. You can do exactly this; make a single point with an add sop, feed it to the second input, retrieve its @P, and treat it as a vector to do the dot product test: | ||

− | <source> | + | <source lang="javascript" > |

vector pos = point(1,'P',0); | vector pos = point(1,'P',0); | ||

@Cd = dot(@N, pos); | @Cd = dot(@N, pos); | ||

Line 43: | Line 43: | ||

This works, but the light gets brighter as the point gets further away. As I mentioned a while back, a lot of functions expect the input vectors to be normalised, this is one of those cases. To do that you just use the '''normalize''' function. | This works, but the light gets brighter as the point gets further away. As I mentioned a while back, a lot of functions expect the input vectors to be normalised, this is one of those cases. To do that you just use the '''normalize''' function. | ||

− | <source> | + | <source lang="javascript" > |

vector pos = point(1,'P',0); | vector pos = point(1,'P',0); | ||

pos = normalize(pos); | pos = normalize(pos); | ||

Line 55: | Line 55: | ||

Switch to a sphere now. Turn on 'view point normals' in the viewport. You see a ball with spikes pointing away from it. Check what happens here: | Switch to a sphere now. Turn on 'view point normals' in the viewport. You see a ball with spikes pointing away from it. Check what happens here: | ||

− | <source> | + | <source lang="javascript" > |

@N = cross(@N, {0,1,0}); | @N = cross(@N, {0,1,0}); | ||

</source> | </source> | ||

Line 73: | Line 73: | ||

What can be interesting is to cross the normal with an axis, save it in a temp vector, and then cross the normal with that temp vector: | What can be interesting is to cross the normal with an axis, save it in a temp vector, and then cross the normal with that temp vector: | ||

− | <source> | + | <source lang="javascript" > |

vector tmp = cross(@N, {0,1,0}); | vector tmp = cross(@N, {0,1,0}); | ||

@N = cross(@N, tmp); | @N = cross(@N, tmp); | ||

Line 82: | Line 82: | ||

This 'double cross' operation as its usually called really combs the normals along the surface. A lot of grooming tools will start with this kind of operation to force hairs to flow along the surface. You can keep doing this operation, and it will rotate the vectors 90 degrees: | This 'double cross' operation as its usually called really combs the normals along the surface. A lot of grooming tools will start with this kind of operation to force hairs to flow along the surface. You can keep doing this operation, and it will rotate the vectors 90 degrees: | ||

− | <source> | + | <source lang="javascript" > |

vector cross1 = cross(@N, {0,1,0}); | vector cross1 = cross(@N, {0,1,0}); | ||

cross1 = cross(@N, cross1); | cross1 = cross(@N, cross1); | ||

Line 90: | Line 90: | ||

vs | vs | ||

− | <source> | + | <source lang="javascript" > |

vector cross1 = cross(@N, {0,1,0}); | vector cross1 = cross(@N, {0,1,0}); | ||

cross1 = cross(@N, cross1); | cross1 = cross(@N, cross1); | ||

Line 99: | Line 99: | ||

vs | vs | ||

− | <source> | + | <source lang="javascript" > |

vector cross1 = cross(@N, {0,1,0}); | vector cross1 = cross(@N, {0,1,0}); | ||

cross1 = cross(@N, cross1); | cross1 = cross(@N, cross1); | ||

Line 109: | Line 109: | ||

vs | vs | ||

− | <source> | + | <source lang="javascript" > |

vector cross1 = cross(@N, {0,1,0}); | vector cross1 = cross(@N, {0,1,0}); | ||

cross1 = cross(@N, cross1); | cross1 = cross(@N, cross1); | ||

Line 128: | Line 128: | ||

The maths textbook definition of adding vectors is to lie the vectors tip-to-tail, then draw a new vector from the start of the first to the end of the last. This is much easier to understand visually with a wrangle. I define 2 vector parameters, and set @N as the addition of both. Adding {1,0,0} (ie a vector pointing along x) and {0,1,0} (a vector pointing along y) will result in {1,1,0} (a vector pointing 45 degrees up and along): | The maths textbook definition of adding vectors is to lie the vectors tip-to-tail, then draw a new vector from the start of the first to the end of the last. This is much easier to understand visually with a wrangle. I define 2 vector parameters, and set @N as the addition of both. Adding {1,0,0} (ie a vector pointing along x) and {0,1,0} (a vector pointing along y) will result in {1,1,0} (a vector pointing 45 degrees up and along): | ||

− | <source> | + | <source lang="javascript" > |

vector a = chv('a'); | vector a = chv('a'); | ||

vector b = chv('b'); | vector b = chv('b'); | ||

Line 138: | Line 138: | ||

Here's a similar thing, but this time taking the normals from a sphere and adding a vector to it; you can see it bends the normals towards whatever new vector I specify. Handy for steering and adjusting initial velocities for particles, for example: | Here's a similar thing, but this time taking the normals from a sphere and adding a vector to it; you can see it bends the normals towards whatever new vector I specify. Handy for steering and adjusting initial velocities for particles, for example: | ||

− | <source> | + | <source lang="javascript" > |

vector a = chv('a'); | vector a = chv('a'); | ||

@N += a; | @N += a; | ||

Line 149: | Line 149: | ||

The textbook definition of vector subtraction is.... something? Frankly I don't remember. In Houdini, I know it as a handy way to create a vector from 2 points. Store @P of the first point as 'a', @P of the second point as 'b', and set @N of the first point as 'a-b'. Now its normal aims at the second point: | The textbook definition of vector subtraction is.... something? Frankly I don't remember. In Houdini, I know it as a handy way to create a vector from 2 points. Store @P of the first point as 'a', @P of the second point as 'b', and set @N of the first point as 'a-b'. Now its normal aims at the second point: | ||

− | <source> | + | <source lang="javascript" > |

vector a = point(0,'P',0); | vector a = point(0,'P',0); | ||

vector b = point(1,'P',0); | vector b = point(1,'P',0); | ||

Line 162: | Line 162: | ||

Extending this to a sphere again, we can make every normal on the sphere aim at a point: | Extending this to a sphere again, we can make every normal on the sphere aim at a point: | ||

− | <source> | + | <source lang="javascript" > |

− | vector a = | + | vector a = @P; |

vector b = point(1,'P',0); | vector b = point(1,'P',0); | ||

Line 173: | Line 173: | ||

If you reverse the terms, this can be a way to set the initial velocity for an explosion for example, where the point defines the center of the explosion: | If you reverse the terms, this can be a way to set the initial velocity for an explosion for example, where the point defines the center of the explosion: | ||

− | <source> | + | <source lang="javascript" > |

vector origin = point(1,'P',0); | vector origin = point(1,'P',0); | ||

@v = @P-origin; | @v = @P-origin; | ||

Line 186: | Line 186: | ||

Multiplying a vector by a single number just multiplies each number inside the vector by the single number. This has the effect of scaling the vector, making it longer or shorter, but maintaining it's direction. Multiplying by 0 cancels out the vector completely. | Multiplying a vector by a single number just multiplies each number inside the vector by the single number. This has the effect of scaling the vector, making it longer or shorter, but maintaining it's direction. Multiplying by 0 cancels out the vector completely. | ||

− | <source> | + | <source lang="javascript" > |

@N *= ch('scale'); | @N *= ch('scale'); | ||

</source> | </source> | ||

Line 198: | Line 198: | ||

Multiplying 2 vectors together will multiply the x components together, the y, the z. In practice, its a way to scale or even cancel out an axis of vectors. Eg, I have a sphere with normals, if I multiply it by {1,0,1}, I cancel out all the vertical parts of the vectors. Increasing the x component so that its {2,0,1} will mean the vectors will be twice as long in that axis: | Multiplying 2 vectors together will multiply the x components together, the y, the z. In practice, its a way to scale or even cancel out an axis of vectors. Eg, I have a sphere with normals, if I multiply it by {1,0,1}, I cancel out all the vertical parts of the vectors. Increasing the x component so that its {2,0,1} will mean the vectors will be twice as long in that axis: | ||

− | <source> | + | <source lang="javascript" > |

@N *= chv('scalevec'); | @N *= chv('scalevec'); | ||

</source> | </source> |

## Latest revision as of 22:27, 28 July 2020

*Dot and cross product, fake lighting, combing normals to a surface*

### Day 9

#### Dot product

Switch to a more complex model now, like tommy, make sure his textures are disabled so we're not distracted by that.

```
@Cd = @N.y;
```

That looks like he's lit from above. If you use @N.x or @N.z, he looks lit from the right or front. Can negate to make him look like he's lit from underneath/left/behind.

```
@Cd = -@N.y;
```

So thats fine, but what if we want him lit from an arbitrary direction? This is where a **dot** product comes in handy. It compares the directions of vectors, if they are pointing the same way (ie, parallel), it returns 1. If they're perpendicular, 0, if facing in exact opposite directions, -1, and angles in between those extreme positions will smoothly vary between 1, 0, -1.

Eg, compare the normals to a vector pointing along the y axis {0,1,0}:

```
@Cd = dot(@N, {0,1,0});
```

Replace that hard coded vector with a chv slider, now we can light from any angle:

```
@Cd = dot(@N, chv('angle'));
```

Adjusting angles with vectors can be a little tricky if you're not used to it, think of it as 'how much x, how much y, how much z', so for a light to come from the left, slightly up, and to the front, would be a value like {1, 0.1, 1}.

An alternative way to think about this is if you had a point in space. You move that where you want, draw a line from the origin to the point, thats your light vector. You can do exactly this; make a single point with an add sop, feed it to the second input, retrieve its @P, and treat it as a vector to do the dot product test:

```
vector pos = point(1,'P',0);
@Cd = dot(@N, pos);
```

The point function here goes to input 1, and retrieves the @P attribute of point 0. (Remember that the point function doesn't need the @ prefix).

This works, but the light gets brighter as the point gets further away. As I mentioned a while back, a lot of functions expect the input vectors to be normalised, this is one of those cases. To do that you just use the **normalize** function.

```
vector pos = point(1,'P',0);
pos = normalize(pos);
@Cd = dot(@N, pos);
```

#### Cross product

Switch to a sphere now. Turn on 'view point normals' in the viewport. You see a ball with spikes pointing away from it. Check what happens here:

```
@N = cross(@N, {0,1,0});
```

It can be a little hard to see against the mesh, append an add sop, turn on 'delete geometry but keep points', to make it easier to see. The normals are now rotating around the surface of the sphere.

The cross product will take 2 vectors, and generate a 3rd perpendicular vector. Easiest to think about with normals at the equator, and doing a vector vulcan hand shape:

Point your thumb up along the y axis, middle finger along a normal at the equator, your index finger now points along the equator. Turn your hand to represent normals at various points around the equator, you can see that it will swirl the normals (or 'combs' the normals as this operation is sometimes called) flat to the surface.

Try giving it different values to cross against, see what happens.

What can be interesting is to cross the normal with an axis, save it in a temp vector, and then cross the normal with that temp vector:

```
vector tmp = cross(@N, {0,1,0});
@N = cross(@N, tmp);
```

This 'double cross' operation as its usually called really combs the normals along the surface. A lot of grooming tools will start with this kind of operation to force hairs to flow along the surface. You can keep doing this operation, and it will rotate the vectors 90 degrees:

```
vector cross1 = cross(@N, {0,1,0});
cross1 = cross(@N, cross1);
@N = cross(@N, cross1);
```

vs

```
vector cross1 = cross(@N, {0,1,0});
cross1 = cross(@N, cross1);
cross1 = cross(@N, cross1);
@N = cross(@N, cross1);
```

vs

```
vector cross1 = cross(@N, {0,1,0});
cross1 = cross(@N, cross1);
cross1 = cross(@N, cross1);
cross1 = cross(@N, cross1);
@N = cross(@N, cross1);
```

vs

```
vector cross1 = cross(@N, {0,1,0});
cross1 = cross(@N, cross1);
cross1 = cross(@N, cross1);
cross1 = cross(@N, cross1);
cross1 = cross(@N, cross1);
@N = cross(@N, cross1);
```

will get you back to where you started.

### Vector maths

This is a good time to have a quick refresher on some vector maths basics. It's way easier than it seems (especially if you half remember stuff from high school or first year uni), with Houdini you get direct visual feedback which is great, and direct useful applications. Once you know how to add, subtract, multiply, dot and cross vectors, that's pretty much all you need to know for 90% of your Houdini needs!

#### Vector addition

The maths textbook definition of adding vectors is to lie the vectors tip-to-tail, then draw a new vector from the start of the first to the end of the last. This is much easier to understand visually with a wrangle. I define 2 vector parameters, and set @N as the addition of both. Adding {1,0,0} (ie a vector pointing along x) and {0,1,0} (a vector pointing along y) will result in {1,1,0} (a vector pointing 45 degrees up and along):

```
vector a = chv('a');
vector b = chv('b');
@N = a+b;
```

Here's a similar thing, but this time taking the normals from a sphere and adding a vector to it; you can see it bends the normals towards whatever new vector I specify. Handy for steering and adjusting initial velocities for particles, for example:

```
vector a = chv('a');
@N += a;
```

#### Vector subtraction

The textbook definition of vector subtraction is.... something? Frankly I don't remember. In Houdini, I know it as a handy way to create a vector from 2 points. Store @P of the first point as 'a', @P of the second point as 'b', and set @N of the first point as 'a-b'. Now its normal aims at the second point:

```
vector a = point(0,'P',0);
vector b = point(1,'P',0);
@N = b-a;
```

Notice that I stated its 'a-b' in the intro paragraph, but the vex code is 'b-a'? That's because I can never remember the order; I'll try it, find my vectors are pointing in the opposite direction to what I expect, swear, and reverse the terms. You'll do the same. :)

Extending this to a sphere again, we can make every normal on the sphere aim at a point:

```
vector a = @P;
vector b = point(1,'P',0);
@N = b-a;
```

If you reverse the terms, this can be a way to set the initial velocity for an explosion for example, where the point defines the center of the explosion:

```
vector origin = point(1,'P',0);
@v = @P-origin;
```

Download hip: File:jov9_rbd_v_origin.hip

#### Vector multiplication

Multiplying a vector by a single number just multiplies each number inside the vector by the single number. This has the effect of scaling the vector, making it longer or shorter, but maintaining it's direction. Multiplying by 0 cancels out the vector completely.

```
@N *= ch('scale');
```

Multiplying by a negative number will reverse the direction of a vector:

Multiplying 2 vectors together will multiply the x components together, the y, the z. In practice, its a way to scale or even cancel out an axis of vectors. Eg, I have a sphere with normals, if I multiply it by {1,0,1}, I cancel out all the vertical parts of the vectors. Increasing the x component so that its {2,0,1} will mean the vectors will be twice as long in that axis:

```
@N *= chv('scalevec');
```

### Exercises

- Cross against @N and noise, what happens?
- double cross @N and noise?
- Against @P?
- Can you fix the colours going negative in the shadowed areas of the dot product examples? (Hint: You'll need to clamp colours to stay within a valid range)

prev: JoyOfVex8 this: JoyOfVex9 next: JoyOfVex10

main menu: JoyOfVex