Reputation: 337
In the Forge Viewer I have written a custom handler. It has a bound double-click event that when it fires with the event, calculates the item being clicked on with
const hitTest = this.viewer.clientToWorld(event.canvasX, event.canvasY, true);
For a normal object/mesh/material, that works OK, and hitTest.dbId is returned, plus a hitTest.point object indicates where in 3D space I clicked. I use that location to create a material, mesh and object, then add to the scene. (A few small steps skipped)
var material_orange = new THREE.MeshLambertMaterial({color: 0xFEBD17});
this.viewer.impl.matman().addMaterial('SBD-Material-' + 'orange', material_orange, true);
var sphereMesh =
new THREE.Mesh(
new THREE.SphereGeometry(niceRadius, 20),
material_orange);
var sphereModel = new THREE.Object3D();
sphereModel.add(sphereMesh);
sphereModel.position.set(atPoint.x, atPoint.y, atPoint.z);
this.viewer.impl.scene.add(sphereModel);
this.viewer.impl.invalidate(true);
All that works great. The sphere is added to my model. (It's black, I still can't figure out why...) But the biggest problem is that I can't click on the sphere. Clicking on it selects the underlying object in the model, usually a wall or floor.
Do I need to use a RayTrace method to get the objects in the scene? Or is there a property to the object/mesh/material that will make it clickable? A viewer update?
Upvotes: 1
Views: 906
Reputation: 7080
In this moment, you must have to create an owned tool that inherits the Autodesk.Viewing.ToolInterface
and put your clicking logic in the handleSingleClick
function. Here is a example for this case:
The custom tool:
class CustomTool extends Autodesk.Viewing.ToolInterface {
constructor( viewer ) {
super();
this._viewer = viewer;
this._active = false;
this._names = [ 'CustomTool' ];
}
get viewer() {
return this._viewer;
}
isActive() {
return this._active;
}
handleSingleClick( event, button ) {
const _viewer = this.viewer;
const intersectObjects = (function () {
const pointerVector = new THREE.Vector3();
const pointerDir = new THREE.Vector3();
const ray = new THREE.Raycaster();
const camera = _viewer.impl.camera;
return function(pointer, objects, recursive) {
const rect = _viewer.impl.canvas.getBoundingClientRect();
const x = (( pointer.clientX - rect.left) / rect.width ) * 2 - 1;
const y = - (( pointer.clientY - rect.top) / rect.height ) * 2 + 1;
if (camera.isPerspective) {
pointerVector.set( x, y, 0.5 );
pointerVector.unproject( camera );
ray.set( camera.position, pointerVector.sub( camera.position ).normalize() );
} else {
pointerVector.set( x, y, -1 );
pointerVector.unproject( camera );
pointerDir.set( 0, 0, -1 );
ray.set( pointerVector, pointerDir.transformDirection( camera.matrixWorld ) );
}
const intersections = ray.intersectObjects( objects, recursive );
return intersections[0] ? intersections[0] : null;
};
})();
const pointer = event.pointers ? event.pointers[ 0 ] : event;
// Intersect objects in the scene
const result = intersectObjects( pointer, _viewer.impl.scene.children );
if( result && result.object ) {
const mesh = result.object;
// Change object color
let curColor = mesh.material.color;
curColor = ( curColor.getHex() == 0xff0000 ? 0x00ff00 : 0xff0000 );
mesh.material.color.setHex( curColor );
// Refreah viewport
this.viewer.impl.invalidate( false, true, true );
}
return false;
}
}
The viewer extension managed this custom tool:
class CustomToolExtension extends Autodesk.Viewing.Extension {
constructor( viewer, options ) {
super( viewer, options );
this._tool = undefined;
}
load() {
// Construct an custom Tool instance
this._tool = new CustomTool( this.viewer );
// Register custom Tool into viewer.toolController
this.viewer.toolController.registerTool( this._tool );
// Activate the Tool
this.viewer.toolController.activateTool( 'CustomTool' );
return true;
}
unload() {
// If tool has been activated, deactivate the tool.
if( this._tool.isActive() ) {
this.viewer.toolController.deactivateTool( 'CustomTool' );
}
// Deregister custom Tool
this.viewer.toolController.deregisterTool( this._tool );
return true;
}
}
Autodesk.Viewing.theExtensionManager.registerExtension( 'Autodesk.ADN.Sample.CustomToolExtension', CustomToolExtension );
Hope this help.
Upvotes: 1