-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
71 lines (60 loc) · 2.24 KB
/
app.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
import * as THREE from 'https://cdn.skypack.dev/[email protected]';
import { OrbitControls } from 'https://cdn.skypack.dev/[email protected]/examples/jsm/controls/OrbitControls.js';
// import { GLTFLoader } from 'https://cdn.skypack.dev/[email protected]/examples/jsm/loaders/GLTFLoader.js';
// Global variables
let scene, camera, renderer, controls, clickMouse, moveMouse, raycaster;
// Create Scene and lights
function init() {
// SCENE
scene = new THREE.Scene();
scene.background = new THREE.Color(0xbfd1e5);
// CAMERA
camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.1, 5000);
camera.position.set(-80, 100, 200);
// RENDERER
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
document.body.appendChild(renderer.domElement);
// CAMERA MOVEMENT CONTROLS
controls = new OrbitControls(camera, renderer.domElement);
controls.target.set(0, 55, 0);
controls.enableDamping = true;
controls.update();
// LIGHTS
let ambientLight = new THREE.AmbientLight(0xffffff, 0.30);
let directionalLight = new THREE.DirectionalLight(0xffffff, 1);
directionalLight.position.set(-30, 50, 150);
scene.add(ambientLight);
scene.add(directionalLight);
// RAYCASTING (mouse functionality)
raycaster = new THREE.Raycaster();
clickMouse = new THREE.Vector2();
moveMouse = new THREE.Vector2();
// FLOOR
let floor = new THREE.Mesh(
new THREE.BoxBufferGeometry(2000, 3, 2000),
new THREE.MeshPhongMaterial({ color: 0x1B8F06 })
);
floor.isDraggable = false;
scene.add(floor);
};
// Recursive function to render the scene
function animate() {
controls.update();
renderer.render(scene, camera);
requestAnimationFrame(animate);
};
// Re-renders the scene upon window resize
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
// Start the program
(function () {
window.addEventListener('resize', onWindowResize, false);
init();
animate();
})();