-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathscene.ts
85 lines (68 loc) · 2.14 KB
/
scene.ts
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
import './styles.scss';
import * as THREE from 'three';
import { RotationJoystickControls } from '../../src';
// import JoystickControls from '../../src';
declare global {
interface Window { rotatingExample: RotatingTargetExample; }
}
class RotatingTargetExample {
element = document.getElementById('target');
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera();
renderer = new THREE.WebGLRenderer({
antialias: true,
});
rotationJoystick: RotationJoystickControls;
earth: THREE.Mesh;
geometry = new THREE.SphereGeometry(1, 36, 36);
material = new THREE.MeshPhongMaterial({
bumpMap: new THREE.TextureLoader().load('images/earth_bump.jpg'),
bumpScale: 0.05,
map: new THREE.TextureLoader().load('images/earth_map.jpg'),
specularMap: new THREE.TextureLoader().load('images/earth_spec.jpg'),
specular: new THREE.Color('grey')
});
ambientLight = new THREE.AmbientLight(0xFFFFFF);
light = new THREE.DirectionalLight(0xFFFFFF, 0.3);
constructor() {
this.earth = new THREE.Mesh(this.geometry, this.material);
this.rotationJoystick = new RotationJoystickControls(
this.camera,
this.scene,
this.earth,
);
this.setupScene();
}
setupScene = () => {
this.element?.appendChild(this.renderer.domElement);
this.camera.position.z = 5;
this.light.position.x = 60;
this.light.position.y = 60;
this.light.position.z = 10000;
this.scene.add(
this.camera,
this.earth,
this.light,
this.ambientLight,
);
this.resize();
this.animate();
window.addEventListener('resize', this.resize);
};
resize = () => {
const width = this.element?.clientWidth || 0;
const height = this.element?.clientHeight || 0;
this.renderer.setSize(width, height);
this.camera.aspect = width / height;
this.camera.updateProjectionMatrix();
};
animate = () => {
requestAnimationFrame(this.animate);
this.earth.rotation.y += 0.001;
this.rotationJoystick.update();
this.renderer.render(this.scene, this.camera);
};
}
window.addEventListener('load', () => {
window.rotatingExample = new RotatingTargetExample();
});