Skip to content

Commit 7f20742

Browse files
committed
refactor renderer to use spheres in circular motion and improve lighting setup
1 parent 30f2393 commit 7f20742

File tree

1 file changed

+27
-28
lines changed

1 file changed

+27
-28
lines changed

src/renderer.ts

+27-28
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,64 @@
11
import * as THREE from 'three';
22
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls';
33

4-
const fps = 60; // FPS desiderati
4+
const fps = 60;
55
const fpsInterval = 1000 / fps;
66
let then = performance.now();
77

8-
98
const scene = new THREE.Scene();
109
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
10+
camera.position.z = 5;
1111

1212
const renderer = new THREE.WebGLRenderer();
1313
renderer.setSize(window.innerWidth, window.innerHeight);
1414
renderer.setClearColor(new THREE.Color(0x021221), 1);
15-
1615
document.body.appendChild(renderer.domElement);
1716

1817
const controls = new OrbitControls(camera, renderer.domElement);
1918

20-
const geometry = new THREE.SphereGeometry(0.2, 100, 100);
21-
const material = new THREE.MeshStandardMaterial({ color: 0x00ff00, flatShading: true });
22-
const cube1 = new THREE.Mesh(geometry, material);
23-
const cube2 = new THREE.Mesh(geometry, material);
24-
scene.add(cube1, cube2);
25-
26-
const directionalLight = new THREE.HemisphereLight(0xffffff, 10);
27-
scene.add(directionalLight);
19+
const hemisphereLight = new THREE.HemisphereLight(0xffffff, 10);
20+
scene.add(hemisphereLight);
2821

2922
const gridHelper = new THREE.GridHelper(10, 10);
3023
scene.add(gridHelper);
3124

3225
const axesHelper = new THREE.AxesHelper(5);
3326
scene.add(axesHelper);
3427

35-
camera.position.z = 5;
36-
3728
renderer.setAnimationLoop(animate);
3829

30+
let spheres: THREE.Mesh[] = [];
31+
addSphere(0.2, spheres);
32+
addSphere(0.2, spheres);
3933

4034
let angle = 0;
41-
const radius = 1; // Raggio del cerchio
42-
const angularSpeed = Math.PI / 2; // Velocità angolare in radianti al secondo
43-
function animate(now: number) {
35+
const radius = 1;
36+
const angularSpeed = Math.PI / 2;
4437

38+
function animate(now: number) {
4539
const elapsed = now - then;
46-
4740
if (elapsed > fpsInterval) {
4841
then = now - (elapsed % fpsInterval);
49-
const deltaTime = elapsed / 1000; // Converti millisecondi in secondi
42+
const deltaTime = elapsed / 1000;
5043

51-
// Aggiorna l'angolo
5244
angle += angularSpeed * deltaTime;
5345
angle = angle % (2 * Math.PI);
54-
55-
// Posizioni delle sfere in moto circolare
56-
cube1.position.z = radius * Math.cos(angle);
57-
cube1.position.x = radius * Math.sin(angle);
58-
59-
cube2.position.z = radius * Math.cos(angle + Math.PI);
60-
cube2.position.x = radius * Math.sin(angle + Math.PI);
61-
46+
47+
let directionMultiplier : number = 1;
48+
for (const sphere of spheres) {
49+
sphere.position.x = directionMultiplier * radius * Math.sin(angle);
50+
sphere.position.z = directionMultiplier * radius * Math.cos(angle);
51+
directionMultiplier *= -1;
52+
}
53+
controls.update();
6254
renderer.render(scene, camera);
6355
}
64-
controls.update();
56+
}
57+
58+
function addSphere(radius: number, spheres: THREE.Mesh[]) {
59+
const geometry = new THREE.SphereGeometry(radius, 100, 100);
60+
const material = new THREE.MeshStandardMaterial({ color: 0x00ff00, flatShading: true });
61+
const sphere = new THREE.Mesh(geometry, material);
62+
scene.add(sphere);
63+
spheres.push(sphere);
6564
}

0 commit comments

Comments
 (0)