diff options
[OKG] Solved practice 02 exercises
1 files changed, 83 insertions, 0 deletions
| -rw-r--r-- | Year 2/Computer Graphics Fundamentals/02-Practice-10.10.2022/02.js | 83 |
diff --git a/Year 2/Computer Graphics Fundamentals/02-Practice-10.10.2022/02.js b/Year 2/Computer Graphics Fundamentals/02-Practice-10.10.2022/02.js new file mode 100644 index 0000000..08640d3 --- /dev/null +++ b/Year 2/Computer Graphics Fundamentals/02-Practice-10.10.2022/02.js @@ -0,0 +1,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 ); +} + |
