-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketchfab.html
159 lines (141 loc) · 6.68 KB
/
sketchfab.html
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
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Sketchfab Viewer API example</title>
<!-- Insert this script -->
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/4.0.1/math.min.js"></script>
<script type="text/javascript" src="https://static.sketchfab.com/api/sketchfab-viewer-1.1.0.js"></script>
<script src="../dist/3dRudder-2.0.2.js"></script>
<script src="js/three.min.js"></script>
</head>
<body style="width:840px;margin:0 auto;">
<!-- Insert an empty iframe -->
<iframe style="border:0" width="840" height="480" src="" id="api-frame" allowfullscreen mozallowfullscreen="true" webkitallowfullscreen="true"></iframe>
<!-- Initialize the viewer -->
<script type="text/javascript">
var iframe = document.getElementById( 'api-frame' );
var urlid = '1ae52e5a8f954cee99b9fae331eca742';
//'6a76697207814abf88f835a9eee2eb8d'; back to the futur
var s = location.search;
if (s)
urlid = s.substring(s.indexOf("=")+1);
var version = '1.0.0';
// By default, the latest version of the viewer API will be used.
//var client = new Sketchfab( iframe );
// Alternatively, you can request a specific version.
// var client = new Sketchfab( '1.0.0', iframe );
var SDK = new Sdk3dRudder();
SDK.init();
SDK.on('connectedDevice', function(device) {
// sample curves with deadzone 0.1
var controller = SDK.controllers[device.port];
controller.setAxesParam({
roll2YawCompensation: 0.0,
nonSymmetricalPitch: false,
curves: {
leftright: {deadzone: 0.15, xSat: 1.0, exp: 2.0},
forwardbackward: {deadzone: 0.15, xSat: 1.0, exp: 2.0},
updown: {deadzone: 0.15, xSat: 1.0, exp: 2.0},
rotation: {deadzone: 0.15, xSat: 1.0, exp: 1.0}
}
});
});
var mode = 0; // 0: orbit, 1: fps
var target = new THREE.Vector3();
var position = new THREE.Vector3();
var rotation = new THREE.Quaternion();
var radius = 20;
var scale = 1;
var phi = 0;
var theta = -1;
var UP = new THREE.Vector3(0,0,1);
var client = new Sketchfab( version, iframe );
var error = function () {
console.error( 'Error api Sketchfab !' );
};
var EPS = 0.000001;
var success = function ( api ) {
window.api = api;
api.start( function () {
api.addEventListener( 'viewerready', function() {
api.getCameraLookAt( function( err, camera ) {
position.fromArray(camera.position);
target.fromArray(camera.target);
console.log(target + " "+ position);
scale = target.distanceTo(position);
radius = scale;
var p = position.clone();
var t = target.clone();
//p.z = t.z = 0;
p.normalize();
t.normalize();
var dir = new THREE.Vector3();
dir.subVectors(t, p);
dir.normalize();
console.log(dir);
var angle = dir.angleTo(new THREE.Vector3(0,1,0));
console.log(angle);
rotation.setFromAxisAngle(UP, angle);
console.log(target);
});
});
var clock = new THREE.Clock(true);
SDK.on('frame', function(frame) {
var timeElapsed = clock.getDelta();
var controller = frame.controllers[0];
if (controller.connected && controller.status >= 6) {
// orbit
if (mode == 0) {
// speed scale
var forward = new THREE.Vector3();
forward.subVectors(target, position);
var speed = forward.length();
var axis = controller.axis;
radius += -axis.forwardbackward * timeElapsed * speed;
radius = Math.max( scale/10, Math.min( scale*10, radius ) );
phi += axis.updown * timeElapsed;
theta += -axis.rotation * timeElapsed;
phi = Math.max( -Math.PI/2, Math.min( Math.PI/2, phi ) );
// orbit + zoom
position.x = target.x + radius * Math.cos( phi ) * Math.cos( theta );
position.y = target.y + radius * Math.cos( phi ) * Math.sin( theta );
position.z = target.z + radius * Math.sin( phi );
// pan
forward.normalize();
var right = new THREE.Vector3();
right.crossVectors(forward, UP);
var pan = axis.leftright * timeElapsed * speed;
right.multiplyScalar(pan);
target.add(right);
}
// mode FPS
else if (mode == 1) {
var axis = controller.axis;
var forwardSpeed = axis.forwardbackward * timeElapsed * scale;
var strafeSpeed = axis.leftright * timeElapsed * scale;
var updownSpeed = axis.updown * timeElapsed * scale;
var delta = new THREE.Quaternion().setFromAxisAngle(UP, -axis.rotation * timeElapsed);
rotation.multiply(delta);
console.log(target);
var translation = new THREE.Vector3(strafeSpeed, forwardSpeed, updownSpeed).applyQuaternion(rotation);
var forward = new THREE.Vector3(0,1,0);
forward.applyQuaternion(rotation);
position.add(translation);
target = target.addVectors(position, forward);//.applyQuaternion(delta);
}
//target[0] += Math.sin(theta) * pan;
//target[1] += Math.cos(theta) * pan;
api.lookat( position.toArray(), target.toArray(), 0 );
}
})
});
};
client.init( urlid, {
success: success,
error: error,
camera: 0
});
</script>
</body>
</html>