Work with selections

Goal: select objects in 3d space and highlight them.

Objects in FinalMesh WebGL may be selected in the same way as in regular 3d application. Selection may be used for marking objects for any operation or to highlight them*.

Node is selected when 3rd bit in node.state is set, or node.state&4 != 0. You may set/clear this bit directly and invalidate 3d window. Or you use iv.space.select mdethod. This method allows to select/deselect single node and optionlally deselect rest nodes.

Appearance of selected nodes is determined by iv.window.clrSelection variable. This varaible defines:

 

Select single node

space.select(node,true,true);	// select node and keep previously selected objects

Select single node by name

function testSelection()
{
	var space=view3d.space;
	var node=space.root.search("Sphere");
	if(node)space.select(node,true,false);// select node and reset old selection
}	

Select all

function _selectAll(node)
{
  node=node.firstChild;
  while(node)
  {
  node.state|=4;
  _selectAll(node);
  node=node.next;
  }
}
function testSelection()
{
  var space=view3d.space;
  var old=space.getSelection(null);	// get list of previously selected nodes - for notification
  _selectAll(space.root);		// mark all nodes as selected
  space.postSelection(null,old);	// send notification and update color of highlight
  space.invalidate();		// update screen
}

Select none

space.select(null,false,false);

Invert selection

function _invertAll(node)
{
  node=node.firstChild;
  while(node)
   {
    node.state^=4;
   _invertAll(node);
    node=node.next;
   }
}
function testSelection()
{
  var space=view3d.space;
  var old=space.getSelection(null);	// get list of previously selected nodes - for notification
  _invertAll(space.root);		// invert selection
  space.postSelection(null,old);	// send notification and update color of highlight
  space.invalidate();		// update screen
}

Listen to selection event

function testSelection()
{
  view3d.addRefTarget(
  function(event)
  {
   switch(event.code)
   {
    case "selection":{
       var oldSelection=event.old;//array. may be null
       var currentSelection=event.current;//array. may be null
       var node=event.node; // node selected now
       // 
     }break;
   }
  }
  )  
}

Initial node selection

This example loads 3d file and selects two nodes.

 view3d=iv.initViewer3d("tree.iv3d",0x777777,null,1.0);view3d.menuOpen=false;view3d.cfgSelZOffset=false;view3d.clrSelection[5]=false?0.5:1;
view3d.addRefTarget(
function(event)
 {
 switch(event.code)
  {
 case 'dataReady':
   {
    var space=view3d.space;
  //select first node
    var node=space.root.search("A");
    if(node)space.select(node,true,false);
  // select second node
    node=space.root.search("D");
    if(node)space.select(node,true,true);
   }break;
  }
 }
);

where iv.initViewe3d function related to specific template used in this example. 

Example

where space is view3d.space. And view3d is variable or 3d window object.

*Highlighting itself may be implemented with creating special material and assigning it node without using selection.