-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.jsx
103 lines (83 loc) · 2.65 KB
/
index.jsx
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import React from 'react';
import * as THREE from 'three';
import orbitControl from 'three-orbit-controls';
const Control = orbitControl(THREE);
class GlobeComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
scrollTop: props.scrollTop
};
}
componentDidMount() {
const width = window.innerWidth;
const height = window.innerHeight;
this.scene = new THREE.Scene();
this.imageLoader = new THREE.TextureLoader();
this.camera = new THREE.PerspectiveCamera(45, width / height, 1, 1500);
this.renderer = new THREE.WebGLRenderer({ alpha: true, antialias: true });
this.renderer.setSize(width, height);
// Appending to DOM
this.el.appendChild(this.renderer.domElement);
this.addControls();
this.addLights();
this.addGlobe();
this.camera.position.z = 880;
this.draw();
}
addControls() {
this.control = new Control(this.camera, this.renderer.domElement);
this.control.enableDamping = true;
this.control.dampingFactor = 0.1;
this.control.autoRotate = this.props.autorotate;
this.control.enablePan = false;
this.control.enableZoom = false;
this.control.rotateSpeed = 0.1;
this.control.autoRotateSpeed = this.props.velocity;
}
addLights() {
const ambientLight = new THREE.AmbientLight(0x333333);
this.directionalLight = new THREE.DirectionalLight(0xffffff, 1);
this.directionalLight.position.set(5, 3, 40);
this.scene.add(ambientLight);
this.scene.add(this.directionalLight);
}
addGlobe() {
const material = new THREE.MeshPhongMaterial({
map: this.imageLoader.load(this.props.basemapImage),
bumpMap: this.imageLoader.load(this.props.earthBumpImage),
bumpScale: 2
});
const geometry = new THREE.SphereGeometry(this.props.radius, 40, 30);
const earth = new THREE.Mesh(geometry, material);
earth.rotation.y = Math.PI;
earth.updateMatrix();
this.scene.add(earth);
}
draw() {
requestAnimationFrame(this.draw.bind(this));
this.directionalLight.position.copy(this.camera.position);
this.control.update();
this.renderer.render(this.scene, this.camera);
}
render() {
return (
<div ref={(node) => { this.el = node; }} className="vizz-component-globe" />
);
}
}
GlobeComponent.defaultProps = {
radius: 200,
autorotate: true,
velocity: 0.15,
scrollTop: 0
};
GlobeComponent.propTypes = {
radius: React.PropTypes.number,
autorotate: React.PropTypes.bool,
velocity: React.PropTypes.number,
scrollTop: React.PropTypes.number,
basemapImage: React.PropTypes.string,
earthBumpImage: React.PropTypes.string
};
export default GlobeComponent;