Summary Files Commits Branches @commit @diff @stats
path: root / Year 2 / Computer Graphics Fundamentals / 02-Practice-10.10.2022 / 02.js
blob: 08640d35c915a1003a80748981a46ba69455770d (plain) (blame)
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
// Задача: свържете кубовете с гради по ръбовете
// на въображаемият голям куб

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( 3, 3, 8 );
camera.lookAt( new THREE.Vector3() );

// кубове във върховете
var geometry = new THREE.BoxGeometry( 1/3, 1/3, 1/3 );
var material = new THREE.MeshPhongMaterial();

for( var x=-1; x<2; x+=2 )
for( var y=-1; y<2; y+=2 )
for( var z=-1; z<2; z+=2 )
{
	var cube = new THREE.Mesh( geometry, material );
	cube.position.set( x, y, z );
	scene.add( cube );
}



// златни греди по ръбовете
var barMaterial = new THREE.MeshPhongMaterial({ color: 0xFFFF00 });

function newBar(barGeometry, x, y, z) {
	var bar = new THREE.Mesh(barGeometry, barMaterial);
	bar.position.set( x, y, z );
	scene.add(bar);
}

// Each coordinate is either 1, 0 or -1, and because we're working with edges, always two
// coordinates will be nonzero.
// So, we have three main triplets: (0, 1, 1), (1, 0, 1) and (1, 1, 0) with four variations
// each, for the combinations of positive and negative 1s. Example:
// (0, 1, 1), (0, 1, -1), (0, -1, 1), (0, -1, -1)
// Finally we notice that the wide side is the one with the zero coordinate (or in another sense,
// we stretch a square in that axis).
// For the main triplets, because it's simpler, we generate their inverse via binary representation
// of numbers, so we generate the numbers 100, 010 and 001, which are then turned into the triplets.
for (var i = 1; i < 5; i <<= 1) {
	var fir = +!(i & 1), sec = +!(i & 2), thr = +!(i & 4); // number to main triplet
	var barGeometry = new THREE.BoxGeometry( 1/8 + !fir * 15/8, 1/8 + !sec * 15/8, 1/8 + !thr * 15/8 );
	newBar(barGeometry, fir, sec, thr);
	
	// Combinations for positive and negative 1s
	for (var m = -1; m < 2; m += 2) {
		newBar(barGeometry, m * fir, m * sec, m * thr);

		if (fir === 1)
			newBar(barGeometry, fir, m * sec, m * thr);
		if (sec === 1)
			newBar(barGeometry, m * fir, sec, m * thr);
		if (thr === 1)
			newBar(barGeometry, m * fir, m * sec, thr);
	}
}



var light = new THREE.PointLight( );
light.position.set( 3, 4, 5 );
scene.add( light );

light = new THREE.PointLight( );
light.position.set( -3, 4, -5 );
scene.add( light );


function drawFrame()
{
	scene.rotation.y += 0.005;
	renderer.render( scene, camera );
}