-
Notifications
You must be signed in to change notification settings - Fork 224
Expand file tree
/
Copy pathViewer.js
More file actions
184 lines (165 loc) · 5.79 KB
/
Copy pathViewer.js
File metadata and controls
184 lines (165 loc) · 5.79 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/**
* @fileOverview
* @author David Gossow - dgossow@willowgarage.com
* @author Russell Toris - rctoris@wpi.edu
* @author Jihoon Lee - jihoonlee.in@gmail.com
*/
/**
* A Viewer can be used to render an interactive 3D scene to a HTML5 canvas.
*
* @constructor
* @param options - object with following keys:
*
* * divID - the ID of the div to place the viewer in
* * elem - the elem to place the viewer in (overrides divID if provided)
* * canvas (optional) - the canvas to render the viewer (overrides divID and elem if provided)
* * width - the initial width, in pixels, of the canvas
* * height - the initial height, in pixels, of the canvas
* * background (optional) - the color to render the background, like '#efefef'
* * alpha (optional) - the alpha of the background
* * antialias (optional) - if antialiasing should be used
* * intensity (optional) - the lighting intensity setting to use
* * cameraPosition (optional) - the starting position of the camera
* * displayPanAndZoomFrame (optional) - whether to display a frame when
* * panning/zooming. Defaults to true.
* * lineTypePanAndZoomFrame - line type for the frame that is displayed when
* * panning/zooming. Only has effect when
* * displayPanAndZoomFrame is set to true.
*/
ROS3D.Viewer = function(options) {
options = options || {};
var divID = options.divID;
var elem = options.elem;
var canvas = options.canvas;
var width = options.width;
var height = options.height;
var background = options.background || '#111111';
var antialias = options.antialias;
var intensity = options.intensity || 0.66;
var near = options.near || 0.01;
var far = options.far || 1000;
var alpha = options.alpha || 1.0;
var cameraPosition = options.cameraPose || {
x : 3,
y : 3,
z : 3
};
var cameraZoomSpeed = options.cameraZoomSpeed || 0.5;
var displayPanAndZoomFrame = (options.displayPanAndZoomFrame === undefined) ? true : !!options.displayPanAndZoomFrame;
var lineTypePanAndZoomFrame = options.lineTypePanAndZoomFrame || 'full';
// create the canvas to render to
this.renderer = new THREE.WebGLRenderer({
antialias : antialias,
alpha: true,
canvas: canvas
});
this.renderer.setClearColor(parseInt(background.replace('#', '0x'), 16), alpha);
this.renderer.sortObjects = false;
this.renderer.setSize(width, height);
this.renderer.shadowMap.enabled = false;
this.renderer.autoClear = false;
// create the global scene
this.scene = new THREE.Scene();
// create the global camera
this.camera = new THREE.PerspectiveCamera(40, width / height, near, far);
this.camera.position.x = cameraPosition.x;
this.camera.position.y = cameraPosition.y;
this.camera.position.z = cameraPosition.z;
// add controls to the camera
this.cameraControls = new ROS3D.OrbitControls({
scene : this.scene,
camera : this.camera,
displayPanAndZoomFrame : displayPanAndZoomFrame,
lineTypePanAndZoomFrame: lineTypePanAndZoomFrame
});
this.cameraControls.userZoomSpeed = cameraZoomSpeed;
// lights
this.scene.add(new THREE.AmbientLight(0x555555));
this.directionalLight = new THREE.DirectionalLight(0xffffff, intensity);
this.scene.add(this.directionalLight);
// propagates mouse events to three.js objects
this.selectableObjects = new THREE.Group();
this.scene.add(this.selectableObjects);
var mouseHandler = new ROS3D.MouseHandler({
renderer : this.renderer,
camera : this.camera,
rootObject : this.selectableObjects,
fallbackTarget : this.cameraControls
});
// highlights the receiver of mouse events
this.highlighter = new ROS3D.Highlighter({
mouseHandler : mouseHandler
});
this.stopped = true;
this.animationRequestId = undefined;
// add the renderer to the page if canvas is not passed into the options
if( canvas === undefined ) {
var node = elem || document.getElementById(divID);
node.appendChild(this.renderer.domElement);
}
// begin the render loop
this.start();
};
/**
* Start the render loop
*/
ROS3D.Viewer.prototype.start = function(){
this.stopped = false;
this.draw();
};
/**
* Renders the associated scene to the viewer.
*/
ROS3D.Viewer.prototype.draw = function(){
if(this.stopped){
// Do nothing if stopped
return;
}
// update the controls
this.cameraControls.update();
// put light to the top-left of the camera
// BUG: position is a read-only property of DirectionalLight,
// attempting to assign to it either does nothing or throws an error.
//this.directionalLight.position = this.camera.localToWorld(new THREE.Vector3(-1, 1, 0));
this.directionalLight.position.normalize();
// set the scene
this.renderer.clear(true, true, true);
this.renderer.render(this.scene, this.camera);
this.highlighter.renderHighlights(this.scene, this.renderer, this.camera);
// draw the frame
this.animationRequestId = requestAnimationFrame(this.draw.bind(this));
};
/**
* Stop the render loop
*/
ROS3D.Viewer.prototype.stop = function(){
if(!this.stopped){
// Stop animation render loop
cancelAnimationFrame(this.animationRequestId);
}
this.stopped = true;
};
/**
* Add the given THREE Object3D to the global scene in the viewer.
*
* @param object - the THREE Object3D to add
* @param selectable (optional) - if the object should be added to the selectable list
*/
ROS3D.Viewer.prototype.addObject = function(object, selectable) {
if (selectable) {
this.selectableObjects.add(object);
} else {
this.scene.add(object);
}
};
/**
* Resize 3D viewer
*
* @param width - new width value
* @param height - new height value
*/
ROS3D.Viewer.prototype.resize = function(width, height) {
this.camera.aspect = width / height;
this.camera.updateProjectionMatrix();
this.renderer.setSize(width, height);
};