Saturday, April 1, 2017

Houville :: nice up vectors

This is for arranging your geo for nice behaviour with the copy sop. If you want to distibute geo, like trees or whatever, and control orientation but still respect the angle to the surface this is for you.


You'll need some way to connect the target points with the surface prim they relate to. Scatter has the sourceprim option which is cool, but if you want to manually do it you'll need a pre-pass. Here's an example using the 'stick a point in the middle of the face' technique. This should run over prims.
int newPtNum = addpoint(0,@P);
addpointattrib(0,"sourceprim", 0);
setpointattrib(0,"sourceprim", newPtNum, @primnum, 'set');
removeprim(0,@primnum,1);


Now lets get the surface geo ready. Here we will sneakily say normals run along the U direction and Up is what would typically be considered a normal. This makes sure our orientation makes sense.

Run this over points, assuming point source is in OpInput1, and reference geo for orientation is OpInput2.
vector center = primuv(s@OpInput2, 'P', i@sourceprim, {0.5,0.5,0.0});
vector dPdu = primuv(s@OpInput2, 'P', i@sourceprim, {0.51,0.5,0.0});
vector dPdv = primuv(s@OpInput2, 'P', i@sourceprim, {0.5,0.51,0.0});

vector u = normalize(dPdu - center);
vector v = normalize(dPdv - center);

v@up = normalize(cross(u,v));
v@N = u;

//matrix rotation shiz!
vector rot[];
rot[0] = u;
rot[1] = v@up;
rot[2] = v;
matrix3 mtx[] = unserialize(serialize(rot));
rotate(mtx[0], radians(chf('heading')), v@up);
rot = unserialize(serialize(mtx));
v@N = rot[0];
v@up = rot[1];



** for another post - passing colour information through points to be picked up by "Point Instance Procedural" as material overrides:
string r = sprintf("%d", @Cd.x);
string g = sprintf("%d", @Cd.y);
string b = sprintf("%d", @Cd.z);


s@shop_materialpath = chs('../theInstancesShaderPath');
s@material_override = "{'basecolorr':" + r + ",'basecolorg':" + g + ",'basecolorb':" + b + ",}";



Soooooo.. some of that was overkill. Here are a few ways to optimise that.

You can for example dispense with the serialise/unserialise phase by seting to the matrix directly. Also you can use quarternion to directly pull rotation information out of the matrix. Bear in mind Houdini understands '@orient' as a vector4 aka p@orient.
## direct casting and assignment of a matrix, rotation, extraction as a quaternion.
matrix3 rot = set(u,v@up,v)
rotate(rot, radians(chf('heading')), v@up);
p@orient = quaternion(rot)


And finally, you don't have to derive a @up if you have a normal. Have the points (scattered or otherwise) pick up their normals from the source they are based on. addpointattrib(0,"N",{0,0,0}); setpointattrib(0,"N",newPtNum,@N,"set");

No comments :

Post a Comment