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
|
// Задача: нарисувайте цветно хапче-капсула,
// с издължено тяло, със заоблени краища, като
// се върти хапчето, а не цялата сцена, за да
// не се движи и камерата
var renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.setAnimationLoop( drawFrame );
document.body.appendChild( renderer.domElement );
var scene = new THREE.Scene();
var aspect = window.innerWidth/window.innerHeight;
var camera = new THREE.PerspectiveCamera( 30, aspect );
camera.position.set( 0, 0, 20 );
camera.lookAt( new THREE.Vector3() );
// Хапчето като един обект
var pill = new THREE.Group();
var redMaterial = new THREE.MeshPhongMaterial( {color:'red'} );
var yellowMaterial = new THREE.MeshPhongMaterial( {color:'yellow'} );
var topCapGeometry = new THREE.SphereGeometry(1, 16, 16, 0, Math.PI * 2, 0, Math.PI / 2);
var topCap = new THREE.Mesh(topCapGeometry, redMaterial);
topCap.position.set(0, 2.5, 0);
pill.add(topCap);
var topCylinderGeometry = new THREE.CylinderGeometry( 1, 1, 2.5, 16 );
var topCylinder = new THREE.Mesh( topCylinderGeometry, redMaterial );
topCylinder.position.set(0, 1.25, 0);
pill.add(topCylinder);
var botCylinderGeometry = new THREE.CylinderGeometry( 1, 1, 2.5, 16 );
var botCylinder = new THREE.Mesh( botCylinderGeometry, yellowMaterial );
botCylinder.position.set(0, -1.25, 0);
pill.add(botCylinder);
var botCapGeometry = new THREE.SphereGeometry(1, 16, 16, 0, Math.PI * 2, Math.PI / 2, Math.PI / 2);
var botCap = new THREE.Mesh(botCapGeometry, yellowMaterial);
botCap.position.set(0, -2.5, 0);
pill.add(botCap);
scene.add( pill );
// Светлина
var light = new THREE.PointLight();
light.position.set( 0, 0, 20 );
scene.add(light);
// Въртене на сцената - в решението трябва да се
// върти хапчето, а не сцената
function drawFrame()
{
pill.rotation.x += 0.005;
pill.rotation.z += 0.005;
renderer.render( scene, camera );
}
|