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:
- color of highlighting
- mixing between this highlight color and original color
- opacity of selected objects
- opacity of non selected objects.
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.initView3d function related to specific template used in this example.
- space is view3d.space.
- view3d is variable or 3d window object.
*Highlighting itself may be implemented with creating special material and assigning it node without using selection.
Show selected objects
function showSelection(view) { function hideAll(node) { node=node.firstChild; while(node) { node.state&=~3; hideAll(node); node=node.next; } } function showSel(node) { if(node.state&4||(node.object&&(node.object instanceof iv.light))) { if(node.state&8) node.showAll(); else { if(node.object) node.state|=1; if(node.firstChild) { var n=node.firstChild; while(n) { if(n.state&0x80) { n.state|=3; node.state|=2; } n=n.next; } } else node.state|=3; } var n=node.parent; while(n) { if(n.object) n.state|=2; else n.state|=3; n=n.parent; } } node=node.firstChild; while(node) { showSel(node); node=node.next; } }; var root=view.space.root; hideAll(root); showSel(root); view.invalidate(iv.INV_STRUCTURE); }