forked from potree/potree
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProfileTool.js
More file actions
133 lines (101 loc) · 3.84 KB
/
ProfileTool.js
File metadata and controls
133 lines (101 loc) · 3.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import * as THREE from "../../libs/three.js/build/three.module.js";
import {Profile} from "./Profile.js";
import {Utils} from "../utils.js";
import { EventDispatcher } from "../EventDispatcher.js";
export class ProfileTool extends EventDispatcher {
constructor (viewer) {
super();
this.viewer = viewer;
this.renderer = viewer.renderer;
this.addEventListener('start_inserting_profile', e => {
this.viewer.dispatchEvent({
type: 'cancel_insertions'
});
});
this.scene = new THREE.Scene();
this.scene.name = 'scene_profile';
this.light = new THREE.PointLight(0xffffff, 1.0);
this.scene.add(this.light);
this.viewer.inputHandler.registerInteractiveScene(this.scene);
this.onRemove = e => this.scene.remove(e.profile);
this.onAdd = e => this.scene.add(e.profile);
for(let profile of viewer.scene.profiles){
this.onAdd({profile: profile});
}
viewer.addEventListener("update", this.update.bind(this));
viewer.addEventListener("render.pass.perspective_overlay", this.render.bind(this));
viewer.addEventListener("scene_changed", this.onSceneChange.bind(this));
viewer.scene.addEventListener('profile_added', this.onAdd);
viewer.scene.addEventListener('profile_removed', this.onRemove);
}
onSceneChange(e){
if(e.oldScene){
e.oldScene.removeEventListeners('profile_added', this.onAdd);
e.oldScene.removeEventListeners('profile_removed', this.onRemove);
}
e.scene.addEventListener('profile_added', this.onAdd);
e.scene.addEventListener('profile_removed', this.onRemove);
}
startInsertion (args = {}) {
let domElement = this.viewer.renderer.domElement;
let profile = new Profile();
profile.name = args.name || 'Profile';
this.dispatchEvent({
type: 'start_inserting_profile',
profile: profile
});
this.scene.add(profile);
let cancel = {
callback: null
};
let insertionCallback = (e) => {
if(e.button === THREE.MOUSE.LEFT){
if(profile.points.length <= 1){
let camera = this.viewer.scene.getActiveCamera();
let distance = camera.position.distanceTo(profile.points[0]);
let clientSize = this.viewer.renderer.getSize(new THREE.Vector2());
let pr = Utils.projectedRadius(1, camera, distance, clientSize.width, clientSize.height);
let width = (10 / pr);
profile.setWidth(width);
}
profile.addMarker(profile.points[profile.points.length - 1].clone());
this.viewer.inputHandler.startDragging(
profile.spheres[profile.spheres.length - 1]);
} else if (e.button === THREE.MOUSE.RIGHT) {
cancel.callback();
}
};
cancel.callback = e => {
profile.removeMarker(profile.points.length - 1);
domElement.removeEventListener('mouseup', insertionCallback, false);
this.viewer.removeEventListener('cancel_insertions', cancel.callback);
};
this.viewer.addEventListener('cancel_insertions', cancel.callback);
domElement.addEventListener('mouseup', insertionCallback, false);
profile.addMarker(new THREE.Vector3(0, 0, 0));
this.viewer.inputHandler.startDragging(
profile.spheres[profile.spheres.length - 1]);
this.viewer.scene.addProfile(profile);
return profile;
}
update(){
let camera = this.viewer.scene.getActiveCamera();
let profiles = this.viewer.scene.profiles;
let renderAreaSize = this.viewer.renderer.getSize(new THREE.Vector2());
let clientWidth = renderAreaSize.width;
let clientHeight = renderAreaSize.height;
this.light.position.copy(camera.position);
// make size independant of distance
for(let profile of profiles){
for(let sphere of profile.spheres){
let distance = camera.position.distanceTo(sphere.getWorldPosition(new THREE.Vector3()));
let pr = Utils.projectedRadius(1, camera, distance, clientWidth, clientHeight);
let scale = (15 / pr);
sphere.scale.set(scale, scale, scale);
}
}
}
render(){
this.viewer.renderer.render(this.scene, this.viewer.scene.getActiveCamera());
}
}