/*
*/
if ( script.isPreview() )
return;
//If randomize is true the object is chosen at random form a pool of objects.
//if false objects are placed in sequence, one after the other
boolean randomize = true;
//The random sequence depends on the seed used.
Random rand = new Random( 12369878);
//initialize random sequence
for (int i = 0; i < 12; i++ )
rand.nextDouble();
scene = script.getScene();
//the tube object and its transformation matrix. Use another name if you wish
Tube t = (Tube) (scene.getObject("Tinsel").object);
Mat4 m = scene.getObject("Tinsel").coords.fromLocal();
//the tube vertices
Vec3[] orv = t.getVertexPositions();
int count = orv.length;
Vec3[] v = new Vec3[ orv.length ];
//increase step if you want to place an object each n vertex instead of each vertex
int step = 1;
//Decorators information
//groups represent groups of objects that are placed on the tube
//groups need not have the same number of objects. The example below consists of two groups
//The first group consists of two objects
//The second group is a single object.
//When a group coordinates are calculated, every object is placed relative to the first object, which acts
//as origin of the group. If need be, placing a null object as first object of a group
//can help group placement.
//Groups are rotated 90° away from the tube axis, and 0= groupsNumber)
randNum = 0;
}
//duplicates creation.
ObjectInfo[] infos= new ObjectInfo[ groups[randNum].length] ;
for (int k = 0; k < groups[randNum].length; ++k )
infos[ k ] = groups[randNum][k].duplicate();
//translation to origin, for convenience
Vec3 trans = new Vec3().minus( infos[0].coords.getOrigin() );
Mat4 m = Mat4.translation( trans.x, trans.y, trans.z );
for (int k = 0; k < groups[randNum].length; ++k )
infos[k].coords.transformCoordinates( m );
//random in plane rotation
Mat4 orient = Mat4.yrotation( rand.nextDouble() * 2 * Math.PI );
orient = orient.times( Mat4.zrotation( Math.PI/2 ) );
for (int k = 0; k < groups[randNum].length; ++k )
infos[k].coords.transformCoordinates( orient );
//90° rotation away from tube Y axis
Vec3 tangent = v[i+1].minus( v[i-1] );
tangent.normalize();
Vec3 yVec = new Vec3( 0, 1, 0 );
Vec3 axis = yVec.cross( tangent );
double matAngle = Math.acos( yVec.dot( tangent ) );
Mat4 nm = Mat4.axisRotation( axis, matAngle );
//translation to relevant tube vertex
Mat4 result = Mat4.translation( v[i].x, v[i].y, v[i].z );
result = result.times( nm );
for (int k = 0; k < groups[randNum].length; ++k )
{
infos[k].coords.transformCoordinates( result );
script.addObject( infos[k] );
}
}