-
Notifications
You must be signed in to change notification settings - Fork 10
/
joint.html
136 lines (114 loc) · 4.49 KB
/
joint.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
<!DOCTYPE html>
<html>
<head>
<title>Appendix A - Joint</title>
<style>
body {
margin: 0;
overflow: hidden;
}
#stats { /* Align stats top-left */
position: absolute;
left: 0px;
top: 0px;
}
</style>
</head>
<body>
<!-- JavaScript libraries -->
<script src="http://cdnjs.cloudflare.com/ajax/libs/three.js/r67/three.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/stats.js/r11/Stats.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.5/dat.gui.min.js"></script>
<script src="assets/libs/TrackballControls.js"></script>
<script>
// once everything is loaded, we run our Three.js stuff.
$(function () {
var stats = initStats();
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(45, window.innerWidth/window.innerHeight, 0.1, 1000);
var renderer = new THREE.WebGLRenderer();
// var trackballControls = new THREE.TrackballControls(camera);
renderer.setClearColor(new THREE.Color(0xEEEEEE,1.0));
renderer.setSize(window.innerWidth,window.innerHeight);
renderer.shadowMapEnabled = true;
// position and point the camera to the center of the scene
camera.position.set(0, 20, 20);
camera.up = new THREE.Vector3(0,1,0);
camera.lookAt(scene.position);
// add subtle ambient lighting
var ambiColor = "#0c0c0c";
var ambientLight = new THREE.AmbientLight(ambiColor);
scene.add(ambientLight);
var lightColor = "#ffffff";
var directionalLight = new THREE.DirectionalLight(lightColor);
directionalLight.position.set(0, 100, 0);
directionalLight.intensity = 1;
scene.add(directionalLight);
var axisHelper = new THREE.AxisHelper(3);
scene.add(axisHelper);
function mkJoint (radius, height) {
var joint = new THREE.Object3D();
var sphereGeometry = new THREE.SphereGeometry(radius, 8, 8);
var sphereMaterial = new THREE.MeshLambertMaterial({color: 0xdddd33, shading: THREE.FlatShading});
var sphere = new THREE.Mesh(sphereGeometry, sphereMaterial);
sphere.position.set(0, 0, 0);
joint.add(sphere);
var cylinderGeometry = new THREE.CylinderGeometry(radius, radius, height, 8, 8, false);
var cylinderMaterial = new THREE.MeshLambertMaterial({color: 0xcccccc, shading: THREE.FlatShading});
var cylinder = new THREE.Mesh(cylinderGeometry, cylinderMaterial);
cylinder.position.set(0, height / 2 + radius, 0);
sphere.add(cylinder);
var hook = new THREE.Object3D();
hook.position.set(0, height/2+radius, 0);
cylinder.add(hook);
joint.sphere = sphere;
joint.cylinder = cylinder;
joint.hook = hook;
return joint;
}
var height = 6;
var radius = 1;
var joint1 = mkJoint(radius, height);
scene.add(joint1);
var joint2 = mkJoint(radius, height);
joint1.hook.add(joint2);
// add the output of the renderer to the html element
$('body').append(renderer.domElement);
var controls = new function () {
this.alpha_1 = 0.0;
this.beta_1 = 0.0;
this.alpha_2 = 0.0;
this.beta_2 = 0.0;
};
var gui = new dat.GUI();
gui.add(controls, 'alpha_1', 0.0, 2*Math.PI).onChange(function (value) {
joint1.rotation.y = value;
});
gui.add(controls, 'beta_1', 0.0, Math.PI/2).onChange(function (value) {
joint1.sphere.rotation.x = value;
});
gui.add(controls, 'alpha_2', 0.0, 2*Math.PI).onChange(function (value) {
joint2.rotation.y = value;
});
gui.add(controls, 'beta_2', 0.0, Math.PI/2).onChange(function (value) {
joint2.sphere.rotation.x = value;
});
function render() {
stats.update();
// trackballControls.update();
// render using requestAnimationFrame
requestAnimationFrame(render);
renderer.render(scene, camera);
}
function initStats() {
var stats = new Stats();
stats.setMode(0); // 0: fps, 1: ms
$('body').append(stats.domElement);
return stats;
}
render();
});
</script>
</body>
</html>