-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera-controller.js.diff
More file actions
177 lines (165 loc) · 7.03 KB
/
camera-controller.js.diff
File metadata and controls
177 lines (165 loc) · 7.03 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
diff --git a/controllers/camera-controller.js b/controllers/camera-controller.js
index 855b290..9e22a8e 100755
--- a/controllers/camera-controller.js
+++ b/controllers/camera-controller.js
@@ -76,12 +76,25 @@ exports.CameraController = Montage.specialize( {
}
},
+ // TSV: Fixes bad cam orientation
+ _lastIdentifier: { value: null, writeable: true },
+
viewPointDidChange: {
value: function() {
this._computeInitialDistance();
}
},
+ // TSV: Use this to explicitly set this._transform.matrix on change
+ // of viewPoint.
+ changeViewPointTrans: {
+ value: function(transMat /*mat4*/) {
+ if (this._transform != null) {
+ this._transform.matrix = transMat;
+ }
+ }
+ },
+
_viewPoint: { value: null, writable: true},
viewPoint: {
@@ -136,28 +149,34 @@ exports.CameraController = Montage.specialize( {
var delta = this._deltaForEvent(event);
var wheelStep = this.zoomStep * delta;
- var self = this;
var eye = vec3.create(this.viewPoint.glTFElement.transform.translation);
var len = 1.0;
var direction = vec3.createFrom(0, 0, len);
+
mat4.rotateVec3(this.viewPoint.glTFElement.transform.matrix, direction);
eye[0] += wheelStep * direction[0];
eye[1] += wheelStep * direction[1];
eye[2] += wheelStep * direction[2];
+
+ this._transform.translation[0] += wheelStep * direction[0];
+ this._transform.translation[1] += wheelStep * direction[1];
+ this._transform.translation[2] += wheelStep * direction[2];
+
this.viewPoint.glTFElement.transform.translation = eye;
}
},
translate: {
value: function(event) {
- //this._transform.matrix = this.viewPoint.glTFElement.worldMatrix;
if (this.moving == false)
return;
- var xDelta = event.translateX-this._lastPosition[0];
- var yDelta = event.translateY-this._lastPosition[1];
+
+ var xDelta = event.translateX - this._lastPosition[0];
+ var yDelta = event.translateY - this._lastPosition[1];
+
xDelta *= 0.05;
yDelta *= -0.05;
- //-----------------------------------------------------------
+
//if (this._axisUp == null) {
this._axisUp = vec3.createFrom(0, 1, 0);
mat4.rotateVec3(this._transform.matrix, this._axisUp);
@@ -165,26 +184,20 @@ exports.CameraController = Montage.specialize( {
var hasTarget = false;
var targetPosition;
if (hasTarget == false) {
- var rootNode = this.node.glTFElement;
var sceneBBox = this.sceneBBox;
targetPosition = [
(sceneBBox[0][0] + sceneBBox[1][0]) / 2,
(sceneBBox[0][1] + sceneBBox[1][1]) / 2,
(sceneBBox[0][2] + sceneBBox[1][2]) / 2];
}
-
-
var direction = vec3.create();
var eye = vec3.create(this._transform.translation);
- var xvec = vec3.createFrom(1, 0, 0);
- mat4.rotateVec3(this._transform.matrix, xvec);
-
var len = -1;
var avec = vec3.createFrom(0, 0, len);
mat4.rotateVec3(this._transform.matrix, avec);
targetPosition=[eye[0]+avec[0],eye[1]+avec[1],eye[2]+avec[2]];
-
+
direction[0] = targetPosition[0] - eye[0];
direction[1] = targetPosition[1] - eye[1];
direction[2] = targetPosition[2] - eye[2];
@@ -199,14 +212,21 @@ exports.CameraController = Montage.specialize( {
var cameraMat = mat4.identity();
- var ok = this.__des;
- if (this.__des == undefined) {
- this.__des=Math.abs(yDelta) > Math.abs(xDelta);
- }
- if (this.__des)
- mat4.rotate(cameraMat, -yDelta*0.65, right);
+ var ratio = 0;
+ if (Math.abs(yDelta) > Math.abs(xDelta)) {
+ ratio = Math.abs(yDelta) / Math.abs(xDelta);
+ } else {
+ ratio = Math.abs(xDelta) / Math.abs(yDelta);
+ }
+
+ if (ratio > 0.5) {
+ mat4.rotate(cameraMat, xDelta, axisUpAdjusted);
+ mat4.rotate(cameraMat, yDelta, right);
+ } else
+ if (Math.abs(yDelta) > Math.abs(xDelta))
+ mat4.rotate(cameraMat, yDelta, right);
else
- mat4.rotate(cameraMat, -xDelta, axisUpAdjusted);
+ mat4.rotate(cameraMat, xDelta, axisUpAdjusted);
eye[0] -= targetPosition[0];
eye[1] -= targetPosition[1];
@@ -219,7 +239,7 @@ exports.CameraController = Montage.specialize( {
eye[2] += targetPosition[2];
var rotationMatrix = mat4.identity();
- mat4.multiply3(cameraMat, this._transform.matrix, rotationMatrix);
+ mat4.multiply3(cameraMat, this._transform.matrix, rotationMatrix);
var translationMatrix = mat4.identity();
mat4.translate(translationMatrix, eye);
@@ -235,17 +255,39 @@ exports.CameraController = Montage.specialize( {
this.moving = true;
if (this._transform == null) {
this._transform = Object.create(Transform).init();
+ // TSV: Fixes bad cam rotation
+ // If this._transform.matrix is updated on each beginTranslate,
+ // the translation begins at a different orientation each
+ // time, causing bad camera behavior. Uncomment the `Old`
+ // code to see what happens.
+ this._transform.matrix = this.viewPoint.glTFElement.transform.matrix;
}
+ /*
+ // Old
this._transform.matrix = this.viewPoint.glTFElement.transform.matrix;
this._lastPosition[0] = event.translateX;
this._lastPosition[1] = event.translateY;
+ */
+
+ // TSV: Fixes bad cam rotation
+ // this._lastPosition should only be updated when cameras are
+ // switched, otherwise the orientation would begin at the
+ // same target every time the left mouse button is pressed and
+ // the camera is moved. Uncomment and see what happens.
+ var currIdentifier = this.viewPoint.glTFElement.name;
+ if (this._lastIdentifier == null ||
+ currIdentifier !== this._lastIdentifier) {
+ this._lastPosition[0] = event.translateX;
+ this._lastPosition[1] = event.translateY;
+ this._lastIdentifier = currIdentifier;
+ }
}
},
endTranslate: {
value: function(event) {
this.moving = false;
- delete this.__des;
+ delete this.__des;
this._axisUp = null;
}
}