//
// This example creates point locators at random surface locations,
// and positions a Null at each of these point locators.
//
// Even if these point locators are simply randomly constructed,
// this example shows how to do it in the most accurate way,
// which is to specify subtriangles which correspond to the actual
// polygon triangulation.
//
NewScene( null, false );
var SphereGeom = CreatePrim("Sphere", "MeshSurface").ActivePrimitive.Geometry;
var NbPtLocators = 50;
var PolygonIndexArray = new Array(NbPtLocators);
var SubTriangleVertexArray = new Array(NbPtLocators*3);
var SubTriangleWeightArray = new Array(NbPtLocators*3);
var Polygons = SphereGeom.Polygons;
for(i = 0; i < NbPtLocators; i++) {
PolygonIndexArray[i] = Math.floor(Math.random() * 0.999999 * Polygons.Count);
var Polygon = Polygons.Item(PolygonIndexArray[i]);
var SubTri = Math.floor(Math.random() * 0.999999 * (Polygon.NbPoints-2));
var PolygonVertices = Polygon.Vertices;
var TriangleSubIndices = Polygon.TriangleSubIndexArray.toArray();
SubTriangleVertexArray[i*3] = PolygonVertices.Item(TriangleSubIndices[SubTri*3]).Index;
SubTriangleVertexArray[i*3+1] = PolygonVertices.Item(TriangleSubIndices[SubTri*3+1]).Index;
SubTriangleVertexArray[i*3+2] = PolygonVertices.Item(TriangleSubIndices[SubTri*3+2]).Index;
var W1 = Math.random();
var W2 = Math.random();
var W3 = Math.random();
var WSum = W1+W2+W3;
SubTriangleWeightArray[i*3] = W1/WSum;
SubTriangleWeightArray[i*3+1] = W2/WSum;
SubTriangleWeightArray[i*3+2] = W3/WSum;
}
var RandomPointLocators = SphereGeom.ConstructPointLocators(PolygonIndexArray, SubTriangleVertexArray, SubTriangleWeightArray);
CreateNullsAtPointLocations(SphereGeom, RandomPointLocators);
function CreateNullsAtPointLocations( InGeom, InPointLocators )
{
var SpherePositions = InGeom.EvaluatePositions(InPointLocators).toArray();
var SphereNormals = InGeom.EvaluateNormals(InPointLocators).toArray();
var TempVector = XSIMath.CreateVector3();
var TempRotation = XSIMath.CreateRotation();
for (i = 0; i < SpherePositions.length; i+=3)
{
var NullObj = GetPrim( "Null" );
TempVector.Set(SpherePositions[i], SpherePositions[i+1], SpherePositions[i+2]);
NullObj.LocalTranslation = TempVector;
TempVector.Set(SphereNormals[i], SphereNormals[i+1], SphereNormals[i+2]);
TempRotation.SetFromXYZAxes( TempVector, TempVector, TempVector );
NullObj.LocalRotation = TempRotation;
}
} |