Skip to content

Commit c161017

Browse files
committed
Fix fly camera
1 parent 19ba445 commit c161017

File tree

3 files changed

+54
-77
lines changed

3 files changed

+54
-77
lines changed

js/scripts/three-class-config.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -239,12 +239,10 @@ module.exports = {
239239
relativePath: './controls/FlyControls',
240240
superClass: 'Controls',
241241
properties: {
242-
forward_speed: new Types.Float(0),
243-
lateral_speed: new Types.Float(0),
244-
upward_speed: new Types.Float(0),
245-
roll: new Types.Float(0),
246-
pitch: new Types.Float(0),
247-
yaw: new Types.Float(0),
242+
moveVector: new Types.Vector3(0, 0, 0),
243+
rotationVector: new Types.Vector3(0, 0, 0),
244+
movementSpeed: new Types.Float(1.0),
245+
rollSpeed: new Types.Float(0.05),
248246
},
249247
},
250248
Picker: {

js/src/controls/FlyControls.js

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,48 @@
11
var _ = require('underscore');
22
var THREE = require('three');
3+
var widgets = require('@jupyter-widgets/base');
34
var FlyControls = require("../examples/controls/MomentumCameraControls.js").FlyControls;
45
var FlyControlsAutogen = require('./FlyControls.autogen');
56

67

78
var FlyControlsModel = FlyControlsAutogen.FlyControlsModel.extend({
89

910
constructThreeObject: function() {
10-
var that = this;
1111
this.clock = new THREE.Clock();
12+
var controlling = this.get('controlling');
13+
this.renderer = null;
14+
15+
obj = new FlyControls(controlling.obj);
16+
obj.dispose(); // Disconnect events, we need to (dis-)connect on freeze/thaw
17+
return obj
18+
},
19+
20+
setupListeners: function() {
21+
FlyControlsAutogen.FlyControlsModel.prototype.setupListeners.call(this);
22+
var that = this;
23+
this.obj.addEventListener('change', function() {
24+
that.update_controlled();
25+
});
26+
this.on('enableControl', this.onEnable, this);
27+
this.on('disableControl', this.onDisable, this);
28+
},
1229

13-
return widgets.resolvePromisesDict(this.model.get('controlling').views).then(function(views) {
14-
// get the view that is tied to the same renderer
15-
that.controlled_view = _.find(views, function(o) {
16-
return o.options.renderer_id === that.options.renderer_id
17-
}, that);
18-
obj = new FlyControls(that.controlled_view.obj, that.options.dom);
19-
that.register_object_parameters();
20-
that.options.register_update(that._update, that);
21-
obj.addEventListener('change', that.options.render_frame);
22-
obj.addEventListener('change', function() { that.update_controlled(); });
23-
that.options.start_update_loop();
24-
that.model.on_some_change(['forward_speed', 'upward_speed', 'lateral_speed',
25-
'roll', 'yaw', 'pitch'], that.update_plane, that);
26-
delete that.options.renderer;
27-
return obj
28-
});
30+
onEnable: function(renderer) {
31+
this.clock.start();
32+
this.renderer = renderer;
33+
this._update();
2934
},
3035

31-
update_plane: function() {
32-
this.obj.moveState.back = this.model.get('forward_speed');
33-
this.obj.moveState.up = this.model.get('upward_speed');
34-
this.obj.moveState.left = this.model.get('lateral_speed');
35-
this.obj.moveState.pitchUp = this.model.get('pitch');
36-
this.obj.moveState.yawRight = this.model.get('yaw');
37-
this.obj.moveState.rollLeft = this.model.get('roll');
38-
this.obj.updateRotationVector();
39-
this.obj.updateMovementVector();
36+
onDisable: function(renderer) {
37+
this.clock.stop();
38+
this.renderer = null;
4039
},
4140

4241
_update: function() {
43-
this.obj.movementSpeed = 0.33;
4442
this.obj.update(this.clock.getDelta());
43+
if (this.renderer !== null) {
44+
requestAnimationFrame(this._update.bind(this));
45+
}
4546
},
4647

4748
update_controlled: function() {

js/src/examples/controls/MomentumCameraControls.js

Lines changed: 22 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,28 @@ var FlyControls = function ( object, domElement ) {
99
this.object = object;
1010

1111
this.domElement = ( domElement !== undefined ) ? domElement : document;
12-
if ( domElement ) this.domElement.setAttribute( 'tabindex', -1 );
1312

1413
// API
1514

15+
// Set to false to disable this control
16+
this.enabled = true;
17+
1618
this.movementSpeed = 1.0;
1719
this.rollSpeed = 0.05;
1820

19-
this.dragToLook = false;
20-
this.autoForward = false;
21-
22-
// disable default target object behavior
21+
this.moveVector = new THREE.Vector3( 0, 0, 0 );
22+
this.rotationVector = new THREE.Vector3( 0, 0, 0 );
2323

2424
// internals
2525

2626
this.tmpQuaternion = new THREE.Quaternion();
2727
var lastPosition = new THREE.Vector3();
2828
var lastQuaternion = new THREE.Quaternion();
29-
var scope = this;
29+
var scope = this;
30+
var EPS = 0.000001;
3031

3132
this.mouseStatus = 0;
3233

33-
this.moveState = { up: 0, down: 0, left: 0, right: 0, forward: 0, back: 0, pitchUp: 0, pitchDown: 0, yawLeft: 0, yawRight: 0, rollLeft: 0, rollRight: 0 };
34-
this.moveVector = new THREE.Vector3( 0, 0, 0 );
35-
this.rotationVector = new THREE.Vector3( 0, 0, 0 );
36-
3734
this.handleEvent = function ( event ) {
3835
if ( typeof this[ event.type ] == 'function' ) {
3936
this[ event.type ]( event );
@@ -45,48 +42,30 @@ var FlyControls = function ( object, domElement ) {
4542
var moveMult = delta * this.movementSpeed;
4643
var rotMult = delta * this.rollSpeed;
4744

48-
this.object.translateX( this.moveVector.x * moveMult );
49-
this.object.translateY( this.moveVector.y * moveMult );
50-
this.object.translateZ( this.moveVector.z * moveMult );
45+
this.object.position.addScaledVector(this.moveVector, moveMult);
5146

5247
this.tmpQuaternion.set( this.rotationVector.x * rotMult, this.rotationVector.y * rotMult, this.rotationVector.z * rotMult, 1 ).normalize();
5348
this.object.quaternion.multiply( this.tmpQuaternion );
5449

5550
// expose the rotation vector for convenience
5651
this.object.rotation.setFromQuaternion( this.object.quaternion, this.object.rotation.order );
57-
this.dispatchEvent( changeEvent );
58-
lastPosition.copy( this.object.position );
59-
lastQuaternion.copy( this.object.position );
60-
};
52+
if (lastPosition.distanceToSquared( this.object.position ) > EPS ||
53+
8 * ( 1 - lastQuaternion.dot( this.object.quaternion ) ) > EPS ) {
6154

62-
this.updateMovementVector = function() {
63-
this.moveVector.x = ( -this.moveState.left + this.moveState.right );
64-
this.moveVector.y = ( -this.moveState.down + this.moveState.up );
65-
this.moveVector.z = ( -this.moveState.forward + this.moveState.back );
66-
};
67-
68-
this.updateRotationVector = function() {
69-
this.rotationVector.x = ( -this.moveState.pitchDown + this.moveState.pitchUp );
70-
this.rotationVector.y = ( -this.moveState.yawRight + this.moveState.yawLeft );
71-
this.rotationVector.z = ( -this.moveState.rollRight + this.moveState.rollLeft );
72-
};
73-
74-
this.getContainerDimensions = function() {
75-
if ( this.domElement != document ) {
76-
return {
77-
size : [ this.domElement.offsetWidth, this.domElement.offsetHeight ],
78-
offset : [ this.domElement.offsetLeft, this.domElement.offsetTop ]
79-
};
80-
} else {
81-
return {
82-
size : [ window.innerWidth, window.innerHeight ],
83-
offset : [ 0, 0 ]
84-
};
55+
this.dispatchEvent( changeEvent );
56+
lastPosition.copy( this.object.position );
57+
lastQuaternion.copy( this.object.quaternion );
58+
return true;
8559
}
60+
return false;
8661
};
8762

8863
this.dispose = function() {};
89-
this.connectEvents = function() {};
64+
this.connectEvents = function(element) {
65+
if (element) {
66+
scope.domElement = element;
67+
}
68+
};
9069

9170
function bind( scope, fn ) {
9271
return function () {
@@ -98,9 +77,8 @@ var FlyControls = function ( object, domElement ) {
9877

9978
var changeEvent = { type: 'change' };
10079

101-
this.updateMovementVector();
102-
this.updateRotationVector();
103-
80+
// Initialize lastPosition/Quaternion to initial values.
81+
this.update(0);
10482
};
10583

10684
FlyControls.prototype = Object.create( THREE.EventDispatcher.prototype );

0 commit comments

Comments
 (0)