forked from ninjadev/webgl-kurs-ntnu
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
160 lines (123 loc) · 3.96 KB
/
script.js
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
var currentDemo;
slideshow.on('afterShowSlide', function(slide) {
console.log(slide.properties);
switch (slide.properties.demo) {
case 'rotate-cube':
currentDemo = rotateCube();
break;
case 'show-sphere':
currentDemo = showSphere();
break;
case 'show-sphere-mesh':
currentDemo = showSphereMesh();
break;
}
});
slideshow.on('hideSlide', function(slide) {
if (currentDemo) currentDemo.stop();
});
function rotateCube() {
var renderer = new THREE.WebGLRenderer({ antialias : true });
var container = document.querySelector('#rotate-cube');
console.log(container.offsetWidth);
renderer.setSize(container.offsetWidth, container.offsetHeight);
container.appendChild(renderer.domElement);
var camera = new THREE.PerspectiveCamera(75, container.offsetWidth / container.offsetHeight, 1, 1000);
camera.position.z = 300;
var scene = new THREE.Scene();
var geometry = new THREE.CubeGeometry(200, 200, 200);
var material = new THREE.MeshLambertMaterial({
color: 0xff0000
});
var mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);
var light = new THREE.PointLight(0xffffff);
light.position = new THREE.Vector3(0,100,400);
scene.add(light);
var pause = false;
function animate() {
!pause && requestAnimationFrame(animate);
mesh.rotation.x += .005;
mesh.rotation.y += .005;
renderer.render(scene, camera);
}
animate();
return {
stop: function() {
pause = true;
container.innerHTML = "";
}
};
}
function showSphere() {
var camera, scene, renderer;
var geometry, material, cube;
renderer = new THREE.WebGLRenderer({alpha: true});
var container = document.querySelector('#show-sphere');
console.log(container.offsetWidth);
renderer.setSize(container.offsetWidth, container.offsetHeight);
container.appendChild(renderer.domElement);
camera = new THREE.PerspectiveCamera(75, container.offsetWidth / container.offsetHeight, 1, 1000);
camera.position.z = 250;
scene = new THREE.Scene();
// Set up geometries, materials and meshes here
var geo = new THREE.SphereGeometry(100, 10, 10);
var mat = new THREE.MeshBasicMaterial({color: 0xffffff, wireframe:true, wireframeLinewidth:2});
cube = new THREE.Mesh(geo, mat);
scene.add(cube);
var light = new THREE.PointLight(0xffffff);
light.position.set(0, 100, 400);
scene.add(light);
var pause = false;
function animate() {
!pause && requestAnimationFrame(animate);
var t = Date.now() / 1000; // Seconds elapsed since 1st of January 1970
// TODO: Add animation
cube.rotation.y = t/2;
cube.rotation.z = t/10;
renderer.render(scene, camera);
}
animate();
return {
stop: function() {
pause = true;
container.innerHTML = "";
}
};
}
function showSphereMesh() {
var camera, scene, renderer;
var geometry, material, cube;
renderer = new THREE.WebGLRenderer({alpha: true});
var container = document.querySelector('#show-sphere-mesh');
console.log(container.offsetWidth);
renderer.setSize(container.offsetWidth, container.offsetHeight);
container.appendChild(renderer.domElement);
camera = new THREE.PerspectiveCamera(75, container.offsetWidth / container.offsetHeight, 1, 1000);
camera.position.z = 250;
scene = new THREE.Scene();
// Set up geometries, materials and meshes here
var geo = new THREE.SphereGeometry(100, 20, 20);
var mat = new THREE.MeshLambertMaterial({color: 0xff0000});
cube = new THREE.Mesh(geo, mat);
scene.add(cube);
var light = new THREE.PointLight(0xffffff);
light.position.set(0, 100, 400);
scene.add(light);
var pause = false;
function animate() {
!pause && requestAnimationFrame(animate);
var t = Date.now() / 1000; // Seconds elapsed since 1st of January 1970
// TODO: Add animation
cube.rotation.y = t/10;
cube.rotation.z = t/25;
renderer.render(scene, camera);
}
animate();
return {
stop: function() {
pause = true;
container.innerHTML = "";
}
};
}