-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
89 lines (56 loc) · 2.48 KB
/
main.js
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
84
85
86
87
import * as THREE from 'https://cdn.jsdelivr.net/npm/[email protected]/build/three.module.js';
import {OrbitControls} from 'https://cdn.jsdelivr.net/npm/[email protected]/examples/jsm/controls/OrbitControls.js';
import {FirstPersonControls} from 'https://cdn.skypack.dev/[email protected]/examples/jsm/controls/FirstPersonControls.js';
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(0, 1.6, 10);
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
const geometry = new THREE.BoxGeometry();
const material = new THREE.MeshBasicMaterial({ color: '#242a54' });
const cube = new THREE.Mesh(geometry, material);
cube.position.y = 2;
scene.add(cube);
const controls = new OrbitControls(camera, renderer.domElement);
controls.update();
const geometry_torus = new THREE.TorusKnotGeometry( 1, 0.3, 64, 8 );
const material_torus = new THREE.MeshBasicMaterial( { color: '#6995c2' } );
const torusKnot = new THREE.Mesh( geometry_torus, material_torus ); scene.add( torusKnot );
torusKnot.position.x = 6;
torusKnot.position.y = 4;
torusKnot.position.z = 4;
scene.add(torusKnot);
const controlsFP = new FirstPersonControls(camera, renderer.domElement);
controlsFP.lookSpeed = 1;
controlsFP.movementSpeed = 10;
controlsFP.lookVertical = true;
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const delta = clock.getDelta();
controlsFP.update(delta);
cube.rotation.x += 0.01;
cube.rotation.y += 0.01;
torusKnot.rotation.x += 0.01;
torusKnot.rotation.y += 0.01;
renderer.render(scene, camera);
}
animate();
window.addEventListener('resize', () => {
const width = window.innerWidth;
const height = window.innerHeight;
renderer.setSize(width, height);
camera.aspect = width / height;
camera.updateProjectionMatrix();
});
const textureLoader = new THREE.TextureLoader();
const texture = textureLoader.load('resources/tundra_northern_lights.jpg');
texture.mapping = THREE.EquirectangularReflectionMapping;
scene.background = texture;
const geometry_plane = new THREE.PlaneGeometry( 100, 100 );
const material_plane = new THREE.MeshBasicMaterial( {color:'gray', side: THREE.DoubleSide} );
const plane = new THREE.Mesh( geometry_plane, material_plane );
plane.rotation.x = - Math.PI / 2;
plane.position.x = 0;
scene.add( plane );