1
1
import * as THREE from 'three' ;
2
2
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls' ;
3
3
4
- const fps = 60 ; // FPS desiderati
4
+ const fps = 60 ;
5
5
const fpsInterval = 1000 / fps ;
6
6
let then = performance . now ( ) ;
7
7
8
-
9
8
const scene = new THREE . Scene ( ) ;
10
9
const camera = new THREE . PerspectiveCamera ( 75 , window . innerWidth / window . innerHeight , 0.1 , 1000 ) ;
10
+ camera . position . z = 5 ;
11
11
12
12
const renderer = new THREE . WebGLRenderer ( ) ;
13
13
renderer . setSize ( window . innerWidth , window . innerHeight ) ;
14
14
renderer . setClearColor ( new THREE . Color ( 0x021221 ) , 1 ) ;
15
-
16
15
document . body . appendChild ( renderer . domElement ) ;
17
16
18
17
const controls = new OrbitControls ( camera , renderer . domElement ) ;
19
18
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 ) ;
28
21
29
22
const gridHelper = new THREE . GridHelper ( 10 , 10 ) ;
30
23
scene . add ( gridHelper ) ;
31
24
32
25
const axesHelper = new THREE . AxesHelper ( 5 ) ;
33
26
scene . add ( axesHelper ) ;
34
27
35
- camera . position . z = 5 ;
36
-
37
28
renderer . setAnimationLoop ( animate ) ;
38
29
30
+ let spheres : THREE . Mesh [ ] = [ ] ;
31
+ addSphere ( 0.2 , spheres ) ;
32
+ addSphere ( 0.2 , spheres ) ;
39
33
40
34
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 ;
44
37
38
+ function animate ( now : number ) {
45
39
const elapsed = now - then ;
46
-
47
40
if ( elapsed > fpsInterval ) {
48
41
then = now - ( elapsed % fpsInterval ) ;
49
- const deltaTime = elapsed / 1000 ; // Converti millisecondi in secondi
42
+ const deltaTime = elapsed / 1000 ;
50
43
51
- // Aggiorna l'angolo
52
44
angle += angularSpeed * deltaTime ;
53
45
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 ( ) ;
62
54
renderer . render ( scene , camera ) ;
63
55
}
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 ) ;
65
64
}
0 commit comments