Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 修改 Viewer.js
- class Viewer {
- constructor(container) {
- // ... other code snippet
- this.selection = []; //!<<< add this line
- }
- init() {
- // ... other code snippet
- this.buildContextMenu(); //!<<< add this line
- }
- // 取代整個
- onMouseClicked( event ) {
- this.clearSelection();
- const result = this.hitTest(event.clientX, event.clientY);
- if (!result) return;
- console.log(result);
- const mesh = result.object;
- this.selection.push(mesh.id);
- const geometry = mesh.geometry.clone();
- const selectionOverly = new THREE.Mesh(geometry, this.selectionMaterial);
- selectionOverly.matrix.copy(mesh.matrixWorld);
- selectionOverly.matrixAutoUpdate = false;
- selectionOverly.matrixWorldNeedsUpdate = true;
- selectionOverly.frustumCulled = false;
- selectionOverly.dbId = mesh.id;
- this.selectionScene.add(selectionOverly);
- }
- clearSelection() {
- while (this.selectionScene.children.length > 0) {
- let mesh = this.selectionScene.children.pop();
- mesh.material.dispose();
- mesh.geometry.dispose();
- mesh.material = null;
- mesh.geometry = null;
- mesh = null;
- }
- while (this.selection.length) {
- this.selection.pop();
- }
- }
- clientToViewport(clientX, clientY) {
- const mouse = new THREE.Vector2();
- mouse.x = (clientX / window.innerWidth) * 2 - 1;
- mouse.y = -(clientY / window.innerHeight) * 2 + 1;
- return mouse;
- }
- hitTestViewport(vpVec) {
- const raycaster = new THREE.Raycaster();
- raycaster.setFromCamera(vpVec, this.camera);
- const intersects = raycaster.intersectObjects(this.models, true);
- console.log(intersects);
- return intersects[0];
- }
- hitTest(clientX, clientY) {
- return this.hitTestViewport(this.clientToViewport(clientX, clientY));
- }
- buildContextMenu() {
- const dataSource = [
- {
- title: 'Hide',
- key: 'hide-selected',
- },
- {
- title: 'Isolate',
- key: 'isolate-selected'
- },
- {
- title: 'Show All',
- key: 'show-all'
- }
- ];
- const contextMenu = new SimpleContextMenu();
- const callBack = (key) => {
- alert(key);
- };
- const options = {
- delay: 500//delay submenu
- };
- contextMenu.register(`#${this.container.id}`, callBack, dataSource, options);
- this.contextMenu = contextMenu;
- }
- // 修改 js/index.js
- (async function () {
- const SimpleContextMenu = await require('simple-context-menu');
- window.SimpleContextMenu = SimpleContextMenu.default;
- // ... other code snippet
- })();
- // 修改 index.html
- <script src="https://unpkg.com/require-unpkg"></script>
Advertisement
Add Comment
Please, Sign In to add comment